@@ -70,9 +70,10 @@ func TestGeneratesWithinRange(t *testing.T) {
70
70
require .NoError (t , quick .Check (f , nil ))
71
71
}
72
72
73
- func TestRBACBindings (t * testing.T ) {
73
+ func TestRBACForClusterServiceVersion (t * testing.T ) {
74
74
serviceAccount1 := "test-service-account"
75
- serviceAccount2 := "second-account"
75
+ serviceAccount2 := "second-service-account"
76
+ csvName := "test-csv.v1.1.0"
76
77
77
78
rules := []rbacv1.PolicyRule {
78
79
{
@@ -82,16 +83,20 @@ func TestRBACBindings(t *testing.T) {
82
83
},
83
84
}
84
85
86
+ // Note: two CSVs have same name and permissions for a cluster role, this is chosen intentionally,
87
+ // to verify that ClusterRole and ClusterRoleBinding have different names when the same CSV is installed
88
+ // twice in the same cluster, but in different namespaces.
85
89
tests := []struct {
86
90
name string
87
91
csv v1alpha1.ClusterServiceVersion
88
92
want map [string ]* OperatorPermissions
89
93
}{
90
94
{
91
- name : "RoleBinding " ,
95
+ name : "RoleBindings and one ClusterRoleBinding " ,
92
96
csv : v1alpha1.ClusterServiceVersion {
93
97
ObjectMeta : metav1.ObjectMeta {
94
- Name : "test-csv-1.1.0" ,
98
+ Name : csvName ,
99
+ Namespace : "test-namespace" ,
95
100
},
96
101
Spec : v1alpha1.ClusterServiceVersionSpec {
97
102
InstallStrategy : v1alpha1.NamedInstallStrategy {
@@ -117,13 +122,20 @@ func TestRBACBindings(t *testing.T) {
117
122
Rules : rules ,
118
123
},
119
124
},
125
+ ClusterPermissions : []v1alpha1.StrategyDeploymentPermissions {
126
+ {
127
+ ServiceAccountName : serviceAccount1 ,
128
+ Rules : rules ,
129
+ },
130
+ },
120
131
},
121
132
},
122
133
},
123
134
},
124
135
want : map [string ]* OperatorPermissions {
125
136
serviceAccount1 : {
126
- RoleBindings : []* rbacv1.RoleBinding {{}, {}},
137
+ RoleBindings : []* rbacv1.RoleBinding {{}, {}},
138
+ ClusterRoleBindings : []* rbacv1.ClusterRoleBinding {{}},
127
139
},
128
140
serviceAccount2 : {
129
141
RoleBindings : []* rbacv1.RoleBinding {{}},
@@ -134,7 +146,8 @@ func TestRBACBindings(t *testing.T) {
134
146
name : "ClusterRoleBinding" ,
135
147
csv : v1alpha1.ClusterServiceVersion {
136
148
ObjectMeta : metav1.ObjectMeta {
137
- Name : "second-csv-1.1.0" ,
149
+ Name : csvName ,
150
+ Namespace : "second-namespace" ,
138
151
},
139
152
Spec : v1alpha1.ClusterServiceVersionSpec {
140
153
InstallStrategy : v1alpha1.NamedInstallStrategy {
@@ -164,13 +177,18 @@ func TestRBACBindings(t *testing.T) {
164
177
},
165
178
},
166
179
}
180
+
181
+ // declared here to verify that names are unique when same csv is install in different namespaces
182
+ clusterRoleBindingNames := map [string ]bool {}
183
+ clusterRolesNames := map [string ]bool {}
184
+
167
185
for _ , tt := range tests {
168
186
t .Run (tt .name , func (t * testing.T ) {
169
187
result , err := RBACForClusterServiceVersion (& tt .csv )
170
188
require .NoError (t , err )
171
189
172
190
roleBindingNames := map [string ]bool {}
173
- clusterRoleBindingNames := map [string ]bool {}
191
+ rolesNames := map [string ]bool {}
174
192
for serviceAccount , permissions := range tt .want {
175
193
// Check that correct number of bindings is created
176
194
require .Equal (t , len (permissions .RoleBindings ), len (result [serviceAccount ].RoleBindings ))
@@ -197,6 +215,20 @@ func TestRBACBindings(t *testing.T) {
197
215
require .False (t , crbWithNameExists , "ClusterRoleBinding with the same name already generated" )
198
216
clusterRoleBindingNames [clusterRoleBinding .Name ] = true
199
217
}
218
+
219
+ // Check that Roles are created with unique names
220
+ for _ , role := range result [serviceAccount ].Roles {
221
+ _ , roleWithNameExists := rolesNames [role .Name ]
222
+ require .False (t , roleWithNameExists , "Role with the same name already generated" )
223
+ rolesNames [role .Name ] = true
224
+ }
225
+
226
+ // Check that ClusterRoles are created with unique names
227
+ for _ , clusterRole := range result [serviceAccount ].ClusterRoles {
228
+ _ , crWithNameExists := clusterRolesNames [clusterRole .Name ]
229
+ require .False (t , crWithNameExists , "ClusterRole with the same name already generated" )
230
+ clusterRolesNames [clusterRole .Name ] = true
231
+ }
200
232
}
201
233
})
202
234
}
0 commit comments