From 63d98c8b4f0d9ebd2aba132ea7ac1e0c7c66175d Mon Sep 17 00:00:00 2001 From: Jure Skelin Date: Mon, 16 Dec 2024 16:11:07 +0100 Subject: [PATCH] test(segment): replace fake server with stub in test --- cmd/monaco/download/download_configs.go | 2 +- cmd/monaco/download/download_configs_test.go | 3 +- pkg/download/segment/download.go | 8 +- pkg/download/segment/download_test.go | 168 ++++++------------ .../segment/testdata/listResponse.json | 14 -- .../segment/testdata/uid_1_getResponse.json | 18 -- .../testdata/uid_1_getResponse_wo_uid.json | 17 -- .../segment/testdata/uid_2_getResponse.json | 18 -- 8 files changed, 66 insertions(+), 182 deletions(-) delete mode 100644 pkg/download/segment/testdata/listResponse.json delete mode 100644 pkg/download/segment/testdata/uid_1_getResponse.json delete mode 100644 pkg/download/segment/testdata/uid_1_getResponse_wo_uid.json delete mode 100644 pkg/download/segment/testdata/uid_2_getResponse.json diff --git a/cmd/monaco/download/download_configs.go b/cmd/monaco/download/download_configs.go index f79422a70..6262fee39 100644 --- a/cmd/monaco/download/download_configs.go +++ b/cmd/monaco/download/download_configs.go @@ -241,7 +241,7 @@ type downloadFn struct { bucketDownload func(client.BucketClient, string) (projectv2.ConfigsPerType, error) documentDownload func(client.DocumentClient, string) (projectv2.ConfigsPerType, error) openPipelineDownload func(client.OpenPipelineClient, string) (projectv2.ConfigsPerType, error) - segmentDownload func(client.SegmentClient, string) (projectv2.ConfigsPerType, error) + segmentDownload func(segment.Client, string) (projectv2.ConfigsPerType, error) } var defaultDownloadFn = downloadFn{ diff --git a/cmd/monaco/download/download_configs_test.go b/cmd/monaco/download/download_configs_test.go index f0e06d880..0c21bd9e8 100644 --- a/cmd/monaco/download/download_configs_test.go +++ b/cmd/monaco/download/download_configs_test.go @@ -34,6 +34,7 @@ import ( "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config" "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/coordinate" "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/download/classic" + "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/download/segment" "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/download/settings" "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/manifest" projectv2 "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/project/v2" @@ -330,7 +331,7 @@ func TestDownload_Options(t *testing.T) { } return nil, nil }, - segmentDownload: func(b client.SegmentClient, s string) (projectv2.ConfigsPerType, error) { + segmentDownload: func(b segment.Client, s string) (projectv2.ConfigsPerType, error) { if !tt.want.segment { t.Fatalf("segment download was not meant to be called but was") } diff --git a/pkg/download/segment/download.go b/pkg/download/segment/download.go index ee5b0e0c5..beb7ffb9c 100644 --- a/pkg/download/segment/download.go +++ b/pkg/download/segment/download.go @@ -21,9 +21,9 @@ import ( "fmt" "github.com/dynatrace/dynatrace-configuration-as-code-core/clients/openpipeline" + "github.com/dynatrace/dynatrace-configuration-as-code-core/clients/segments" "github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/log" "github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/log/field" - "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/client" "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config" "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/coordinate" "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/template" @@ -31,7 +31,11 @@ import ( project "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/project/v2" ) -func Download(client client.SegmentClient, projectName string) (project.ConfigsPerType, error) { +type Client interface { + GetAll(ctx context.Context) ([]segments.Response, error) +} + +func Download(client Client, projectName string) (project.ConfigsPerType, error) { result := project.ConfigsPerType{} downloadedConfigs, err := client.GetAll(context.TODO()) diff --git a/pkg/download/segment/download_test.go b/pkg/download/segment/download_test.go index 7c5141a6f..cea51a056 100644 --- a/pkg/download/segment/download_test.go +++ b/pkg/download/segment/download_test.go @@ -17,149 +17,95 @@ package segment_test import ( + "context" + "errors" "net/http" - "os" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/dynatrace/dynatrace-configuration-as-code-core/api/rest" coreLib "github.com/dynatrace/dynatrace-configuration-as-code-core/clients/segments" - "github.com/dynatrace/dynatrace-configuration-as-code-core/testutils" - "github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/featureflags" "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config" + "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/coordinate" "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/download/segment" ) +type stubClient struct { + getAll func() ([]coreLib.Response, error) +} + +func (s stubClient) GetAll(_ context.Context) ([]coreLib.Response, error) { + return s.getAll() +} + func TestDownloader_Download(t *testing.T) { t.Run("download segments works", func(t *testing.T) { - t.Setenv(featureflags.Temporary[featureflags.Segments].EnvName(), "true") - server := testutils.NewHTTPTestServer(t, []testutils.ResponseDef{ - { - GET: func(t *testing.T, req *http.Request) testutils.Response { - data, err := os.ReadFile("./testdata/listResponse.json") - assert.NoError(t, err) - - return testutils.Response{ - ResponseCode: http.StatusOK, - ResponseBody: string(data), - } - }, - ValidateRequest: func(t *testing.T, request *http.Request) { - assert.Equal(t, "/platform/storage/filter-segments/v1/filter-segments:lean", request.URL.Path) - assert.Equal(t, "add-fields=EXTERNALID", request.URL.RawQuery) - }, - }, - { - GET: func(t *testing.T, req *http.Request) testutils.Response { - data, err := os.ReadFile("./testdata/uid_1_getResponse.json") - assert.NoError(t, err) - - return testutils.Response{ - ResponseCode: http.StatusOK, - ResponseBody: string(data), - } - }, - ValidateRequest: func(t *testing.T, request *http.Request) { - assert.Equal(t, "/platform/storage/filter-segments/v1/filter-segments/uid_1", request.URL.Path) - assert.Equal(t, "add-fields=INCLUDES&add-fields=VARIABLES&add-fields=EXTERNALID&add-fields=RESOURCECONTEXT", request.URL.RawQuery) - }, - }, - { - GET: func(t *testing.T, req *http.Request) testutils.Response { - data, err := os.ReadFile("./testdata/uid_2_getResponse.json") - assert.NoError(t, err) - - return testutils.Response{ - ResponseCode: http.StatusOK, - ResponseBody: string(data), - } - }, - ValidateRequest: func(t *testing.T, request *http.Request) { - assert.Equal(t, "/platform/storage/filter-segments/v1/filter-segments/uid_2", request.URL.Path) - assert.Equal(t, "add-fields=INCLUDES&add-fields=VARIABLES&add-fields=EXTERNALID&add-fields=RESOURCECONTEXT", request.URL.RawQuery) + c := stubClient{getAll: func() ([]coreLib.Response, error) { + return []coreLib.Response{ + { + StatusCode: http.StatusOK, + Data: []byte(`{ + "uid": "uid", + "externalId": "some_external_ID", + "version": 1, + "name": "segment_name" +}`), }, - }, - }) - defer server.Close() + }, nil + }} - client := coreLib.NewClient(rest.NewClient(server.URL(), server.Client())) - result, err := segment.Download(client, "project") + result, err := segment.Download(c, "project") assert.NoError(t, err) assert.Len(t, result, 1) - require.Len(t, result[string(config.SegmentID)], 2, "all listed segments should be downloaded") + require.Len(t, result[string(config.SegmentID)], 1, "all listed segments should be downloaded") + + actual := result[string(config.SegmentID)][0] + + assert.Equal(t, config.Segment{}, actual.Type) + assert.Equal(t, coordinate.Coordinate{Project: "project", Type: "segment", ConfigId: "uid"}, actual.Coordinate) + assert.Equal(t, "uid", actual.OriginObjectId) + actualTemplate, err := actual.Template.Content() + assert.NoError(t, err) + assert.JSONEq(t, `{"name":"segment_name"}`, actualTemplate) + + assert.False(t, actual.Skip) + assert.Empty(t, actual.Group) + assert.Empty(t, actual.Environment) + assert.Empty(t, actual.Parameters) + assert.Empty(t, actual.SkipForConversion) }) t.Run("segment without uio is ignored", func(t *testing.T) { - t.Setenv(featureflags.Temporary[featureflags.Segments].EnvName(), "true") - server := testutils.NewHTTPTestServer(t, []testutils.ResponseDef{ - { - GET: func(t *testing.T, req *http.Request) testutils.Response { - data, err := os.ReadFile("./testdata/listResponse.json") - assert.NoError(t, err) - - return testutils.Response{ - ResponseCode: http.StatusOK, - ResponseBody: string(data), - } - }, - ValidateRequest: func(t *testing.T, request *http.Request) { - assert.Equal(t, "/platform/storage/filter-segments/v1/filter-segments:lean", request.URL.Path) - assert.Equal(t, "add-fields=EXTERNALID", request.URL.RawQuery) - }, - }, - { - GET: func(t *testing.T, req *http.Request) testutils.Response { - data, err := os.ReadFile("./testdata/uid_1_getResponse_wo_uid.json") - assert.NoError(t, err) - - return testutils.Response{ - ResponseCode: http.StatusOK, - ResponseBody: string(data), - } - }, - ValidateRequest: func(t *testing.T, request *http.Request) { - assert.Equal(t, "/platform/storage/filter-segments/v1/filter-segments/uid_1", request.URL.Path) - assert.Equal(t, "add-fields=INCLUDES&add-fields=VARIABLES&add-fields=EXTERNALID&add-fields=RESOURCECONTEXT", request.URL.RawQuery) - }, - }, - { - GET: func(t *testing.T, req *http.Request) testutils.Response { - data, err := os.ReadFile("./testdata/uid_2_getResponse.json") - assert.NoError(t, err) - - return testutils.Response{ - ResponseCode: http.StatusOK, - ResponseBody: string(data), - } - }, - ValidateRequest: func(t *testing.T, request *http.Request) { - assert.Equal(t, "/platform/storage/filter-segments/v1/filter-segments/uid_2", request.URL.Path) - assert.Equal(t, "add-fields=INCLUDES&add-fields=VARIABLES&add-fields=EXTERNALID&add-fields=RESOURCECONTEXT", request.URL.RawQuery) + c := stubClient{getAll: func() ([]coreLib.Response, error) { + return []coreLib.Response{ + { + StatusCode: http.StatusOK, + Data: []byte(`{ + "externalId": "some_external_ID", + "version": 1, + "name": "segment_name" +}`), }, - }, - }) - defer server.Close() + }, nil + }} - client := coreLib.NewClient(rest.NewClient(server.URL(), server.Client())) - result, err := segment.Download(client, "project") + result, err := segment.Download(c, "project") assert.NoError(t, err) assert.Len(t, result, 1) - assert.Len(t, result[string(config.SegmentID)], 1, "all listed segments should be downloaded") - assert.Equal(t, "uid_2", result[string(config.SegmentID)][0].OriginObjectId) + assert.Empty(t, result[string(config.SegmentID)]) }) t.Run("no error downloading segments with faulty client", func(t *testing.T) { - server := testutils.NewHTTPTestServer(t, []testutils.ResponseDef{}) - defer server.Close() + c := stubClient{getAll: func() ([]coreLib.Response, error) { + return []coreLib.Response{}, errors.New("some unexpected error") + }} - client := coreLib.NewClient(rest.NewClient(server.URL(), server.FaultyClient())) - result, err := segment.Download(client, "project") + result, err := segment.Download(c, "project") assert.NoError(t, err) assert.Empty(t, result) }) diff --git a/pkg/download/segment/testdata/listResponse.json b/pkg/download/segment/testdata/listResponse.json deleted file mode 100644 index 59a1a204a..000000000 --- a/pkg/download/segment/testdata/listResponse.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "filterSegments": [ - { - "uid": "uid_1", - "name": "private_segment", - "isPublic": false - }, - { - "uid": "uid_2", - "name": "public_segment", - "isPublic": true - } - ] -} diff --git a/pkg/download/segment/testdata/uid_1_getResponse.json b/pkg/download/segment/testdata/uid_1_getResponse.json deleted file mode 100644 index 36ea099a4..000000000 --- a/pkg/download/segment/testdata/uid_1_getResponse.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "uid": "uid_1", - "name": "private_segment", - "description": "description for private test segment", - "variables": { - "type": "query", - "value": "fetch logs | limit 1" - }, - "isPublic": false, - "owner": "bd62720e-e72a-49c6-aff2-43ca82662bee", - "allowedOperations": [ - "READ", - "WRITE", - "DELETE" - ], - "includes": [], - "version": 1 -} diff --git a/pkg/download/segment/testdata/uid_1_getResponse_wo_uid.json b/pkg/download/segment/testdata/uid_1_getResponse_wo_uid.json deleted file mode 100644 index 68c474f9c..000000000 --- a/pkg/download/segment/testdata/uid_1_getResponse_wo_uid.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "name": "private_segment", - "description": "description for private test segment", - "variables": { - "type": "query", - "value": "fetch logs | limit 1" - }, - "isPublic": false, - "owner": "bd62720e-e72a-49c6-aff2-43ca82662bee", - "allowedOperations": [ - "READ", - "WRITE", - "DELETE" - ], - "includes": [], - "version": 1 -} diff --git a/pkg/download/segment/testdata/uid_2_getResponse.json b/pkg/download/segment/testdata/uid_2_getResponse.json deleted file mode 100644 index 6c353ddc3..000000000 --- a/pkg/download/segment/testdata/uid_2_getResponse.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "uid": "uid_2", - "name": "public_segment", - "description": "description for public test segment", - "variables": { - "type": "query", - "value": "fetch logs | limit 1" - }, - "isPublic": true, - "owner": "bd62720e-e72a-49c6-aff2-43ca82662bee", - "allowedOperations": [ - "READ", - "WRITE", - "DELETE" - ], - "includes": [], - "version": 888 -}