Skip to content

Commit b696321

Browse files
committed
Add some rudimentary tests for cleanup
1 parent 3d4a463 commit b696321

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package cleanup_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
"github.com/spf13/afero"
10+
11+
"github.com/go-logr/logr"
12+
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/cleanup"
13+
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/env"
14+
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/store"
15+
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/testhelpers"
16+
"sigs.k8s.io/controller-runtime/pkg/envtest/setup/versions"
17+
)
18+
19+
var (
20+
testLog logr.Logger
21+
ctx context.Context
22+
)
23+
24+
func TestCleanup(t *testing.T) {
25+
testLog = testhelpers.GetLogger()
26+
ctx = logr.NewContext(context.Background(), testLog)
27+
28+
RegisterFailHandler(Fail)
29+
RunSpecs(t, "Cleanup Suite")
30+
}
31+
32+
var _ = Describe("Cleanup", func() {
33+
var (
34+
defaultEnvOpts []env.Option
35+
s *store.Store
36+
)
37+
38+
BeforeEach(func() {
39+
s = testhelpers.NewMockStore()
40+
})
41+
42+
JustBeforeEach(func() {
43+
defaultEnvOpts = []env.Option{
44+
env.WithClient(nil), // ensures we fail if we try to connect
45+
env.WithStore(s),
46+
env.WithFS(afero.NewIOFS(s.Root)),
47+
}
48+
})
49+
50+
Context("when cleanup is run", func() {
51+
version := versions.Spec{
52+
Selector: versions.Concrete{
53+
Major: 1,
54+
Minor: 16,
55+
Patch: 1,
56+
},
57+
}
58+
59+
var (
60+
matching, nonMatching []store.Item
61+
)
62+
63+
BeforeEach(func() {
64+
// ensure there are some versions matching what we're about to delete
65+
var err error
66+
matching, err = s.List(ctx, store.Filter{Version: version, Platform: versions.Platform{OS: "linux", Arch: "amd64"}})
67+
Expect(err).NotTo(HaveOccurred())
68+
Expect(matching).NotTo(BeEmpty(), "found no matching versions before cleanup")
69+
70+
// ensure there are some versions _not_ matching what we're about to delete
71+
nonMatching, err = s.List(ctx, store.Filter{Version: versions.Spec{Selector: versions.PatchSelector{Major: 1, Minor: 17, Patch: versions.AnyPoint}}, Platform: versions.Platform{OS: "linux", Arch: "amd64"}})
72+
Expect(err).NotTo(HaveOccurred())
73+
Expect(nonMatching).NotTo(BeEmpty(), "found no non-matching versions before cleanup")
74+
})
75+
76+
JustBeforeEach(func() {
77+
cleanup.Cleanup(
78+
ctx,
79+
version,
80+
cleanup.WithPlatform("linux", "amd64"),
81+
cleanup.WithEnvOptions(defaultEnvOpts...),
82+
)
83+
})
84+
85+
It("should remove matching versions", func() {
86+
items, err := s.List(ctx, store.Filter{Version: version, Platform: versions.Platform{OS: "linux", Arch: "amd64"}})
87+
Expect(err).NotTo(HaveOccurred())
88+
Expect(items).To(BeEmpty(), "found matching versions after cleanup")
89+
})
90+
91+
It("should not remove non-matching versions", func() {
92+
items, err := s.List(ctx, store.Filter{Version: versions.AnyVersion, Platform: versions.Platform{OS: "*", Arch: "*"}})
93+
Expect(err).NotTo(HaveOccurred())
94+
Expect(items).To(ContainElements(nonMatching), "non-matching items were affected")
95+
})
96+
})
97+
})

0 commit comments

Comments
 (0)