-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstate_store_test.go
194 lines (152 loc) · 5.06 KB
/
state_store_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Copyright 2025 The KCP Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sync
import (
"context"
"testing"
dummyv1alpha1 "github.com/kcp-dev/api-syncagent/internal/sync/apis/dummy/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestStateStoreBasics(t *testing.T) {
primaryObject := newUnstructured(&dummyv1alpha1.Thing{
ObjectMeta: metav1.ObjectMeta{
Name: "my-test-thing",
},
Spec: dummyv1alpha1.ThingSpec{
Username: "Miss Scarlet",
},
}, withKind("RemoteThing"))
serviceClusterClient := buildFakeClient()
ctx := context.Background()
stateNamespace := "kcp-system"
primaryObjectSide := syncSide{
object: primaryObject,
}
stateSide := syncSide{
ctx: ctx,
client: serviceClusterClient,
}
storeCreator := newKubernetesStateStoreCreator(stateNamespace)
store := storeCreator(primaryObjectSide, stateSide)
///////////////////////////////////////
// get nil from empty store
result, err := store.Get(syncSide{object: primaryObject})
if err != nil {
t.Fatalf("Failed to get primary object from empty cache: %v", err)
}
if result != nil {
t.Fatalf("Should not have been able to find a state, but got: %+v\n", result)
}
///////////////////////////////////////
// store a first object
firstObject := newUnstructured(&dummyv1alpha1.Thing{
ObjectMeta: metav1.ObjectMeta{
Name: "my-test-thing",
},
Spec: dummyv1alpha1.ThingSpec{
Username: "Miss Scarlet",
},
}, withKind("RemoteThing"))
err = store.Put(firstObject, "", nil)
if err != nil {
t.Fatalf("Failed to store object in empty cache: %v", err)
}
secrets := corev1.SecretList{}
if err := serviceClusterClient.List(ctx, &secrets); err != nil {
t.Fatalf("Failed to list secrets: %v", err)
}
if len(secrets.Items) != 1 {
t.Fatalf("Expected exactly 1 state Secret, got %d.", len(secrets.Items))
}
///////////////////////////////////////
// retrieve the stored object
result, err = store.Get(syncSide{object: firstObject})
if err != nil {
t.Fatalf("Failed to get stored object from cache: %v", err)
}
if result == nil {
t.Fatal("Could not retrieve stored object.")
}
assertObjectsEqual(t, "RemoteThing", firstObject, result)
///////////////////////////////////////
// retrieve another object
secondObject := newUnstructured(&dummyv1alpha1.Thing{
ObjectMeta: metav1.ObjectMeta{
Name: "another-object",
},
}, withKind("RemoteThing"))
result, err = store.Get(syncSide{object: secondObject})
if err != nil {
t.Fatalf("Failed to get second object from cache: %v", err)
}
if result != nil {
t.Fatalf("Should not have been able to find a state for an second object, but got: %+v\n", result)
}
///////////////////////////////////////
// store a 2nd object
err = store.Put(secondObject, "", nil)
if err != nil {
t.Fatalf("Failed to store second object in cache: %v", err)
}
result, err = store.Get(syncSide{object: secondObject})
if err != nil {
t.Fatalf("Failed to get second object from cache: %v", err)
}
assertObjectsEqual(t, "RemoteThing", secondObject, result)
///////////////////////////////////////
// retrieve the first, ensure it's not overwritten
result, err = store.Get(syncSide{object: firstObject})
if err != nil {
t.Fatalf("Failed to get first object from cache again: %v", err)
}
assertObjectsEqual(t, "RemoteThing", firstObject, result)
///////////////////////////////////////
// strip subresources
thirdObject := newUnstructured(&dummyv1alpha1.ThingWithStatusSubresource{
ObjectMeta: metav1.ObjectMeta{
Name: "subresourced",
},
Spec: dummyv1alpha1.ThingSpec{
Username: "Jerry",
},
Status: dummyv1alpha1.ThingStatus{
CurrentVersion: "latest",
},
}, withKind("RemoteThing"))
err = store.Put(thirdObject, "", nil)
if err != nil {
t.Fatalf("Failed to store third object in cache: %v", err)
}
///////////////////////////////////////
// ensure status is kept
result, err = store.Get(syncSide{object: thirdObject})
if err != nil {
t.Fatalf("Failed to get third object from cache again: %v", err)
}
assertObjectsEqual(t, "RemoteThing", thirdObject, result)
///////////////////////////////////////
// overwrite, but this time strip subresource
err = store.Put(thirdObject, "", []string{"status"})
if err != nil {
t.Fatalf("Failed to store third object in cache: %v", err)
}
///////////////////////////////////////
// ensure status is gone
result, err = store.Get(syncSide{object: thirdObject})
if err != nil {
t.Fatalf("Failed to get third object from cache again: %v", err)
}
delete(thirdObject.Object, "status")
assertObjectsEqual(t, "RemoteThing", thirdObject, result)
}