handler_test.go 11.9 KB
Newer Older
1 2 3 4
package v1alpha1

import (
	"bytes"
Z
zryfish 已提交
5 6
	"encoding/json"
	"fmt"
7 8 9 10
	"github.com/google/go-cmp/cmp"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/cli-runtime/pkg/printers"
Z
zryfish 已提交
11
	k8sfake "k8s.io/client-go/kubernetes/fake"
12 13 14
	"kubesphere.io/kubesphere/pkg/apis/cluster/v1alpha1"
	"kubesphere.io/kubesphere/pkg/client/clientset/versioned/fake"
	"kubesphere.io/kubesphere/pkg/informers"
Z
zryfish 已提交
15 16 17 18 19 20
	"kubesphere.io/kubesphere/pkg/version"
	"net/http"
	"net/http/httptest"
	"net/url"
	"sigs.k8s.io/controller-runtime/pkg/envtest"
	"sigs.k8s.io/testing_frameworks/integration"
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	"testing"
)

const (
	proxyAddress = "http://139.198.121.121:8080"
	agentImage   = "kubesphere/tower:v1.0"
	proxyService = "tower.kubesphere-system.svc"
)

var cluster = &v1alpha1.Cluster{
	ObjectMeta: metav1.ObjectMeta{
		Name: "gondor",
	},
	Spec: v1alpha1.ClusterSpec{
		Connection: v1alpha1.Connection{
			Type:  v1alpha1.ConnectionTypeProxy,
			Token: "randomtoken",
		},
	},
}

var service = &corev1.Service{
	ObjectMeta: metav1.ObjectMeta{
		Name:      "tower",
		Namespace: "kubesphere-system",
	},
	Spec: corev1.ServiceSpec{
		Ports: []corev1.ServicePort{
			{
				Port:     8080,
				Protocol: corev1.ProtocolTCP,
			},
		},
	},
	Status: corev1.ServiceStatus{
		LoadBalancer: corev1.LoadBalancerStatus{
			Ingress: []corev1.LoadBalancerIngress{
				{
					IP:       "139.198.121.121",
					Hostname: "foo.bar",
				},
			},
		},
	},
}

var expected = `apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  name: cluster-agent
Z
zryfish 已提交
72
  namespace: kubesphere-system
73
spec:
Z
zryfish 已提交
74 75 76 77
  selector:
    matchLabels:
      app: agent
      app.kubernetes.io/part-of: tower
78 79 80 81
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
Z
zryfish 已提交
82 83 84
      labels:
        app: agent
        app.kubernetes.io/part-of: tower
85 86 87 88 89 90 91
    spec:
      containers:
      - command:
        - /agent
        - --name=gondor
        - --token=randomtoken
        - --proxy-server=http://139.198.121.121:8080
92
        - --keepalive=10s
93 94
        - --kubesphere-service=ks-apiserver.kubesphere-system.svc:80
        - --kubernetes-service=kubernetes.default.svc:443
95
        - --v=0
96 97 98 99 100 101 102 103 104 105 106 107 108 109
        image: kubesphere/tower:v1.0
        name: agent
        resources:
          limits:
            cpu: "1"
            memory: 200M
          requests:
            cpu: 100m
            memory: 100M
      serviceAccountName: kubesphere
status: {}
`

func TestGeranteAgentDeployment(t *testing.T) {
Z
zryfish 已提交
110
	k8sclient := k8sfake.NewSimpleClientset(service)
111 112
	ksclient := fake.NewSimpleClientset(cluster)

113
	informersFactory := informers.NewInformerFactories(k8sclient, ksclient, nil, nil, nil, nil)
114 115 116 117

	informersFactory.KubernetesSharedInformerFactory().Core().V1().Services().Informer().GetIndexer().Add(service)
	informersFactory.KubeSphereSharedInformerFactory().Cluster().V1alpha1().Clusters().Informer().GetIndexer().Add(cluster)

Z
zryfish 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	directConnectionCluster := cluster.DeepCopy()
	directConnectionCluster.Spec.Connection.Type = v1alpha1.ConnectionTypeDirect

	var testCases = []struct {
		description    string
		expectingError bool
		expectedError  error
		cluster        *v1alpha1.Cluster
		expected       string
	}{
		{
			description:    "test normal case",
			expectingError: false,
			expected:       expected,
			cluster:        cluster,
		},
		{
			description:    "test direct connection cluster",
			expectingError: true,
Z
zryfish 已提交
137
			expectedError:  errClusterConnectionIsNotProxy,
Z
zryfish 已提交
138 139
			cluster:        directConnectionCluster,
		},
140 141
	}

Z
zryfish 已提交
142 143 144
	for _, testCase := range testCases {

		t.Run(testCase.description, func(t *testing.T) {
145
			h := newHandler(informersFactory.KubernetesSharedInformerFactory().Core().V1().Services().Lister(),
Z
zryfish 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
				informersFactory.KubeSphereSharedInformerFactory().Cluster().V1alpha1().Clusters().Lister(),
				proxyService,
				"",
				agentImage)

			var buf bytes.Buffer

			err := h.populateProxyAddress()
			if err != nil {
				t.Error(err)
			}

			err = h.generateDefaultDeployment(testCase.cluster, &buf)
			if testCase.expectingError {
				if err == nil {
					t.Fatalf("expecting error %v, got nil", testCase.expectedError)
				} else if err != testCase.expectedError {
					t.Fatalf("expecting error %v, got %v", testCase.expectedError, err)
				}
			}
Z
zryfish 已提交
166 167 168 169

			if diff := cmp.Diff(testCase.expected, buf.String()); len(diff) != 0 {
				t.Errorf("%T, got +, expected -, %s", testCase.expected, diff)
			}
Z
zryfish 已提交
170
		})
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	}
}

func TestInnerGenerateAgentDeployment(t *testing.T) {
	h := &handler{
		proxyAddress: proxyAddress,
		agentImage:   agentImage,
		yamlPrinter:  &printers.YAMLPrinter{},
	}

	var buf bytes.Buffer

	err := h.generateDefaultDeployment(cluster, &buf)

	if err != nil {
		t.Error(err)
	}

	if diff := cmp.Diff(buf.String(), expected); len(diff) != 0 {
		t.Error(diff)
	}

}
Z
zryfish 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

var base64EncodedKubeConfig = `
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdw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ERXejMzclUySCs0RjNMOThzWGE1OWNITWZPb1kzZ3pzUE5LYnQwbWsxeTNOUmZocTZYTTJXemkrUFZkcAppbUVqUlY4UUl5c3Zhc25sTjFIY1FtcWtMaFdLYlRoOEc5MGhiTzlwdTFRNE0wMmZ3STg0ZVlSUWZNUT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
    server: 127.0.0.1:6578
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQWRxZ0F3SUJBZ0lJZkcyYVlKYnRYT2N3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWE53YUdWeVpUQWVGdzB5TURBMU1qY3dOekl4TkRCYUZ3MHpNREExTWpVd056SXhOREJhTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1Sa3dGd1lEVlFRREV4QnJkV0psY201bGRHVnpMV0ZrCmJXbHVNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQXRiR3JpeFBxOWF6QU1wNGsKM0VsaElmanhOZXhkY2VORGJENkFMMXVGSWJUMk1uVnRXeFFXOU5ueVFwVU9NMzN4clNJMkJlVW1uU0U5RlR6cAo1bTBmaGVBOGhtRHRSZWVOckNxVnBJQy9kNUV2eDJ3NTJ2NXVCYUNXd09ZUGpQMi9uL0Y3THQwMmN3TkFXWE5pCmFRTi9uRGl6TjJrRXFoSmZiL0tyNGx3eEEzTDExVXJhMDNTRUp0U3FXSzBKQ1pnL0lzUnRFNFFqZXp1WWhiVWkKcWU0TmdqZjN1ZFBMMXQyeVpCK3hSTE1sNTFqenhYaXQ0U2pHSFJ2UEt1VHlkc1AxdEtINXdYdn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WdDNVMk1XSzN2azFjcy9oWjZrK0F2aE9iSXBrLzR2VEVRUW1FCjh4bkl5bkpPNWJleFQybkEyUWVYblVPbjZzdWtxVnk3YlBLSnNOa2hBb0dBV1YzNUxhQWZvQk1uUXdYOFN5RnYKUThiQVptNlp1RTRPMkY1QjFlN1AyWFcrUHh4bUFaaytLWTkxYVNEVVFXUXdvVVdRbmVlRU96aXBwWXVaVURNegpMUnk5cmcvOWdwLzY5MVlBSHlUNjgrUWlvRXQwVllna0diUFp2NFhmYXYzV3AxNUNySU9iU0RBaGpCcWt3U09rCjhhMXU4WmNYT09qa0FFTEJGVHF3RGhVQ2dZQVVqQjFvY1A4NkJHWW53SDRPU0tORmRRbHozWjJKQkcvZGMyS1UKUlo0dURmV1pTcjV5RC92YzFLbFRJbmlzNVR4YzRpQjFqMWNycDFqNE16ZmFmdXVySW9VVDBCWUNpTkIrUitLZgpFZGUrUTNPZFhhRW9FK2YrR2Z0bFF5R3J4cTAzWEJwdk5veHAxWHJjRXBUWURjemN5RjJLcjBoVGlHZDVxekN0Cnkyd3BBUUtCZ1FDWTFKdlp0YkFlZll2RXQ4c3JETVZsb1FOWVVKTURxWnBLenpqR0w5S3FqdlFwbjJOL2EwNmsKUzNsMndWWExXNy8xb0RMV2gxZGZLbUJlUlJhZUxJMXIvS3FyeTRjUStiVEIzTzBwS2R3WjFFYXNQajBDTnlaUAp0YnFkOEFWa09pRkFNYlRWaVZuR3RzSnJ3c1V2N1NSTUU4ckloMVJva0ZtRGRpN0J1UVl5emc9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=
`

func TestValidateKubeConfig(t *testing.T) {
218 219 220 221 222 223 224 225 226 227 228 229 230 231
	k8sclient := k8sfake.NewSimpleClientset(service)
	ksclient := fake.NewSimpleClientset(cluster)

	informersFactory := informers.NewInformerFactories(k8sclient, ksclient, nil, nil, nil, nil)

	informersFactory.KubernetesSharedInformerFactory().Core().V1().Services().Informer().GetIndexer().Add(service)
	informersFactory.KubeSphereSharedInformerFactory().Cluster().V1alpha1().Clusters().Informer().GetIndexer().Add(cluster)

	h := newHandler(informersFactory.KubernetesSharedInformerFactory().Core().V1().Services().Lister(),
		informersFactory.KubeSphereSharedInformerFactory().Cluster().V1alpha1().Clusters().Lister(),
		proxyService,
		"",
		agentImage)

Z
zryfish 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	config, err := loadKubeConfigFromBytes([]byte(base64EncodedKubeConfig))
	if err != nil {
		t.Fatal(err)
	}

	// config.Host is schemaless, we need to add schema manually
	u, err := url.Parse(fmt.Sprintf("http://%s", config.Host))
	if err != nil {
		t.Fatal(err)
	}

	// we need to specify apiserver port to match above kubeconfig
	env := &envtest.Environment{
		Config: config,
		ControlPlane: integration.ControlPlane{
			APIServer: &integration.APIServer{
				Args: envtest.DefaultKubeAPIServerFlags,
				URL:  u,
			},
		},
	}

	cfg, err := env.Start()
	if err != nil {
		t.Log(cfg)
		t.Fatal(err)
	}

	defer func() {
		_ = env.Stop()
	}()

264
	err = h.validateKubeConfig([]byte(base64EncodedKubeConfig))
Z
zryfish 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	if err != nil {
		t.Fatal(err)
	}
}

var ver = version.Get()

func endpoint(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	_ = json.NewEncoder(w).Encode(ver)
}

func TestValidateKubeSphereEndpoint(t *testing.T) {
	svr := httptest.NewServer(http.HandlerFunc(endpoint))
	defer svr.Close()

Z
zryfish 已提交
281
	got, err := validateKubeSphereAPIServer(svr.URL, nil)
Z
zryfish 已提交
282 283 284 285 286 287 288 289 290
	if err != nil {
		t.Fatal(err)
	}

	if diff := cmp.Diff(&ver, got); len(diff) != 0 {
		t.Errorf("%T +got, -expected %v", ver, diff)
	}

}