Skip to content

Commit d6346a1

Browse files
authored
Move user defined service account e2e to one namespace per spec (#2717)
Signed-off-by: perdasilva <[email protected]>
1 parent 70f5cd3 commit d6346a1

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

test/e2e/user_defined_sa_test.go

+42-38
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
1414
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/kubernetes/pkg/apis/rbac"
1515
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
16+
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/ctx"
1617
"github.com/stretchr/testify/assert"
1718
"github.com/stretchr/testify/require"
1819
corev1 "k8s.io/api/core/v1"
@@ -23,56 +24,67 @@ import (
2324
)
2425

2526
var _ = Describe("User defined service account", func() {
27+
var (
28+
generatedNamespace corev1.Namespace
29+
)
30+
31+
BeforeEach(func() {
32+
generatedNamespace = corev1.Namespace{
33+
ObjectMeta: metav1.ObjectMeta{
34+
Name: genName("user-defined-sa-e2e-"),
35+
},
36+
}
37+
Eventually(func() error {
38+
return ctx.Ctx().Client().Create(context.Background(), &generatedNamespace)
39+
}).Should(Succeed())
40+
})
41+
2642
AfterEach(func() {
27-
TearDown(testNamespace)
43+
TeardownNamespace(generatedNamespace.GetName())
2844
})
2945

3046
It("with no permission", func() {
3147

3248
kubeclient := newKubeClient()
3349
crclient := newCRClient()
3450

35-
namespace := genName("scoped-ns-")
36-
_, cleanupNS := newNamespace(kubeclient, namespace)
37-
defer cleanupNS()
38-
3951
// Create a service account, but add no permission to it.
4052
saName := genName("scoped-sa-")
41-
_, cleanupSA := newServiceAccount(kubeclient, namespace, saName)
53+
_, cleanupSA := newServiceAccount(kubeclient, generatedNamespace.GetName(), saName)
4254
defer cleanupSA()
4355

4456
// Add an OperatorGroup and specify the service account.
4557
ogName := genName("scoped-og-")
46-
_, cleanupOG := newOperatorGroupWithServiceAccount(crclient, namespace, ogName, saName)
58+
_, cleanupOG := newOperatorGroupWithServiceAccount(crclient, generatedNamespace.GetName(), ogName, saName)
4759
defer cleanupOG()
4860

4961
permissions := deploymentPermissions()
50-
catsrc, subSpec, catsrcCleanup := newCatalogSource(GinkgoT(), kubeclient, crclient, "scoped", namespace, permissions)
62+
catsrc, subSpec, catsrcCleanup := newCatalogSource(GinkgoT(), kubeclient, crclient, "scoped", generatedNamespace.GetName(), permissions)
5163
defer catsrcCleanup()
5264

5365
// Ensure that the catalog source is resolved before we create a subscription.
54-
_, err := fetchCatalogSourceOnStatus(crclient, catsrc.GetName(), namespace, catalogSourceRegistryPodSynced)
66+
_, err := fetchCatalogSourceOnStatus(crclient, catsrc.GetName(), generatedNamespace.GetName(), catalogSourceRegistryPodSynced)
5567
require.NoError(GinkgoT(), err)
5668

5769
subscriptionName := genName("scoped-sub-")
58-
cleanupSubscription := createSubscriptionForCatalog(crclient, namespace, subscriptionName, catsrc.GetName(), subSpec.Package, subSpec.Channel, subSpec.StartingCSV, subSpec.InstallPlanApproval)
70+
cleanupSubscription := createSubscriptionForCatalog(crclient, generatedNamespace.GetName(), subscriptionName, catsrc.GetName(), subSpec.Package, subSpec.Channel, subSpec.StartingCSV, subSpec.InstallPlanApproval)
5971
defer cleanupSubscription()
6072

6173
// Wait until an install plan is created.
62-
subscription, err := fetchSubscription(crclient, namespace, subscriptionName, subscriptionHasInstallPlanChecker)
74+
subscription, err := fetchSubscription(crclient, generatedNamespace.GetName(), subscriptionName, subscriptionHasInstallPlanChecker)
6375
require.NoError(GinkgoT(), err)
6476
require.NotNil(GinkgoT(), subscription)
6577

6678
// We expect the InstallPlan to be in status: Failed.
6779
ipName := subscription.Status.Install.Name
6880
ipPhaseCheckerFunc := buildInstallPlanPhaseCheckFunc(v1alpha1.InstallPlanPhaseFailed)
69-
ipGot, err := fetchInstallPlanWithNamespace(GinkgoT(), crclient, ipName, namespace, ipPhaseCheckerFunc)
81+
ipGot, err := fetchInstallPlanWithNamespace(GinkgoT(), crclient, ipName, generatedNamespace.GetName(), ipPhaseCheckerFunc)
7082
require.NoError(GinkgoT(), err)
7183

7284
conditionGot := mustHaveCondition(GinkgoT(), ipGot, v1alpha1.InstallPlanInstalled)
7385
assert.Equal(GinkgoT(), corev1.ConditionFalse, conditionGot.Status)
7486
assert.Equal(GinkgoT(), v1alpha1.InstallPlanReasonComponentFailed, conditionGot.Reason)
75-
assert.Contains(GinkgoT(), conditionGot.Message, fmt.Sprintf("is forbidden: User \"system:serviceaccount:%s:%s\" cannot create resource", namespace, saName))
87+
assert.Contains(GinkgoT(), conditionGot.Message, fmt.Sprintf("is forbidden: User \"system:serviceaccount:%s:%s\" cannot create resource", generatedNamespace.GetName(), saName))
7688

7789
// Verify that all step resources are in Unknown state.
7890
for _, step := range ipGot.Status.Plan {
@@ -85,43 +97,39 @@ var _ = Describe("User defined service account", func() {
8597
kubeclient := newKubeClient()
8698
crclient := newCRClient()
8799

88-
namespace := genName("scoped-ns-")
89-
_, cleanupNS := newNamespace(kubeclient, namespace)
90-
defer cleanupNS()
91-
92100
// Create a service account, add enough permission to it so that operator install is successful.
93101
saName := genName("scoped-sa")
94-
_, cleanupSA := newServiceAccount(kubeclient, namespace, saName)
102+
_, cleanupSA := newServiceAccount(kubeclient, generatedNamespace.GetName(), saName)
95103
defer cleanupSA()
96-
cleanupPerm := grantPermission(GinkgoT(), kubeclient, namespace, saName)
104+
cleanupPerm := grantPermission(GinkgoT(), kubeclient, generatedNamespace.GetName(), saName)
97105
defer cleanupPerm()
98106

99107
// Add an OperatorGroup and specify the service account.
100108
ogName := genName("scoped-og-")
101-
_, cleanupOG := newOperatorGroupWithServiceAccount(crclient, namespace, ogName, saName)
109+
_, cleanupOG := newOperatorGroupWithServiceAccount(crclient, generatedNamespace.GetName(), ogName, saName)
102110
defer cleanupOG()
103111

104112
permissions := deploymentPermissions()
105-
catsrc, subSpec, catsrcCleanup := newCatalogSource(GinkgoT(), kubeclient, crclient, "scoped", namespace, permissions)
113+
catsrc, subSpec, catsrcCleanup := newCatalogSource(GinkgoT(), kubeclient, crclient, "scoped", generatedNamespace.GetName(), permissions)
106114
defer catsrcCleanup()
107115

108116
// Ensure that the catalog source is resolved before we create a subscription.
109-
_, err := fetchCatalogSourceOnStatus(crclient, catsrc.GetName(), namespace, catalogSourceRegistryPodSynced)
117+
_, err := fetchCatalogSourceOnStatus(crclient, catsrc.GetName(), generatedNamespace.GetName(), catalogSourceRegistryPodSynced)
110118
require.NoError(GinkgoT(), err)
111119

112120
subscriptionName := genName("scoped-sub-")
113-
cleanupSubscription := createSubscriptionForCatalog(crclient, namespace, subscriptionName, catsrc.GetName(), subSpec.Package, subSpec.Channel, subSpec.StartingCSV, subSpec.InstallPlanApproval)
121+
cleanupSubscription := createSubscriptionForCatalog(crclient, generatedNamespace.GetName(), subscriptionName, catsrc.GetName(), subSpec.Package, subSpec.Channel, subSpec.StartingCSV, subSpec.InstallPlanApproval)
114122
defer cleanupSubscription()
115123

116124
// Wait until an install plan is created.
117-
subscription, err := fetchSubscription(crclient, namespace, subscriptionName, subscriptionHasInstallPlanChecker)
125+
subscription, err := fetchSubscription(crclient, generatedNamespace.GetName(), subscriptionName, subscriptionHasInstallPlanChecker)
118126
require.NoError(GinkgoT(), err)
119127
require.NotNil(GinkgoT(), subscription)
120128

121129
// We expect the InstallPlan to be in status: Complete.
122130
ipName := subscription.Status.Install.Name
123131
ipPhaseCheckerFunc := buildInstallPlanPhaseCheckFunc(v1alpha1.InstallPlanPhaseComplete)
124-
ipGot, err := fetchInstallPlanWithNamespace(GinkgoT(), crclient, ipName, namespace, ipPhaseCheckerFunc)
132+
ipGot, err := fetchInstallPlanWithNamespace(GinkgoT(), crclient, ipName, generatedNamespace.GetName(), ipPhaseCheckerFunc)
125133
require.NoError(GinkgoT(), err)
126134

127135
conditionGot := mustHaveCondition(GinkgoT(), ipGot, v1alpha1.InstallPlanInstalled)
@@ -141,50 +149,46 @@ var _ = Describe("User defined service account", func() {
141149
kubeclient := newKubeClient()
142150
crclient := newCRClient()
143151

144-
namespace := genName("scoped-ns-")
145-
_, cleanupNS := newNamespace(kubeclient, namespace)
146-
defer cleanupNS()
147-
148152
// Create a service account, but add no permission to it.
149153
saName := genName("scoped-sa-")
150-
_, cleanupSA := newServiceAccount(kubeclient, namespace, saName)
154+
_, cleanupSA := newServiceAccount(kubeclient, generatedNamespace.GetName(), saName)
151155
defer cleanupSA()
152156

153157
// Add an OperatorGroup and specify the service account.
154158
ogName := genName("scoped-og-")
155-
_, cleanupOG := newOperatorGroupWithServiceAccount(crclient, namespace, ogName, saName)
159+
_, cleanupOG := newOperatorGroupWithServiceAccount(crclient, generatedNamespace.GetName(), ogName, saName)
156160
defer cleanupOG()
157161

158162
permissions := deploymentPermissions()
159-
catsrc, subSpec, catsrcCleanup := newCatalogSource(GinkgoT(), kubeclient, crclient, "scoped", namespace, permissions)
163+
catsrc, subSpec, catsrcCleanup := newCatalogSource(GinkgoT(), kubeclient, crclient, "scoped", generatedNamespace.GetName(), permissions)
160164
defer catsrcCleanup()
161165

162166
// Ensure that the catalog source is resolved before we create a subscription.
163-
_, err := fetchCatalogSourceOnStatus(crclient, catsrc.GetName(), namespace, catalogSourceRegistryPodSynced)
167+
_, err := fetchCatalogSourceOnStatus(crclient, catsrc.GetName(), generatedNamespace.GetName(), catalogSourceRegistryPodSynced)
164168
require.NoError(GinkgoT(), err)
165169

166170
subscriptionName := genName("scoped-sub-")
167-
cleanupSubscription := createSubscriptionForCatalog(crclient, namespace, subscriptionName, catsrc.GetName(), subSpec.Package, subSpec.Channel, subSpec.StartingCSV, subSpec.InstallPlanApproval)
171+
cleanupSubscription := createSubscriptionForCatalog(crclient, generatedNamespace.GetName(), subscriptionName, catsrc.GetName(), subSpec.Package, subSpec.Channel, subSpec.StartingCSV, subSpec.InstallPlanApproval)
168172
defer cleanupSubscription()
169173

170174
// Wait until an install plan is created.
171-
subscription, err := fetchSubscription(crclient, namespace, subscriptionName, subscriptionHasInstallPlanChecker)
175+
subscription, err := fetchSubscription(crclient, generatedNamespace.GetName(), subscriptionName, subscriptionHasInstallPlanChecker)
172176
require.NoError(GinkgoT(), err)
173177
require.NotNil(GinkgoT(), subscription)
174178

175179
// We expect the InstallPlan to be in status: Failed.
176180
ipNameOld := subscription.Status.InstallPlanRef.Name
177181
ipPhaseCheckerFunc := buildInstallPlanPhaseCheckFunc(v1alpha1.InstallPlanPhaseFailed)
178-
ipGotOld, err := fetchInstallPlanWithNamespace(GinkgoT(), crclient, ipNameOld, namespace, ipPhaseCheckerFunc)
182+
ipGotOld, err := fetchInstallPlanWithNamespace(GinkgoT(), crclient, ipNameOld, generatedNamespace.GetName(), ipPhaseCheckerFunc)
179183
require.NoError(GinkgoT(), err)
180184
require.Equal(GinkgoT(), v1alpha1.InstallPlanPhaseFailed, ipGotOld.Status.Phase)
181185

182186
// Grant permission now and this should trigger an retry of InstallPlan.
183-
cleanupPerm := grantPermission(GinkgoT(), kubeclient, namespace, saName)
187+
cleanupPerm := grantPermission(GinkgoT(), kubeclient, generatedNamespace.GetName(), saName)
184188
defer cleanupPerm()
185189

186190
ipPhaseCheckerFunc = buildInstallPlanPhaseCheckFunc(v1alpha1.InstallPlanPhaseComplete)
187-
ipGotNew, err := fetchInstallPlanWithNamespace(GinkgoT(), crclient, ipNameOld, namespace, ipPhaseCheckerFunc)
191+
ipGotNew, err := fetchInstallPlanWithNamespace(GinkgoT(), crclient, ipNameOld, generatedNamespace.GetName(), ipPhaseCheckerFunc)
188192
require.NoError(GinkgoT(), err)
189193
require.Equal(GinkgoT(), v1alpha1.InstallPlanPhaseComplete, ipGotNew.Status.Phase)
190194
})

0 commit comments

Comments
 (0)