Skip to content

Commit e52cb0a

Browse files
committed
Convert scoped_client_test to ginkgo
1 parent a90b83a commit e52cb0a

File tree

4 files changed

+62
-77
lines changed

4 files changed

+62
-77
lines changed

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,7 @@ k8s.io/kube-openapi v0.0.0-20200121204235-bf4fb3bd569c h1:/KUFqjjqAcY4Us6luF5RDN
10431043
k8s.io/kube-openapi v0.0.0-20200121204235-bf4fb3bd569c/go.mod h1:GRQhZsXIAJ1xR0C9bd8UpWHZ5plfAS9fzPjJuQ6JL3E=
10441044
k8s.io/kubectl v0.18.0 h1:hu52Ndq/d099YW+3sS3VARxFz61Wheiq8K9S7oa82Dk=
10451045
k8s.io/kubectl v0.18.0/go.mod h1:LOkWx9Z5DXMEg5KtOjHhRiC1fqJPLyCr3KtQgEolCkU=
1046+
k8s.io/kubernetes v1.13.0 h1:qTfB+u5M92k2fCCCVP2iuhgwwSOv1EkAkvQY1tQODD8=
10461047
k8s.io/kubernetes v1.13.0/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk=
10471048
k8s.io/metrics v0.18.0/go.mod h1:8aYTW18koXqjLVKL7Ds05RPMX9ipJZI3mywYvBOxXd4=
10481049
k8s.io/utils v0.0.0-20190801114015-581e00157fb1/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew=

test/e2e/scoped_client_test.go

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@ package e2e
22

33
import (
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.
@@ -87,67 +84,53 @@ var _ = Describe("Scoped Client", func() {
8784
// 5. Invoke Get API call on non existent object(s) to check if
8885
// the call can be made successfully.
8986
namespace := genName("a")
90-
_, cleanupNS := newNamespace(GinkgoT(), kubeclient, namespace)
87+
_, cleanupNS := newNamespace(kubeclient, namespace)
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-
}

test/e2e/user_defined_sa_test.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/blang/semver"
88
. "github.com/onsi/ginkgo"
9+
. "github.com/onsi/gomega"
910
v1 "github.com/operator-framework/api/pkg/operators/v1"
1011
"github.com/operator-framework/api/pkg/operators/v1alpha1"
1112
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
@@ -29,17 +30,17 @@ var _ = Describe("User defined service account", func() {
2930
crclient := newCRClient()
3031

3132
namespace := genName("scoped-ns-")
32-
_, cleanupNS := newNamespace(GinkgoT(), kubeclient, namespace)
33+
_, cleanupNS := newNamespace(kubeclient, namespace)
3334
defer cleanupNS()
3435

3536
// Create a service account, but add no permission to it.
3637
saName := genName("scoped-sa-")
37-
_, cleanupSA := newServiceAccount(GinkgoT(), kubeclient, namespace, saName)
38+
_, cleanupSA := newServiceAccount(kubeclient, namespace, saName)
3839
defer cleanupSA()
3940

4041
// Add an OperatorGroup and specify the service account.
4142
ogName := genName("scoped-og-")
42-
_, cleanupOG := newOperatorGroupWithServiceAccount(GinkgoT(), crclient, namespace, ogName, saName)
43+
_, cleanupOG := newOperatorGroupWithServiceAccount(crclient, namespace, ogName, saName)
4344
defer cleanupOG()
4445

4546
permissions := deploymentPermissions()
@@ -84,19 +85,19 @@ var _ = Describe("User defined service account", func() {
8485
crclient := newCRClient()
8586

8687
namespace := genName("scoped-ns-")
87-
_, cleanupNS := newNamespace(GinkgoT(), kubeclient, namespace)
88+
_, cleanupNS := newNamespace(kubeclient, namespace)
8889
defer cleanupNS()
8990

9091
// Create a service account, add enough permission to it so that operator install is successful.
9192
saName := genName("scoped-sa")
92-
_, cleanupSA := newServiceAccount(GinkgoT(), kubeclient, namespace, saName)
93+
_, cleanupSA := newServiceAccount(kubeclient, namespace, saName)
9394
defer cleanupSA()
9495
cleanupPerm := grantPermission(GinkgoT(), kubeclient, namespace, saName)
9596
defer cleanupPerm()
9697

9798
// Add an OperatorGroup and specify the service account.
9899
ogName := genName("scoped-og-")
99-
_, cleanupOG := newOperatorGroupWithServiceAccount(GinkgoT(), crclient, namespace, ogName, saName)
100+
_, cleanupOG := newOperatorGroupWithServiceAccount(crclient, namespace, ogName, saName)
100101
defer cleanupOG()
101102

102103
permissions := deploymentPermissions()
@@ -140,17 +141,17 @@ var _ = Describe("User defined service account", func() {
140141
crclient := newCRClient()
141142

142143
namespace := genName("scoped-ns-")
143-
_, cleanupNS := newNamespace(GinkgoT(), kubeclient, namespace)
144+
_, cleanupNS := newNamespace(kubeclient, namespace)
144145
defer cleanupNS()
145146

146147
// Create a service account, but add no permission to it.
147148
saName := genName("scoped-sa-")
148-
_, cleanupSA := newServiceAccount(GinkgoT(), kubeclient, namespace, saName)
149+
_, cleanupSA := newServiceAccount(kubeclient, namespace, saName)
149150
defer cleanupSA()
150151

151152
// Add an OperatorGroup and specify the service account.
152153
ogName := genName("scoped-og-")
153-
_, cleanupOG := newOperatorGroupWithServiceAccount(GinkgoT(), crclient, namespace, ogName, saName)
154+
_, cleanupOG := newOperatorGroupWithServiceAccount(crclient, namespace, ogName, saName)
154155
defer cleanupOG()
155156

156157
permissions := deploymentPermissions()
@@ -188,26 +189,26 @@ var _ = Describe("User defined service account", func() {
188189
})
189190
})
190191

191-
func newNamespace(t GinkgoTInterface, client operatorclient.ClientInterface, name string) (ns *corev1.Namespace, cleanup cleanupFunc) {
192+
func newNamespace(client operatorclient.ClientInterface, name string) (ns *corev1.Namespace, cleanup cleanupFunc) {
192193
request := &corev1.Namespace{
193194
ObjectMeta: metav1.ObjectMeta{
194195
Name: name,
195196
},
196197
}
197198

198199
ns, err := client.KubernetesInterface().CoreV1().Namespaces().Create(context.TODO(), request, metav1.CreateOptions{})
199-
require.NoError(t, err)
200-
require.NotNil(t, ns)
200+
Expect(err).ToNot(HaveOccurred())
201+
Expect(ns).ToNot(BeNil())
201202

202203
cleanup = func() {
203204
err := client.KubernetesInterface().CoreV1().Namespaces().Delete(context.TODO(), ns.GetName(), metav1.DeleteOptions{})
204-
require.NoError(t, err)
205+
Expect(err).ToNot(HaveOccurred())
205206
}
206207

207208
return
208209
}
209210

210-
func newServiceAccount(t GinkgoTInterface, client operatorclient.ClientInterface, namespace, name string) (sa *corev1.ServiceAccount, cleanup cleanupFunc) {
211+
func newServiceAccount(client operatorclient.ClientInterface, namespace, name string) (sa *corev1.ServiceAccount, cleanup cleanupFunc) {
211212
request := &corev1.ServiceAccount{
212213
ObjectMeta: metav1.ObjectMeta{
213214
Namespace: namespace,
@@ -216,18 +217,18 @@ func newServiceAccount(t GinkgoTInterface, client operatorclient.ClientInterface
216217
}
217218

218219
sa, err := client.KubernetesInterface().CoreV1().ServiceAccounts(namespace).Create(context.TODO(), request, metav1.CreateOptions{})
219-
require.NoError(t, err)
220-
require.NotNil(t, sa)
220+
Expect(err).ToNot(HaveOccurred())
221+
Expect(sa).ToNot(BeNil())
221222

222223
cleanup = func() {
223224
err := client.KubernetesInterface().CoreV1().ServiceAccounts(sa.GetNamespace()).Delete(context.TODO(), sa.GetName(), metav1.DeleteOptions{})
224-
require.NoError(t, err)
225+
Expect(err).ToNot(HaveOccurred())
225226
}
226227

227228
return
228229
}
229230

230-
func newOperatorGroupWithServiceAccount(t GinkgoTInterface, client versioned.Interface, namespace, name, serviceAccountName string) (og *v1.OperatorGroup, cleanup cleanupFunc) {
231+
func newOperatorGroupWithServiceAccount(client versioned.Interface, namespace, name, serviceAccountName string) (og *v1.OperatorGroup, cleanup cleanupFunc) {
231232
request := &v1.OperatorGroup{
232233
ObjectMeta: metav1.ObjectMeta{
233234
Namespace: namespace,
@@ -242,12 +243,12 @@ func newOperatorGroupWithServiceAccount(t GinkgoTInterface, client versioned.Int
242243
}
243244

244245
og, err := client.OperatorsV1().OperatorGroups(namespace).Create(context.TODO(), request, metav1.CreateOptions{})
245-
require.NoError(t, err)
246-
require.NotNil(t, og)
246+
Expect(err).ToNot(HaveOccurred())
247+
Expect(og).ToNot(BeNil())
247248

248249
cleanup = func() {
249250
err := client.OperatorsV1().OperatorGroups(og.GetNamespace()).Delete(context.TODO(), og.GetName(), metav1.DeleteOptions{})
250-
require.NoError(t, err)
251+
Expect(err).ToNot(HaveOccurred())
251252
}
252253

253254
return

test/e2e/webhook_e2e_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,10 @@ var _ = Describe("CSVs with a Webhook", func() {
488488
})
489489
It("Allows multiple installs of the same webhook", func() {
490490
var csv v1alpha1.ClusterServiceVersion
491-
namespace1, ns1CleanupFunc := newNamespace(GinkgoT(), c, genName("webhook-test-"))
491+
namespace1, ns1CleanupFunc := newNamespace(c, genName("webhook-test-"))
492492
defer ns1CleanupFunc()
493493

494-
namespace2, ns2CleanupFunc := newNamespace(GinkgoT(), c, genName("webhook-test-"))
494+
namespace2, ns2CleanupFunc := newNamespace(c, genName("webhook-test-"))
495495
defer ns2CleanupFunc()
496496

497497
og1 := newOperatorGroup(namespace1.Name, genName("test-og-"), nil, nil, []string{"test-go-"}, false)

0 commit comments

Comments
 (0)