@@ -2,17 +2,14 @@ package e2e
22
33import (
44 "context"
5- "time"
6-
75 . "github.com/onsi/ginkgo"
86 "github.com/onsi/ginkgo/extensions/table"
7+ . "github.com/onsi/gomega"
98 "github.com/sirupsen/logrus"
10- "github.com/stretchr/testify/require"
119 corev1 "k8s.io/api/core/v1"
1210 k8serrors "k8s.io/apimachinery/pkg/api/errors"
1311 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1412 "k8s.io/apimachinery/pkg/runtime/schema"
15- "k8s.io/apimachinery/pkg/util/wait"
1613 "k8s.io/client-go/dynamic"
1714 "k8s.io/client-go/rest"
1815
@@ -22,17 +19,18 @@ import (
2219 "github.com/operator-framework/operator-lifecycle-manager/test/e2e/ctx"
2320)
2421
25- var _ = Describe ("Scoped Client" , func () {
22+ var _ = Describe ("Scoped Client bound to a service account can be used to make API calls " , func () {
2623 // TestScopedClient ensures that we can create a scoped client bound to a
2724 // service account and then we can use the scoped client to make API calls.
25+ var (
26+ config * rest.Config
2827
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
3431
35- var logger * logrus.Logger
32+ logger * logrus.Logger
33+ )
3634
3735 BeforeEach (func () {
3836 config = ctx .Ctx ().RESTConfig ()
@@ -42,6 +40,7 @@ var _ = Describe("Scoped Client", func() {
4240 dynamicclient = ctx .Ctx ().DynamicClient ()
4341
4442 logger = logrus .New ()
43+ logger .SetOutput (GinkgoWriter )
4544 })
4645
4746 type testParameter struct {
@@ -55,30 +54,28 @@ var _ = Describe("Scoped Client", func() {
5554 // scoped client has enough permission, we expect a NotFound error code.
5655 // Otherwise, we expect a 'Forbidden' error code due to lack of permission.
5756
58- table .Entry ("ServiceAccountDoesNotHaveAnyPermission " , testParameter {
57+ table .Entry ("returns error on API calls as ServiceAccount does not have any permission " , testParameter {
5958 // The service account does not have any permission granted to it.
6059 // We expect the get api call to return 'Forbidden' error due to
6160 // lack of permission.
62- name : "ServiceAccountDoesNotHaveAnyPermission" ,
6361 assertFunc : func (errGot error ) {
64- require . True ( GinkgoT (), k8serrors .IsForbidden (errGot ))
62+ Expect ( k8serrors .IsForbidden (errGot )). To ( BeTrue ( ))
6563 },
6664 }),
67- table .Entry ("ServiceAccountHasPermission " , testParameter {
65+ table .Entry ("successfully allows API calls to be made when ServiceAccount has permission " , testParameter {
6866 // The service account does have permission granted to it.
6967 // We expect the get api call to return 'NotFound' error.
70- name : "ServiceAccountHasPermission" ,
7168 grant : func (namespace , name string ) (cleanup cleanupFunc ) {
7269 cleanup = grantPermission (GinkgoT (), kubeclient , namespace , name )
7370 return
7471 },
7572 assertFunc : func (errGot error ) {
76- require . True ( GinkgoT (), k8serrors .IsNotFound (errGot ))
73+ Expect ( k8serrors .IsNotFound (errGot )). To ( BeTrue ( ))
7774 },
7875 }),
7976 }
8077
81- table .DescribeTable ("Test " , func (tt testParameter ) {
78+ table .DescribeTable ("API call using scoped client " , func (tc testParameter ) {
8279 // Steps:
8380 // 1. Create a new namespace
8481 // 2. Create a service account.
@@ -91,63 +88,49 @@ var _ = Describe("Scoped Client", func() {
9188 defer cleanupNS ()
9289
9390 saName := genName ("user-defined-" )
94- sa , cleanupSA := newServiceAccount (GinkgoT (), kubeclient , namespace , saName )
91+ sa , cleanupSA := newServiceAccount (kubeclient , namespace , saName )
9592 defer cleanupSA ()
9693
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 ()))
98101
99102 strategy := scoped .NewClientAttenuator (logger , config , kubeclient , crclient , dynamicclient )
100103 getter := func () (reference * corev1.ObjectReference , err error ) {
101104 reference = & corev1.ObjectReference {
102105 Namespace : namespace ,
103106 Name : saName ,
104107 }
105-
106108 return
107109 }
108110
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 ())
111113 defer cleanupPerm ()
112114 }
113115
114- // We expect to get scoped client instance(s).
116+ By ( "Get scoped client instance(s)" )
115117 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 ())
119122
120123 _ , 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 )
123126
124127 _ , 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 )
127130
128131 gvr := schema.GroupVersionResource {Group : "" , Version : "v1" , Resource : "ConfigMap" }
129132 _ , 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 )
132135 }, tableEntries ... )
133136})
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