From e98dfa50f8f39c8197c55d4be05bc10c51f4e500 Mon Sep 17 00:00:00 2001 From: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com> Date: Tue, 31 Dec 2024 12:21:05 +0500 Subject: [PATCH] fixed github issue 3819 for endpoint customizer tests (#3823) * fixed github issue 3819 for endpoint customizer tests * added negative test cases * improved comments * using ahrav's approach --- pkg/detectors/endpoint_customizer_test.go | 52 +++++++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/pkg/detectors/endpoint_customizer_test.go b/pkg/detectors/endpoint_customizer_test.go index 229cf915a..0f54118b5 100644 --- a/pkg/detectors/endpoint_customizer_test.go +++ b/pkg/detectors/endpoint_customizer_test.go @@ -8,9 +8,53 @@ import ( func TestEmbeddedEndpointSetter(t *testing.T) { type Scanner struct{ EndpointSetter } + var s Scanner - assert.Equal(t, []string{"baz"}, s.Endpoints("baz")) - assert.NoError(t, s.SetConfiguredEndpoints("foo", "bar")) - assert.Error(t, s.SetConfiguredEndpoints()) - assert.Equal(t, []string{"foo", "bar"}, s.Endpoints("baz")) + + t.Run("useFoundEndpoints is true", func(t *testing.T) { + s.useFoundEndpoints = true + + // "baz" is passed to Endpoints, should appear in the result + assert.Equal(t, []string{"baz"}, s.Endpoints("baz")) + }) + + t.Run("setting configured endpoints", func(t *testing.T) { + // Setting "foo" and "bar" + assert.NoError(t, s.SetConfiguredEndpoints("foo", "bar")) + + // Returning error because no endpoints are passed + assert.Error(t, s.SetConfiguredEndpoints()) + }) + + // "foo" and "bar" are added as configured endpoint + + t.Run("useFoundEndpoints adds new endpoints", func(t *testing.T) { + // "baz" is added because useFoundEndpoints is true + assert.Equal(t, []string{"foo", "bar", "baz"}, s.Endpoints("baz")) + }) + + t.Run("useCloudEndpoint is true", func(t *testing.T) { + s.useCloudEndpoint = true + s.cloudEndpoint = "test" + + // "test" is added because useCloudEndpoint is true and cloudEndpoint is set + assert.Equal(t, []string{"foo", "bar", "test"}, s.Endpoints()) + }) + + t.Run("disable both foundEndpoints and cloudEndpoint", func(t *testing.T) { + // now disable both useFoundEndpoints and useCloudEndpoint + s.useFoundEndpoints = false + s.useCloudEndpoint = false + + // "test" won't be added + assert.Equal(t, []string{"foo", "bar"}, s.Endpoints("test")) + }) + + t.Run("cloudEndpoint not added when useCloudEndpoint is false", func(t *testing.T) { + s.cloudEndpoint = "new" + + // "new" is not added because useCloudEndpoint is false + assert.Equal(t, []string{"foo", "bar"}, s.Endpoints()) + }) + }