From c0d65e66b7d62b2761c09f3ae716c127e192354c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= Date: Tue, 17 Dec 2024 12:45:33 +0100 Subject: [PATCH] tests: refactor controlplane tests, add list tests --- test/integration/controlplane_test.go | 131 ++++++++++++++++++-------- test/integration/funcs.go | 5 + 2 files changed, 99 insertions(+), 37 deletions(-) diff --git a/test/integration/controlplane_test.go b/test/integration/controlplane_test.go index 4e9ede5..781a946 100644 --- a/test/integration/controlplane_test.go +++ b/test/integration/controlplane_test.go @@ -4,8 +4,10 @@ import ( "context" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + sdkkonnectgo "github.com/Kong/sdk-konnect-go" sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations" ) @@ -46,43 +48,98 @@ func TestControlPlaneList(t *testing.T) { sdk := SDK(t) - ctx := context.Background() - reqList := sdkkonnectops.ListControlPlanesRequest{ - // TODO listing doesn't work with criteria yet. + testcases := []struct { + name string + createFunc func(ctx context.Context, t *testing.T, sdk *sdkkonnectgo.ControlPlanes) + req sdkkonnectops.ListControlPlanesRequest + expectedError bool + assert func(t *testing.T, resp *sdkkonnectops.ListControlPlanesResponse) + }{ + { + name: "no filter", + req: sdkkonnectops.ListControlPlanesRequest{}, + assert: func(t *testing.T, resp *sdkkonnectops.ListControlPlanesResponse) { + require.NotNil(t, resp) + }, + }, + { + name: "filter by name eq", + createFunc: func(ctx context.Context, t *testing.T, sdk *sdkkonnectgo.ControlPlanes) { + req := sdkkonnectcomp.CreateControlPlaneRequest{ + Name: KonnectTestRunID(t) + "-filter-1", + } + resp, err := sdk.CreateControlPlane(ctx, req) + require.NoError(t, err) + t.Cleanup(func() { + _, err := sdk.DeleteControlPlane(ctx, resp.ControlPlane.ID) + assert.NoError(t, err) + }) + }, + req: sdkkonnectops.ListControlPlanesRequest{ + Filter: &sdkkonnectcomp.ControlPlaneFilterParameters{ + Name: &sdkkonnectcomp.Name{ + StringFieldEqualsFilter: &sdkkonnectcomp.StringFieldEqualsFilter{ + Str: Ptr(KonnectTestRunID(t) + "-filter-1"), + }, + }, + }, + }, + assert: func(t *testing.T, resp *sdkkonnectops.ListControlPlanesResponse) { + require.NotNil(t, resp) + require.Len(t, resp.ListControlPlanesResponse.Data, 1) + }, + }, + { + name: "filter by name and cluster type", + createFunc: func(ctx context.Context, t *testing.T, sdk *sdkkonnectgo.ControlPlanes) { + req := sdkkonnectcomp.CreateControlPlaneRequest{ + Name: KonnectTestRunID(t) + "-type-1", + ClusterType: Ptr(sdkkonnectcomp.CreateControlPlaneRequestClusterTypeClusterTypeK8SIngressController), + } + resp, err := sdk.CreateControlPlane(ctx, req) + require.NoError(t, err) + t.Cleanup(func() { + _, err := sdk.DeleteControlPlane(ctx, resp.ControlPlane.ID) + assert.NoError(t, err) + }) + }, + req: sdkkonnectops.ListControlPlanesRequest{ + Filter: &sdkkonnectcomp.ControlPlaneFilterParameters{ + Name: &sdkkonnectcomp.Name{ + StringFieldEqualsFilter: &sdkkonnectcomp.StringFieldEqualsFilter{ + Str: Ptr(KonnectTestRunID(t) + "-type-1"), + }, + }, + ClusterType: &sdkkonnectcomp.ClusterType{ + StringFieldEqualsFilter: &sdkkonnectcomp.StringFieldEqualsFilter{ + Str: Ptr(string(sdkkonnectcomp.CreateControlPlaneRequestClusterTypeClusterTypeK8SIngressController)), + }, + }, + }, + }, + assert: func(t *testing.T, resp *sdkkonnectops.ListControlPlanesResponse) { + require.NotNil(t, resp) + require.Len(t, resp.ListControlPlanesResponse.Data, 1) + }, + }, } - respList, err := sdk.ControlPlanes.ListControlPlanes(ctx, reqList) - require.NoError(t, err) - require.NotNil(t, respList.ListControlPlanesResponse) - - // TODO listing doesn't work with criteria yet. - - // require.Empty(t, respList.ListControlPlanesResponse.Data) - // req := sdkkonnectcomp.CreateControlPlaneRequest{ - // Name: cpName, - // Labels: Labels(t), - // } - // resp, err := sdk.ControlPlanes.CreateControlPlane(ctx, req) - // require.NoError(t, err) - // t.Cleanup(func() { - // _, err := sdk.ControlPlanes.DeleteControlPlane(ctx, resp.ControlPlane.ID) - // require.NoError(t, err) - // }) - - // require.NotNil(t, resp) - - // reqList = sdkkonnectops.ListControlPlanesRequest{ - // Filter: &sdkkonnectcomp.ControlPlaneFilterParameters{ - // ID: &sdkkonnectcomp.ID{ - // StringFieldOEQFilter: &sdkkonnectcomp.StringFieldOEQFilter{ - // Oeq: resp.ControlPlane.GetID(), - // }, - // }, - // }, - // } - // respList, err = sdk.ControlPlanes.ListControlPlanes(ctx, reqList) - // require.NoError(t, err) - // require.NotEmpty(t, respList.ListControlPlanesResponse.Data) - // require.Len(t, respList.ListControlPlanesResponse.Data, 1) - // require.Equal(t, respList.ListControlPlanesResponse.Data[0].ID, resp.ControlPlane.ID) + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + ctx := context.Background() + if tc.createFunc != nil { + tc.createFunc(ctx, t, sdk.ControlPlanes) + } + respList, err := sdk.ControlPlanes.ListControlPlanes(ctx, tc.req) + if tc.expectedError { + require.Error(t, err) + return + } + + require.NoError(t, err) + if tc.assert != nil { + tc.assert(t, respList) + } + }) + } } diff --git a/test/integration/funcs.go b/test/integration/funcs.go index ef5a18d..97fe08e 100644 --- a/test/integration/funcs.go +++ b/test/integration/funcs.go @@ -14,3 +14,8 @@ func Labels(t *testing.T) map[string]string { "test_run_id": KonnectTestRunID(t), } } + +// Ptr returns a pointer to the given object. +func Ptr[T any](obj T) *T { + return &obj +}