test(source): cover unhappy paths (#5369)

* Improve test coverage store

* Improve test coverage
This commit is contained in:
Lino Layani 2025-05-08 13:55:15 -04:00 committed by GitHub
parent d60d96176f
commit 7aaa274f17
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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) {