sigs.k8s.io/external-dns@v0.14.1/source/store_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package source 18 19 import ( 20 "context" 21 "errors" 22 "testing" 23 24 cfclient "github.com/cloudfoundry-community/go-cfclient" 25 openshift "github.com/openshift/client-go/route/clientset/versioned" 26 "github.com/stretchr/testify/mock" 27 "github.com/stretchr/testify/suite" 28 istioclient "istio.io/client-go/pkg/clientset/versioned" 29 istiofake "istio.io/client-go/pkg/clientset/versioned/fake" 30 "k8s.io/apimachinery/pkg/runtime" 31 "k8s.io/apimachinery/pkg/runtime/schema" 32 "k8s.io/client-go/dynamic" 33 fakeDynamic "k8s.io/client-go/dynamic/fake" 34 "k8s.io/client-go/kubernetes" 35 fakeKube "k8s.io/client-go/kubernetes/fake" 36 gateway "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned" 37 ) 38 39 type MockClientGenerator struct { 40 mock.Mock 41 kubeClient kubernetes.Interface 42 gatewayClient gateway.Interface 43 istioClient istioclient.Interface 44 cloudFoundryClient *cfclient.Client 45 dynamicKubernetesClient dynamic.Interface 46 openshiftClient openshift.Interface 47 } 48 49 func (m *MockClientGenerator) KubeClient() (kubernetes.Interface, error) { 50 args := m.Called() 51 if args.Error(1) == nil { 52 m.kubeClient = args.Get(0).(kubernetes.Interface) 53 return m.kubeClient, nil 54 } 55 return nil, args.Error(1) 56 } 57 58 func (m *MockClientGenerator) GatewayClient() (gateway.Interface, error) { 59 args := m.Called() 60 if args.Error(1) != nil { 61 return nil, args.Error(1) 62 } 63 m.gatewayClient = args.Get(0).(gateway.Interface) 64 return m.gatewayClient, nil 65 } 66 67 func (m *MockClientGenerator) IstioClient() (istioclient.Interface, error) { 68 args := m.Called() 69 if args.Error(1) == nil { 70 m.istioClient = args.Get(0).(istioclient.Interface) 71 return m.istioClient, nil 72 } 73 return nil, args.Error(1) 74 } 75 76 func (m *MockClientGenerator) CloudFoundryClient(cfAPIEndpoint string, cfUsername string, cfPassword string) (*cfclient.Client, error) { 77 args := m.Called() 78 if args.Error(1) == nil { 79 m.cloudFoundryClient = args.Get(0).(*cfclient.Client) 80 return m.cloudFoundryClient, nil 81 } 82 return nil, args.Error(1) 83 } 84 85 func (m *MockClientGenerator) DynamicKubernetesClient() (dynamic.Interface, error) { 86 args := m.Called() 87 if args.Error(1) == nil { 88 m.dynamicKubernetesClient = args.Get(0).(dynamic.Interface) 89 return m.dynamicKubernetesClient, nil 90 } 91 return nil, args.Error(1) 92 } 93 94 func (m *MockClientGenerator) OpenShiftClient() (openshift.Interface, error) { 95 args := m.Called() 96 if args.Error(1) == nil { 97 m.openshiftClient = args.Get(0).(openshift.Interface) 98 return m.openshiftClient, nil 99 } 100 return nil, args.Error(1) 101 } 102 103 type ByNamesTestSuite struct { 104 suite.Suite 105 } 106 107 func (suite *ByNamesTestSuite) TestAllInitialized() { 108 mockClientGenerator := new(MockClientGenerator) 109 mockClientGenerator.On("KubeClient").Return(fakeKube.NewSimpleClientset(), nil) 110 mockClientGenerator.On("IstioClient").Return(istiofake.NewSimpleClientset(), nil) 111 mockClientGenerator.On("DynamicKubernetesClient").Return(fakeDynamic.NewSimpleDynamicClientWithCustomListKinds(runtime.NewScheme(), 112 map[schema.GroupVersionResource]string{ 113 { 114 Group: "projectcontour.io", 115 Version: "v1", 116 Resource: "httpproxies", 117 }: "HTTPPRoxiesList", 118 { 119 Group: "contour.heptio.com", 120 Version: "v1beta1", 121 Resource: "tcpingresses", 122 }: "TCPIngressesList", 123 { 124 Group: "configuration.konghq.com", 125 Version: "v1beta1", 126 Resource: "tcpingresses", 127 }: "TCPIngressesList", 128 { 129 Group: "cis.f5.com", 130 Version: "v1", 131 Resource: "virtualservers", 132 }: "VirtualServersList", 133 { 134 Group: "traefik.containo.us", 135 Version: "v1alpha1", 136 Resource: "ingressroutes", 137 }: "IngressRouteList", 138 { 139 Group: "traefik.containo.us", 140 Version: "v1alpha1", 141 Resource: "ingressroutetcps", 142 }: "IngressRouteTCPList", 143 { 144 Group: "traefik.containo.us", 145 Version: "v1alpha1", 146 Resource: "ingressrouteudps", 147 }: "IngressRouteUDPList", 148 { 149 Group: "traefik.io", 150 Version: "v1alpha1", 151 Resource: "ingressroutes", 152 }: "IngressRouteList", 153 { 154 Group: "traefik.io", 155 Version: "v1alpha1", 156 Resource: "ingressroutetcps", 157 }: "IngressRouteTCPList", 158 { 159 Group: "traefik.io", 160 Version: "v1alpha1", 161 Resource: "ingressrouteudps", 162 }: "IngressRouteUDPList", 163 }), nil) 164 165 sources, err := ByNames(context.TODO(), mockClientGenerator, []string{"service", "ingress", "istio-gateway", "contour-httpproxy", "kong-tcpingress", "f5-virtualserver", "traefik-proxy", "fake"}, &Config{}) 166 suite.NoError(err, "should not generate errors") 167 suite.Len(sources, 8, "should generate all eight sources") 168 } 169 170 func (suite *ByNamesTestSuite) TestOnlyFake() { 171 mockClientGenerator := new(MockClientGenerator) 172 mockClientGenerator.On("KubeClient").Return(fakeKube.NewSimpleClientset(), nil) 173 174 sources, err := ByNames(context.TODO(), mockClientGenerator, []string{"fake"}, &Config{}) 175 suite.NoError(err, "should not generate errors") 176 suite.Len(sources, 1, "should generate fake source") 177 suite.Nil(mockClientGenerator.kubeClient, "client should not be created") 178 } 179 180 func (suite *ByNamesTestSuite) TestSourceNotFound() { 181 mockClientGenerator := new(MockClientGenerator) 182 mockClientGenerator.On("KubeClient").Return(fakeKube.NewSimpleClientset(), nil) 183 184 sources, err := ByNames(context.TODO(), mockClientGenerator, []string{"foo"}, &Config{}) 185 suite.Equal(err, ErrSourceNotFound, "should return source not found") 186 suite.Len(sources, 0, "should not returns any source") 187 } 188 189 func (suite *ByNamesTestSuite) TestKubeClientFails() { 190 mockClientGenerator := new(MockClientGenerator) 191 mockClientGenerator.On("KubeClient").Return(nil, errors.New("foo")) 192 193 _, err := ByNames(context.TODO(), mockClientGenerator, []string{"service"}, &Config{}) 194 suite.Error(err, "should return an error if kubernetes client cannot be created") 195 196 _, err = ByNames(context.TODO(), mockClientGenerator, []string{"ingress"}, &Config{}) 197 suite.Error(err, "should return an error if kubernetes client cannot be created") 198 199 _, err = ByNames(context.TODO(), mockClientGenerator, []string{"istio-gateway"}, &Config{}) 200 suite.Error(err, "should return an error if kubernetes client cannot be created") 201 202 _, err = ByNames(context.TODO(), mockClientGenerator, []string{"kong-tcpingress"}, &Config{}) 203 suite.Error(err, "should return an error if kubernetes client cannot be created") 204 } 205 206 func (suite *ByNamesTestSuite) TestIstioClientFails() { 207 mockClientGenerator := new(MockClientGenerator) 208 mockClientGenerator.On("KubeClient").Return(fakeKube.NewSimpleClientset(), nil) 209 mockClientGenerator.On("IstioClient").Return(nil, errors.New("foo")) 210 mockClientGenerator.On("DynamicKubernetesClient").Return(nil, errors.New("foo")) 211 212 _, err := ByNames(context.TODO(), mockClientGenerator, []string{"istio-gateway"}, &Config{}) 213 suite.Error(err, "should return an error if istio client cannot be created") 214 215 _, err = ByNames(context.TODO(), mockClientGenerator, []string{"contour-httpproxy"}, &Config{}) 216 suite.Error(err, "should return an error if contour client cannot be created") 217 } 218 219 func TestByNames(t *testing.T) { 220 suite.Run(t, new(ByNamesTestSuite)) 221 }