Skip to content

Commit 7ec1e60

Browse files
committed
Refactor code duplication
1 parent 5dc1988 commit 7ec1e60

File tree

1 file changed

+39
-135
lines changed

1 file changed

+39
-135
lines changed

e2e/single-cluster/go_getter_custom_ca_test.go

Lines changed: 39 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,34 @@ var _ = Describe("Testing go-getter CA bundles and insecureSkipVerify for clonin
110110
Expect(err).ToNot(HaveOccurred())
111111
}
112112

113+
type gitRepoOptions struct {
114+
CABundle string
115+
InsecureSkipTLSVerify bool
116+
HelmSecretName string
117+
HelmSecretNameForPaths string
118+
}
119+
120+
createGitRepo := func(options gitRepoOptions) {
121+
GinkgoHelper()
122+
values := gitRepoTestValues{
123+
// Defaults
124+
Name: gitrepoName,
125+
Repo: gh.GetInClusterURL(host, HTTPSPort, "repo"),
126+
Branch: gh.Branch,
127+
PollingInterval: "15s", // default
128+
TargetNamespace: targetNamespace, // to avoid conflicts with other tests
129+
Path: entrypoint,
130+
// Customizations
131+
CABundle: options.CABundle,
132+
InsecureSkipTLSVerify: options.InsecureSkipTLSVerify,
133+
HelmSecretName: options.HelmSecretName,
134+
HelmSecretNameForPaths: options.HelmSecretNameForPaths,
135+
}
136+
137+
err := testenv.ApplyTemplate(k, testenv.AssetPath("gitrepo/gitrepo.yaml"), values)
138+
Expect(err).ToNot(HaveOccurred())
139+
}
140+
113141
BeforeEach(func() {
114142
k = env.Kubectl.Namespace(env.Namespace)
115143
})
@@ -154,53 +182,20 @@ var _ = Describe("Testing go-getter CA bundles and insecureSkipVerify for clonin
154182
// Create and apply GitRepo, don't configure CABundle, InsecureSkipTLSVerify,
155183
// helmSecretName, or helmSecretNameForPath in GitRepo.spec which makes it fall back to
156184
// Rancher certificates that work.
157-
err := testenv.ApplyTemplate(k, testenv.AssetPath("gitrepo/gitrepo.yaml"), gitRepoTestValues{
158-
Name: gitrepoName,
159-
Repo: gh.GetInClusterURL(host, HTTPSPort, "repo"),
160-
Branch: gh.Branch,
161-
PollingInterval: "15s", // default
162-
TargetNamespace: targetNamespace, // to avoid conflicts with other tests
163-
Path: entrypoint,
164-
})
165-
Expect(err).ToNot(HaveOccurred())
166-
185+
createGitRepo(gitRepoOptions{})
167186
Eventually(expectGitRepoToBeReady).Should(Succeed())
168187
})
169188

170189
It("should succeed when using the correct CA bundle provided in GitRepo's CABundle field", func() {
171190
encodedCert := mustReadFileAsBase64(getCertificateFilePath())
172-
173-
// Create and apply GitRepo
174-
err := testenv.ApplyTemplate(k, testenv.AssetPath("gitrepo/gitrepo.yaml"), gitRepoTestValues{
175-
Name: gitrepoName,
176-
Repo: gh.GetInClusterURL(host, HTTPSPort, "repo"),
177-
Branch: gh.Branch,
178-
PollingInterval: "15s", // default
179-
TargetNamespace: targetNamespace, // to avoid conflicts with other tests
180-
Path: entrypoint,
181-
CABundle: encodedCert,
182-
})
183-
Expect(err).ToNot(HaveOccurred())
184-
191+
createGitRepo(gitRepoOptions{CABundle: encodedCert})
185192
Eventually(expectGitRepoToBeReady).Should(Succeed())
186193
})
187194

188195
It("should fail when using the incorrect CA bundle provided in GitRepo's CABundle field", func() {
189196
// But it should fail in gitcloner already, not later in fleet apply.
190197
encodedCert := base64.StdEncoding.EncodeToString([]byte("invalid-ca-bundle"))
191-
192-
// Create and apply GitRepo
193-
err := testenv.ApplyTemplate(k, testenv.AssetPath("gitrepo/gitrepo.yaml"), gitRepoTestValues{
194-
Name: gitrepoName,
195-
Repo: gh.GetInClusterURL(host, HTTPSPort, "repo"),
196-
Branch: gh.Branch,
197-
PollingInterval: "15s", // default
198-
TargetNamespace: targetNamespace, // to avoid conflicts with other tests
199-
Path: entrypoint,
200-
CABundle: encodedCert,
201-
})
202-
Expect(err).ToNot(HaveOccurred())
203-
198+
createGitRepo(gitRepoOptions{CABundle: encodedCert})
204199
Eventually(expectGitRepoToNotBeReady).Should(Succeed())
205200
Consistently(expectGitRepoToNotBeReady).Should(Succeed())
206201
})
@@ -213,18 +208,7 @@ var _ = Describe("Testing go-getter CA bundles and insecureSkipVerify for clonin
213208
out, err := k.Create("secret", "generic", secretName, "--from-file=cacerts="+helmCrtFile, "-n", env.Namespace)
214209
Expect(err).ToNot(HaveOccurred(), out)
215210

216-
// Create and apply GitRepo
217-
err = testenv.ApplyTemplate(k, testenv.AssetPath("gitrepo/gitrepo.yaml"), gitRepoTestValues{
218-
Name: gitrepoName,
219-
Repo: gh.GetInClusterURL(host, HTTPSPort, "repo"),
220-
Branch: gh.Branch,
221-
PollingInterval: "15s", // default
222-
TargetNamespace: targetNamespace, // to avoid conflicts with other tests
223-
Path: entrypoint,
224-
HelmSecretName: secretName,
225-
})
226-
Expect(err).ToNot(HaveOccurred())
227-
211+
createGitRepo(gitRepoOptions{HelmSecretName: secretName})
228212
Eventually(expectGitRepoToBeReady).Should(Succeed())
229213
})
230214

@@ -239,18 +223,7 @@ var _ = Describe("Testing go-getter CA bundles and insecureSkipVerify for clonin
239223
out, err := k.Create("secret", "generic", secretName, "--from-file=cacerts="+invalidCACertsFile.Name(), "-n", env.Namespace)
240224
Expect(err).ToNot(HaveOccurred(), out)
241225

242-
// Create and apply GitRepo
243-
err = testenv.ApplyTemplate(k, testenv.AssetPath("gitrepo/gitrepo.yaml"), gitRepoTestValues{
244-
Name: gitrepoName,
245-
Repo: gh.GetInClusterURL(host, HTTPSPort, "repo"),
246-
Branch: gh.Branch,
247-
PollingInterval: "15s", // default
248-
TargetNamespace: targetNamespace, // to avoid conflicts with other tests
249-
Path: entrypoint,
250-
HelmSecretName: secretName,
251-
})
252-
Expect(err).ToNot(HaveOccurred())
253-
226+
createGitRepo(gitRepoOptions{HelmSecretName: secretName})
254227
Eventually(expectGitRepoToNotBeReady).Should(Succeed())
255228
Consistently(expectGitRepoToNotBeReady).Should(Succeed())
256229
})
@@ -266,18 +239,7 @@ var _ = Describe("Testing go-getter CA bundles and insecureSkipVerify for clonin
266239
},
267240
)
268241

269-
// Create and apply GitRepo
270-
err := testenv.ApplyTemplate(k, testenv.AssetPath("gitrepo/gitrepo.yaml"), gitRepoTestValues{
271-
Name: gitrepoName,
272-
Repo: gh.GetInClusterURL(host, HTTPSPort, "repo"),
273-
Branch: gh.Branch,
274-
PollingInterval: "15s", // default
275-
TargetNamespace: targetNamespace, // to avoid conflicts with other tests
276-
Path: entrypoint,
277-
HelmSecretNameForPaths: secretName,
278-
})
279-
Expect(err).ToNot(HaveOccurred())
280-
242+
createGitRepo(gitRepoOptions{HelmSecretNameForPaths: secretName})
281243
Eventually(expectGitRepoToBeReady).Should(Succeed())
282244
})
283245

@@ -292,18 +254,7 @@ var _ = Describe("Testing go-getter CA bundles and insecureSkipVerify for clonin
292254
},
293255
)
294256

295-
// Create and apply GitRepo
296-
err := testenv.ApplyTemplate(k, testenv.AssetPath("gitrepo/gitrepo.yaml"), gitRepoTestValues{
297-
Name: gitrepoName,
298-
Repo: gh.GetInClusterURL(host, HTTPSPort, "repo"),
299-
Branch: gh.Branch,
300-
PollingInterval: "15s", // default
301-
TargetNamespace: targetNamespace, // to avoid conflicts with other tests
302-
Path: entrypoint,
303-
HelmSecretNameForPaths: secretName,
304-
})
305-
Expect(err).ToNot(HaveOccurred())
306-
257+
createGitRepo(gitRepoOptions{HelmSecretNameForPaths: secretName})
307258
Consistently(expectGitRepoToNotBeReady).Should(Succeed())
308259
})
309260

@@ -318,18 +269,7 @@ var _ = Describe("Testing go-getter CA bundles and insecureSkipVerify for clonin
318269
},
319270
)
320271

321-
// Create and apply GitRepo
322-
err := testenv.ApplyTemplate(k, testenv.AssetPath("gitrepo/gitrepo.yaml"), gitRepoTestValues{
323-
Name: gitrepoName,
324-
Repo: gh.GetInClusterURL(host, HTTPSPort, "repo"),
325-
Branch: gh.Branch,
326-
PollingInterval: "15s", // default
327-
TargetNamespace: targetNamespace, // to avoid conflicts with other tests
328-
Path: entrypoint,
329-
HelmSecretNameForPaths: secretName,
330-
})
331-
Expect(err).ToNot(HaveOccurred())
332-
272+
createGitRepo(gitRepoOptions{HelmSecretNameForPaths: secretName})
333273
Eventually(expectGitRepoToBeReady).Should(Succeed())
334274
})
335275
})
@@ -338,47 +278,22 @@ var _ = Describe("Testing go-getter CA bundles and insecureSkipVerify for clonin
338278
It("should succeed when using the incorrect CA bundle provided in GitRepo's CABundle field but setting InsecureSkipTLSVerify to true", func() {
339279
// But it should fail in gitcloner already, not later in fleet apply.
340280
encodedCert := base64.StdEncoding.EncodeToString([]byte("invalid-ca-bundle"))
341-
342-
// Create and apply GitRepo
343-
err := testenv.ApplyTemplate(k, testenv.AssetPath("gitrepo/gitrepo.yaml"), gitRepoTestValues{
344-
Name: gitrepoName,
345-
Repo: gh.GetInClusterURL(host, HTTPSPort, "repo"),
346-
Branch: gh.Branch,
347-
PollingInterval: "15s", // default
348-
TargetNamespace: targetNamespace, // to avoid conflicts with other tests
349-
Path: entrypoint,
350-
CABundle: encodedCert,
351-
InsecureSkipTLSVerify: true,
352-
})
353-
Expect(err).ToNot(HaveOccurred())
354-
281+
createGitRepo(gitRepoOptions{CABundle: encodedCert, InsecureSkipTLSVerify: true})
355282
Eventually(expectGitRepoToBeReady).Should(Succeed())
356283
})
357284

358285
It("should succeed when using the incorrect CA bundle provided in helmSecretName but setting InsecureSkipVerify to true", func() {
359286
invalidCertFile := createInvalidCACertFile()
360287
defer os.Remove(invalidCertFile.Name())
361288

362-
// Create secret with CA bundle
363289
secretName := testenv.RandomFilename("helm-ca-bundle", r)
364290
out, err := k.Create("secret", "generic", secretName,
365291
"--from-file=cacerts="+invalidCertFile.Name(),
366292
"--from-literal=insecureSkipVerify=true",
367293
"-n", env.Namespace)
368294
Expect(err).ToNot(HaveOccurred(), out)
369295

370-
err = testenv.ApplyTemplate(k, testenv.AssetPath("gitrepo/gitrepo.yaml"), gitRepoTestValues{
371-
Name: gitrepoName,
372-
Repo: gh.GetInClusterURL(host, HTTPSPort, "repo"),
373-
Branch: gh.Branch,
374-
PollingInterval: "15s", // default
375-
TargetNamespace: targetNamespace, // to avoid conflicts with other tests
376-
Path: entrypoint,
377-
InsecureSkipTLSVerify: false,
378-
HelmSecretName: secretName,
379-
})
380-
Expect(err).ToNot(HaveOccurred())
381-
296+
createGitRepo(gitRepoOptions{HelmSecretName: secretName})
382297
Eventually(expectGitRepoToBeReady).Should(Succeed())
383298
})
384299

@@ -394,18 +309,7 @@ var _ = Describe("Testing go-getter CA bundles and insecureSkipVerify for clonin
394309
},
395310
)
396311

397-
err := testenv.ApplyTemplate(k, testenv.AssetPath("gitrepo/gitrepo.yaml"), gitRepoTestValues{
398-
Name: gitrepoName,
399-
Repo: gh.GetInClusterURL(host, HTTPSPort, "repo"),
400-
Branch: gh.Branch,
401-
PollingInterval: "15s", // default
402-
TargetNamespace: targetNamespace, // to avoid conflicts with other tests
403-
Path: entrypoint,
404-
InsecureSkipTLSVerify: false,
405-
HelmSecretNameForPaths: secretName,
406-
})
407-
Expect(err).ToNot(HaveOccurred())
408-
312+
createGitRepo(gitRepoOptions{HelmSecretNameForPaths: secretName})
409313
Eventually(expectGitRepoToBeReady).Should(Succeed())
410314
})
411315
})

0 commit comments

Comments
 (0)