Skip to content
Snippets Groups Projects
client_test.go 12.9 KiB
Newer Older
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package kubernetes

import (
	"encoding/json"
scott.wang's avatar
scott.wang committed
	_ "net/http/pprof"
	"os"
	"strings"
	"sync"
	"testing"
import (
	v1 "k8s.io/api/core/v1"
)

// tests dataset
var tests = []struct {
	input struct {
		k string
		v string
	}
}{
	{input: struct {
		k string
		v string
	}{k: "name", v: "scott.wang"}},
	{input: struct {
		k string
		v string
	}{k: "namePrefix", v: "prefix.scott.wang"}},
	{input: struct {
		k string
		v string
	}{k: "namePrefix1", v: "prefix1.scott.wang"}},
	{input: struct {
		k string
		v string
	}{k: "age", v: "27"}},
}

// test dataset prefix
const prefix = "name"

scott.wang's avatar
scott.wang committed
var clientPodListJsonData = `{
    "apiVersion": "v1",
scott.wang's avatar
scott.wang committed
    "items": [
        {
            "apiVersion": "v1",
            "kind": "Pod",
            "metadata": {
                "annotations": {
                    "dubbo.io/annotation": "W3siayI6Ii9kdWJiby9jb20uaWt1cmVudG8udXNlci5Vc2VyUHJvdmlkZXIvcHJvdmlkZXJzIiwidiI6IiJ9LHsiayI6Ii9kdWJiby9jb20uaWt1cmVudG8udXNlci5Vc2VyUHJvdmlkZXIvcHJvdmlkZXJzL2R1YmJvJTNBJTJGJTJGMTcyLjE3LjAuNiUzQTIwMDAwJTJGVXNlclByb3ZpZGVyJTNGYWNjZXNzbG9nJTNEJTI2YW55aG9zdCUzRHRydWUlMjZhcHA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92aWRlciUyNnRpbWVzdGFtcCUzRDE1OTExNTYxNTUlMjZ0cHMubGltaXQuaW50ZXJ2YWwlM0QlMjZ0cHMubGltaXQucmF0ZSUzRCUyNnRwcy5saW1pdC5yZWplY3RlZC5oYW5kbGVyJTNEJTI2dHBzLmxpbWl0LnN0cmF0ZWd5JTNEJTI2dHBzLmxpbWl0ZXIlM0QlMjZ2ZXJzaW9uJTNEJTI2d2FybXVwJTNEMTAwIiwidiI6IiJ9XQ=="
                },
                "creationTimestamp": "2020-06-03T03:49:14Z",
                "generateName": "server-84c864f5bc-",
                "labels": {
                    "dubbo.io/label": "dubbo.io-value",
                    "pod-template-hash": "84c864f5bc",
                    "role": "server"
                },
                "name": "server-84c864f5bc-r8qvz",
                "namespace": "default",
                "ownerReferences": [
                    {
                        "apiVersion": "apps/v1",
                        "blockOwnerDeletion": true,
                        "controller": true,
                        "kind": "ReplicaSet",
                        "name": "server-84c864f5bc",
                        "uid": "fa376dbb-4f37-4705-8e80-727f592c19b3"
                    }
                ],
                "resourceVersion": "517460",
                "selfLink": "/api/v1/namespaces/default/pods/server-84c864f5bc-r8qvz",
                "uid": "f4fc811c-200c-4445-8d4f-532144957dcc"
            },
            "spec": {
                "containers": [
scott.wang's avatar
scott.wang committed
                        "env": [
                            {
                                "name": "DUBBO_NAMESPACE",
                                "value": "default"
                            },
                            {
                                "name": "NAMESPACE",
                                "valueFrom": {
                                    "fieldRef": {
                                        "apiVersion": "v1",
                                        "fieldPath": "metadata.namespace"
                                    }
                                }
scott.wang's avatar
scott.wang committed
                        ],
                        "image": "192.168.240.101:5000/scott/go-server",
                        "imagePullPolicy": "Always",
                        "name": "server",
                        "resources": {},
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File",
                        "volumeMounts": [
                            {
                                "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
                                "name": "dubbo-sa-token-5qbtb",
                                "readOnly": true
                            }
                        ]
scott.wang's avatar
scott.wang committed
                "dnsPolicy": "ClusterFirst",
                "enableServiceLinks": true,
                "nodeName": "minikube",
                "priority": 0,
                "restartPolicy": "Always",
                "schedulerName": "default-scheduler",
                "securityContext": {},
                "serviceAccount": "dubbo-sa",
                "serviceAccountName": "dubbo-sa",
                "terminationGracePeriodSeconds": 30,
                "tolerations": [
                    {
                        "effect": "NoExecute",
                        "key": "node.kubernetes.io/not-ready",
                        "operator": "Exists",
                        "tolerationSeconds": 300
                    },
scott.wang's avatar
scott.wang committed
                        "effect": "NoExecute",
                        "key": "node.kubernetes.io/unreachable",
                        "operator": "Exists",
                        "tolerationSeconds": 300
                    }
                ],
                "volumes": [
                    {
                        "name": "dubbo-sa-token-5qbtb",
                        "secret": {
                            "defaultMode": 420,
                            "secretName": "dubbo-sa-token-5qbtb"
                        }
scott.wang's avatar
scott.wang committed
            "status": {
                "conditions": [
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2020-06-03T03:49:14Z",
                        "status": "True",
                        "type": "Initialized"
                    },
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2020-06-03T03:49:15Z",
                        "status": "True",
                        "type": "Ready"
                    },
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2020-06-03T03:49:15Z",
                        "status": "True",
                        "type": "ContainersReady"
                    },
                    {
                        "lastProbeTime": null,
                        "lastTransitionTime": "2020-06-03T03:49:14Z",
                        "status": "True",
                        "type": "PodScheduled"
                    }
                ],
                "containerStatuses": [
                    {
                        "containerID": "docker://b6421e05ce44f8a1c4fa6b72274980777c7c0f945516209f7c0558cd0cd65406",
                        "image": "192.168.240.101:5000/scott/go-server:latest",
                        "imageID": "docker-pullable://192.168.240.101:5000/scott/go-server@sha256:4eecf895054f0ff93d80db64992a561d10504e55582def6dcb6093a6d6d92461",
                        "lastState": {},
                        "name": "server",
                        "ready": true,
                        "restartCount": 0,
                        "started": true,
                        "state": {
                            "running": {
                                "startedAt": "2020-06-03T03:49:15Z"
                            }
                        }
                    }
                ],
                "hostIP": "10.0.2.15",
                "phase": "Running",
                "podIP": "172.17.0.6",
                "podIPs": [
                    {
                        "ip": "172.17.0.6"
scott.wang's avatar
scott.wang committed
                ],
                "qosClass": "BestEffort",
                "startTime": "2020-06-03T03:49:14Z"
scott.wang's avatar
scott.wang committed
        }
    ],
    "kind": "List",
    "metadata": {
        "resourceVersion": "",
        "selfLink": ""
scott.wang's avatar
scott.wang committed
func getTestClient(t *testing.T) *Client {
scott.wang's avatar
scott.wang committed
	pl := &v1.PodList{}
	// 1. install test data
scott.wang's avatar
scott.wang committed
	if err := json.Unmarshal([]byte(clientPodListJsonData), &pl); err != nil {
		t.Fatal(err)
	}
scott.wang's avatar
scott.wang committed
	currentPod := pl.Items[0]
scott.wang's avatar
scott.wang committed
	env := map[string]string{
		nameSpaceKey:            currentPod.GetNamespace(),
		podNameKey:              currentPod.GetName(),
		needWatchedNameSpaceKey: "default",
scott.wang's avatar
scott.wang committed
	for k, v := range env {
		if err := os.Setenv(k, v); err != nil {
			t.Fatal(err)
		}
scott.wang's avatar
scott.wang committed
	client, err := NewMockClient(pl)
	if err != nil {
		t.Fatal(err)
	}

scott.wang's avatar
scott.wang committed
	return client
scott.wang's avatar
scott.wang committed
func TestClientValid(t *testing.T) {
scott.wang's avatar
scott.wang committed
	client := getTestClient(t)
	defer client.Close()

	if client.Valid() != true {
		t.Fatal("client is not valid")
	}

	client.Close()
	if client.Valid() != false {
		t.Fatal("client is valid")
	}
}

scott.wang's avatar
scott.wang committed
func TestClientDone(t *testing.T) {
scott.wang's avatar
scott.wang committed
	client := getTestClient(t)
	go func() {
		client.Close()
	<-client.Done()
	if client.Valid() == true {
scott.wang's avatar
scott.wang committed
		t.Fatal("client should be invalid")
scott.wang's avatar
scott.wang committed
func TestClientCreateKV(t *testing.T) {
scott.wang's avatar
scott.wang committed
	client := getTestClient(t)
	defer client.Close()

	for _, tc := range tests {

		k := tc.input.k
		v := tc.input.v

		if err := client.Create(k, v); err != nil {
scott.wang's avatar
scott.wang committed
func TestClientGetChildrenKVList(t *testing.T) {
scott.wang's avatar
scott.wang committed
	client := getTestClient(t)
	defer client.Close()
	wg := sync.WaitGroup{}
	wg.Add(1)

scott.wang's avatar
scott.wang committed
	syncDataComplete := make(chan struct{})

		wc, done, err := client.WatchWithPrefix(prefix)
		if err != nil {
			t.Fatal(err)
		}
scott.wang's avatar
scott.wang committed
		wg.Done()

		for {
			select {
			case e := <-wc:
				i++
scott.wang's avatar
scott.wang committed
				fmt.Printf("got event %v k %s v %s\n", e.EventType, e.Key, e.Value)
				if i == 3 {
					// already sync all event
scott.wang's avatar
scott.wang committed
					syncDataComplete <- struct{}{}
					return
				}
			case <-done:
scott's avatar
scott committed
				t.Log("the watcherSet watcher was stopped")
scott.wang's avatar
scott.wang committed
	// wait the watch goroutine start
	wg.Wait()

	expect := make(map[string]string)
	got := make(map[string]string)

	for _, tc := range tests {

		k := tc.input.k
		v := tc.input.v

		if strings.Contains(k, prefix) {
			expect[k] = v
		}

		if err := client.Create(k, v); err != nil {
scott.wang's avatar
scott.wang committed
	<-syncDataComplete

	// start get all children
	kList, vList, err := client.GetChildren(prefix)
	if err != nil {
		t.Fatal(err)
	}

	for i := 0; i < len(kList); i++ {
		got[kList[i]] = vList[i]
	}

	for expectK, expectV := range expect {

		if got[expectK] != expectV {
			t.Fatalf("expect {%s: %s} but got {%s: %v}", expectK, expectV, expectK, got[expectK])
		}
	}

}

scott.wang's avatar
scott.wang committed
func TestClientWatchPrefix(t *testing.T) {
scott.wang's avatar
scott.wang committed
	client := getTestClient(t)
	wg := sync.WaitGroup{}
	wg.Add(1)

	go func() {

		wc, done, err := client.WatchWithPrefix(prefix)
		if err != nil {
			t.Fatal(err)
		}

scott's avatar
scott committed
		wg.Done()

		for {
			select {
			case e := <-wc:
scott.wang's avatar
scott.wang committed
				t.Logf("got event %v k %s v %s", e.EventType, e.Key, e.Value)
			case <-done:
scott's avatar
scott committed
				t.Log("the watcherSet watcher was stopped")
scott's avatar
scott committed
	// must wait the watch goroutine work
	wg.Wait()

	for _, tc := range tests {

		k := tc.input.k
		v := tc.input.v

		if err := client.Create(k, v); err != nil {
	client.Close()
scott.wang's avatar
scott.wang committed
func TestClientWatch(t *testing.T) {
scott.wang's avatar
scott.wang committed
	client := getTestClient(t)

	wg := sync.WaitGroup{}
	wg.Add(1)

	go func() {

		wc, done, err := client.Watch(prefix)
		if err != nil {
			t.Fatal(err)
		}
scott's avatar
scott committed
		wg.Done()
		for {
			select {
			case e := <-wc:
scott.wang's avatar
scott.wang committed
				t.Logf("got event %v k %s v %s", e.EventType, e.Key, e.Value)
			case <-done:
scott's avatar
scott committed
				t.Log("the watcherSet watcher was stopped")
scott's avatar
scott committed
	// must wait the watch goroutine already start the watch goroutine
	wg.Wait()

	for _, tc := range tests {

		k := tc.input.k
		v := tc.input.v

		if err := client.Create(k, v); err != nil {
			t.Fatal(err)
		}
	}

	client.Close()
}