forked from flanksource/platform-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup_test.go
131 lines (110 loc) · 3.76 KB
/
cleanup_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package test
import (
"context"
"fmt"
"time"
"k8s.io/apimachinery/pkg/api/errors"
"github.com/flanksource/commons/utils"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
var _ = Describe("Cleanup Controller", func() {
const timeout = time.Second * 30
const interval = time.Second * 1
const cleanupLabel = "auto-delete"
var namespace1, namespace2 *v1.Namespace
BeforeEach(func() {
namespace1 = &v1.Namespace{
TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Namespace"},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("namespace-to-delete-%s", utils.RandomString(3)),
Labels: map[string]string{
"auto-delete": "5s",
},
},
}
namespace2 = &v1.Namespace{
TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Namespace"},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("namespace-to-not-delete-%s", utils.RandomString(3)),
},
}
err := k8sClient.Create(context.Background(), namespace1)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Create(context.Background(), namespace2)
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
// Add any teardown steps that needs to be executed after each test
k8sClient.Delete(context.Background(), namespace1)
k8sClient.Delete(context.Background(), namespace2)
})
Context("Namespace with label", func() {
It("Should remove namespace created with auto-delete tag", func() {
key := types.NamespacedName{Name: namespace1.Name}
// First check that the namespace exists
ns := &v1.Namespace{}
Eventually(func() error {
return k8sClient.Get(context.Background(), key, ns)
}, 5*time.Second, 1*time.Second).Should(Not(HaveOccurred()))
time.Sleep(time.Second * 10)
err := k8sClient.Get(context.Background(), key, ns)
Expect(err).ToNot(HaveOccurred())
fetched := &v1.Namespace{}
Eventually(func() bool {
err := k8sClient.Get(context.Background(), key, fetched)
if errors.IsNotFound(err) {
return true
} else if err != nil {
fmt.Printf("Error fetching namespace: %v", err)
return false
}
return fetched.Status.Phase == v1.NamespaceTerminating
}, timeout, interval).Should(BeTrue())
})
})
Context("Namespace without label", func() {
It("Should not remove namespace without auto-delete tag", func() {
key := types.NamespacedName{Name: namespace2.Name}
// First check that the namespace exists
ns := &v1.Namespace{}
Eventually(func() error {
return k8sClient.Get(context.Background(), key, ns)
}, 5*time.Second, 1*time.Second).Should(Not(HaveOccurred()))
time.Sleep(time.Second * 5)
// Check it still exists after 5s
ns = &v1.Namespace{}
err := k8sClient.Get(context.Background(), key, ns)
Expect(err).ToNot(HaveOccurred())
Expect(ns.Status.Phase).To(Equal(v1.NamespaceActive))
})
It("Should remove namespace updated with auto-delete tag", func() {
key := types.NamespacedName{Name: namespace2.Name}
// First check that the namespace exists
ns := &v1.Namespace{}
Eventually(func() error {
return k8sClient.Get(context.Background(), key, ns)
}, 5*time.Second, 1*time.Second).Should(Not(HaveOccurred()))
if ns.Labels == nil {
ns.Labels = map[string]string{}
}
ns.Labels["auto-delete"] = "5s"
err := k8sClient.Update(context.Background(), ns)
Expect(err).ToNot(HaveOccurred())
fetched := &v1.Namespace{}
Eventually(func() bool {
err := k8sClient.Get(context.Background(), key, fetched)
if errors.IsNotFound(err) {
return true
} else if err != nil {
fmt.Printf("Error fetching namespace: %v", err)
return false
}
return fetched.Status.Phase == v1.NamespaceTerminating
}, timeout, interval).Should(BeTrue())
})
})
})