-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefresh_test.go
153 lines (139 loc) · 4.42 KB
/
refresh_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
package refresh_test
import (
"context"
"testing"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
util "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
refresh "github.com/wantedly/resource-refresher"
ut "github.com/wantedly/resource-refresher/testing"
)
type testcase struct {
name string
explanation string
initialState []runtime.Object
objectList refresh.ObjectList
}
func TestRefresh(t *testing.T) {
scheme := runtime.NewScheme()
if err := clientgoscheme.AddToScheme(scheme); err != nil {
t.Fatal(err)
}
parent := ut.GenService("some-svc")
setOwner := func(child client.Object) client.Object {
if err := util.SetControllerReference(parent, child, scheme); err != nil {
t.Fatal(err)
}
return child
}
const labelKey = "some-label-key"
labelGetter := func(obj client.Object) (string, error) {
key, ok := obj.GetLabels()[labelKey]
if !ok {
return "", errors.Errorf("service doesn't have label %q", labelKey)
}
return key, nil
}
gvk, err := apiutil.GVKForObject(&appsv1.DeploymentList{}, scheme)
if err != nil {
t.Fatal(err)
}
testcases := []testcase{
{
name: "empty state",
explanation: "When both desired and cluster stat are empty it does nothing",
initialState: nil,
objectList: refresh.ObjectList{
Items: nil,
GroupVersionKind: gvk,
Identity: labelGetter,
},
},
{
name: "one existing resource",
explanation: "when objects with the same kind detected, it doesn't affect the existings",
initialState: []runtime.Object{
ut.GenDeployment("deploy-1", map[string]string{labelKey: "1"}),
},
objectList: refresh.ObjectList{
Items: []client.Object{
ut.GenDeployment("", map[string]string{labelKey: "2"}),
},
GroupVersionKind: gvk,
Identity: labelGetter,
},
},
{
name: "one existing resource owned by parent",
explanation: "when the resource is owned by the parent and not listed in the ObjectList, it deletes the object",
initialState: []runtime.Object{
setOwner(ut.GenDeployment("deploy-1", map[string]string{labelKey: "1"})),
},
objectList: refresh.ObjectList{
Items: []client.Object{
ut.GenDeployment("", map[string]string{labelKey: "2"}),
},
GroupVersionKind: gvk,
Identity: labelGetter,
},
},
{
name: "one existing resource owned by parent to be updated",
explanation: "when old object found with the same identity fonud, it updates respecting current object name",
initialState: []runtime.Object{
setOwner(ut.GenDeployment("deploy-1", map[string]string{
labelKey: "1",
"this-key-should-be-updated": "this-is-before-update",
})),
},
objectList: refresh.ObjectList{
Items: []client.Object{
ut.GenDeployment("", map[string]string{labelKey: "2"}),
ut.GenDeployment("", map[string]string{
labelKey: "1", // because of this, deploy-1 will be updated
"this-key-should-be-updated": "this-is-after-update",
}),
},
GroupVersionKind: gvk,
Identity: labelGetter,
},
},
{
name: "if object has long name",
explanation: "Names longer than 63 characters will need to be trimmed.",
initialState: nil,
objectList: refresh.ObjectList{
Items: []client.Object{
ut.GenDeployment("random-70-character-name-cmfuzg9tltcwlwnoyxjhy3rlci1uyw1lcci1uyw1lc==", map[string]string{labelKey: "1"}),
ut.GenDeployment("", map[string]string{labelKey: "random-70-character-objkey-cmfuzg9tltcwlwnoyxjhy3rlci1uyw1lcci1uyw1lc"}),
},
GroupVersionKind: gvk,
Identity: labelGetter,
},
},
}
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
initObjs := append(tc.initialState, parent)
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(initObjs...).Build()
ref := refresh.New(fakeClient, scheme)
ctx := context.Background()
if err := ref.Refresh(ctx, parent, tc.objectList); err != nil {
t.Fatalf("%+v", err)
}
{
dl := &appsv1.DeploymentList{}
if err := fakeClient.List(ctx, dl); err != nil {
t.Fatalf("%+v", err)
}
ut.SnapshotYaml(t, dl)
}
})
}
}