Skip to content

Commit ff829b4

Browse files
committed
continue rewriting tests
1 parent 8d43103 commit ff829b4

File tree

1 file changed

+76
-20
lines changed

1 file changed

+76
-20
lines changed

pkg/streamline/store/db_store_test.go

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,74 @@ func createComponent(uid string, option ...CreateComponentOption) unstructured.U
5151
return u
5252
}
5353

54+
func createPod(uid, name, namespace string, status client.ComponentState, timestamp int64) unstructured.Unstructured {
55+
u := createComponent(uid, WithGVK("", "v1", "Pod"), WithName(name), WithNamespace(namespace))
56+
u.SetCreationTimestamp(v1.Time{Time: time.Unix(timestamp, 0)})
57+
u.Object["spec"] = map[string]any{"nodeName": testNode}
58+
59+
switch status {
60+
case client.ComponentStateFailed:
61+
u.Object["status"] = map[string]any{
62+
"phase": "Failed",
63+
"message": "Pod failed",
64+
"containerStatuses": []any{
65+
map[string]any{
66+
"name": "container",
67+
"ready": false,
68+
"state": map[string]any{
69+
"terminated": map[string]any{
70+
"exitCode": 1,
71+
"reason": "Error",
72+
"message": "Container failed",
73+
},
74+
},
75+
},
76+
},
77+
}
78+
case client.ComponentStateRunning:
79+
u.Object["status"] = map[string]any{
80+
"phase": "Running",
81+
"message": "",
82+
"containerStatuses": []any{
83+
map[string]any{
84+
"name": "container",
85+
"ready": true,
86+
"state": map[string]any{
87+
"running": map[string]any{
88+
"startedAt": v1.Time{Time: time.Unix(timestamp, 0)},
89+
},
90+
},
91+
},
92+
},
93+
"conditions": []any{
94+
map[string]any{
95+
"type": "Ready",
96+
"status": "True",
97+
},
98+
},
99+
}
100+
case client.ComponentStatePending:
101+
u.Object["status"] = map[string]any{
102+
"phase": "Pending",
103+
"message": "Pod is pending",
104+
"containerStatuses": []any{
105+
map[string]any{
106+
"name": "container",
107+
"ready": false,
108+
"state": map[string]any{
109+
"waiting": map[string]any{
110+
"reason": "ContainerCreating",
111+
"message": "Container is being created",
112+
},
113+
},
114+
},
115+
},
116+
}
117+
}
118+
119+
return u
120+
}
121+
54122
type CreateComponentOption func(u *unstructured.Unstructured)
55123

56124
func WithParent(uid string) CreateComponentOption {
@@ -752,19 +820,6 @@ func TestComponentCache_UniqueConstraint(t *testing.T) {
752820
})
753821
}
754822

755-
func createPod(s store.Store, name, uid string, timestamp int64) error {
756-
component := createComponent(uid, WithGVK("", testVersion, "Pod"), WithName(name), WithNamespace(testNamespace))
757-
758-
if component.Object["metadata"] == nil {
759-
component.Object["metadata"] = map[string]interface{}{}
760-
}
761-
762-
meta := component.Object["metadata"].(map[string]interface{})
763-
meta["nodeName"] = testNode
764-
765-
return s.SaveComponent(component)
766-
}
767-
768823
func TestPendingPodsCache(t *testing.T) {
769824
t.Run("cache should store pods with all required attributes", func(t *testing.T) {
770825
storeInstance, err := store.NewDatabaseStore(store.WithStorage(api.StorageFile))
@@ -773,8 +828,8 @@ func TestPendingPodsCache(t *testing.T) {
773828
require.NoError(t, storeInstance.Shutdown(), "failed to shutdown store")
774829
}(storeInstance)
775830

776-
require.NoError(t, createPod(storeInstance, "pending-pod-1", "pod-1-uid", hourAgoTimestamp))
777-
require.NoError(t, createPod(storeInstance, "pending-pod-2", "pod-2-uid", hourAgoTimestamp))
831+
require.NoError(t, storeInstance.SaveComponent(createPod("pod-1-uid", "pending-pod-1", "default", client.ComponentStatePending, hourAgoTimestamp)))
832+
require.NoError(t, storeInstance.SaveComponent(createPod("pod-2-uid", "pending-pod-2", "default", client.ComponentStatePending, hourAgoTimestamp)))
778833

779834
stats, err := storeInstance.GetNodeStatistics()
780835
require.NoError(t, err)
@@ -790,9 +845,9 @@ func TestPendingPodsCache(t *testing.T) {
790845
require.NoError(t, storeInstance.Shutdown(), "failed to shutdown store")
791846
}(storeInstance)
792847

793-
require.NoError(t, createPod(storeInstance, "fresh-pending-pod", "pod-uid", nowTimestamp))
794-
require.NoError(t, createPod(storeInstance, "pending-pod-1", "pod-1-uid", hourAgoTimestamp))
795-
require.NoError(t, createPod(storeInstance, "pending-pod-2", "pod-2-uid", hourAgoTimestamp))
848+
require.NoError(t, storeInstance.SaveComponent(createPod("pod-uid", "fresh-pending-pod", "default", client.ComponentStatePending, nowTimestamp)))
849+
require.NoError(t, storeInstance.SaveComponent(createPod("pod-1-uid", "pending-pod-1", "default", client.ComponentStatePending, hourAgoTimestamp)))
850+
require.NoError(t, storeInstance.SaveComponent(createPod("pod-2-uid", "pending-pod-2", "default", client.ComponentStatePending, hourAgoTimestamp)))
796851

797852
stats, err := storeInstance.GetNodeStatistics()
798853
require.NoError(t, err)
@@ -808,14 +863,15 @@ func TestPendingPodsCache(t *testing.T) {
808863
require.NoError(t, storeInstance.Shutdown(), "failed to shutdown store")
809864
}(storeInstance)
810865

811-
require.NoError(t, createPod(storeInstance, "pending-pod-1", "pod-1-uid", hourAgoTimestamp))
866+
pod := createPod("pod-1-uid", "pending-pod-1", "default", client.ComponentStatePending, hourAgoTimestamp)
867+
require.NoError(t, storeInstance.SaveComponent(pod))
812868

813869
stats, err := storeInstance.GetNodeStatistics()
814870
require.NoError(t, err)
815871
require.Len(t, stats, 1)
816872
assert.Equal(t, int64(1), *stats[0].PendingPods)
817873

818-
err = storeInstance.DeleteComponent(createStoreKey(WithStoreKeyName("pending-pod-1"), WithStoreKeyKind("Pod")))
874+
err = storeInstance.DeleteComponent(common.NewStoreKeyFromUnstructured(pod))
819875
require.NoError(t, err)
820876

821877
stats, err = storeInstance.GetNodeStatistics()

0 commit comments

Comments
 (0)