Skip to content

Commit 3aa0559

Browse files
committed
Add options to test namespace creation
Signed-off-by: perdasilva <[email protected]>
1 parent 2194336 commit 3aa0559

File tree

5 files changed

+137
-13
lines changed

5 files changed

+137
-13
lines changed

Diff for: test/e2e/bundle_e2e_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var _ = Describe("Installing bundles with new object types", func() {
4040
dynamicClient = ctx.Ctx().DynamicClient()
4141

4242
By("creating a test namespace")
43-
generatedNamespace = SetupGeneratedTestNamespace(genName("bundle-e2e-"))
43+
generatedNamespace = SetupGeneratedTestNamespace(genName("bundle-e2e-"), WithOwnNamespaceOperatorGroup())
4444
})
4545

4646
AfterEach(func() {

Diff for: test/e2e/crd_e2e_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var _ = Describe("CRD Versions", func() {
2626
)
2727

2828
BeforeEach(func() {
29-
ns = SetupGeneratedTestNamespace(genName("crd-e2e-"))
29+
ns = SetupGeneratedTestNamespace(genName("crd-e2e-"), WithOwnNamespaceOperatorGroup())
3030
})
3131

3232
AfterEach(func() {

Diff for: test/e2e/gc_e2e_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ var _ = Describe("Garbage collection for dependent resources", func() {
3434
kubeClient = ctx.Ctx().KubeClient()
3535
operatorClient = ctx.Ctx().OperatorClient()
3636

37-
namespaceName := genName("gc-e2e-")
38-
ns = SetupGeneratedTestNamespace(namespaceName, namespaceName)
37+
ns = SetupGeneratedTestNamespace(genName("gc-e2e-"), WithAllNamespaceOperatorGroup())
3938
})
4039

4140
AfterEach(func() {

Diff for: test/e2e/subscription_e2e_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var _ = Describe("Subscription", func() {
5555
)
5656

5757
BeforeEach(func() {
58-
generatedNamespace = SetupGeneratedTestNamespace(genName("subscription-e2e-"))
58+
generatedNamespace = SetupGeneratedTestNamespace(genName("subscription-e2e-"), WithAllNamespaceOperatorGroup())
5959
})
6060

6161
AfterEach(func() {

Diff for: test/e2e/util.go

+133-8
Original file line numberDiff line numberDiff line change
@@ -984,18 +984,143 @@ func SetupGeneratedTestNamespaceWithOperatorGroup(name string, og operatorsv1.Op
984984
return ns
985985
}
986986

987-
func SetupGeneratedTestNamespace(name string, targetNamespaces ...string) corev1.Namespace {
988-
og := operatorsv1.OperatorGroup{
987+
type TestNamespaceOption func(cfg *testNamespaceConfiguration)
988+
type TestNamespaceModifier func(ns *corev1.Namespace)
989+
990+
type testNamespaceConfiguration struct {
991+
preCreateHooks []TestNamespaceModifier
992+
postCreateHooks []TestNamespaceModifier
993+
}
994+
995+
func (t *testNamespaceConfiguration) ApplyPreCreateHooks(ns *corev1.Namespace) {
996+
for _, hook := range t.preCreateHooks {
997+
hook(ns)
998+
}
999+
}
1000+
1001+
func (t *testNamespaceConfiguration) ApplyPostCreateHooks(ns *corev1.Namespace) {
1002+
for _, hook := range t.postCreateHooks {
1003+
hook(ns)
1004+
}
1005+
}
1006+
1007+
func (t *testNamespaceConfiguration) RegisterPreCreateHook(fn TestNamespaceModifier) {
1008+
if fn == nil {
1009+
panic("test namespace modifier is nil")
1010+
}
1011+
t.preCreateHooks = append(t.preCreateHooks, fn)
1012+
}
1013+
1014+
func (t *testNamespaceConfiguration) RegisterPostCreateHook(fn TestNamespaceModifier) {
1015+
if fn == nil {
1016+
panic("test namespace modifier is nil")
1017+
}
1018+
t.postCreateHooks = append(t.postCreateHooks, fn)
1019+
}
1020+
1021+
func WithAllNamespaceOperatorGroup() TestNamespaceOption {
1022+
return func(cfg *testNamespaceConfiguration) {
1023+
cfg.RegisterPostCreateHook(func(ns *corev1.Namespace) {
1024+
og := operatorsv1.OperatorGroup{
1025+
ObjectMeta: metav1.ObjectMeta{
1026+
Name: fmt.Sprintf("%s-operatorgroup", ns.GetName()),
1027+
Namespace: ns.GetName(),
1028+
},
1029+
}
1030+
Eventually(func() error {
1031+
return ctx.Ctx().Client().Create(context.Background(), &og)
1032+
}).Should(Succeed())
1033+
})
1034+
}
1035+
}
1036+
1037+
func WithOwnNamespaceOperatorGroup() TestNamespaceOption {
1038+
return func(cfg *testNamespaceConfiguration) {
1039+
cfg.RegisterPostCreateHook(func(ns *corev1.Namespace) {
1040+
og := operatorsv1.OperatorGroup{
1041+
ObjectMeta: metav1.ObjectMeta{
1042+
Name: fmt.Sprintf("%s-operatorgroup", ns.GetName()),
1043+
Namespace: ns.GetName(),
1044+
},
1045+
Spec: operatorsv1.OperatorGroupSpec{
1046+
TargetNamespaces: []string{ns.GetName()},
1047+
},
1048+
}
1049+
Eventually(func() error {
1050+
return ctx.Ctx().Client().Create(context.Background(), &og)
1051+
}).Should(Succeed())
1052+
})
1053+
}
1054+
}
1055+
1056+
func WithSingleNamespaceOperatorGroup(watchedNamespace string) TestNamespaceOption {
1057+
return func(cfg *testNamespaceConfiguration) {
1058+
cfg.RegisterPostCreateHook(func(ns *corev1.Namespace) {
1059+
og := operatorsv1.OperatorGroup{
1060+
ObjectMeta: metav1.ObjectMeta{
1061+
Name: fmt.Sprintf("%s-operatorgroup", ns.GetName()),
1062+
Namespace: ns.GetName(),
1063+
},
1064+
Spec: operatorsv1.OperatorGroupSpec{
1065+
TargetNamespaces: []string{watchedNamespace},
1066+
},
1067+
}
1068+
Eventually(func() error {
1069+
return ctx.Ctx().Client().Create(context.Background(), &og)
1070+
}).Should(Succeed())
1071+
})
1072+
}
1073+
}
1074+
1075+
func WithMultiNamespaceOperatorGroup(watchedNamespaceNames ...string) TestNamespaceOption {
1076+
return func(cfg *testNamespaceConfiguration) {
1077+
cfg.RegisterPostCreateHook(func(ns *corev1.Namespace) {
1078+
og := operatorsv1.OperatorGroup{
1079+
ObjectMeta: metav1.ObjectMeta{
1080+
Name: fmt.Sprintf("%s-operatorgroup", ns.GetName()),
1081+
Namespace: ns.GetNamespace(),
1082+
},
1083+
Spec: operatorsv1.OperatorGroupSpec{
1084+
TargetNamespaces: watchedNamespaceNames,
1085+
},
1086+
}
1087+
Eventually(func() error {
1088+
return ctx.Ctx().Client().Create(context.Background(), &og)
1089+
}).Should(Succeed())
1090+
})
1091+
}
1092+
}
1093+
1094+
func WithLabels(labels map[string]string) TestNamespaceOption {
1095+
return func(cfg *testNamespaceConfiguration) {
1096+
cfg.RegisterPreCreateHook(func(ns *corev1.Namespace) {
1097+
ns.SetLabels(labels)
1098+
})
1099+
}
1100+
}
1101+
1102+
func SetupGeneratedTestNamespace(name string, options ...TestNamespaceOption) corev1.Namespace {
1103+
// Setup test namespace configuration
1104+
cfg := testNamespaceConfiguration{}
1105+
for _, opt := range options {
1106+
opt(&cfg)
1107+
}
1108+
ns := corev1.Namespace{
9891109
ObjectMeta: metav1.ObjectMeta{
990-
Name: fmt.Sprintf("%s-operatorgroup", name),
991-
Namespace: name,
992-
},
993-
Spec: operatorsv1.OperatorGroupSpec{
994-
TargetNamespaces: targetNamespaces,
1110+
Name: name,
9951111
},
9961112
}
9971113

998-
return SetupGeneratedTestNamespaceWithOperatorGroup(name, og)
1114+
ctx.Ctx().Logf("applying pre hooks")
1115+
cfg.ApplyPreCreateHooks(&ns)
1116+
ctx.Ctx().Logf("creating")
1117+
Eventually(func() error {
1118+
return ctx.Ctx().Client().Create(context.Background(), &ns)
1119+
}).Should(Succeed())
1120+
ctx.Ctx().Logf("applying post hooks")
1121+
cfg.ApplyPostCreateHooks(&ns)
1122+
1123+
return ns
9991124
}
10001125

10011126
func TeardownNamespace(ns string) {

0 commit comments

Comments
 (0)