From 7aaa274f170e36742c9c50bdab7940d887657a5c Mon Sep 17 00:00:00 2001 From: Lino Layani <39967417+linoleparquet@users.noreply.github.com> Date: Thu, 8 May 2025 13:55:15 -0400 Subject: [PATCH] test(source): cover unhappy paths (#5369) * Improve test coverage store * Improve test coverage --- source/store_test.go | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/source/store_test.go b/source/store_test.go index d65889328..94b42b4f1 100644 --- a/source/store_test.go +++ b/source/store_test.go @@ -195,17 +195,16 @@ func (suite *ByNamesTestSuite) TestKubeClientFails() { mockClientGenerator := new(MockClientGenerator) mockClientGenerator.On("KubeClient").Return(nil, errors.New("foo")) - _, err := ByNames(context.TODO(), mockClientGenerator, []string{"service"}, &Config{}) - suite.Error(err, "should return an error if kubernetes client cannot be created") + sourcesDependentOnKubeClient := []string{ + "node", "service", "ingress", "pod", "istio-gateway", "istio-virtualservice", + "ambassador-host", "gloo-proxy", "traefik-proxy", "crd", "kong-tcpingress", + "f5-virtualserver", "f5-transportserver", + } - _, err = ByNames(context.TODO(), mockClientGenerator, []string{"ingress"}, &Config{}) - suite.Error(err, "should return an error if kubernetes client cannot be created") - - _, err = ByNames(context.TODO(), mockClientGenerator, []string{"istio-gateway"}, &Config{}) - suite.Error(err, "should return an error if kubernetes client cannot be created") - - _, err = ByNames(context.TODO(), mockClientGenerator, []string{"kong-tcpingress"}, &Config{}) - suite.Error(err, "should return an error if kubernetes client cannot be created") + for _, source := range sourcesDependentOnKubeClient { + _, err := ByNames(context.TODO(), mockClientGenerator, []string{source}, &Config{}) + suite.Error(err, source+" should return an error if kubernetes client cannot be created") + } } func (suite *ByNamesTestSuite) TestIstioClientFails() { @@ -214,11 +213,27 @@ func (suite *ByNamesTestSuite) TestIstioClientFails() { mockClientGenerator.On("IstioClient").Return(nil, errors.New("foo")) mockClientGenerator.On("DynamicKubernetesClient").Return(nil, errors.New("foo")) - _, err := ByNames(context.TODO(), mockClientGenerator, []string{"istio-gateway"}, &Config{}) - suite.Error(err, "should return an error if istio client cannot be created") + sourcesDependentOnIstioClient := []string{"istio-gateway", "istio-virtualservice"} - _, err = ByNames(context.TODO(), mockClientGenerator, []string{"contour-httpproxy"}, &Config{}) - suite.Error(err, "should return an error if contour client cannot be created") + for _, source := range sourcesDependentOnIstioClient { + _, err := ByNames(context.TODO(), mockClientGenerator, []string{source}, &Config{}) + suite.Error(err, source+" should return an error if istio client cannot be created") + } +} + +func (suite *ByNamesTestSuite) TestDynamicKubernetesClientFails() { + mockClientGenerator := new(MockClientGenerator) + mockClientGenerator.On("KubeClient").Return(fakeKube.NewSimpleClientset(), nil) + mockClientGenerator.On("IstioClient").Return(istiofake.NewSimpleClientset(), nil) + mockClientGenerator.On("DynamicKubernetesClient").Return(nil, errors.New("foo")) + + sourcesDependentOnDynamicKubernetesClient := []string{"ambassador-host", "contour-httpproxy", "gloo-proxy", "traefik-proxy", + "kong-tcpingress", "f5-virtualserver", "f5-transportserver"} + + for _, source := range sourcesDependentOnDynamicKubernetesClient { + _, err := ByNames(context.TODO(), mockClientGenerator, []string{source}, &Config{}) + suite.Error(err, source+" should return an error if dynamic kubernetes client cannot be created") + } } func TestByNames(t *testing.T) {