-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add Alert Method e2e tests (#481)
Extends end-to-end tests with `v1alpha.AlertMethod` coverage. It utilizes generated examples to easily test application of different Alert Method variants.
- Loading branch information
1 parent
9732f54
commit 75d8472
Showing
6 changed files
with
286 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//go:build e2e_test | ||
|
||
package tests | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
v1alphaExamples "github.com/nobl9/nobl9-go/internal/manifest/v1alpha/examples" | ||
"github.com/nobl9/nobl9-go/manifest" | ||
) | ||
|
||
type exampleWrapper struct { | ||
v1alphaExamples.Example | ||
rawObject []byte | ||
} | ||
|
||
var examplesRegistry = func() map[manifest.Kind][]exampleWrapper { | ||
kindToExamples := map[manifest.Kind][]v1alphaExamples.Example{ | ||
manifest.KindProject: v1alphaExamples.Project(), | ||
manifest.KindService: v1alphaExamples.Service(), | ||
manifest.KindAlertMethod: v1alphaExamples.AlertMethod(), | ||
manifest.KindSLO: v1alphaExamples.SLO(), | ||
manifest.KindAgent: v1alphaExamples.Agent(), | ||
manifest.KindDirect: v1alphaExamples.Direct(), | ||
manifest.KindAlertPolicy: v1alphaExamples.AlertPolicy(), | ||
manifest.KindAlertSilence: v1alphaExamples.AlertSilence(), | ||
manifest.KindAnnotation: v1alphaExamples.Annotation(), | ||
manifest.KindBudgetAdjustment: v1alphaExamples.BudgetAdjustment(), | ||
manifest.KindDataExport: v1alphaExamples.DataExport(), | ||
manifest.KindRoleBinding: v1alphaExamples.RoleBinding(), | ||
} | ||
wrapped := make(map[manifest.Kind][]exampleWrapper, len(kindToExamples)) | ||
for kind, examples := range kindToExamples { | ||
wrapped[kind] = make([]exampleWrapper, 0, len(examples)) | ||
for _, example := range examples { | ||
object := example.GetObject() | ||
rawObject, err := json.Marshal(object) | ||
if err != nil { | ||
log.Panicf("failed to marshal example %T object: %v", object, err) | ||
} | ||
wrapped[kind] = append(wrapped[kind], exampleWrapper{ | ||
Example: example, | ||
rawObject: rawObject, | ||
}) | ||
} | ||
} | ||
return wrapped | ||
}() | ||
|
||
func getExample[T any](t *testing.T, kind manifest.Kind, variant, subVariant string) *T { | ||
t.Helper() | ||
examples, ok := examplesRegistry[kind] | ||
if !ok { | ||
require.True(t, ok, "%s kind not found in registry", kind) | ||
} | ||
decode := func(rawObject []byte) *T { | ||
var object T | ||
if err := json.Unmarshal(rawObject, &object); err != nil { | ||
log.Panicf("failed to unmarshal example %T object: %v", object, err) | ||
} | ||
return &object | ||
} | ||
if variant == "" && subVariant == "" { | ||
return decode(examples[0].rawObject) | ||
} | ||
for _, example := range examples { | ||
if example.GetVariant() == variant && (subVariant == "" || example.GetSubVariant() == subVariant) { | ||
return decode(example.rawObject) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
//go:build e2e_test | ||
|
||
package tests | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/nobl9/nobl9-go/manifest" | ||
"github.com/nobl9/nobl9-go/manifest/v1alpha" | ||
v1alphaAlertMethod "github.com/nobl9/nobl9-go/manifest/v1alpha/alertmethod" | ||
"github.com/nobl9/nobl9-go/sdk" | ||
objectsV1 "github.com/nobl9/nobl9-go/sdk/endpoints/objects/v1" | ||
) | ||
|
||
func Test_Objects_V1_V1alpha_AlertMethod(t *testing.T) { | ||
t.Parallel() | ||
ctx := context.Background() | ||
project := generateV1alphaProject(t) | ||
alertMethodTypes := v1alpha.AlertMethodTypeValues() | ||
allObjects := make([]manifest.Object, 0, len(alertMethodTypes)+1) | ||
allObjects = append(allObjects, project) | ||
|
||
for i, typ := range v1alpha.AlertMethodTypeValues() { | ||
method := newV1alphaAlertMethod(t, | ||
typ, | ||
v1alphaAlertMethod.Metadata{ | ||
Name: generateName(), | ||
DisplayName: fmt.Sprintf("Alert Method %d", i), | ||
Project: project.GetName(), | ||
}, | ||
) | ||
if i == 0 { | ||
method.Metadata.Project = defaultProject | ||
} | ||
allObjects = append(allObjects, method) | ||
} | ||
|
||
v1Apply(t, ctx, allObjects) | ||
t.Cleanup(func() { v1Delete(t, ctx, allObjects) }) | ||
inputs := manifest.FilterByKind[v1alphaAlertMethod.AlertMethod](allObjects) | ||
|
||
filterTests := map[string]struct { | ||
request objectsV1.GetAlertMethodsRequest | ||
expected []v1alphaAlertMethod.AlertMethod | ||
returnsAll bool | ||
}{ | ||
"all": { | ||
request: objectsV1.GetAlertMethodsRequest{Project: sdk.ProjectsWildcard}, | ||
expected: manifest.FilterByKind[v1alphaAlertMethod.AlertMethod](allObjects), | ||
returnsAll: true, | ||
}, | ||
"default project": { | ||
request: objectsV1.GetAlertMethodsRequest{}, | ||
expected: []v1alphaAlertMethod.AlertMethod{inputs[0]}, | ||
returnsAll: true, | ||
}, | ||
"filter by project": { | ||
request: objectsV1.GetAlertMethodsRequest{ | ||
Project: project.GetName(), | ||
}, | ||
expected: inputs[1:], | ||
}, | ||
"filter by name": { | ||
request: objectsV1.GetAlertMethodsRequest{ | ||
Project: project.GetName(), | ||
Names: []string{inputs[3].Metadata.Name}, | ||
}, | ||
expected: []v1alphaAlertMethod.AlertMethod{inputs[3]}, | ||
}, | ||
} | ||
for name, test := range filterTests { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
actual, err := client.Objects().V1().GetV1alphaAlertMethods(ctx, test.request) | ||
require.NoError(t, err) | ||
if !test.returnsAll { | ||
require.Len(t, actual, len(test.expected)) | ||
} | ||
assertSubset(t, actual, test.expected, assertV1alphaAlertMethodsAreEqual) | ||
}) | ||
} | ||
} | ||
|
||
func newV1alphaAlertMethod( | ||
t *testing.T, | ||
typ v1alpha.AlertMethodType, | ||
metadata v1alphaAlertMethod.Metadata, | ||
) v1alphaAlertMethod.AlertMethod { | ||
t.Helper() | ||
variant := getExample[v1alphaAlertMethod.AlertMethod](t, manifest.KindAlertMethod, typ.String(), "") | ||
variant.Spec.Description = objectDescription | ||
return v1alphaAlertMethod.New(metadata, variant.Spec) | ||
} | ||
|
||
func assertV1alphaAlertMethodsAreEqual(t *testing.T, expected, actual v1alphaAlertMethod.AlertMethod) { | ||
t.Helper() | ||
expected = deepCopyObject(t, expected) | ||
actual.Status = nil | ||
typ, err := expected.Spec.GetType() | ||
require.NoError(t, err) | ||
switch typ { | ||
case v1alpha.AlertMethodTypeDiscord: | ||
expected.Spec.Discord.URL = "[hidden]" | ||
case v1alpha.AlertMethodTypeJira: | ||
expected.Spec.Jira.APIToken = "[hidden]" | ||
case v1alpha.AlertMethodTypeOpsgenie: | ||
expected.Spec.Opsgenie.Auth = "[hidden]" | ||
case v1alpha.AlertMethodTypePagerDuty: | ||
expected.Spec.PagerDuty.IntegrationKey = "[hidden]" | ||
case v1alpha.AlertMethodTypeServiceNow: | ||
expected.Spec.ServiceNow.Password = "[hidden]" | ||
case v1alpha.AlertMethodTypeSlack: | ||
expected.Spec.Slack.URL = "[hidden]" | ||
case v1alpha.AlertMethodTypeTeams: | ||
expected.Spec.Teams.URL = "[hidden]" | ||
case v1alpha.AlertMethodTypeWebhook: | ||
expected.Spec.Webhook.URL = "[hidden]" | ||
for i, header := range expected.Spec.Webhook.Headers { | ||
if header.IsSecret { | ||
expected.Spec.Webhook.Headers[i].Value = "[hidden]" | ||
} | ||
} | ||
} | ||
assert.Equal(t, expected, actual) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.