From 8432156fa3f9a46c820e64c674e1c965a0c64c85 Mon Sep 17 00:00:00 2001 From: Mateusz Hawrus Date: Tue, 9 Jul 2024 13:06:58 +0200 Subject: [PATCH] add e2e tests --- tests/v1alpha_alertpolicy_test.go | 150 ++++++++++++++++++++++++++++++ tests/v1alpha_project_test.go | 2 +- tests/v1alpha_service_test.go | 2 +- 3 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 tests/v1alpha_alertpolicy_test.go diff --git a/tests/v1alpha_alertpolicy_test.go b/tests/v1alpha_alertpolicy_test.go new file mode 100644 index 00000000..3c5c7146 --- /dev/null +++ b/tests/v1alpha_alertpolicy_test.go @@ -0,0 +1,150 @@ +//go:build e2e_test + +package tests + +import ( + "context" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + v1alphaExamples "github.com/nobl9/nobl9-go/internal/manifest/v1alpha/examples" + "github.com/nobl9/nobl9-go/manifest" + "github.com/nobl9/nobl9-go/manifest/v1alpha" + v1alphaAlertMethod "github.com/nobl9/nobl9-go/manifest/v1alpha/alertmethod" + v1alphaAlertPolicy "github.com/nobl9/nobl9-go/manifest/v1alpha/alertpolicy" + "github.com/nobl9/nobl9-go/sdk" + objectsV1 "github.com/nobl9/nobl9-go/sdk/endpoints/objects/v1" +) + +func Test_Objects_V1_V1alpha_AlertPolicy(t *testing.T) { + t.Parallel() + ctx := context.Background() + project := generateV1alphaProject(t) + alertMethod := newV1alphaAlertMethod(t, v1alpha.AlertMethodTypeSlack, v1alphaAlertMethod.Metadata{ + Name: generateName(), + DisplayName: "Alert Method", + Project: project.GetName(), + }) + examples := v1alphaExamples.AlertPolicy() + allObjects := make([]manifest.Object, 0, len(examples)+2) + allObjects = append(allObjects, project) + allObjects = append(allObjects, alertMethod) + + for i, example := range examples { + policy := newV1alphaAlertPolicy(t, + v1alphaAlertPolicy.Metadata{ + Name: generateName(), + DisplayName: fmt.Sprintf("Alert Policy %d", i), + Project: project.GetName(), + }, + example.GetVariant(), + example.GetSubVariant(), + ) + policy.Spec.AlertMethods = []v1alphaAlertPolicy.AlertMethodRef{ + { + Metadata: v1alphaAlertPolicy.AlertMethodRefMetadata{ + Name: alertMethod.Metadata.Name, + Project: alertMethod.Metadata.Project, + }, + }, + } + for i := range policy.Spec.Conditions { + if policy.Spec.Conditions[i].AlertingWindow == "" && policy.Spec.Conditions[i].LastsForDuration == "" { + policy.Spec.Conditions[i].LastsForDuration = "0m" + } + } + switch i { + case 0: + policy.Metadata.Project = defaultProject + case 1: + policy.Metadata.Labels["team"] = []string{"green"} + case 2: + policy.Metadata.Labels["team"] = []string{"orange"} + case 3: + policy.Metadata.Labels["team"] = []string{"orange"} + } + allObjects = append(allObjects, policy) + } + + v1Apply(t, ctx, allObjects) + t.Cleanup(func() { v1Delete(t, ctx, allObjects) }) + inputs := manifest.FilterByKind[v1alphaAlertPolicy.AlertPolicy](allObjects) + + filterTests := map[string]struct { + request objectsV1.GetAlertPolicyRequest + expected []v1alphaAlertPolicy.AlertPolicy + returnsAll bool + }{ + "all": { + request: objectsV1.GetAlertPolicyRequest{Project: sdk.ProjectsWildcard}, + expected: manifest.FilterByKind[v1alphaAlertPolicy.AlertPolicy](allObjects), + returnsAll: true, + }, + "default project": { + request: objectsV1.GetAlertPolicyRequest{}, + expected: []v1alphaAlertPolicy.AlertPolicy{inputs[0]}, + returnsAll: true, + }, + "filter by project": { + request: objectsV1.GetAlertPolicyRequest{ + Project: project.GetName(), + }, + expected: inputs[1:], + }, + "filter by name": { + request: objectsV1.GetAlertPolicyRequest{ + Project: project.GetName(), + Names: []string{inputs[4].Metadata.Name}, + }, + expected: []v1alphaAlertPolicy.AlertPolicy{inputs[4]}, + }, + "filter by label": { + request: objectsV1.GetAlertPolicyRequest{ + Project: project.GetName(), + Labels: annotateLabels(t, v1alpha.Labels{"team": []string{"green"}}), + }, + expected: []v1alphaAlertPolicy.AlertPolicy{inputs[1]}, + }, + "filter by label and name": { + request: objectsV1.GetAlertPolicyRequest{ + Project: project.GetName(), + Names: []string{inputs[3].Metadata.Name}, + Labels: annotateLabels(t, v1alpha.Labels{"team": []string{"orange"}}), + }, + expected: []v1alphaAlertPolicy.AlertPolicy{inputs[3]}, + }, + } + for name, test := range filterTests { + t.Run(name, func(t *testing.T) { + t.Parallel() + actual, err := client.Objects().V1().GetV1alphaAlertPolicies(ctx, test.request) + require.NoError(t, err) + if !test.returnsAll { + require.Len(t, actual, len(test.expected)) + } + assertSubset(t, actual, test.expected, assertV1alphaAlertPoliciesAreEqual) + }) + } +} + +func newV1alphaAlertPolicy( + t *testing.T, + metadata v1alphaAlertPolicy.Metadata, + variant, + subVariant string, +) v1alphaAlertPolicy.AlertPolicy { + t.Helper() + metadata.Labels = annotateLabels(t, metadata.Labels) + metadata.Annotations = commonAnnotations + ap := getExample[v1alphaAlertPolicy.AlertPolicy](t, manifest.KindAlertPolicy, variant, subVariant) + ap.Spec.Description = objectDescription + return v1alphaAlertPolicy.New(metadata, ap.Spec) +} + +func assertV1alphaAlertPoliciesAreEqual(t *testing.T, expected, actual v1alphaAlertPolicy.AlertPolicy) { + t.Helper() + assert.Equal(t, expected, actual) +} diff --git a/tests/v1alpha_project_test.go b/tests/v1alpha_project_test.go index f6d384d5..1004c50d 100644 --- a/tests/v1alpha_project_test.go +++ b/tests/v1alpha_project_test.go @@ -92,7 +92,7 @@ func newV1alphaProject( metadata v1alphaProject.Metadata, ) v1alphaProject.Project { t.Helper() - annotateLabels(t, metadata.Labels) + metadata.Labels = annotateLabels(t, metadata.Labels) metadata.Annotations = commonAnnotations return v1alphaProject.New(metadata, v1alphaProject.Spec{Description: objectDescription}) } diff --git a/tests/v1alpha_service_test.go b/tests/v1alpha_service_test.go index 0f77661c..048b763d 100644 --- a/tests/v1alpha_service_test.go +++ b/tests/v1alpha_service_test.go @@ -125,7 +125,7 @@ func newV1alphaService( metadata v1alphaService.Metadata, ) v1alphaService.Service { t.Helper() - annotateLabels(t, metadata.Labels) + metadata.Labels = annotateLabels(t, metadata.Labels) metadata.Annotations = commonAnnotations return v1alphaService.New(metadata, v1alphaService.Spec{Description: objectDescription}) }