@@ -6,13 +6,15 @@ import (
6
6
"strings"
7
7
"testing"
8
8
9
- appsv1 "k8s.io/api/apps/v1"
10
- "k8s.io/apimachinery/pkg/api/errors"
11
- "k8s.io/apimachinery/pkg/util/wait"
12
9
"github.com/coreos/go-semver/semver"
13
10
"github.com/stretchr/testify/require"
11
+ appsv1 "k8s.io/api/apps/v1"
14
12
corev1 "k8s.io/api/core/v1"
13
+ rbacv1 "k8s.io/api/rbac/v1"
14
+ extv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
15
+ "k8s.io/apimachinery/pkg/api/errors"
15
16
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
+ "k8s.io/apimachinery/pkg/util/wait"
16
18
17
19
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
18
20
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha2"
@@ -27,39 +29,49 @@ func DeploymentComplete(deployment *appsv1.Deployment, newStatus *appsv1.Deploym
27
29
}
28
30
29
31
// Currently this function only modifies the watchedNamespace in the container command
30
- func patchOlmDeployment (t * testing.T , c operatorclient.ClientInterface , newNamespace string ) [] string {
32
+ func patchOlmDeployment (t * testing.T , c operatorclient.ClientInterface , newNamespace string ) ( cleanupFunc func () error ) {
31
33
runningDeploy , err := c .GetDeployment (testNamespace , "olm-operator" )
32
34
require .NoError (t , err )
33
35
34
- command := runningDeploy .Spec .Template .Spec .Containers [0 ].Command
36
+ oldCommand := runningDeploy .Spec .Template .Spec .Containers [0 ].Command
35
37
re , err := regexp .Compile (`-watchedNamespaces\W(\S+)` )
36
38
require .NoError (t , err )
37
- newCommand := re .ReplaceAllString (strings .Join (command , " " ), "$0" + "," + newNamespace )
38
- t .Logf ("original=%#v newCommand=%#v" , command , newCommand )
39
+ newCommand := re .ReplaceAllString (strings .Join (oldCommand , " " ), "$0" + "," + newNamespace )
40
+ t .Logf ("original=%#v newCommand=%#v" , oldCommand , newCommand )
39
41
finalNewCommand := strings .Split (newCommand , " " )
40
42
runningDeploy .Spec .Template .Spec .Containers [0 ].Command = make ([]string , len (finalNewCommand ))
41
43
copy (runningDeploy .Spec .Template .Spec .Containers [0 ].Command , finalNewCommand )
42
44
43
- newDeployment , updated , err := c .UpdateDeployment (runningDeploy )
45
+ olmDeployment , updated , err := c .UpdateDeployment (runningDeploy )
44
46
if err != nil || updated == false {
45
47
t .Fatalf ("Deployment update failed: (updated %v) %v\n " , updated , err )
46
48
}
47
49
require .NoError (t , err )
48
50
49
51
err = wait .Poll (pollInterval , pollDuration , func () (bool , error ) {
50
52
t .Log ("Polling for OLM deployment update..." )
51
- fetchedDeployment , err := c .GetDeployment (newDeployment .Namespace , newDeployment .Name )
53
+ fetchedDeployment , err := c .GetDeployment (olmDeployment .Namespace , olmDeployment .Name )
52
54
if err != nil {
53
55
return false , err
54
56
}
55
- if DeploymentComplete (newDeployment , & fetchedDeployment .Status ) {
57
+ if DeploymentComplete (olmDeployment , & fetchedDeployment .Status ) {
56
58
return true , nil
57
59
}
58
60
return false , nil
59
61
})
60
-
61
62
require .NoError (t , err )
62
- return command
63
+
64
+ return func () error {
65
+ olmDeployment .Spec .Template .Spec .Containers [0 ].Command = oldCommand
66
+ _ , updated , err := c .UpdateDeployment (olmDeployment )
67
+ if err != nil || updated == false {
68
+ t .Fatalf ("Deployment update failed: (updated %v) %v\n " , updated , err )
69
+ }
70
+ if err != nil {
71
+ return err
72
+ }
73
+ return nil
74
+ }
63
75
}
64
76
65
77
func checkOperatorGroupAnnotations (obj metav1.Object , op * v1alpha2.OperatorGroup , targetNamespaces string ) error {
@@ -78,6 +90,7 @@ func checkOperatorGroupAnnotations(obj metav1.Object, op *v1alpha2.OperatorGroup
78
90
79
91
func TestOperatorGroup (t * testing.T ) {
80
92
// Create namespace with specific label
93
+ // Create CRD
81
94
// Create CSV in operator namespace
82
95
// Create operator group that watches namespace and uses specific label
83
96
// Verify operator group status contains correct status
@@ -101,10 +114,18 @@ func TestOperatorGroup(t *testing.T) {
101
114
createdOtherNamespace , err := c .KubernetesInterface ().CoreV1 ().Namespaces ().Create (& otherNamespace )
102
115
require .NoError (t , err )
103
116
104
- oldCommand := patchOlmDeployment (t , c , otherNamespaceName )
117
+ cleanupOlmDeployment := patchOlmDeployment (t , c , otherNamespaceName )
118
+
119
+ t .Log ("Creating CRD" )
120
+ mainCRDPlural := genName ("ins" )
121
+ apiGroup := "cluster.com"
122
+ mainCRDName := mainCRDPlural + "." + apiGroup
123
+ mainCRD := newCRD (mainCRDName , testNamespace , mainCRDPlural )
124
+ cleanupCRD , err := createCRD (c , mainCRD )
125
+ require .NoError (t , err )
105
126
106
127
t .Log ("Creating CSV" )
107
- aCSV := newCSV (csvName , testNamespace , "" , * semver .New ("0.0.0" ), nil , nil , newNginxInstallStrategy ("operator-deployment" , nil , nil ))
128
+ aCSV := newCSV (csvName , testNamespace , "" , * semver .New ("0.0.0" ), []extv1beta1. CustomResourceDefinition { mainCRD } , nil , newNginxInstallStrategy ("operator-deployment" , nil , nil ))
108
129
createdCSV , err := crc .OperatorsV1alpha1 ().ClusterServiceVersions (testNamespace ).Create (& aCSV )
109
130
require .NoError (t , err )
110
131
@@ -119,6 +140,7 @@ func TestOperatorGroup(t *testing.T) {
119
140
MatchLabels : matchingLabel ,
120
141
},
121
142
},
143
+ //ServiceAccountName: "default-sa",
122
144
}
123
145
_ , err = crc .OperatorsV1alpha2 ().OperatorGroups (testNamespace ).Create (& operatorGroup )
124
146
require .NoError (t , err )
@@ -133,12 +155,36 @@ func TestOperatorGroup(t *testing.T) {
133
155
return false , fetchErr
134
156
}
135
157
if len (fetched .Status .Namespaces ) > 0 {
136
- require .Equal (t , expectedOperatorGroupStatus .Namespaces [0 ].Name , fetched .Status .Namespaces [0 ].Name )
158
+ require .EqualValues (t , expectedOperatorGroupStatus .Namespaces [0 ].Name , fetched .Status .Namespaces [0 ].Name )
137
159
return true , nil
138
160
}
139
161
return false , nil
140
162
})
141
163
164
+ t .Log ("Checking for proper RBAC permissions in target namespace" )
165
+ roleList , err := c .KubernetesInterface ().RbacV1 ().ClusterRoles ().List (metav1.ListOptions {})
166
+ for _ , item := range roleList .Items {
167
+ role , err := c .GetClusterRole (item .GetName ())
168
+ require .NoError (t , err )
169
+ switch roleName := item .GetName (); roleName {
170
+ case "owned-crd-manager-another-csv" :
171
+ managerPolicyRules := []rbacv1.PolicyRule {
172
+ rbacv1.PolicyRule {Verbs : []string {"*" }, APIGroups : []string {apiGroup }, Resources : []string {mainCRDPlural }},
173
+ }
174
+ require .Equal (t , managerPolicyRules , role .Rules )
175
+ case "e2e-operator-group-edit" :
176
+ editPolicyRules := []rbacv1.PolicyRule {
177
+ rbacv1.PolicyRule {Verbs : []string {"create" , "update" , "patch" , "delete" }, APIGroups : []string {apiGroup }, Resources : []string {mainCRDPlural }},
178
+ }
179
+ require .Equal (t , editPolicyRules , role .Rules )
180
+ case "e2e-operator-group-view" :
181
+ viewPolicyRules := []rbacv1.PolicyRule {
182
+ rbacv1.PolicyRule {Verbs : []string {"get" , "list" , "watch" }, APIGroups : []string {apiGroup }, Resources : []string {mainCRDPlural }},
183
+ }
184
+ require .Equal (t , viewPolicyRules , role .Rules )
185
+ }
186
+ }
187
+
142
188
t .Log ("Waiting for operator namespace csv to have annotations" )
143
189
err = wait .Poll (pollInterval , pollDuration , func () (bool , error ) {
144
190
fetchedCSV , fetchErr := crc .OperatorsV1alpha1 ().ClusterServiceVersions (testNamespace ).Get (csvName , metav1.GetOptions {})
@@ -171,8 +217,8 @@ func TestOperatorGroup(t *testing.T) {
171
217
require .NoError (t , err )
172
218
require .EqualValues (t , v1alpha1 .CSVReasonCopied , fetchedCSV .Status .Reason )
173
219
// also check name and spec
174
- require .Equal (t , createdCSV .Name , fetchedCSV .Name )
175
- require .Equal (t , createdCSV .Spec , fetchedCSV .Spec )
220
+ require .EqualValues (t , createdCSV .Name , fetchedCSV .Name )
221
+ require .EqualValues (t , createdCSV .Spec , fetchedCSV .Spec )
176
222
177
223
t .Log ("Waiting on deployment to have correct annotations" )
178
224
err = wait .Poll (pollInterval , pollDuration , func () (bool , error ) {
@@ -195,29 +241,18 @@ func TestOperatorGroup(t *testing.T) {
195
241
require .NoError (t , err )
196
242
197
243
t .Log ("Waiting for orphaned CSV to be deleted" )
198
- err = wait . Poll ( pollInterval , pollDuration , func () ( bool , error ) {
244
+ err = waitForDelete ( func () error {
199
245
_ , err = crc .OperatorsV1alpha1 ().ClusterServiceVersions (otherNamespaceName ).Get (csvName , metav1.GetOptions {})
200
- if err != nil {
201
- if errors .IsNotFound (err ) {
202
- return true , nil
203
- }
204
- return false , err
205
- }
206
- return false , nil
246
+ return err
207
247
})
208
248
require .NoError (t , err )
209
249
210
250
// clean up
211
- // TODO: unpatch function
212
- runningDeploy , err := c .GetDeployment (testNamespace , "olm-operator" )
213
- require .NoError (t , err )
214
- runningDeploy .Spec .Template .Spec .Containers [0 ].Command = oldCommand
215
- _ , updated , err := c .UpdateDeployment (runningDeploy )
216
- if err != nil || updated == false {
217
- t .Fatalf ("Deployment update failed: (updated %v) %v\n " , updated , err )
218
- }
251
+ err = cleanupOlmDeployment ()
219
252
require .NoError (t , err )
220
253
254
+ cleanupCRD ()
255
+
221
256
err = c .KubernetesInterface ().CoreV1 ().Namespaces ().Delete (otherNamespaceName , & metav1.DeleteOptions {})
222
257
require .NoError (t , err )
223
258
err = crc .OperatorsV1alpha2 ().OperatorGroups (testNamespace ).Delete (operatorGroup .Name , & metav1.DeleteOptions {})
0 commit comments