Skip to content

Commit

Permalink
tests: fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsinai committed May 15, 2024
1 parent 4e16fdb commit 82e252b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pkg/crd/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/crd/crd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -292,13 +292,13 @@ 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) {
f := newFixture(t, "", "", "", false, "false")

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"})
}
14 changes: 7 additions & 7 deletions pkg/defaults/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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{})
}
18 changes: 16 additions & 2 deletions pkg/port/cli/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
14 changes: 13 additions & 1 deletion test_utils/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 82e252b

Please sign in to comment.