|
| 1 | +//go:build e2e_test |
| 2 | + |
| 3 | +package tests |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + v1alphaExamples "github.com/nobl9/nobl9-go/internal/manifest/v1alpha/examples" |
| 14 | + "github.com/nobl9/nobl9-go/manifest" |
| 15 | + "github.com/nobl9/nobl9-go/manifest/v1alpha" |
| 16 | + v1alphaAlertMethod "github.com/nobl9/nobl9-go/manifest/v1alpha/alertmethod" |
| 17 | + v1alphaAlertPolicy "github.com/nobl9/nobl9-go/manifest/v1alpha/alertpolicy" |
| 18 | + "github.com/nobl9/nobl9-go/sdk" |
| 19 | + objectsV1 "github.com/nobl9/nobl9-go/sdk/endpoints/objects/v1" |
| 20 | +) |
| 21 | + |
| 22 | +func Test_Objects_V1_V1alpha_AlertPolicy(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + ctx := context.Background() |
| 25 | + project := generateV1alphaProject(t) |
| 26 | + alertMethod := newV1alphaAlertMethod(t, v1alpha.AlertMethodTypeSlack, v1alphaAlertMethod.Metadata{ |
| 27 | + Name: generateName(), |
| 28 | + DisplayName: "Alert Method", |
| 29 | + Project: project.GetName(), |
| 30 | + }) |
| 31 | + examples := v1alphaExamples.AlertPolicy() |
| 32 | + allObjects := make([]manifest.Object, 0, len(examples)+2) |
| 33 | + allObjects = append(allObjects, project) |
| 34 | + allObjects = append(allObjects, alertMethod) |
| 35 | + |
| 36 | + for i, example := range examples { |
| 37 | + policy := newV1alphaAlertPolicy(t, |
| 38 | + v1alphaAlertPolicy.Metadata{ |
| 39 | + Name: generateName(), |
| 40 | + DisplayName: fmt.Sprintf("Alert Policy %d", i), |
| 41 | + Project: project.GetName(), |
| 42 | + }, |
| 43 | + example.GetVariant(), |
| 44 | + example.GetSubVariant(), |
| 45 | + ) |
| 46 | + policy.Spec.AlertMethods = []v1alphaAlertPolicy.AlertMethodRef{ |
| 47 | + { |
| 48 | + Metadata: v1alphaAlertPolicy.AlertMethodRefMetadata{ |
| 49 | + Name: alertMethod.Metadata.Name, |
| 50 | + Project: alertMethod.Metadata.Project, |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + for i := range policy.Spec.Conditions { |
| 55 | + if policy.Spec.Conditions[i].AlertingWindow == "" && policy.Spec.Conditions[i].LastsForDuration == "" { |
| 56 | + policy.Spec.Conditions[i].LastsForDuration = "0m" |
| 57 | + } |
| 58 | + } |
| 59 | + switch i { |
| 60 | + case 0: |
| 61 | + policy.Metadata.Project = defaultProject |
| 62 | + case 1: |
| 63 | + policy.Metadata.Labels["team"] = []string{"green"} |
| 64 | + case 2: |
| 65 | + policy.Metadata.Labels["team"] = []string{"orange"} |
| 66 | + case 3: |
| 67 | + policy.Metadata.Labels["team"] = []string{"orange"} |
| 68 | + } |
| 69 | + allObjects = append(allObjects, policy) |
| 70 | + } |
| 71 | + |
| 72 | + v1Apply(t, ctx, allObjects) |
| 73 | + t.Cleanup(func() { v1Delete(t, ctx, allObjects) }) |
| 74 | + inputs := manifest.FilterByKind[v1alphaAlertPolicy.AlertPolicy](allObjects) |
| 75 | + |
| 76 | + filterTests := map[string]struct { |
| 77 | + request objectsV1.GetAlertPolicyRequest |
| 78 | + expected []v1alphaAlertPolicy.AlertPolicy |
| 79 | + returnsAll bool |
| 80 | + }{ |
| 81 | + "all": { |
| 82 | + request: objectsV1.GetAlertPolicyRequest{Project: sdk.ProjectsWildcard}, |
| 83 | + expected: manifest.FilterByKind[v1alphaAlertPolicy.AlertPolicy](allObjects), |
| 84 | + returnsAll: true, |
| 85 | + }, |
| 86 | + "default project": { |
| 87 | + request: objectsV1.GetAlertPolicyRequest{}, |
| 88 | + expected: []v1alphaAlertPolicy.AlertPolicy{inputs[0]}, |
| 89 | + returnsAll: true, |
| 90 | + }, |
| 91 | + "filter by project": { |
| 92 | + request: objectsV1.GetAlertPolicyRequest{ |
| 93 | + Project: project.GetName(), |
| 94 | + }, |
| 95 | + expected: inputs[1:], |
| 96 | + }, |
| 97 | + "filter by name": { |
| 98 | + request: objectsV1.GetAlertPolicyRequest{ |
| 99 | + Project: project.GetName(), |
| 100 | + Names: []string{inputs[4].Metadata.Name}, |
| 101 | + }, |
| 102 | + expected: []v1alphaAlertPolicy.AlertPolicy{inputs[4]}, |
| 103 | + }, |
| 104 | + "filter by label": { |
| 105 | + request: objectsV1.GetAlertPolicyRequest{ |
| 106 | + Project: project.GetName(), |
| 107 | + Labels: annotateLabels(t, v1alpha.Labels{"team": []string{"green"}}), |
| 108 | + }, |
| 109 | + expected: []v1alphaAlertPolicy.AlertPolicy{inputs[1]}, |
| 110 | + }, |
| 111 | + "filter by label and name": { |
| 112 | + request: objectsV1.GetAlertPolicyRequest{ |
| 113 | + Project: project.GetName(), |
| 114 | + Names: []string{inputs[3].Metadata.Name}, |
| 115 | + Labels: annotateLabels(t, v1alpha.Labels{"team": []string{"orange"}}), |
| 116 | + }, |
| 117 | + expected: []v1alphaAlertPolicy.AlertPolicy{inputs[3]}, |
| 118 | + }, |
| 119 | + } |
| 120 | + for name, test := range filterTests { |
| 121 | + t.Run(name, func(t *testing.T) { |
| 122 | + t.Parallel() |
| 123 | + actual, err := client.Objects().V1().GetV1alphaAlertPolicies(ctx, test.request) |
| 124 | + require.NoError(t, err) |
| 125 | + if !test.returnsAll { |
| 126 | + require.Len(t, actual, len(test.expected)) |
| 127 | + } |
| 128 | + assertSubset(t, actual, test.expected, assertV1alphaAlertPoliciesAreEqual) |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func newV1alphaAlertPolicy( |
| 134 | + t *testing.T, |
| 135 | + metadata v1alphaAlertPolicy.Metadata, |
| 136 | + variant, |
| 137 | + subVariant string, |
| 138 | +) v1alphaAlertPolicy.AlertPolicy { |
| 139 | + t.Helper() |
| 140 | + metadata.Labels = annotateLabels(t, metadata.Labels) |
| 141 | + metadata.Annotations = commonAnnotations |
| 142 | + ap := getExample[v1alphaAlertPolicy.AlertPolicy](t, manifest.KindAlertPolicy, variant, subVariant) |
| 143 | + ap.Spec.Description = objectDescription |
| 144 | + return v1alphaAlertPolicy.New(metadata, ap.Spec) |
| 145 | +} |
| 146 | + |
| 147 | +func assertV1alphaAlertPoliciesAreEqual(t *testing.T, expected, actual v1alphaAlertPolicy.AlertPolicy) { |
| 148 | + t.Helper() |
| 149 | + assert.Equal(t, expected, actual) |
| 150 | +} |
0 commit comments