@@ -16,6 +16,7 @@ import (
1616 "gopkg.in/yaml.v2"
1717 appsv1 "k8s.io/api/apps/v1"
1818 corev1 "k8s.io/api/core/v1"
19+ rbacv1 "k8s.io/api/rbac/v1"
1920 "k8s.io/apimachinery/pkg/api/errors"
2021 apimeta "k8s.io/apimachinery/pkg/api/meta"
2122 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -38,7 +39,65 @@ const (
3839var pollDuration = time .Minute
3940var pollInterval = time .Second
4041
41- func testInit (t * testing.T ) (* ocv1alpha1.ClusterExtension , * catalogd.ClusterCatalog ) {
42+ func createServiceAccount (ctx context.Context , name types.NamespacedName ) (* corev1.ServiceAccount , error ) {
43+ sa := & corev1.ServiceAccount {
44+ ObjectMeta : metav1.ObjectMeta {
45+ Name : name .Name ,
46+ Namespace : name .Namespace ,
47+ },
48+ }
49+ err := c .Create (ctx , sa )
50+ if err != nil {
51+ return nil , err
52+ }
53+ cr := & rbacv1.ClusterRole {
54+ ObjectMeta : metav1.ObjectMeta {
55+ Name : name .Name ,
56+ },
57+ Rules : []rbacv1.PolicyRule {
58+ {
59+ APIGroups : []string {
60+ "*" ,
61+ },
62+ Resources : []string {
63+ "*" ,
64+ },
65+ Verbs : []string {
66+ "*" ,
67+ },
68+ },
69+ },
70+ }
71+ err = c .Create (ctx , cr )
72+ if err != nil {
73+ return nil , err
74+ }
75+ crb := & rbacv1.ClusterRoleBinding {
76+ ObjectMeta : metav1.ObjectMeta {
77+ Name : name .Name ,
78+ },
79+ Subjects : []rbacv1.Subject {
80+ {
81+ Kind : "ServiceAccount" ,
82+ Name : name .Name ,
83+ Namespace : name .Namespace ,
84+ },
85+ },
86+ RoleRef : rbacv1.RoleRef {
87+ APIGroup : "rbac.authorization.k8s.io" ,
88+ Kind : "ClusterRole" ,
89+ Name : name .Name ,
90+ },
91+ }
92+ err = c .Create (ctx , crb )
93+ if err != nil {
94+ return nil , err
95+ }
96+
97+ return sa , nil
98+ }
99+
100+ func testInit (t * testing.T ) (* ocv1alpha1.ClusterExtension , * catalogd.ClusterCatalog , * corev1.ServiceAccount ) {
42101 var err error
43102 extensionCatalog , err := createTestCatalog (context .Background (), testCatalogName , os .Getenv (testCatalogRefEnvVar ))
44103 require .NoError (t , err )
@@ -49,10 +108,18 @@ func testInit(t *testing.T) (*ocv1alpha1.ClusterExtension, *catalogd.ClusterCata
49108 Name : clusterExtensionName ,
50109 },
51110 }
52- return clusterExtension , extensionCatalog
111+
112+ defaultNamespace := types.NamespacedName {
113+ Name : clusterExtensionName ,
114+ Namespace : "default" ,
115+ }
116+
117+ sa , err := createServiceAccount (context .Background (), defaultNamespace )
118+ require .NoError (t , err )
119+ return clusterExtension , extensionCatalog , sa
53120}
54121
55- func testCleanup (t * testing.T , cat * catalogd.ClusterCatalog , clusterExtension * ocv1alpha1.ClusterExtension ) {
122+ func testCleanup (t * testing.T , cat * catalogd.ClusterCatalog , clusterExtension * ocv1alpha1.ClusterExtension , sa * corev1. ServiceAccount ) {
56123 require .NoError (t , c .Delete (context .Background (), cat ))
57124 require .Eventually (t , func () bool {
58125 err := c .Get (context .Background (), types.NamespacedName {Name : cat .Name }, & catalogd.ClusterCatalog {})
@@ -63,21 +130,26 @@ func testCleanup(t *testing.T, cat *catalogd.ClusterCatalog, clusterExtension *o
63130 err := c .Get (context .Background (), types.NamespacedName {Name : clusterExtension .Name }, & ocv1alpha1.ClusterExtension {})
64131 return errors .IsNotFound (err )
65132 }, pollDuration , pollInterval )
133+ require .NoError (t , c .Delete (context .Background (), sa ))
134+ require .Eventually (t , func () bool {
135+ err := c .Get (context .Background (), types.NamespacedName {Name : sa .Name , Namespace : sa .Namespace }, & corev1.ServiceAccount {})
136+ return errors .IsNotFound (err )
137+ }, pollDuration , pollInterval )
66138}
67139
68140func TestClusterExtensionInstallRegistry (t * testing.T ) {
69141 t .Log ("When a cluster extension is installed from a catalog" )
70142 t .Log ("When the extension bundle format is registry+v1" )
71143
72- clusterExtension , extensionCatalog := testInit (t )
73- defer testCleanup (t , extensionCatalog , clusterExtension )
144+ clusterExtension , extensionCatalog , sa := testInit (t )
145+ defer testCleanup (t , extensionCatalog , clusterExtension , sa )
74146 defer getArtifactsOutput (t )
75147
76148 clusterExtension .Spec = ocv1alpha1.ClusterExtensionSpec {
77149 PackageName : "prometheus" ,
78150 InstallNamespace : "default" ,
79151 ServiceAccount : ocv1alpha1.ServiceAccountReference {
80- Name : "default" ,
152+ Name : sa . Name ,
81153 },
82154 }
83155 t .Log ("It resolves the specified package with correct bundle path" )
@@ -128,8 +200,8 @@ func TestClusterExtensionBlockInstallNonSuccessorVersion(t *testing.T) {
128200 t .Log ("When a cluster extension is installed from a catalog" )
129201 t .Log ("When resolving upgrade edges" )
130202
131- clusterExtension , extensionCatalog := testInit (t )
132- defer testCleanup (t , extensionCatalog , clusterExtension )
203+ clusterExtension , extensionCatalog , sa := testInit (t )
204+ defer testCleanup (t , extensionCatalog , clusterExtension , sa )
133205 defer getArtifactsOutput (t )
134206
135207 t .Log ("By creating an ClusterExtension at a specified version" )
@@ -138,7 +210,7 @@ func TestClusterExtensionBlockInstallNonSuccessorVersion(t *testing.T) {
138210 Version : "1.0.0" ,
139211 InstallNamespace : "default" ,
140212 ServiceAccount : ocv1alpha1.ServiceAccountReference {
141- Name : "default" ,
213+ Name : sa . Name ,
142214 },
143215 }
144216 require .NoError (t , c .Create (context .Background (), clusterExtension ))
@@ -177,8 +249,8 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
177249 t .Log ("When a cluster extension is installed from a catalog" )
178250 t .Log ("When resolving upgrade edges" )
179251
180- clusterExtension , extensionCatalog := testInit (t )
181- defer testCleanup (t , extensionCatalog , clusterExtension )
252+ clusterExtension , extensionCatalog , sa := testInit (t )
253+ defer testCleanup (t , extensionCatalog , clusterExtension , sa )
182254 defer getArtifactsOutput (t )
183255
184256 t .Log ("By creating an ClusterExtension at a specified version" )
@@ -187,7 +259,7 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
187259 Version : "1.0.0" ,
188260 InstallNamespace : "default" ,
189261 ServiceAccount : ocv1alpha1.ServiceAccountReference {
190- Name : "default" ,
262+ Name : sa . Name ,
191263 },
192264 }
193265 require .NoError (t , c .Create (context .Background (), clusterExtension ))
@@ -225,8 +297,8 @@ func TestClusterExtensionForceInstallNonSuccessorVersion(t *testing.T) {
225297func TestClusterExtensionInstallSuccessorVersion (t * testing.T ) {
226298 t .Log ("When a cluster extension is installed from a catalog" )
227299 t .Log ("When resolving upgrade edges" )
228- clusterExtension , extensionCatalog := testInit (t )
229- defer testCleanup (t , extensionCatalog , clusterExtension )
300+ clusterExtension , extensionCatalog , sa := testInit (t )
301+ defer testCleanup (t , extensionCatalog , clusterExtension , sa )
230302 defer getArtifactsOutput (t )
231303
232304 t .Log ("By creating an ClusterExtension at a specified version" )
@@ -235,7 +307,7 @@ func TestClusterExtensionInstallSuccessorVersion(t *testing.T) {
235307 Version : "1.0.0" ,
236308 InstallNamespace : "default" ,
237309 ServiceAccount : ocv1alpha1.ServiceAccountReference {
238- Name : "default" ,
310+ Name : sa . Name ,
239311 },
240312 }
241313 require .NoError (t , c .Create (context .Background (), clusterExtension ))
@@ -272,15 +344,15 @@ func TestClusterExtensionInstallSuccessorVersion(t *testing.T) {
272344func TestClusterExtensionInstallReResolvesWhenCatalogIsPatched (t * testing.T ) {
273345 t .Log ("When a cluster extension is installed from a catalog" )
274346 t .Log ("It resolves again when a catalog is patched with new ImageRef" )
275- clusterExtension , extensionCatalog := testInit (t )
276- defer testCleanup (t , extensionCatalog , clusterExtension )
347+ clusterExtension , extensionCatalog , sa := testInit (t )
348+ defer testCleanup (t , extensionCatalog , clusterExtension , sa )
277349 defer getArtifactsOutput (t )
278350
279351 clusterExtension .Spec = ocv1alpha1.ClusterExtensionSpec {
280352 PackageName : "prometheus" ,
281353 InstallNamespace : "default" ,
282354 ServiceAccount : ocv1alpha1.ServiceAccountReference {
283- Name : "default" ,
355+ Name : sa . Name ,
284356 },
285357 }
286358 t .Log ("It resolves the specified package with correct bundle path" )
@@ -351,14 +423,16 @@ func TestClusterExtensionInstallReResolvesWhenNewCatalog(t *testing.T) {
351423 Name : clusterExtensionName ,
352424 },
353425 }
354- defer testCleanup (t , extensionCatalog , clusterExtension )
426+ sa , err := createServiceAccount (context .Background (), types.NamespacedName {Name : clusterExtensionName , Namespace : "default" })
427+ require .NoError (t , err )
428+ defer testCleanup (t , extensionCatalog , clusterExtension , sa )
355429 defer getArtifactsOutput (t )
356430
357431 clusterExtension .Spec = ocv1alpha1.ClusterExtensionSpec {
358432 PackageName : "prometheus" ,
359433 InstallNamespace : "default" ,
360434 ServiceAccount : ocv1alpha1.ServiceAccountReference {
361- Name : "default" ,
435+ Name : sa . Name ,
362436 },
363437 }
364438 t .Log ("It resolves the specified package with correct bundle path" )
0 commit comments