Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add v1alpha RoleBinding e2e tests #486

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions internal/cmd/examplegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,17 @@ func getV1alphaExamplesConfigs() []examplesGeneratorConfig {
object.GetKind().ToLower(),
)
grouped := groupBy(examples, func(e v1alphaExamples.Example) string { return e.GetVariant() })
// If we don't have any variants, we can write all examples into examples.yaml file.
if len(grouped) == 1 {
configs = append(configs, examplesGeneratorConfig{
Examples: examples,
Path: filepath.Join(basePath, "examples.yaml"),
})
continue
}
for variant, examples := range grouped {
var path string
if len(grouped) == 1 {
// If we don't have any variants, we can write all examples into examples.yaml file.
path = filepath.Join(basePath, "examples.yaml")
} else {
path = filepath.Join(basePath, "examples", strings.ReplaceAll(strings.ToLower(variant), " ", "-")+".yaml")
}
config := examplesGeneratorConfig{
Examples: examples,
Path: filepath.Join(basePath, "examples", strings.ReplaceAll(strings.ToLower(variant), " ", "-")+".yaml"),
Path: path,
Comments: make(yaml.CommentMap),
}
if len(examples) == 1 {
Expand Down
32 changes: 18 additions & 14 deletions manifest/v1alpha/alertsilence/examples.yaml
Original file line number Diff line number Diff line change
@@ -1,54 +1,58 @@
# duration
- apiVersion: n9/v1alpha
kind: AlertSilence
metadata:
name: scheduled-maintenance-2024-05-01
name: incident-70
project: default
spec:
description: Scheduled maintenance alerts silence
description: Alerts silenced for the duration of the active incident 70
slo: api-server-latency
alertPolicy:
name: fast-burn
project: default
period:
startTime: 2024-05-01T12:00:00Z
endTime: 2024-05-01T14:00:00Z
duration: 4h
# end time
- apiVersion: n9/v1alpha
kind: AlertSilence
metadata:
name: scheduled-maintenance-2024-05-02
name: incident-71
project: default
spec:
description: Scheduled maintenance alerts silence
description: Alerts silenced until incident 71 is resolved
slo: api-server-latency
alertPolicy:
name: fast-burn
project: default
period:
startTime: 2024-05-02T12:00:00Z
duration: 2h
endTime: 2024-05-01T20:00:00Z
# start and end time
- apiVersion: n9/v1alpha
kind: AlertSilence
metadata:
name: incident-70
name: scheduled-maintenance-2024-05-01
project: default
spec:
description: Alerts silenced for the duration of the active incident 70
description: Scheduled maintenance alerts silence
slo: api-server-latency
alertPolicy:
name: fast-burn
project: default
period:
duration: 4h
startTime: 2024-05-01T12:00:00Z
endTime: 2024-05-01T14:00:00Z
# start time and duration
- apiVersion: n9/v1alpha
kind: AlertSilence
metadata:
name: incident-71
name: scheduled-maintenance-2024-05-02
project: default
spec:
description: Alerts silenced until incident 71 is resolved
description: Scheduled maintenance alerts silence
slo: api-server-latency
alertPolicy:
name: fast-burn
project: default
period:
endTime: 2024-05-01T20:00:00Z
startTime: 2024-05-02T12:00:00Z
duration: 2h
2 changes: 2 additions & 0 deletions manifest/v1alpha/dataexport/examples.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# gcs
- apiVersion: n9/v1alpha
kind: DataExport
metadata:
Expand All @@ -9,6 +10,7 @@
spec:
bucketName: prod-data-export-bucket
status: null
# s3
- apiVersion: n9/v1alpha
kind: DataExport
metadata:
Expand Down
20 changes: 12 additions & 8 deletions manifest/v1alpha/rolebinding/examples.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
- apiVersion: n9/v1alpha
kind: RoleBinding
metadata:
name: default-project-binding
spec:
user: 00u2y4e4atkzaYkXP4x8
roleRef: project-viewer
projectRef: default
# organization binding
- apiVersion: n9/v1alpha
kind: RoleBinding
metadata:
name: organization-binding-john-admin
spec:
user: 00u2y4e4atkzaYkXP4x8
roleRef: organization-admin
# organization group binding
- apiVersion: n9/v1alpha
kind: RoleBinding
metadata:
name: group-binding-admin
spec:
groupRef: group-Q72HorLyjjCc
roleRef: organization-admin
# project binding
- apiVersion: n9/v1alpha
kind: RoleBinding
metadata:
name: default-project-binding
spec:
user: 00u2y4e4atkzaYkXP4x8
roleRef: project-viewer
projectRef: default
# project group binding
- apiVersion: n9/v1alpha
kind: RoleBinding
metadata:
Expand Down
12 changes: 12 additions & 0 deletions tests/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,15 @@ func deepCopyObject[T any](t *testing.T, object T) T {
require.NoError(t, json.Unmarshal(data, &copied))
return copied
}

func filterSlice[T any](s []T, filter func(T) bool) []T {
result := make([]T, 0, len(s))
for i := range s {
if filter(s[i]) {
result = append(result, s[i])
}
}
return result
}

func ptr[T any](v T) *T { return &v }
2 changes: 1 addition & 1 deletion tests/v1alpha_alertmethod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func Test_Objects_V1_V1alpha_AlertMethod(t *testing.T) {
}{
"all": {
request: objectsV1.GetAlertMethodsRequest{Project: sdk.ProjectsWildcard},
expected: manifest.FilterByKind[v1alphaAlertMethod.AlertMethod](allObjects),
expected: inputs,
returnsAll: true,
},
"default project": {
Expand Down
134 changes: 134 additions & 0 deletions tests/v1alpha_rolebinding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
//go:build e2e_test

package tests

import (
"context"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/nobl9/nobl9-go/manifest"
v1alphaRoleBinding "github.com/nobl9/nobl9-go/manifest/v1alpha/rolebinding"
"github.com/nobl9/nobl9-go/sdk"
objectsV1 "github.com/nobl9/nobl9-go/sdk/endpoints/objects/v1"
)

func Test_Objects_V1_V1alpha_RoleBinding(t *testing.T) {
t.Parallel()
ctx := context.Background()

project := generateV1alphaProject(t)
v1Apply(t, ctx, []manifest.Object{project})
implicitBindings, err := client.Objects().V1().GetV1alphaRoleBindings(ctx,
objectsV1.GetRoleBindingsRequest{Project: project.GetName()})
require.NoError(t, err)
require.Len(t, implicitBindings, 1)
implicitProjectBinding := implicitBindings[0]

inputs := []v1alphaRoleBinding.RoleBinding{
v1alphaRoleBinding.New(
v1alphaRoleBinding.Metadata{Name: generateName()},
v1alphaRoleBinding.Spec{
User: ptr(generateName()),
RoleRef: "organization-blank",
},
),
v1alphaRoleBinding.New(
v1alphaRoleBinding.Metadata{Name: generateName()},
v1alphaRoleBinding.Spec{
GroupRef: ptr(generateName()),
RoleRef: "organization-blank",
},
),
v1alphaRoleBinding.New(
v1alphaRoleBinding.Metadata{Name: generateName()},
v1alphaRoleBinding.Spec{
User: ptr(generateName()),
RoleRef: "project-viewer",
ProjectRef: project.GetName(),
},
),
v1alphaRoleBinding.New(
v1alphaRoleBinding.Metadata{Name: generateName()},
v1alphaRoleBinding.Spec{
GroupRef: ptr(generateName()),
RoleRef: "project-viewer",
ProjectRef: project.GetName(),
},
),
v1alphaRoleBinding.New(
v1alphaRoleBinding.Metadata{Name: generateName()},
v1alphaRoleBinding.Spec{
User: ptr(generateName()),
RoleRef: "project-viewer",
ProjectRef: defaultProject,
},
),
v1alphaRoleBinding.New(
v1alphaRoleBinding.Metadata{Name: generateName()},
v1alphaRoleBinding.Spec{
GroupRef: ptr(generateName()),
RoleRef: "project-viewer",
ProjectRef: defaultProject,
},
),
}

v1Apply(t, ctx, inputs)
t.Cleanup(func() {
// Organization role bindings cannot be deleted.
filterOrganizationBindings := func(r v1alphaRoleBinding.RoleBinding) bool {
return !strings.HasPrefix(r.Spec.RoleRef, "organization-")
}
v1Delete(t, ctx, filterSlice(inputs, filterOrganizationBindings))
})

filterTests := map[string]struct {
request objectsV1.GetRoleBindingsRequest
expected []v1alphaRoleBinding.RoleBinding
returnsAll bool
}{
"all": {
request: objectsV1.GetRoleBindingsRequest{Project: sdk.ProjectsWildcard},
expected: inputs,
returnsAll: true,
},
"default project": {
request: objectsV1.GetRoleBindingsRequest{},
expected: []v1alphaRoleBinding.RoleBinding{inputs[4], inputs[5]},
returnsAll: true,
},
"filter by project": {
request: objectsV1.GetRoleBindingsRequest{
Project: project.GetName(),
},
expected: []v1alphaRoleBinding.RoleBinding{implicitProjectBinding, inputs[2], inputs[3]},
},
"filter by name": {
request: objectsV1.GetRoleBindingsRequest{
Project: project.GetName(),
Names: []string{inputs[2].Metadata.Name},
},
expected: []v1alphaRoleBinding.RoleBinding{inputs[2]},
},
}
for name, test := range filterTests {
t.Run(name, func(t *testing.T) {
t.Parallel()
actual, err := client.Objects().V1().GetV1alphaRoleBindings(ctx, test.request)
require.NoError(t, err)
if !test.returnsAll {
require.Len(t, actual, len(test.expected))
}
assertSubset(t, actual, test.expected, assertV1alphaRoleBindingsAreEqual)
})
}
}

func assertV1alphaRoleBindingsAreEqual(t *testing.T, expected, actual v1alphaRoleBinding.RoleBinding) {
t.Helper()
assert.Equal(t, expected, actual)
}
2 changes: 1 addition & 1 deletion tests/v1alpha_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func Test_Objects_V1_V1alpha_Service(t *testing.T) {
}{
"all": {
request: objectsV1.GetServicesRequest{Project: sdk.ProjectsWildcard},
expected: manifest.FilterByKind[v1alphaService.Service](allObjects),
expected: inputs,
returnsAll: true,
},
"default project": {
Expand Down
Loading