Skip to content

Commit 501b55e

Browse files
committed
Adding ErrorIfCRDPathMissing on Environment struct and propagating to options
1 parent a0e9c1d commit 501b55e

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{}
@@ -72,7 +75,7 @@ var _ = Describe("Test", func() {
7275
It("should install the CRDs into the cluster", func(done Done) {
7376

7477
crds, err = InstallCRDs(env.Config, CRDInstallOptions{
75-
Paths: []string{filepath.Join(".", "testdata")},
78+
Paths: []string{validDirectory},
7679
})
7780
Expect(err).NotTo(HaveOccurred())
7881

@@ -164,14 +167,16 @@ var _ = Describe("Test", func() {
164167
}, 5)
165168

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

170173
close(done)
171174
}, 5)
172175

173176
It("should return an error if the directory doesn't exist", func(done Done) {
174-
crds, err = InstallCRDs(env.Config, CRDInstallOptions{Paths: []string{"fake"}, ErrorIfPathMissing: true})
177+
crds, err = InstallCRDs(env.Config, CRDInstallOptions{
178+
Paths: []string{invalidDirectory}, ErrorIfPathMissing: true,
179+
})
175180
Expect(err).To(HaveOccurred())
176181

177182
close(done)
@@ -232,7 +237,7 @@ var _ = Describe("Test", func() {
232237
It("should uninstall the CRDs from the cluster", func(done Done) {
233238

234239
crds, err = InstallCRDs(env.Config, CRDInstallOptions{
235-
Paths: []string{filepath.Join(".", "testdata")},
240+
Paths: []string{validDirectory},
236241
})
237242
Expect(err).NotTo(HaveOccurred())
238243

@@ -321,7 +326,7 @@ var _ = Describe("Test", func() {
321326
Expect(err).NotTo(HaveOccurred())
322327

323328
err = UninstallCRDs(env.Config, CRDInstallOptions{
324-
Paths: []string{filepath.Join(".", "testdata")},
329+
Paths: []string{validDirectory},
325330
})
326331
Expect(err).NotTo(HaveOccurred())
327332

@@ -349,4 +354,20 @@ var _ = Describe("Test", func() {
349354
close(done)
350355
}, 30)
351356
})
357+
358+
Describe("Start", func() {
359+
It("should raise an error", func(done Done) {
360+
env = &Environment{ErrorIfCRDPathMissing: true, CRDDirectoryPaths: []string{invalidDirectory}}
361+
_, err := env.Start()
362+
Expect(err).To(HaveOccurred())
363+
close(done)
364+
}, 30)
365+
366+
It("should not raise an error", func(done Done) {
367+
env = &Environment{ErrorIfCRDPathMissing: false, CRDDirectoryPaths: []string{invalidDirectory}}
368+
_, err := env.Start()
369+
Expect(err).NotTo(HaveOccurred())
370+
close(done)
371+
}, 30)
372+
})
352373
})

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)