@@ -2,17 +2,14 @@ package e2e
2
2
3
3
import (
4
4
"context"
5
- "time"
6
-
7
5
. "github.com/onsi/ginkgo"
8
6
"github.com/onsi/ginkgo/extensions/table"
7
+ . "github.com/onsi/gomega"
9
8
"github.com/sirupsen/logrus"
10
- "github.com/stretchr/testify/require"
11
9
corev1 "k8s.io/api/core/v1"
12
10
k8serrors "k8s.io/apimachinery/pkg/api/errors"
13
11
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
12
"k8s.io/apimachinery/pkg/runtime/schema"
15
- "k8s.io/apimachinery/pkg/util/wait"
16
13
"k8s.io/client-go/dynamic"
17
14
"k8s.io/client-go/rest"
18
15
@@ -22,17 +19,18 @@ import (
22
19
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/ctx"
23
20
)
24
21
25
- var _ = Describe ("Scoped Client" , func () {
22
+ var _ = Describe ("Scoped Client bound to a service account can be used to make API calls " , func () {
26
23
// TestScopedClient ensures that we can create a scoped client bound to a
27
24
// service account and then we can use the scoped client to make API calls.
25
+ var (
26
+ config * rest.Config
28
27
29
- var config * rest.Config
30
-
31
- var kubeclient operatorclient.ClientInterface
32
- var crclient versioned.Interface
33
- var dynamicclient dynamic.Interface
28
+ kubeclient operatorclient.ClientInterface
29
+ crclient versioned.Interface
30
+ dynamicclient dynamic.Interface
34
31
35
- var logger * logrus.Logger
32
+ logger * logrus.Logger
33
+ )
36
34
37
35
BeforeEach (func () {
38
36
config = ctx .Ctx ().RESTConfig ()
@@ -42,6 +40,7 @@ var _ = Describe("Scoped Client", func() {
42
40
dynamicclient = ctx .Ctx ().DynamicClient ()
43
41
44
42
logger = logrus .New ()
43
+ logger .SetOutput (GinkgoWriter )
45
44
})
46
45
47
46
type testParameter struct {
@@ -55,30 +54,28 @@ var _ = Describe("Scoped Client", func() {
55
54
// scoped client has enough permission, we expect a NotFound error code.
56
55
// Otherwise, we expect a 'Forbidden' error code due to lack of permission.
57
56
58
- table .Entry ("ServiceAccountDoesNotHaveAnyPermission " , testParameter {
57
+ table .Entry ("returns error on API calls as ServiceAccount does not have any permission " , testParameter {
59
58
// The service account does not have any permission granted to it.
60
59
// We expect the get api call to return 'Forbidden' error due to
61
60
// lack of permission.
62
- name : "ServiceAccountDoesNotHaveAnyPermission" ,
63
61
assertFunc : func (errGot error ) {
64
- require . True ( GinkgoT (), k8serrors .IsForbidden (errGot ))
62
+ Expect ( k8serrors .IsForbidden (errGot )). To ( BeTrue ( ))
65
63
},
66
64
}),
67
- table .Entry ("ServiceAccountHasPermission " , testParameter {
65
+ table .Entry ("successfully allows API calls to be made when ServiceAccount has permission " , testParameter {
68
66
// The service account does have permission granted to it.
69
67
// We expect the get api call to return 'NotFound' error.
70
- name : "ServiceAccountHasPermission" ,
71
68
grant : func (namespace , name string ) (cleanup cleanupFunc ) {
72
69
cleanup = grantPermission (GinkgoT (), kubeclient , namespace , name )
73
70
return
74
71
},
75
72
assertFunc : func (errGot error ) {
76
- require . True ( GinkgoT (), k8serrors .IsNotFound (errGot ))
73
+ Expect ( k8serrors .IsNotFound (errGot )). To ( BeTrue ( ))
77
74
},
78
75
}),
79
76
}
80
77
81
- table .DescribeTable ("Test " , func (tt testParameter ) {
78
+ table .DescribeTable ("API call using scoped client " , func (tc testParameter ) {
82
79
// Steps:
83
80
// 1. Create a new namespace
84
81
// 2. Create a service account.
@@ -91,63 +88,49 @@ var _ = Describe("Scoped Client", func() {
91
88
defer cleanupNS ()
92
89
93
90
saName := genName ("user-defined-" )
94
- sa , cleanupSA := newServiceAccount (GinkgoT (), kubeclient , namespace , saName )
91
+ sa , cleanupSA := newServiceAccount (kubeclient , namespace , saName )
95
92
defer cleanupSA ()
96
93
97
- waitForServiceAccountSecretAvailable (GinkgoT (), kubeclient , sa .GetNamespace (), sa .GetName ())
94
+ By ("Wait for ServiceAccount secret to be available" )
95
+ Eventually (func () (* corev1.ServiceAccount , error ) {
96
+ sa , err := kubeclient .KubernetesInterface ().CoreV1 ().ServiceAccounts (sa .GetNamespace ()).Get (context .TODO (), sa .GetName (), metav1.GetOptions {})
97
+ return sa , err
98
+ }).ShouldNot (WithTransform (func (v * corev1.ServiceAccount ) []corev1.ObjectReference {
99
+ return v .Secrets
100
+ }, BeEmpty ()))
98
101
99
102
strategy := scoped .NewClientAttenuator (logger , config , kubeclient , crclient , dynamicclient )
100
103
getter := func () (reference * corev1.ObjectReference , err error ) {
101
104
reference = & corev1.ObjectReference {
102
105
Namespace : namespace ,
103
106
Name : saName ,
104
107
}
105
-
106
108
return
107
109
}
108
110
109
- if tt .grant != nil {
110
- cleanupPerm := tt .grant (sa .GetNamespace (), sa .GetName ())
111
+ if tc .grant != nil {
112
+ cleanupPerm := tc .grant (sa .GetNamespace (), sa .GetName ())
111
113
defer cleanupPerm ()
112
114
}
113
115
114
- // We expect to get scoped client instance(s).
116
+ By ( "Get scoped client instance(s)" )
115
117
kubeclientGot , crclientGot , dynamicClientGot , errGot := strategy .AttenuateClient (getter )
116
- require .NoError (GinkgoT (), errGot )
117
- require .NotNil (GinkgoT (), kubeclientGot )
118
- require .NotNil (GinkgoT (), crclientGot )
118
+ Expect (errGot ).ToNot (HaveOccurred ())
119
+ Expect (kubeclientGot ).ToNot (BeNil ())
120
+ Expect (crclientGot ).ToNot (BeNil ())
121
+ Expect (dynamicClientGot ).ToNot (BeNil ())
119
122
120
123
_ , errGot = kubeclientGot .KubernetesInterface ().CoreV1 ().ConfigMaps (namespace ).Get (context .TODO (), genName ("does-not-exist-" ), metav1.GetOptions {})
121
- require . Error ( GinkgoT (), errGot )
122
- tt .assertFunc (errGot )
124
+ Expect ( errGot ). To ( HaveOccurred () )
125
+ tc .assertFunc (errGot )
123
126
124
127
_ , errGot = crclientGot .OperatorsV1alpha1 ().CatalogSources (namespace ).Get (context .TODO (), genName ("does-not-exist-" ), metav1.GetOptions {})
125
- require . Error ( GinkgoT (), errGot )
126
- tt .assertFunc (errGot )
128
+ Expect ( errGot ). To ( HaveOccurred () )
129
+ tc .assertFunc (errGot )
127
130
128
131
gvr := schema.GroupVersionResource {Group : "" , Version : "v1" , Resource : "ConfigMap" }
129
132
_ , errGot = dynamicClientGot .Resource (gvr ).Namespace (namespace ).Get (context .TODO (), genName ("does-not-exist-" ), metav1.GetOptions {})
130
- require . Error ( GinkgoT (), errGot )
131
- tt .assertFunc (errGot )
133
+ Expect ( errGot ). To ( HaveOccurred () )
134
+ tc .assertFunc (errGot )
132
135
}, tableEntries ... )
133
136
})
134
-
135
- func waitForServiceAccountSecretAvailable (t GinkgoTInterface , client operatorclient.ClientInterface , namespace , name string ) * corev1.ServiceAccount {
136
- var sa * corev1.ServiceAccount
137
- err := wait .Poll (5 * time .Second , time .Minute , func () (bool , error ) {
138
- sa , err := client .KubernetesInterface ().CoreV1 ().ServiceAccounts (namespace ).Get (context .TODO (), name , metav1.GetOptions {})
139
- if err != nil {
140
- return false , err
141
- }
142
-
143
- if len (sa .Secrets ) > 0 {
144
- return true , nil
145
- }
146
-
147
- return false , nil
148
-
149
- })
150
-
151
- require .NoError (t , err )
152
- return sa
153
- }
0 commit comments