Skip to content

Commit ff1b761

Browse files
committed
Convert scoped_client_test to ginkgo
1 parent 110997c commit ff1b761

File tree

3 files changed

+54
-54
lines changed

3 files changed

+54
-54
lines changed

test/e2e/scoped_client_test.go

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import (
66

77
. "github.com/onsi/ginkgo"
88
"github.com/onsi/ginkgo/extensions/table"
9+
. "github.com/onsi/gomega"
910
"github.com/sirupsen/logrus"
10-
"github.com/stretchr/testify/require"
1111
corev1 "k8s.io/api/core/v1"
1212
k8serrors "k8s.io/apimachinery/pkg/api/errors"
1313
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1414
"k8s.io/apimachinery/pkg/runtime/schema"
15-
"k8s.io/apimachinery/pkg/util/wait"
1615
"k8s.io/client-go/dynamic"
1716
"k8s.io/client-go/rest"
1817

@@ -22,17 +21,18 @@ import (
2221
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/ctx"
2322
)
2423

25-
var _ = Describe("Scoped Client", func() {
24+
var _ = Describe("Scoped Client bound to a service account can be used to make API calls", func() {
2625
// TestScopedClient ensures that we can create a scoped client bound to a
2726
// service account and then we can use the scoped client to make API calls.
27+
var (
28+
config *rest.Config
2829

29-
var config *rest.Config
30+
kubeclient operatorclient.ClientInterface
31+
crclient versioned.Interface
32+
dynamicclient dynamic.Interface
3033

31-
var kubeclient operatorclient.ClientInterface
32-
var crclient versioned.Interface
33-
var dynamicclient dynamic.Interface
34-
35-
var logger *logrus.Logger
34+
logger *logrus.Logger
35+
)
3636

3737
BeforeEach(func() {
3838
config = ctx.Ctx().RESTConfig()
@@ -61,7 +61,7 @@ var _ = Describe("Scoped Client", func() {
6161
// lack of permission.
6262
name: "ServiceAccountDoesNotHaveAnyPermission",
6363
assertFunc: func(errGot error) {
64-
require.True(GinkgoT(), k8serrors.IsForbidden(errGot))
64+
Expect(k8serrors.IsForbidden(errGot)).To(BeTrue())
6565
},
6666
}),
6767
table.Entry("ServiceAccountHasPermission", testParameter{
@@ -73,12 +73,12 @@ var _ = Describe("Scoped Client", func() {
7373
return
7474
},
7575
assertFunc: func(errGot error) {
76-
require.True(GinkgoT(), k8serrors.IsNotFound(errGot))
76+
Expect(k8serrors.IsNotFound(errGot)).To(BeTrue())
7777
},
7878
}),
7979
}
8080

81-
table.DescribeTable("Test", func(tt testParameter) {
81+
table.DescribeTable("API call using scoped client", func(testParam testParameter) {
8282
// Steps:
8383
// 1. Create a new namespace
8484
// 2. Create a service account.
@@ -87,14 +87,14 @@ var _ = Describe("Scoped Client", func() {
8787
// 5. Invoke Get API call on non existent object(s) to check if
8888
// the call can be made successfully.
8989
namespace := genName("a")
90-
_, cleanupNS := newNamespace(GinkgoT(), kubeclient, namespace)
90+
_, cleanupNS := newNamespace(kubeclient, namespace)
9191
defer cleanupNS()
9292

9393
saName := genName("user-defined-")
94-
sa, cleanupSA := newServiceAccount(GinkgoT(), kubeclient, namespace, saName)
94+
sa, cleanupSA := newServiceAccount(kubeclient, namespace, saName)
9595
defer cleanupSA()
9696

97-
waitForServiceAccountSecretAvailable(GinkgoT(), kubeclient, sa.GetNamespace(), sa.GetName())
97+
waitForServiceAccountSecretAvailable(kubeclient, sa.GetNamespace(), sa.GetName())
9898

9999
strategy := scoped.NewClientAttenuator(logger, config, kubeclient, crclient, dynamicclient)
100100
getter := func() (reference *corev1.ObjectReference, err error) {
@@ -106,35 +106,36 @@ var _ = Describe("Scoped Client", func() {
106106
return
107107
}
108108

109-
if tt.grant != nil {
110-
cleanupPerm := tt.grant(sa.GetNamespace(), sa.GetName())
109+
if testParam.grant != nil {
110+
cleanupPerm := testParam.grant(sa.GetNamespace(), sa.GetName())
111111
defer cleanupPerm()
112112
}
113113

114114
// We expect to get scoped client instance(s).
115115
kubeclientGot, crclientGot, dynamicClientGot, errGot := strategy.AttenuateClient(getter)
116-
require.NoError(GinkgoT(), errGot)
117-
require.NotNil(GinkgoT(), kubeclientGot)
118-
require.NotNil(GinkgoT(), crclientGot)
116+
Expect(errGot).ToNot(HaveOccurred())
117+
Expect(kubeclientGot).ToNot(BeNil())
118+
Expect(crclientGot).ToNot(BeNil())
119119

120120
_, errGot = kubeclientGot.KubernetesInterface().CoreV1().ConfigMaps(namespace).Get(context.TODO(), genName("does-not-exist-"), metav1.GetOptions{})
121-
require.Error(GinkgoT(), errGot)
122-
tt.assertFunc(errGot)
121+
Expect(errGot).To(HaveOccurred())
122+
testParam.assertFunc(errGot)
123123

124124
_, errGot = crclientGot.OperatorsV1alpha1().CatalogSources(namespace).Get(context.TODO(), genName("does-not-exist-"), metav1.GetOptions{})
125-
require.Error(GinkgoT(), errGot)
126-
tt.assertFunc(errGot)
125+
Expect(errGot).To(HaveOccurred())
126+
testParam.assertFunc(errGot)
127127

128128
gvr := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "ConfigMap"}
129129
_, errGot = dynamicClientGot.Resource(gvr).Namespace(namespace).Get(context.TODO(), genName("does-not-exist-"), metav1.GetOptions{})
130-
require.Error(GinkgoT(), errGot)
131-
tt.assertFunc(errGot)
130+
Expect(errGot).To(HaveOccurred())
131+
testParam.assertFunc(errGot)
132132
}, tableEntries...)
133133
})
134134

135-
func waitForServiceAccountSecretAvailable(t GinkgoTInterface, client operatorclient.ClientInterface, namespace, name string) *corev1.ServiceAccount {
135+
func waitForServiceAccountSecretAvailable(client operatorclient.ClientInterface, namespace, name string) *corev1.ServiceAccount {
136136
var sa *corev1.ServiceAccount
137-
err := wait.Poll(5*time.Second, time.Minute, func() (bool, error) {
137+
138+
Eventually(func() (bool, error) {
138139
sa, err := client.KubernetesInterface().CoreV1().ServiceAccounts(namespace).Get(context.TODO(), name, metav1.GetOptions{})
139140
if err != nil {
140141
return false, err
@@ -145,9 +146,7 @@ func waitForServiceAccountSecretAvailable(t GinkgoTInterface, client operatorcli
145146
}
146147

147148
return false, nil
149+
}, time.Minute, 5*time.Second).Should(BeTrue())
148150

149-
})
150-
151-
require.NoError(t, err)
152151
return sa
153152
}

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)