From 82e252b61e8e563a969b5f88521824538eda9c3f Mon Sep 17 00:00:00 2001 From: danielsinai Date: Wed, 15 May 2024 14:46:13 +0300 Subject: [PATCH] tests: fixes --- pkg/crd/crd.go | 2 +- pkg/crd/crd_test.go | 14 +++++++------- pkg/defaults/defaults_test.go | 14 +++++++------- pkg/port/cli/action.go | 18 ++++++++++++++++-- test_utils/cleanup.go | 14 +++++++++++++- 5 files changed, 44 insertions(+), 18 deletions(-) diff --git a/pkg/crd/crd.go b/pkg/crd/crd.go index f259578..0d25abe 100644 --- a/pkg/crd/crd.go +++ b/pkg/crd/crd.go @@ -349,7 +349,7 @@ func handleNestedSchema(schema *v1.JSONSchemaProps, parent string, originalSchem if v.Type != "object" { originalSchema.Properties[shallowedKey] = v - if slices.Contains(originalSchema.Required, strings.Split(shallowedKey, NestedSchemaSeperator)[0]) { + if shallowedKey != k && slices.Contains(originalSchema.Required, strings.Split(shallowedKey, NestedSchemaSeperator)[0]) { originalSchema.Required = append(originalSchema.Required, shallowedKey) originalSchema.Required = goutils.Filter(originalSchema.Required, strings.Split(shallowedKey, NestedSchemaSeperator)[0]) } diff --git a/pkg/crd/crd_test.go b/pkg/crd/crd_test.go index 51ba8d0..c61c5ad 100644 --- a/pkg/crd/crd_test.go +++ b/pkg/crd/crd_test.go @@ -162,7 +162,7 @@ func checkBlueprintAndActionsProperties(t *testing.T, f *Fixture, namespaced boo } }) - createAction, err := cli.GetAction(f.portClient, "testkind", "create_testkind") + createAction, err := cli.GetAction(f.portClient, "create_testkind") if err != nil { t.Errorf("Error getting create action: %s", err.Error()) } @@ -208,7 +208,7 @@ func checkBlueprintAndActionsProperties(t *testing.T, f *Fixture, namespaced boo } }) - updateAction, err := cli.GetAction(f.portClient, "testkind", "update_testkind") + updateAction, err := cli.GetAction(f.portClient, "update_testkind") if err != nil { t.Errorf("Error getting update action: %s", err.Error()) } @@ -254,7 +254,7 @@ func checkBlueprintAndActionsProperties(t *testing.T, f *Fixture, namespaced boo } }) - deleteAction, err := cli.GetAction(f.portClient, "testkind", "delete_testkind") + deleteAction, err := cli.GetAction(f.portClient, "delete_testkind") if err != nil { t.Errorf("Error getting delete action: %s", err.Error()) } @@ -264,7 +264,7 @@ func checkBlueprintAndActionsProperties(t *testing.T, f *Fixture, namespaced boo } // Delete action takes the namespace using control the payload feature if namespaced { - if _, ok := updateAction.Trigger.UserInputs.Properties["namespace"]; ok { + if _, ok := deleteAction.Trigger.UserInputs.Properties["namespace"]; ok { t.Errorf("namespace should not be present") } } else { @@ -282,7 +282,7 @@ func TestCRD_crd_autoDiscoverCRDsToActionsClusterScoped(t *testing.T) { checkBlueprintAndActionsProperties(t, f, false) - testUtils.CheckResourcesExistence(true, f.portClient, t, []string{"testkind"}, []string{}) + testUtils.CheckResourcesExistence(true, f.portClient, t, []string{"testkind"}, []string{}, []string{"create_testkind", "update_testkind", "delete_testkind"}) } func TestCRD_crd_autoDiscoverCRDsToActionsNamespaced(t *testing.T) { @@ -292,7 +292,7 @@ func TestCRD_crd_autoDiscoverCRDsToActionsNamespaced(t *testing.T) { checkBlueprintAndActionsProperties(t, f, true) - testUtils.CheckResourcesExistence(true, f.portClient, t, []string{"testkind"}, []string{}) + testUtils.CheckResourcesExistence(true, f.portClient, t, []string{"testkind"}, []string{}, []string{"create_testkind", "update_testkind", "delete_testkind"}) } func TestCRD_crd_autoDiscoverCRDsToActionsNoCRDs(t *testing.T) { @@ -300,5 +300,5 @@ func TestCRD_crd_autoDiscoverCRDsToActionsNoCRDs(t *testing.T) { AutodiscoverCRDsToActions(f.portConfig, f.apiextensionClient, f.portClient) - testUtils.CheckResourcesExistence(false, f.portClient, t, []string{"testkind"}, []string{}) + testUtils.CheckResourcesExistence(false, f.portClient, t, []string{"testkind"}, []string{}, []string{"create_testkind", "update_testkind", "delete_testkind"}) } diff --git a/pkg/defaults/defaults_test.go b/pkg/defaults/defaults_test.go index c262fee..32ed744 100644 --- a/pkg/defaults/defaults_test.go +++ b/pkg/defaults/defaults_test.go @@ -100,7 +100,7 @@ func Test_InitIntegration_InitDefaults_CreateDefaultResources_False(t *testing.T _, err := integration.GetIntegration(f.portClient, f.stateKey) assert.Nil(t, err) - testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}) + testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{}) } func Test_InitIntegration_BlueprintExists(t *testing.T) { @@ -128,7 +128,7 @@ func Test_InitIntegration_BlueprintExists(t *testing.T) { _, err = blueprint.GetBlueprint(f.portClient, "workload") assert.Nil(t, err) - testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}) + testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{}) } func Test_InitIntegration_PageExists(t *testing.T) { @@ -153,7 +153,7 @@ func Test_InitIntegration_PageExists(t *testing.T) { _, err = page.GetPage(f.portClient, "workload_overview_dashboard") assert.Nil(t, err) - testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"availability_scorecard_dashboard"}) + testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"availability_scorecard_dashboard"}, []string{}) } func Test_InitIntegration_ExistingIntegration(t *testing.T) { @@ -172,7 +172,7 @@ func Test_InitIntegration_ExistingIntegration(t *testing.T) { _, err = integration.GetIntegration(f.portClient, f.stateKey) assert.Nil(t, err) - testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}) + testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{}) } func Test_InitIntegration_LocalResourcesConfiguration(t *testing.T) { @@ -213,7 +213,7 @@ func Test_InitIntegration_LocalResourcesConfiguration(t *testing.T) { assert.Equal(t, expectedResources, i.Config.Resources) assert.Nil(t, err) - testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}) + testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{}) } func Test_InitIntegration_LocalResourcesConfiguration_ExistingIntegration_EmptyConfiguration(t *testing.T) { @@ -234,7 +234,7 @@ func Test_InitIntegration_LocalResourcesConfiguration_ExistingIntegration_EmptyC assert.Nil(t, err) assert.Equal(t, "KAFKA", i.EventListener.Type) - testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}) + testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{}) } func Test_InitIntegration_LocalResourcesConfiguration_ExistingIntegration_WithConfiguration_WithOverwriteConfigurationOnRestartFlag(t *testing.T) { @@ -280,5 +280,5 @@ func Test_InitIntegration_LocalResourcesConfiguration_ExistingIntegration_WithCo assert.Nil(t, err) assert.Equal(t, expectedConfig.Resources, i.Config.Resources) - testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}) + testUtils.CheckResourcesExistence(false, f.portClient, f.t, []string{"workload", "namespace", "cluster"}, []string{"workload_overview_dashboard", "availability_scorecard_dashboard"}, []string{}) } diff --git a/pkg/port/cli/action.go b/pkg/port/cli/action.go index 40d97e2..e50f5bc 100644 --- a/pkg/port/cli/action.go +++ b/pkg/port/cli/action.go @@ -36,11 +36,11 @@ func UpdateAction(portClient *PortClient, action port.Action) (*port.Action, err return &pb.Action, nil } -func GetAction(portClient *PortClient, blueprintIdentifier string, actionIdentifier string) (*port.Action, error) { +func GetAction(portClient *PortClient, actionIdentifier string) (*port.Action, error) { pb := &port.ResponseBody{} resp, err := portClient.Client.R(). SetResult(&pb). - Get(fmt.Sprintf("v1/blueprints/%s/actions/%s", blueprintIdentifier, actionIdentifier)) + Get(fmt.Sprintf("v1/actions/%s", actionIdentifier)) if err != nil { return nil, err } @@ -49,3 +49,17 @@ func GetAction(portClient *PortClient, blueprintIdentifier string, actionIdentif } return &pb.Action, nil } + +func DeleteAction(portClient *PortClient, actionIdentifier string) error { + pb := &port.ResponseBody{} + resp, err := portClient.Client.R(). + SetResult(&pb). + Delete(fmt.Sprintf("v1/actions/%s", actionIdentifier)) + if err != nil { + return err + } + if !pb.OK { + return fmt.Errorf("failed to delete action, got: %s", resp.Body()) + } + return nil +} diff --git a/test_utils/cleanup.go b/test_utils/cleanup.go index 39ff260..2f12c25 100644 --- a/test_utils/cleanup.go +++ b/test_utils/cleanup.go @@ -9,7 +9,19 @@ import ( "github.com/stretchr/testify/assert" ) -func CheckResourcesExistence(shouldExist bool, portClient *cli.PortClient, t *testing.T, blueprints []string, pages []string) { +func CheckResourcesExistence(shouldExist bool, portClient *cli.PortClient, t *testing.T, blueprints []string, pages []string, actions []string) { + for _, a := range actions { + _, err := cli.GetAction(portClient, a) + if err == nil { + _ = cli.DeleteAction(portClient, a) + } + if shouldExist { + assert.Nil(t, err) + } else { + assert.NotNil(t, err) + } + } + for _, bp := range blueprints { _, err := blueprint.GetBlueprint(portClient, bp) if err == nil {