1
2
3
4 package patchjson6902
5
6 import (
7 "strings"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 filtertest "sigs.k8s.io/kustomize/api/testutils/filtertest"
12 )
13
14 const input = `
15 apiVersion: apps/v1
16 kind: Deployment
17 metadata:
18 name: myDeploy
19 spec:
20 replica: 2
21 template:
22 metadata:
23 labels:
24 old-label: old-value
25 spec:
26 containers:
27 - image: nginx
28 name: nginx
29 `
30
31 func TestSomething(t *testing.T) {
32 testCases := []struct {
33 testName string
34 input string
35 filter Filter
36 expectedOutput string
37 }{
38 {
39 testName: "single operation, json",
40 input: input,
41 filter: Filter{
42 Patch: `[
43 {"op": "replace", "path": "/spec/replica", "value": 5}
44 ]`,
45 },
46 expectedOutput: `
47 apiVersion: apps/v1
48 kind: Deployment
49 metadata:
50 name: myDeploy
51 spec:
52 replica: 5
53 template:
54 metadata:
55 labels:
56 old-label: old-value
57 spec:
58 containers:
59 - image: nginx
60 name: nginx
61 `,
62 },
63 {
64 testName: "multiple operations, json",
65 input: input,
66 filter: Filter{
67 Patch: `[
68 {"op": "replace", "path": "/spec/template/spec/containers/0/name", "value": "my-nginx"},
69 {"op": "add", "path": "/spec/replica", "value": 999},
70 {"op": "add", "path": "/spec/template/spec/containers/0/command", "value": ["arg1", "arg2", "arg3"]}
71 ]`,
72 },
73 expectedOutput: `
74 apiVersion: apps/v1
75 kind: Deployment
76 metadata:
77 name: myDeploy
78 spec:
79 replica: 999
80 template:
81 metadata:
82 labels:
83 old-label: old-value
84 spec:
85 containers:
86 - command:
87 - arg1
88 - arg2
89 - arg3
90 image: nginx
91 name: my-nginx
92 `,
93 },
94 {
95 testName: "single operation, yaml",
96 input: input,
97 filter: Filter{
98 Patch: `
99 - op: replace
100 path: /spec/replica
101 value: 5
102 `,
103 },
104 expectedOutput: `
105 apiVersion: apps/v1
106 kind: Deployment
107 metadata:
108 name: myDeploy
109 spec:
110 replica: 5
111 template:
112 metadata:
113 labels:
114 old-label: old-value
115 spec:
116 containers:
117 - image: nginx
118 name: nginx
119 `,
120 },
121 {
122 testName: "multiple operations, yaml",
123 input: input,
124 filter: Filter{
125 Patch: `
126 - op: replace
127 path: /spec/template/spec/containers/0/name
128 value: my-nginx
129 - op: add
130 path: /spec/replica
131 value: 999
132 - op: add
133 path: /spec/template/spec/containers/0/command
134 value:
135 - arg1
136 - arg2
137 - arg3
138 `,
139 },
140 expectedOutput: `
141 apiVersion: apps/v1
142 kind: Deployment
143 metadata:
144 name: myDeploy
145 spec:
146 replica: 999
147 template:
148 metadata:
149 labels:
150 old-label: old-value
151 spec:
152 containers:
153 - command:
154 - arg1
155 - arg2
156 - arg3
157 image: nginx
158 name: my-nginx
159 `,
160 },
161 }
162
163 for _, tc := range testCases {
164 t.Run(tc.testName, func(t *testing.T) {
165 if !assert.Equal(t,
166 strings.TrimSpace(tc.expectedOutput),
167 strings.TrimSpace(
168 filtertest.RunFilter(t, tc.input, tc.filter))) {
169 t.FailNow()
170 }
171 })
172 }
173 }
174
View as plain text