Skip to content

Commit 8794a87

Browse files
authored
chore: Add Alert Method e2e tests (#488)
Adds v1alpha `AlertMethod` e2e test cases.
1 parent 8338531 commit 8794a87

3 files changed

+152
-2
lines changed

tests/v1alpha_alertpolicy_test.go

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
}

tests/v1alpha_project_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func newV1alphaProject(
9292
metadata v1alphaProject.Metadata,
9393
) v1alphaProject.Project {
9494
t.Helper()
95-
annotateLabels(t, metadata.Labels)
95+
metadata.Labels = annotateLabels(t, metadata.Labels)
9696
metadata.Annotations = commonAnnotations
9797
return v1alphaProject.New(metadata, v1alphaProject.Spec{Description: objectDescription})
9898
}

tests/v1alpha_service_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func newV1alphaService(
125125
metadata v1alphaService.Metadata,
126126
) v1alphaService.Service {
127127
t.Helper()
128-
annotateLabels(t, metadata.Labels)
128+
metadata.Labels = annotateLabels(t, metadata.Labels)
129129
metadata.Annotations = commonAnnotations
130130
return v1alphaService.New(metadata, v1alphaService.Spec{Description: objectDescription})
131131
}

0 commit comments

Comments
 (0)