123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- /*
- Copyright 2018 The Kubernetes Authors.
- Licensed 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 etcd
- import (
- "fmt"
- "reflect"
- "strconv"
- "testing"
- "github.com/pkg/errors"
- apierrors "k8s.io/apimachinery/pkg/api/errors"
- "k8s.io/apimachinery/pkg/runtime"
- "k8s.io/apimachinery/pkg/util/wait"
- clientsetfake "k8s.io/client-go/kubernetes/fake"
- clienttesting "k8s.io/client-go/testing"
- kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
- "k8s.io/kubernetes/cmd/kubeadm/app/constants"
- testresources "k8s.io/kubernetes/cmd/kubeadm/test/resources"
- )
- func testGetURL(t *testing.T, getURLFunc func(*kubeadmapi.APIEndpoint) string, port int) {
- portStr := strconv.Itoa(port)
- var tests = []struct {
- name string
- advertiseAddress string
- expectedURL string
- }{
- {
- name: "IPv4",
- advertiseAddress: "10.10.10.10",
- expectedURL: fmt.Sprintf("https://10.10.10.10:%s", portStr),
- },
- {
- name: "IPv6",
- advertiseAddress: "2001:db8::2",
- expectedURL: fmt.Sprintf("https://[2001:db8::2]:%s", portStr),
- },
- {
- name: "IPv4 localhost",
- advertiseAddress: "127.0.0.1",
- expectedURL: fmt.Sprintf("https://127.0.0.1:%s", portStr),
- },
- {
- name: "IPv6 localhost",
- advertiseAddress: "::1",
- expectedURL: fmt.Sprintf("https://[::1]:%s", portStr),
- },
- }
- for _, test := range tests {
- url := getURLFunc(&kubeadmapi.APIEndpoint{AdvertiseAddress: test.advertiseAddress})
- if url != test.expectedURL {
- t.Errorf("expected %s, got %s", test.expectedURL, url)
- }
- }
- }
- func TestGetClientURL(t *testing.T) {
- testGetURL(t, GetClientURL, constants.EtcdListenClientPort)
- }
- func TestGetPeerURL(t *testing.T) {
- testGetURL(t, GetClientURL, constants.EtcdListenClientPort)
- }
- func TestGetClientURLByIP(t *testing.T) {
- portStr := strconv.Itoa(constants.EtcdListenClientPort)
- var tests = []struct {
- name string
- ip string
- expectedURL string
- }{
- {
- name: "IPv4",
- ip: "10.10.10.10",
- expectedURL: fmt.Sprintf("https://10.10.10.10:%s", portStr),
- },
- {
- name: "IPv6",
- ip: "2001:db8::2",
- expectedURL: fmt.Sprintf("https://[2001:db8::2]:%s", portStr),
- },
- {
- name: "IPv4 localhost",
- ip: "127.0.0.1",
- expectedURL: fmt.Sprintf("https://127.0.0.1:%s", portStr),
- },
- {
- name: "IPv6 localhost",
- ip: "::1",
- expectedURL: fmt.Sprintf("https://[::1]:%s", portStr),
- },
- }
- for _, test := range tests {
- url := GetClientURLByIP(test.ip)
- if url != test.expectedURL {
- t.Errorf("expected %s, got %s", test.expectedURL, url)
- }
- }
- }
- func TestGetEtcdEndpointsWithBackoff(t *testing.T) {
- var tests = []struct {
- name string
- pods []testresources.FakeStaticPod
- configMap *testresources.FakeConfigMap
- expectedEndpoints []string
- expectedErr bool
- }{
- {
- name: "no pod annotations; no ClusterStatus",
- expectedEndpoints: []string{},
- },
- {
- name: "ipv4 endpoint in pod annotation; no ClusterStatus; port is preserved",
- pods: []testresources.FakeStaticPod{
- {
- Component: constants.Etcd,
- Annotations: map[string]string{
- constants.EtcdAdvertiseClientUrlsAnnotationKey: "https://1.2.3.4:1234",
- },
- },
- },
- expectedEndpoints: []string{"https://1.2.3.4:1234"},
- },
- {
- name: "no pod annotations; ClusterStatus with valid ipv4 endpoint; port is inferred",
- configMap: testresources.ClusterStatusWithAPIEndpoint("cp-0", kubeadmapi.APIEndpoint{AdvertiseAddress: "1.2.3.4", BindPort: 1234}),
- expectedEndpoints: []string{"https://1.2.3.4:2379"},
- },
- }
- for _, rt := range tests {
- t.Run(rt.name, func(t *testing.T) {
- client := clientsetfake.NewSimpleClientset()
- for _, pod := range rt.pods {
- if err := pod.Create(client); err != nil {
- t.Errorf("error setting up test creating pod for node %q", pod.NodeName)
- }
- }
- if rt.configMap != nil {
- if err := rt.configMap.Create(client); err != nil {
- t.Error("could not create ConfigMap")
- }
- }
- endpoints, err := getEtcdEndpointsWithBackoff(client, wait.Backoff{Duration: 0, Jitter: 0, Steps: 1})
- if err != nil && !rt.expectedErr {
- t.Errorf("got error %q; was expecting no errors", err)
- return
- } else if err == nil && rt.expectedErr {
- t.Error("got no error; was expecting an error")
- return
- } else if err != nil && rt.expectedErr {
- return
- }
- if !reflect.DeepEqual(endpoints, rt.expectedEndpoints) {
- t.Errorf("expected etcd endpoints: %v; got: %v", rt.expectedEndpoints, endpoints)
- }
- })
- }
- }
- func TestGetRawEtcdEndpointsFromPodAnnotation(t *testing.T) {
- var tests = []struct {
- name string
- pods []testresources.FakeStaticPod
- clientSetup func(*clientsetfake.Clientset)
- expectedEndpoints []string
- expectedErr bool
- }{
- {
- name: "exactly one pod with annotation",
- pods: []testresources.FakeStaticPod{
- {
- NodeName: "cp-0",
- Component: constants.Etcd,
- Annotations: map[string]string{constants.EtcdAdvertiseClientUrlsAnnotationKey: "https://1.2.3.4:2379"},
- },
- },
- expectedEndpoints: []string{"https://1.2.3.4:2379"},
- },
- {
- name: "no pods with annotation",
- expectedErr: true,
- },
- {
- name: "exactly one pod with annotation; all requests fail",
- pods: []testresources.FakeStaticPod{
- {
- NodeName: "cp-0",
- Component: constants.Etcd,
- Annotations: map[string]string{constants.EtcdAdvertiseClientUrlsAnnotationKey: "https://1.2.3.4:2379"},
- },
- },
- clientSetup: func(clientset *clientsetfake.Clientset) {
- clientset.PrependReactor("list", "pods", func(action clienttesting.Action) (handled bool, ret runtime.Object, err error) {
- return true, nil, apierrors.NewInternalError(errors.New("API server down"))
- })
- },
- expectedErr: true,
- },
- }
- for _, rt := range tests {
- t.Run(rt.name, func(t *testing.T) {
- client := clientsetfake.NewSimpleClientset()
- for i, pod := range rt.pods {
- if err := pod.CreateWithPodSuffix(client, strconv.Itoa(i)); err != nil {
- t.Errorf("error setting up test creating pod for node %q", pod.NodeName)
- }
- }
- if rt.clientSetup != nil {
- rt.clientSetup(client)
- }
- endpoints, err := getRawEtcdEndpointsFromPodAnnotation(client, wait.Backoff{Duration: 0, Jitter: 0, Steps: 1})
- if err != nil && !rt.expectedErr {
- t.Errorf("got error %v, but wasn't expecting any error", err)
- return
- } else if err == nil && rt.expectedErr {
- t.Error("didn't get any error; but was expecting an error")
- return
- } else if err != nil && rt.expectedErr {
- return
- }
- if !reflect.DeepEqual(endpoints, rt.expectedEndpoints) {
- t.Errorf("expected etcd endpoints: %v; got: %v", rt.expectedEndpoints, endpoints)
- }
- })
- }
- }
- func TestGetRawEtcdEndpointsFromPodAnnotationWithoutRetry(t *testing.T) {
- var tests = []struct {
- name string
- pods []testresources.FakeStaticPod
- clientSetup func(*clientsetfake.Clientset)
- expectedEndpoints []string
- expectedErr bool
- }{
- {
- name: "no pods",
- expectedEndpoints: []string{},
- },
- {
- name: "exactly one pod with annotation",
- pods: []testresources.FakeStaticPod{
- {
- NodeName: "cp-0",
- Component: constants.Etcd,
- Annotations: map[string]string{constants.EtcdAdvertiseClientUrlsAnnotationKey: "https://1.2.3.4:2379"},
- },
- },
- expectedEndpoints: []string{"https://1.2.3.4:2379"},
- },
- {
- name: "two pods with annotation",
- pods: []testresources.FakeStaticPod{
- {
- NodeName: "cp-0",
- Component: constants.Etcd,
- Annotations: map[string]string{constants.EtcdAdvertiseClientUrlsAnnotationKey: "https://1.2.3.4:2379"},
- },
- {
- NodeName: "cp-1",
- Component: constants.Etcd,
- Annotations: map[string]string{constants.EtcdAdvertiseClientUrlsAnnotationKey: "https://1.2.3.5:2379"},
- },
- },
- expectedEndpoints: []string{"https://1.2.3.4:2379", "https://1.2.3.5:2379"},
- },
- {
- name: "exactly one pod with annotation; request fails",
- pods: []testresources.FakeStaticPod{
- {
- NodeName: "cp-0",
- Component: constants.Etcd,
- Annotations: map[string]string{constants.EtcdAdvertiseClientUrlsAnnotationKey: "https://1.2.3.4:2379"},
- },
- },
- clientSetup: func(clientset *clientsetfake.Clientset) {
- clientset.PrependReactor("list", "pods", func(action clienttesting.Action) (handled bool, ret runtime.Object, err error) {
- return true, nil, apierrors.NewInternalError(errors.New("API server down"))
- })
- },
- expectedErr: true,
- },
- }
- for _, rt := range tests {
- t.Run(rt.name, func(t *testing.T) {
- client := clientsetfake.NewSimpleClientset()
- for _, pod := range rt.pods {
- if err := pod.Create(client); err != nil {
- t.Errorf("error setting up test creating pod for node %q", pod.NodeName)
- return
- }
- }
- if rt.clientSetup != nil {
- rt.clientSetup(client)
- }
- endpoints, _, err := getRawEtcdEndpointsFromPodAnnotationWithoutRetry(client)
- if err != nil && !rt.expectedErr {
- t.Errorf("got error %v, but wasn't expecting any error", err)
- return
- } else if err == nil && rt.expectedErr {
- t.Error("didn't get any error; but was expecting an error")
- return
- } else if err != nil && rt.expectedErr {
- return
- }
- if !reflect.DeepEqual(endpoints, rt.expectedEndpoints) {
- t.Errorf("expected etcd endpoints: %v; got: %v", rt.expectedEndpoints, endpoints)
- }
- })
- }
- }
|