15
15
package release
16
16
17
17
import (
18
- "encoding/json"
19
18
"testing"
20
19
21
- "github.com/stretchr/testify/assert"
22
20
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
21
+ apitypes "k8s.io/apimachinery/pkg/types"
22
+ "k8s.io/cli-runtime/pkg/resource"
23
+
24
+ appsv1 "k8s.io/api/apps/v1"
25
+ v1 "k8s.io/api/core/v1"
26
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
+
28
+ "github.com/stretchr/testify/assert"
29
+ "k8s.io/apimachinery/pkg/runtime"
23
30
)
24
31
25
- func newTestDeployment (containers []interface {}) * unstructured.Unstructured {
32
+ func newTestUnstructured (containers []interface {}) * unstructured.Unstructured {
26
33
return & unstructured.Unstructured {
27
34
Object : map [string ]interface {}{
28
- "kind" : "Deployment " ,
29
- "apiVersion" : "apps/v1 " ,
35
+ "kind" : "MyResource " ,
36
+ "apiVersion" : "myApi " ,
30
37
"metadata" : map [string ]interface {}{
31
38
"name" : "test" ,
32
39
"namespace" : "ns" ,
@@ -42,99 +49,167 @@ func newTestDeployment(containers []interface{}) *unstructured.Unstructured {
42
49
}
43
50
}
44
51
45
- func TestManagerGeneratePatch (t * testing.T ) {
52
+ func newTestDeployment (containers []v1.Container ) * appsv1.Deployment {
53
+ return & appsv1.Deployment {
54
+ TypeMeta : metav1.TypeMeta {Kind : "Deployment" , APIVersion : "apps/v1" },
55
+ ObjectMeta : metav1.ObjectMeta {Name : "test" , Namespace : "ns" },
56
+ Spec : appsv1.DeploymentSpec {
57
+ Template : v1.PodTemplateSpec {
58
+ Spec : v1.PodSpec {
59
+ Containers : containers ,
60
+ },
61
+ },
62
+ },
63
+ }
64
+ }
65
+
66
+ func TestManagerGenerateStrategicMergePatch (t * testing.T ) {
46
67
47
68
tests := []struct {
48
- o1 * unstructured.Unstructured
49
- o2 * unstructured.Unstructured
50
- patch []map [string ]interface {}
69
+ o1 runtime.Object
70
+ o2 runtime.Object
71
+ patch string
72
+ patchType apitypes.PatchType
51
73
}{
52
74
{
53
- o1 : newTestDeployment ([]interface {}{
75
+ o1 : newTestUnstructured ([]interface {}{
54
76
map [string ]interface {}{
55
77
"name" : "test1" ,
56
78
},
57
79
map [string ]interface {}{
58
80
"name" : "test2" ,
59
81
},
60
82
}),
61
- o2 : newTestDeployment ([]interface {}{
83
+ o2 : newTestUnstructured ([]interface {}{
62
84
map [string ]interface {}{
63
85
"name" : "test1" ,
64
86
},
65
87
}),
66
- patch : []map [string ]interface {}{},
88
+ patch : `` ,
89
+ patchType : apitypes .JSONPatchType ,
67
90
},
68
91
{
69
- o1 : newTestDeployment ([]interface {}{
92
+ o1 : newTestUnstructured ([]interface {}{
70
93
map [string ]interface {}{
71
94
"name" : "test1" ,
72
95
},
73
96
}),
74
- o2 : newTestDeployment ([]interface {}{
97
+ o2 : newTestUnstructured ([]interface {}{
75
98
map [string ]interface {}{
76
99
"name" : "test1" ,
77
100
},
78
101
map [string ]interface {}{
79
102
"name" : "test2" ,
80
103
},
81
104
}),
82
- patch : []map [string ]interface {}{
83
- {
84
- "op" : "add" ,
85
- "path" : "/spec/template/spec/containers/1" ,
86
- "value" : map [string ]interface {}{
87
- "name" : string ("test2" ),
88
- },
89
- },
90
- },
105
+ patch : `[{"op":"add","path":"/spec/template/spec/containers/1","value":{"name":"test2"}}]` ,
106
+ patchType : apitypes .JSONPatchType ,
91
107
},
92
108
{
93
- o1 : newTestDeployment ([]interface {}{
109
+ o1 : newTestUnstructured ([]interface {}{
94
110
map [string ]interface {}{
95
111
"name" : "test1" ,
96
112
},
97
113
}),
98
- o2 : newTestDeployment ([]interface {}{
114
+ o2 : newTestUnstructured ([]interface {}{
99
115
map [string ]interface {}{
100
116
"name" : "test1" ,
101
117
"test" : nil ,
102
118
},
103
119
}),
104
- patch : []map [string ]interface {}{},
120
+ patch : `` ,
121
+ patchType : apitypes .JSONPatchType ,
105
122
},
106
123
{
107
- o1 : newTestDeployment ([]interface {}{
124
+ o1 : newTestUnstructured ([]interface {}{
108
125
map [string ]interface {}{
109
126
"name" : "test1" ,
110
127
},
111
128
}),
112
- o2 : newTestDeployment ([]interface {}{
129
+ o2 : newTestUnstructured ([]interface {}{
113
130
map [string ]interface {}{
114
131
"name" : "test2" ,
115
132
},
116
133
}),
117
- patch : []map [string ]interface {}{
118
- {
119
- "op" : "replace" ,
120
- "path" : "/spec/template/spec/containers/0/name" ,
121
- "value" : "test2" ,
134
+ patch : `[{"op":"replace","path":"/spec/template/spec/containers/0/name","value":"test2"}]` ,
135
+ patchType : apitypes .JSONPatchType ,
136
+ },
137
+ {
138
+ o1 : newTestDeployment ([]v1.Container {
139
+ {Name : "test1" },
140
+ {Name : "test2" },
141
+ }),
142
+ o2 : newTestDeployment ([]v1.Container {
143
+ {Name : "test1" },
144
+ }),
145
+ patch : `{"spec":{"template":{"spec":{"$setElementOrder/containers":[{"name":"test1"}]}}}}` , //nolint:lll
146
+ patchType : apitypes .StrategicMergePatchType ,
147
+ },
148
+ {
149
+ o1 : newTestDeployment ([]v1.Container {
150
+ {Name : "test1" },
151
+ }),
152
+ o2 : newTestDeployment ([]v1.Container {
153
+ {Name : "test1" },
154
+ {Name : "test2" },
155
+ }),
156
+ patch : `{"spec":{"template":{"spec":{"$setElementOrder/containers":[{"name":"test1"},{"name":"test2"}],"containers":[{"name":"test2","resources":{}}]}}}}` , //nolint:lll
157
+ patchType : apitypes .StrategicMergePatchType ,
158
+ },
159
+ {
160
+ o1 : newTestDeployment ([]v1.Container {
161
+ {Name : "test1" },
162
+ }),
163
+ o2 : newTestDeployment ([]v1.Container {
164
+ {Name : "test1" , LivenessProbe : nil },
165
+ }),
166
+ patch : `{}` ,
167
+ patchType : apitypes .StrategicMergePatchType ,
168
+ },
169
+ {
170
+ o1 : newTestDeployment ([]v1.Container {
171
+ {Name : "test1" },
172
+ }),
173
+ o2 : newTestDeployment ([]v1.Container {
174
+ {Name : "test2" },
175
+ }),
176
+ patch : `{"spec":{"template":{"spec":{"$setElementOrder/containers":[{"name":"test2"}],"containers":[{"name":"test2","resources":{}}]}}}}` , //nolint:lll
177
+ patchType : apitypes .StrategicMergePatchType ,
178
+ },
179
+ {
180
+ o1 : & appsv1.Deployment {
181
+ TypeMeta : metav1.TypeMeta {Kind : "Deployment" , APIVersion : "apps/v1" },
182
+ ObjectMeta : metav1.ObjectMeta {
183
+ Name : "test" ,
184
+ Namespace : "ns" ,
185
+ Annotations : map [string ]string {
186
+ "testannotation" : "testvalue" ,
187
+ },
122
188
},
189
+ Spec : appsv1.DeploymentSpec {},
123
190
},
191
+ o2 : & appsv1.Deployment {
192
+ TypeMeta : metav1.TypeMeta {Kind : "Deployment" , APIVersion : "apps/v1" },
193
+ ObjectMeta : metav1.ObjectMeta {
194
+ Name : "test" ,
195
+ Namespace : "ns" ,
196
+ },
197
+ Spec : appsv1.DeploymentSpec {},
198
+ },
199
+ patch : `{}` , //nolint:lll
200
+ patchType : apitypes .StrategicMergePatchType ,
124
201
},
125
202
}
126
203
127
204
for _ , test := range tests {
128
- diff , err := generatePatch (test .o1 , test .o2 )
129
- assert .NoError (t , err )
130
205
131
- if len (test .patch ) == 0 {
132
- assert .Equal (t , 0 , len (test .patch ))
133
- } else {
134
- x := []map [string ]interface {}{}
135
- err = json .Unmarshal (diff , & x )
136
- assert .NoError (t , err )
137
- assert .Equal (t , test .patch , x )
206
+ o2Info := & resource.Info {
207
+ Object : test .o2 ,
138
208
}
209
+
210
+ diff , patchType , err := createPatch (test .o1 , o2Info )
211
+ assert .NoError (t , err )
212
+ assert .Equal (t , test .patchType , patchType )
213
+ assert .Equal (t , test .patch , string (diff ))
139
214
}
140
215
}
0 commit comments