...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package provider
17
18 import (
19 "context"
20 "strconv"
21 "time"
22
23 . "github.com/onsi/ginkgo/v2"
24 . "github.com/onsi/gomega"
25 corev1 "k8s.io/api/core/v1"
26 v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27 "k8s.io/apimachinery/pkg/types"
28 )
29
30 var _ = Describe("UpdatedClient", func() {
31
32 BeforeEach(func() {
33
34 })
35
36 AfterEach(func() {
37
38 })
39
40 Context(("UpdatedClient"), func() {
41 It(("Should create and delete successfully"), func() {
42 obj := &corev1.ConfigMap{
43 ObjectMeta: v1.ObjectMeta{
44 Namespace: "default",
45 Name: "test-configmap-create-delete",
46 },
47 Data: map[string]string{
48 "test": "1",
49 },
50 }
51 err := k8sClient.Create(context.TODO(), obj)
52 Expect(err).ToNot(HaveOccurred())
53
54 err = k8sClient.Delete(context.TODO(), obj)
55 Expect(err).ToNot(HaveOccurred())
56 })
57
58 It("Data should always be updated", func() {
59 obj := &corev1.ConfigMap{
60 ObjectMeta: v1.ObjectMeta{
61 Namespace: "default",
62 Name: "test-configmap-update",
63 },
64 Data: map[string]string{
65 "test": "0",
66 },
67 }
68 err := k8sClient.Create(context.TODO(), obj)
69 Expect(err).ToNot(HaveOccurred())
70
71 for i := 0; i <= 200; i++ {
72 data := strconv.Itoa(i)
73
74 obj.Data["test"] = data
75 err = k8sClient.Update(context.TODO(), obj)
76 Expect(err).ToNot(HaveOccurred())
77
78 err = k8sClient.Get(context.TODO(), types.NamespacedName{
79 Namespace: "default",
80 Name: "test-configmap-update",
81 }, obj)
82 Expect(err).ToNot(HaveOccurred())
83
84 Expect(obj.Data["test"]).To(Equal(data))
85 }
86
87 err = k8sClient.Delete(context.TODO(), obj)
88 Expect(err).ToNot(HaveOccurred())
89 })
90
91 It("Newer data should be returned", func() {
92 obj := &corev1.ConfigMap{
93 ObjectMeta: v1.ObjectMeta{
94 Namespace: "default",
95 Name: "test-configmap-another-update",
96 },
97 Data: map[string]string{
98 "test": "0",
99 },
100 }
101 err := k8sClient.Create(context.TODO(), obj)
102 Expect(err).ToNot(HaveOccurred())
103
104 obj.Data["test"] = "1"
105 err = k8sClient.Update(context.TODO(), obj)
106 Expect(err).ToNot(HaveOccurred())
107
108 newObj := &corev1.ConfigMap{}
109 err = k8sClient.Get(context.TODO(), types.NamespacedName{
110 Namespace: "default",
111 Name: "test-configmap-another-update",
112 }, newObj)
113 Expect(err).ToNot(HaveOccurred())
114
115 Expect(newObj.Data["test"]).To(Equal("1"))
116 newObj.Data["test"] = "2"
117 anotherCleanClient := mgr.GetClient()
118 err = anotherCleanClient.Update(context.TODO(), newObj)
119 Expect(err).ToNot(HaveOccurred())
120
121 time.Sleep(1 * time.Second)
122 newObj = &corev1.ConfigMap{}
123 err = k8sClient.Get(context.TODO(), types.NamespacedName{
124 Namespace: "default",
125 Name: "test-configmap-another-update",
126 }, newObj)
127 Expect(err).ToNot(HaveOccurred())
128 Expect(newObj.Data["test"]).To(Equal("2"))
129 })
130 })
131 })
132