Skip to content

Commit 55ea949

Browse files
authored
Merge pull request #715 from knabben/master
✨ ErrorIfCRDPathMissing on Environment
2 parents 26ad5a7 + 7ae0e20 commit 55ea949

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

pkg/envtest/envtest_test.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ var _ = Describe("Test", func() {
3636
var s *runtime.Scheme
3737
var c client.Client
3838

39+
var validDirectory = filepath.Join(".", "testdata")
40+
var invalidDirectory = "fake"
41+
3942
// Initialize the client
4043
BeforeEach(func(done Done) {
4144
crds = []*v1beta1.CustomResourceDefinition{}
@@ -71,7 +74,7 @@ var _ = Describe("Test", func() {
7174
Describe("InstallCRDs", func() {
7275
It("should install the CRDs into the cluster using directory", func(done Done) {
7376
crds, err = InstallCRDs(env.Config, CRDInstallOptions{
74-
Paths: []string{filepath.Join(".", "testdata")},
77+
Paths: []string{validDirectory},
7578
})
7679
Expect(err).NotTo(HaveOccurred())
7780

@@ -222,14 +225,16 @@ var _ = Describe("Test", func() {
222225
}, 10)
223226

224227
It("should not return an not error if the directory doesn't exist", func(done Done) {
225-
crds, err = InstallCRDs(env.Config, CRDInstallOptions{Paths: []string{"fake"}})
228+
crds, err = InstallCRDs(env.Config, CRDInstallOptions{Paths: []string{invalidDirectory}})
226229
Expect(err).NotTo(HaveOccurred())
227230

228231
close(done)
229232
}, 5)
230233

231234
It("should return an error if the directory doesn't exist", func(done Done) {
232-
crds, err = InstallCRDs(env.Config, CRDInstallOptions{Paths: []string{"fake"}, ErrorIfPathMissing: true})
235+
crds, err = InstallCRDs(env.Config, CRDInstallOptions{
236+
Paths: []string{invalidDirectory}, ErrorIfPathMissing: true,
237+
})
233238
Expect(err).To(HaveOccurred())
234239

235240
close(done)
@@ -299,7 +304,7 @@ var _ = Describe("Test", func() {
299304
It("should uninstall the CRDs from the cluster", func(done Done) {
300305

301306
crds, err = InstallCRDs(env.Config, CRDInstallOptions{
302-
Paths: []string{filepath.Join(".", "testdata")},
307+
Paths: []string{validDirectory},
303308
})
304309
Expect(err).NotTo(HaveOccurred())
305310

@@ -388,7 +393,7 @@ var _ = Describe("Test", func() {
388393
Expect(err).NotTo(HaveOccurred())
389394

390395
err = UninstallCRDs(env.Config, CRDInstallOptions{
391-
Paths: []string{filepath.Join(".", "testdata")},
396+
Paths: []string{validDirectory},
392397
})
393398
Expect(err).NotTo(HaveOccurred())
394399

@@ -416,4 +421,20 @@ var _ = Describe("Test", func() {
416421
close(done)
417422
}, 30)
418423
})
424+
425+
Describe("Start", func() {
426+
It("should raise an error on invalid dir when flag is enabled", func(done Done) {
427+
env = &Environment{ErrorIfCRDPathMissing: true, CRDDirectoryPaths: []string{invalidDirectory}}
428+
_, err := env.Start()
429+
Expect(err).To(HaveOccurred())
430+
close(done)
431+
}, 30)
432+
433+
It("should not raise an error on invalid dir when flag is disabled", func(done Done) {
434+
env = &Environment{ErrorIfCRDPathMissing: false, CRDDirectoryPaths: []string{invalidDirectory}}
435+
_, err := env.Start()
436+
Expect(err).NotTo(HaveOccurred())
437+
close(done)
438+
}, 30)
439+
})
419440
})

pkg/envtest/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ type Environment struct {
100100
// CRDInstallOptions are the options for installing CRDs.
101101
CRDInstallOptions CRDInstallOptions
102102

103+
// ErrorIfCRDPathMissing provides an interface for the underlying
104+
// CRDInstallOptions.ErrorIfPathMissing. It prevents silent failures
105+
// for missing CRD paths.
106+
ErrorIfCRDPathMissing bool
107+
103108
// CRDs is a list of CRDs to install.
104109
// If both this field and CRDs field in CRDInstallOptions are specified, the
105110
// values are merged.
@@ -246,6 +251,7 @@ func (te *Environment) Start() (*rest.Config, error) {
246251
log.V(1).Info("installing CRDs")
247252
te.CRDInstallOptions.CRDs = mergeCRDs(te.CRDInstallOptions.CRDs, te.CRDs)
248253
te.CRDInstallOptions.Paths = mergePaths(te.CRDInstallOptions.Paths, te.CRDDirectoryPaths)
254+
te.CRDInstallOptions.ErrorIfPathMissing = te.ErrorIfCRDPathMissing
249255
crds, err := InstallCRDs(te.Config, te.CRDInstallOptions)
250256
te.CRDs = crds
251257
return te.Config, err

0 commit comments

Comments
 (0)