Skip to content

Commit

Permalink
test(segment): replace fake server with stub in test
Browse files Browse the repository at this point in the history
  • Loading branch information
jskelin committed Dec 20, 2024
1 parent f1bae4a commit 63d98c8
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 182 deletions.
2 changes: 1 addition & 1 deletion cmd/monaco/download/download_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
3 changes: 2 additions & 1 deletion cmd/monaco/download/download_configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/download/segment/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@ 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"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/download/internal/templatetools"
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())
Expand Down
168 changes: 57 additions & 111 deletions pkg/download/segment/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
14 changes: 0 additions & 14 deletions pkg/download/segment/testdata/listResponse.json

This file was deleted.

18 changes: 0 additions & 18 deletions pkg/download/segment/testdata/uid_1_getResponse.json

This file was deleted.

17 changes: 0 additions & 17 deletions pkg/download/segment/testdata/uid_1_getResponse_wo_uid.json

This file was deleted.

18 changes: 0 additions & 18 deletions pkg/download/segment/testdata/uid_2_getResponse.json

This file was deleted.

0 comments on commit 63d98c8

Please sign in to comment.