Skip to content

Commit 21b3de4

Browse files
committed
refactor testutil/testutil.go into util package
1 parent 2bcb8ae commit 21b3de4

File tree

13 files changed

+197
-206
lines changed

13 files changed

+197
-206
lines changed

pkg/auth/auth_service_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"testing"
88

99
. "github.com/onsi/gomega"
10-
"github.com/oracle/oci-native-ingress-controller/pkg/testutil"
1110
"github.com/oracle/oci-native-ingress-controller/pkg/types"
11+
"github.com/oracle/oci-native-ingress-controller/pkg/util"
1212
v1 "k8s.io/api/core/v1"
1313
fakeclientset "k8s.io/client-go/kubernetes/fake"
1414
)
@@ -24,7 +24,7 @@ func setUp(secret *v1.Secret, setClient bool) *fakeclientset.Clientset {
2424
action := "get"
2525
resource := "secrets"
2626
obj := secret
27-
testutil.FakeClientGetCall(client, action, resource, obj)
27+
util.FakeClientGetCall(client, action, resource, obj)
2828
}
2929
return client
3030
}
@@ -38,7 +38,7 @@ func TestGetConfigurationProviderSuccess(t *testing.T) {
3838
}
3939
configName := "config"
4040
privateKey := "private-key"
41-
secret := testutil.GetSampleSecret(configName, privateKey, data, PrivateKey)
41+
secret := util.GetSampleSecret(configName, privateKey, data, PrivateKey)
4242
client := setUp(secret, true)
4343

4444
auth, err := GetConfigurationProvider(ctx, opts, client)
@@ -53,7 +53,7 @@ func TestGetConfigurationProviderFailSecret(t *testing.T) {
5353
AuthType: "user",
5454
AuthSecretName: "oci-config",
5555
}
56-
secret := testutil.GetSampleSecret("test", "error", data, PrivateKey)
56+
secret := util.GetSampleSecret("test", "error", data, PrivateKey)
5757

5858
client := setUp(secret, false)
5959
auth, err := GetConfigurationProvider(ctx, opts, client)
@@ -67,14 +67,14 @@ func TestGetConfigurationProviderFailSecret(t *testing.T) {
6767
Expect(err != nil).Should(BeTrue())
6868
Expect(err.Error()).Should(Equal("auth config data is empty: oci-config"))
6969

70-
secret = testutil.GetSampleSecret("config", "error", data, PrivateKey)
70+
secret = util.GetSampleSecret("config", "error", data, PrivateKey)
7171
client = setUp(secret, true)
7272
auth, err = GetConfigurationProvider(ctx, opts, client)
7373
Expect(auth == nil).Should(BeTrue())
7474
Expect(err != nil).Should(BeTrue())
7575
Expect(err.Error()).Should(Equal("missing auth config data: invalid user auth config data: oci-config"))
7676

77-
secret = testutil.GetSampleSecret("configs", "error", data, PrivateKey)
77+
secret = util.GetSampleSecret("configs", "error", data, PrivateKey)
7878
client = setUp(secret, true)
7979
auth, err = GetConfigurationProvider(ctx, opts, client)
8080
Expect(auth == nil).Should(BeTrue())
@@ -108,7 +108,7 @@ func TestParseAuthConfig(t *testing.T) {
108108
RegisterTestingT(t)
109109
configName := "config"
110110
privateKey := "private-key"
111-
secret := testutil.GetSampleSecret(configName, privateKey, data, PrivateKey)
111+
secret := util.GetSampleSecret(configName, privateKey, data, PrivateKey)
112112
authCfg, err := ParseAuthConfig(secret, "oci-config")
113113
Expect(err == nil).Should(BeTrue())
114114
Expect(authCfg.TenancyID).Should(Equal("ocid1.tenancy.oc1..aaaaaaaa_example"))
@@ -121,12 +121,12 @@ func TestParseAuthConfig(t *testing.T) {
121121

122122
func TestParseAuthConfigWithError(t *testing.T) {
123123
RegisterTestingT(t)
124-
secret := testutil.GetSampleSecret("error", "", data, PrivateKey)
124+
secret := util.GetSampleSecret("error", "", data, PrivateKey)
125125
_, err := ParseAuthConfig(secret, "oci-configs")
126126
Expect(err != nil).Should(BeTrue())
127127
Expect(err.Error()).Should(Equal("invalid auth config data: oci-configs"))
128128

129-
secret = testutil.GetSampleSecret("config", "", data, PrivateKey)
129+
secret = util.GetSampleSecret("config", "", data, PrivateKey)
130130
_, err = ParseAuthConfig(secret, "oci-configs")
131131
Expect(err != nil).Should(BeTrue())
132132
Expect(err.Error()).Should(Equal("invalid user auth config data: oci-configs"))

pkg/controllers/backend/backend_test.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/oracle/oci-native-ingress-controller/pkg/client"
1414
lb "github.com/oracle/oci-native-ingress-controller/pkg/loadbalancer"
1515
ociclient "github.com/oracle/oci-native-ingress-controller/pkg/oci/client"
16-
"github.com/oracle/oci-native-ingress-controller/pkg/testutil"
1716
"github.com/oracle/oci-native-ingress-controller/pkg/util"
1817
corev1 "k8s.io/api/core/v1"
1918
networkingv1 "k8s.io/api/networking/v1"
@@ -36,11 +35,11 @@ func setUp(ctx context.Context, ingressClassList *networkingv1.IngressClassList,
3635
client := fakeclientset.NewSimpleClientset()
3736

3837
action := "list"
39-
testutil.UpdateFakeClientCall(client, action, "ingressclasses", ingressClassList)
40-
testutil.UpdateFakeClientCall(client, action, "ingresses", ingressList)
41-
testutil.UpdateFakeClientCall(client, action, "services", testService)
42-
testutil.UpdateFakeClientCall(client, action, "endpoints", endpoints)
43-
testutil.UpdateFakeClientCall(client, action, "pods", pod)
38+
util.UpdateFakeClientCall(client, action, "ingressclasses", ingressClassList)
39+
util.UpdateFakeClientCall(client, action, "ingresses", ingressList)
40+
util.UpdateFakeClientCall(client, action, "services", testService)
41+
util.UpdateFakeClientCall(client, action, "endpoints", endpoints)
42+
util.UpdateFakeClientCall(client, action, "pods", pod)
4443

4544
informerFactory := informers.NewSharedInformerFactory(client, 0)
4645
ingressClassInformer := informerFactory.Networking().V1().IngressClasses()
@@ -71,7 +70,7 @@ func TestEnsureBackend(t *testing.T) {
7170
RegisterTestingT(t)
7271
ctx, cancel := context.WithCancel(context.Background())
7372
defer cancel()
74-
ingressClassList := testutil.GetIngressClassList()
73+
ingressClassList := util.GetIngressClassList()
7574
c := inits(ctx, ingressClassList, backendPath)
7675

7776
err := c.ensureBackends(&ingressClassList.Items[0], "id")
@@ -82,7 +81,7 @@ func TestRunPusher(t *testing.T) {
8281
RegisterTestingT(t)
8382
ctx, cancel := context.WithCancel(context.Background())
8483
defer cancel()
85-
ingressClassList := testutil.GetIngressClassList()
84+
ingressClassList := util.GetIngressClassList()
8685
c := inits(ctx, ingressClassList, backendPath)
8786

8887
c.runPusher()
@@ -93,7 +92,7 @@ func TestProcessNextItem(t *testing.T) {
9392
RegisterTestingT(t)
9493
ctx, cancel := context.WithCancel(context.Background())
9594
defer cancel()
96-
ingressClassList := testutil.GetIngressClassList()
95+
ingressClassList := util.GetIngressClassList()
9796
c := inits(ctx, ingressClassList, backendPath)
9897

9998
c.queue.Add("default-ingress-class")
@@ -107,7 +106,7 @@ func TestProcessNextItemWithNginx(t *testing.T) {
107106
RegisterTestingT(t)
108107
ctx, cancel := context.WithCancel(context.Background())
109108
defer cancel()
110-
ingressClassList := testutil.GetIngressClassListWithNginx()
109+
ingressClassList := util.GetIngressClassListWithNginx()
111110
c := inits(ctx, ingressClassList, backendPath)
112111

113112
c.queue.Add("nginx-ingress-class")
@@ -120,7 +119,7 @@ func TestNoDefaultBackends(t *testing.T) {
120119
RegisterTestingT(t)
121120
ctx, cancel := context.WithCancel(context.Background())
122121
defer cancel()
123-
ingressClassList := testutil.GetIngressClassList()
122+
ingressClassList := util.GetIngressClassList()
124123
c := inits(ctx, ingressClassList, backendPath)
125124
ingresses, _ := util.GetIngressesForClass(c.ingressLister, &ingressClassList.Items[0])
126125
backends, err := c.getDefaultBackends(ingresses)
@@ -131,7 +130,7 @@ func TestDefaultBackends(t *testing.T) {
131130
RegisterTestingT(t)
132131
ctx, cancel := context.WithCancel(context.Background())
133132
defer cancel()
134-
ingressClassList := testutil.GetIngressClassList()
133+
ingressClassList := util.GetIngressClassList()
135134
c := inits(ctx, ingressClassList, backendPathWithDefaultBackend)
136135
ingresses, _ := util.GetIngressesForClass(c.ingressLister, &ingressClassList.Items[0])
137136
backends, err := c.getDefaultBackends(ingresses)
@@ -143,7 +142,7 @@ func TestEnsurePodReadinessConditionWithExistingReadiness(t *testing.T) {
143142
RegisterTestingT(t)
144143
ctx, cancel := context.WithCancel(context.Background())
145144
defer cancel()
146-
ingressClassList := testutil.GetIngressClassList()
145+
ingressClassList := util.GetIngressClassList()
147146
c := inits(ctx, ingressClassList, backendPathWithDefaultBackend)
148147
ingresses, _ := util.GetIngressesForClass(c.ingressLister, &ingressClassList.Items[0])
149148
ingress := ingresses[0]
@@ -170,17 +169,17 @@ func TestEnsurePodReadinessConditionWithExistingReadiness(t *testing.T) {
170169
Reason: "backend is healthy",
171170
})
172171

173-
err := c.ensurePodReadinessCondition(testutil.GetPodResourceWithReadiness("testecho1", "echoserver", "ingress-readiness", "foo.bar.com", condition), readinessCondition, &backendHealth, "testecho1")
172+
err := c.ensurePodReadinessCondition(util.GetPodResourceWithReadiness("testecho1", "echoserver", "ingress-readiness", "foo.bar.com", condition), readinessCondition, &backendHealth, "testecho1")
174173

175174
Expect(err == nil).Should(Equal(true))
176175
}
177176

178177
func inits(ctx context.Context, ingressClassList *networkingv1.IngressClassList, yamlPath string) *Controller {
179178

180-
ingressList := testutil.ReadResourceAsIngressList(yamlPath)
181-
testService := testutil.GetServiceListResource(namespace, "testecho1", 80)
182-
endpoints := testutil.GetEndpointsResourceList("testecho1", namespace, false)
183-
pod := testutil.GetPodResourceList("testpod", "echoserver")
179+
ingressList := util.ReadResourceAsIngressList(yamlPath)
180+
testService := util.GetServiceListResource(namespace, "testecho1", 80)
181+
endpoints := util.GetEndpointsResourceList("testecho1", namespace, false)
182+
pod := util.GetPodResourceList("testpod", "echoserver")
184183
lbClient := getLoadBalancerClient()
185184

186185
loadBalancerClient := &lb.LoadBalancerClient{
@@ -199,7 +198,7 @@ func TestGetIngressesForClass(t *testing.T) {
199198
RegisterTestingT(t)
200199
ctx, cancel := context.WithCancel(context.Background())
201200
defer cancel()
202-
ingressClassList := testutil.GetIngressClassList()
201+
ingressClassList := util.GetIngressClassList()
203202
c := inits(ctx, ingressClassList, backendPath)
204203
ic, err := util.GetIngressesForClass(c.ingressLister, &ingressClassList.Items[0])
205204
Expect(err == nil).Should(Equal(true))
@@ -257,7 +256,7 @@ func (m MockLoadBalancerClient) UpdateLoadBalancerShape(ctx context.Context, req
257256
}
258257

259258
func (m MockLoadBalancerClient) GetLoadBalancer(ctx context.Context, request ociloadbalancer.GetLoadBalancerRequest) (ociloadbalancer.GetLoadBalancerResponse, error) {
260-
res := testutil.SampleLoadBalancerResponse()
259+
res := util.SampleLoadBalancerResponse()
261260
return res, nil
262261
}
263262

pkg/controllers/ingress/ingress_test.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/oracle/oci-native-ingress-controller/pkg/client"
1313
lb "github.com/oracle/oci-native-ingress-controller/pkg/loadbalancer"
1414
ociclient "github.com/oracle/oci-native-ingress-controller/pkg/oci/client"
15-
"github.com/oracle/oci-native-ingress-controller/pkg/testutil"
15+
"github.com/oracle/oci-native-ingress-controller/pkg/util"
1616
"k8s.io/api/core/v1"
1717
networkingv1 "k8s.io/api/networking/v1"
1818
"k8s.io/client-go/informers"
@@ -33,12 +33,12 @@ func setUp(ctx context.Context, ingressClassList *networkingv1.IngressClassList,
3333
client := fakeclientset.NewSimpleClientset()
3434
action := "list"
3535

36-
testutil.UpdateFakeClientCall(client, action, "ingressclasses", ingressClassList)
37-
testutil.UpdateFakeClientCall(client, action, "ingresses", ingressList)
38-
testutil.UpdateFakeClientCall(client, "get", "ingresses", &ingressList.Items[0])
39-
testutil.UpdateFakeClientCall(client, "update", "ingresses", &ingressList.Items[0])
40-
testutil.UpdateFakeClientCall(client, "patch", "ingresses", &ingressList.Items[0])
41-
testutil.UpdateFakeClientCall(client, action, "services", testService)
36+
util.UpdateFakeClientCall(client, action, "ingressclasses", ingressClassList)
37+
util.UpdateFakeClientCall(client, action, "ingresses", ingressList)
38+
util.UpdateFakeClientCall(client, "get", "ingresses", &ingressList.Items[0])
39+
util.UpdateFakeClientCall(client, "update", "ingresses", &ingressList.Items[0])
40+
util.UpdateFakeClientCall(client, "patch", "ingresses", &ingressList.Items[0])
41+
util.UpdateFakeClientCall(client, action, "services", testService)
4242

4343
informerFactory := informers.NewSharedInformerFactory(client, 0)
4444
ingressClassInformer := informerFactory.Networking().V1().IngressClasses()
@@ -58,7 +58,7 @@ func setUp(ctx context.Context, ingressClassList *networkingv1.IngressClassList,
5858

5959
func inits(ctx context.Context, ingressClassList *networkingv1.IngressClassList, ingressList *networkingv1.IngressList) *Controller {
6060

61-
testService := testutil.GetServiceListResource(namespace, "testecho1", 80)
61+
testService := util.GetServiceListResource(namespace, "testecho1", 80)
6262
lbClient := GetLoadBalancerClient()
6363
certClient := GetCertClient()
6464
certManageClient := GetCertManageClient()
@@ -87,8 +87,8 @@ func TestSync(t *testing.T) {
8787
RegisterTestingT(t)
8888
ctx, cancel := context.WithCancel(context.Background())
8989
defer cancel()
90-
ingressClassList := testutil.GetIngressClassList()
91-
ingressList := testutil.ReadResourceAsIngressList(ingressPath)
90+
ingressClassList := util.GetIngressClassList()
91+
ingressList := util.ReadResourceAsIngressList(ingressPath)
9292
c := inits(ctx, ingressClassList, ingressList)
9393
err := c.sync("default/ingress-readiness")
9494

@@ -100,8 +100,8 @@ func TestEnsureIngressSuccess(t *testing.T) {
100100
RegisterTestingT(t)
101101
ctx, cancel := context.WithCancel(context.Background())
102102
defer cancel()
103-
ingressClassList := testutil.GetIngressClassList()
104-
ingressList := testutil.ReadResourceAsIngressList(ingressPath)
103+
ingressClassList := util.GetIngressClassList()
104+
ingressList := util.ReadResourceAsIngressList(ingressPath)
105105
c := inits(ctx, ingressClassList, ingressList)
106106
err := c.ensureIngress(&ingressList.Items[0], &ingressClassList.Items[0])
107107

@@ -111,8 +111,8 @@ func TestEnsureLoadBalancerIP(t *testing.T) {
111111
RegisterTestingT(t)
112112
ctx, cancel := context.WithCancel(context.Background())
113113
defer cancel()
114-
ingressClassList := testutil.GetIngressClassList()
115-
ingressList := testutil.ReadResourceAsIngressList(ingressPath)
114+
ingressClassList := util.GetIngressClassList()
115+
ingressList := util.ReadResourceAsIngressList(ingressPath)
116116
c := inits(ctx, ingressClassList, ingressList)
117117
err := c.ensureLoadBalancerIP("ip", &ingressList.Items[0])
118118
Expect(err == nil).Should(Equal(true))
@@ -122,8 +122,8 @@ func TestEnsureFinalizer(t *testing.T) {
122122
RegisterTestingT(t)
123123
ctx, cancel := context.WithCancel(context.Background())
124124
defer cancel()
125-
ingressClassList := testutil.GetIngressClassList()
126-
ingressList := testutil.ReadResourceAsIngressList(ingressPathWithFinalizer)
125+
ingressClassList := util.GetIngressClassList()
126+
ingressList := util.ReadResourceAsIngressList(ingressPathWithFinalizer)
127127
c := inits(ctx, ingressClassList, ingressList)
128128
err := c.ensureFinalizer(&ingressList.Items[0])
129129
Expect(err == nil).Should(Equal(true))
@@ -135,8 +135,8 @@ func TestDeleteIngress(t *testing.T) {
135135
RegisterTestingT(t)
136136
ctx, cancel := context.WithCancel(context.Background())
137137
defer cancel()
138-
ingressClassList := testutil.GetIngressClassList()
139-
ingressList := testutil.ReadResourceAsIngressList(ingressPathWithFinalizer)
138+
ingressClassList := util.GetIngressClassList()
139+
ingressList := util.ReadResourceAsIngressList(ingressPathWithFinalizer)
140140
c := inits(ctx, ingressClassList, ingressList)
141141
err := c.deleteIngress(&ingressList.Items[0])
142142
Expect(err == nil).Should(Equal(true))
@@ -148,8 +148,8 @@ func TestIngressAdd(t *testing.T) {
148148
RegisterTestingT(t)
149149
ctx, cancel := context.WithCancel(context.Background())
150150
defer cancel()
151-
ingressClassList := testutil.GetIngressClassList()
152-
ingressList := testutil.ReadResourceAsIngressList(ingressPath)
151+
ingressClassList := util.GetIngressClassList()
152+
ingressList := util.ReadResourceAsIngressList(ingressPath)
153153
c := inits(ctx, ingressClassList, ingressList)
154154
queueSize := c.queue.Len()
155155
c.ingressAdd(&ingressList.Items[0])
@@ -160,8 +160,8 @@ func TestIngressUpdate(t *testing.T) {
160160
RegisterTestingT(t)
161161
ctx, cancel := context.WithCancel(context.Background())
162162
defer cancel()
163-
ingressClassList := testutil.GetIngressClassList()
164-
ingressList := testutil.ReadResourceAsIngressList(ingressPathWithFinalizer)
163+
ingressClassList := util.GetIngressClassList()
164+
ingressList := util.ReadResourceAsIngressList(ingressPathWithFinalizer)
165165
c := inits(ctx, ingressClassList, ingressList)
166166
queueSize := c.queue.Len()
167167
c.ingressUpdate(&ingressList.Items[0], &ingressList.Items[1])
@@ -171,8 +171,8 @@ func TestIngressDelete(t *testing.T) {
171171
RegisterTestingT(t)
172172
ctx, cancel := context.WithCancel(context.Background())
173173
defer cancel()
174-
ingressClassList := testutil.GetIngressClassList()
175-
ingressList := testutil.ReadResourceAsIngressList(ingressPathWithFinalizer)
174+
ingressClassList := util.GetIngressClassList()
175+
ingressList := util.ReadResourceAsIngressList(ingressPathWithFinalizer)
176176
c := inits(ctx, ingressClassList, ingressList)
177177
queueSize := c.queue.Len()
178178
c.ingressDelete(&ingressList.Items[0])
@@ -183,8 +183,8 @@ func TestProcessNextItem(t *testing.T) {
183183
RegisterTestingT(t)
184184
ctx, cancel := context.WithCancel(context.Background())
185185
defer cancel()
186-
ingressClassList := testutil.GetIngressClassListWithLBSet("id")
187-
ingressList := testutil.ReadResourceAsIngressList(ingressPathWithFinalizer)
186+
ingressClassList := util.GetIngressClassListWithLBSet("id")
187+
ingressList := util.ReadResourceAsIngressList(ingressPathWithFinalizer)
188188
c := inits(ctx, ingressClassList, ingressList)
189189

190190
c.queue.Add("default-ingress-class")
@@ -200,7 +200,7 @@ type MockLoadBalancerClient struct {
200200
}
201201

202202
func (m MockLoadBalancerClient) GetLoadBalancer(ctx context.Context, request ociloadbalancer.GetLoadBalancerRequest) (ociloadbalancer.GetLoadBalancerResponse, error) {
203-
res := testutil.SampleLoadBalancerResponse()
203+
res := util.SampleLoadBalancerResponse()
204204
return res, nil
205205
}
206206

0 commit comments

Comments
 (0)