Skip to content

Commit b94b97c

Browse files
committed
fix: don't build diff doc for creates and deletes
This commit fixes a 'visual' regression introduced in #463, which is making decK print full diff documents for 'create' and 'delete' actions. This can result in very verbose output in case of configuration files containing hundreds/thousands of resources.
1 parent be0eaa0 commit b94b97c

File tree

12 files changed

+221
-105
lines changed

12 files changed

+221
-105
lines changed

diff/diff.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -401,23 +401,15 @@ func (sc *Syncer) Solve(ctx context.Context, parallelism int, dry bool) (Stats,
401401
c := e.Obj.(state.ConsoleString)
402402
switch e.Op {
403403
case crud.Create:
404-
diffString, err := generateDiffString(e, false, sc.noMaskValues)
405-
if err != nil {
406-
return nil, err
407-
}
408-
sc.createPrintln("creating", e.Kind, c.Console(), diffString)
404+
sc.createPrintln("creating", e.Kind, c.Console())
409405
case crud.Update:
410406
diffString, err := generateDiffString(e, false, sc.noMaskValues)
411407
if err != nil {
412408
return nil, err
413409
}
414410
sc.updatePrintln("updating", e.Kind, c.Console(), diffString)
415411
case crud.Delete:
416-
diffString, err := generateDiffString(e, true, sc.noMaskValues)
417-
if err != nil {
418-
return nil, err
419-
}
420-
sc.deletePrintln("deleting", e.Kind, c.Console(), diffString)
412+
sc.deletePrintln("deleting", e.Kind, c.Console())
421413
default:
422414
panic("unknown operation " + e.Op.String())
423415
}

tests/integration/diff_test.go

+141-79
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,53 @@ import (
1111
)
1212

1313
var (
14-
expectedOutputMasked = `creating workspace test
15-
creating service svc1 {
16-
+ "connect_timeout": 60000
17-
+ "host": "[masked]"
18-
+ "id": "9ecf5708-f2f4-444e-a4c7-fcd3a57f9a6d"
19-
+ "name": "svc1"
20-
+ "protocol": "http"
21-
+ "read_timeout": 60000
14+
expectedOutputMasked = `updating service svc1 {
15+
"connect_timeout": 60000,
16+
"enabled": true,
17+
"host": "[masked]",
18+
"id": "9ecf5708-f2f4-444e-a4c7-fcd3a57f9a6d",
19+
"name": "svc1",
20+
"port": 80,
21+
"protocol": "http",
22+
"read_timeout": 60000,
23+
"retries": 5,
24+
"write_timeout": 60000
2225
+ "tags": [
2326
+ "[masked] is an external host. I like [masked]!",
2427
+ "foo:foo",
2528
+ "baz:[masked]",
2629
+ "another:[masked]",
2730
+ "bar:[masked]"
2831
+ ]
29-
+ "write_timeout": 60000
30-
}
31-
32-
creating plugin rate-limiting (global) {
33-
+ "config": {
34-
+ "minute": 123
35-
+ }
36-
+ "id": "a1368a28-cb5c-4eee-86d8-03a6bdf94b5e"
37-
+ "name": "rate-limiting"
3832
}
3933
34+
creating plugin rate-limiting (global)
4035
Summary:
41-
Created: 2
42-
Updated: 0
36+
Created: 1
37+
Updated: 1
4338
Deleted: 0
4439
`
4540

46-
expectedOutputUnMasked = `creating workspace test
47-
creating service svc1 {
48-
+ "connect_timeout": 60000
49-
+ "host": "mockbin.org"
50-
+ "id": "9ecf5708-f2f4-444e-a4c7-fcd3a57f9a6d"
51-
+ "name": "svc1"
52-
+ "protocol": "http"
53-
+ "read_timeout": 60000
41+
expectedOutputUnMasked = `updating service svc1 {
42+
"connect_timeout": 60000,
43+
"enabled": true,
44+
"host": "mockbin.org",
45+
"id": "9ecf5708-f2f4-444e-a4c7-fcd3a57f9a6d",
46+
"name": "svc1",
47+
"port": 80,
48+
"protocol": "http",
49+
"read_timeout": 60000,
50+
"retries": 5,
51+
"write_timeout": 60000
5452
+ "tags": [
55-
+ "mockbin.org is an external host. I like mockbin.org!",
56-
+ "foo:foo",
57-
+ "baz:bazbaz",
58-
+ "another:bazbaz",
59-
+ "bar:barbar"
53+
+ "test"
6054
+ ]
61-
+ "write_timeout": 60000
62-
}
63-
64-
creating plugin rate-limiting (global) {
65-
+ "config": {
66-
+ "minute": 123
67-
+ }
68-
+ "id": "a1368a28-cb5c-4eee-86d8-03a6bdf94b5e"
69-
+ "name": "rate-limiting"
7055
}
7156
57+
creating plugin rate-limiting (global)
7258
Summary:
73-
Created: 2
74-
Updated: 0
59+
Created: 1
60+
Updated: 1
7561
Deleted: 0
7662
`
7763

@@ -87,48 +73,69 @@ Summary:
8773
// test scope:
8874
// - 1.x
8975
// - 2.x
90-
func Test_Diff_Workspace_UnMasked_OlderThan3x(t *testing.T) {
76+
func Test_Diff_Workspace_OlderThan3x(t *testing.T) {
9177
tests := []struct {
9278
name string
9379
stateFile string
9480
expectedState utils.KongRawState
95-
envVars map[string]string
9681
}{
9782
{
9883
name: "diff with not existent workspace doesn't error out",
9984
stateFile: "testdata/diff/001-not-existing-workspace/kong.yaml",
100-
envVars: diffEnvVars,
10185
},
10286
}
10387
for _, tc := range tests {
10488
t.Run(tc.name, func(t *testing.T) {
105-
for k, v := range tc.envVars {
106-
os.Setenv(k, v)
107-
defer func(k string) {
108-
os.Unsetenv(k)
109-
}(k)
110-
}
11189
runWhen(t, "kong", "<3.0.0")
11290
teardown := setup(t)
11391
defer teardown(t)
11492

115-
out, err := diff(tc.stateFile, "--no-mask-deck-env-vars-value")
93+
_, err := diff(tc.stateFile)
11694
assert.NoError(t, err)
117-
assert.Equal(t, out, expectedOutputUnMasked)
11895
})
11996
}
12097
}
121-
func Test_Diff_Workspace_Masked_OlderThan3x(t *testing.T) {
98+
99+
// test scope:
100+
// - 3.x
101+
func Test_Diff_Workspace_NewerThan3x(t *testing.T) {
122102
tests := []struct {
123103
name string
124104
stateFile string
125105
expectedState utils.KongRawState
126-
envVars map[string]string
127106
}{
128107
{
129108
name: "diff with not existent workspace doesn't error out",
130-
stateFile: "testdata/diff/001-not-existing-workspace/kong.yaml",
131-
envVars: diffEnvVars,
109+
stateFile: "testdata/diff/001-not-existing-workspace/kong3x.yaml",
110+
},
111+
}
112+
for _, tc := range tests {
113+
t.Run(tc.name, func(t *testing.T) {
114+
runWhen(t, "kong", ">=3.0.0")
115+
teardown := setup(t)
116+
defer teardown(t)
117+
118+
_, err := diff(tc.stateFile)
119+
assert.NoError(t, err)
120+
})
121+
}
122+
}
123+
124+
// test scope:
125+
// - 2.8.0
126+
func Test_Diff_Masked_OlderThan3x(t *testing.T) {
127+
tests := []struct {
128+
name string
129+
initialStateFile string
130+
stateFile string
131+
expectedState utils.KongRawState
132+
envVars map[string]string
133+
}{
134+
{
135+
name: "env variable are masked",
136+
initialStateFile: "testdata/diff/002-mask/initial.yaml",
137+
stateFile: "testdata/diff/002-mask/kong.yaml",
138+
envVars: diffEnvVars,
132139
},
133140
}
134141
for _, tc := range tests {
@@ -139,30 +146,35 @@ func Test_Diff_Workspace_Masked_OlderThan3x(t *testing.T) {
139146
os.Unsetenv(k)
140147
}(k)
141148
}
142-
runWhen(t, "kong", "<3.0.0")
149+
runWhen(t, "kong", "==2.8.0")
143150
teardown := setup(t)
144151
defer teardown(t)
145152

153+
// initialize state
154+
assert.NoError(t, sync(tc.initialStateFile))
155+
146156
out, err := diff(tc.stateFile)
147157
assert.NoError(t, err)
148-
assert.Equal(t, out, expectedOutputMasked)
158+
assert.Equal(t, expectedOutputMasked, out)
149159
})
150160
}
151161
}
152162

153163
// test scope:
154164
// - 3.x
155-
func Test_Diff_Workspace_UnMasked_NewerThan3x(t *testing.T) {
165+
func Test_Diff_Masked_NewerThan3x(t *testing.T) {
156166
tests := []struct {
157-
name string
158-
stateFile string
159-
expectedState utils.KongRawState
160-
envVars map[string]string
167+
name string
168+
initialStateFile string
169+
stateFile string
170+
expectedState utils.KongRawState
171+
envVars map[string]string
161172
}{
162173
{
163-
name: "diff with not existent workspace doesn't error out",
164-
stateFile: "testdata/diff/001-not-existing-workspace/kong3x.yaml",
165-
envVars: diffEnvVars,
174+
name: "env variable are masked",
175+
initialStateFile: "testdata/diff/002-mask/initial3x.yaml",
176+
stateFile: "testdata/diff/002-mask/kong3x.yaml",
177+
envVars: diffEnvVars,
166178
},
167179
}
168180
for _, tc := range tests {
@@ -177,23 +189,70 @@ func Test_Diff_Workspace_UnMasked_NewerThan3x(t *testing.T) {
177189
teardown := setup(t)
178190
defer teardown(t)
179191

192+
// initialize state
193+
assert.NoError(t, sync(tc.initialStateFile))
194+
195+
out, err := diff(tc.stateFile)
196+
assert.NoError(t, err)
197+
assert.Equal(t, expectedOutputMasked, out)
198+
})
199+
}
200+
}
201+
202+
// test scope:
203+
// - 2.8.0
204+
func Test_Diff_Unasked_OlderThan3x(t *testing.T) {
205+
tests := []struct {
206+
name string
207+
initialStateFile string
208+
stateFile string
209+
expectedState utils.KongRawState
210+
envVars map[string]string
211+
}{
212+
{
213+
name: "env variable are unmasked",
214+
initialStateFile: "testdata/diff/003-unmask/initial.yaml",
215+
stateFile: "testdata/diff/003-unmask/kong.yaml",
216+
envVars: diffEnvVars,
217+
},
218+
}
219+
for _, tc := range tests {
220+
t.Run(tc.name, func(t *testing.T) {
221+
for k, v := range tc.envVars {
222+
os.Setenv(k, v)
223+
defer func(k string) {
224+
os.Unsetenv(k)
225+
}(k)
226+
}
227+
runWhen(t, "kong", "==2.8.0")
228+
teardown := setup(t)
229+
defer teardown(t)
230+
231+
// initialize state
232+
assert.NoError(t, sync(tc.initialStateFile))
233+
180234
out, err := diff(tc.stateFile, "--no-mask-deck-env-vars-value")
181235
assert.NoError(t, err)
182-
assert.Equal(t, out, expectedOutputUnMasked)
236+
assert.Equal(t, expectedOutputUnMasked, out)
183237
})
184238
}
185239
}
186-
func Test_Diff_Workspace_Masked_NewerThan3x(t *testing.T) {
240+
241+
// test scope:
242+
// - 3.x
243+
func Test_Diff_Unasked_NewerThan3x(t *testing.T) {
187244
tests := []struct {
188-
name string
189-
stateFile string
190-
expectedState utils.KongRawState
191-
envVars map[string]string
245+
name string
246+
initialStateFile string
247+
stateFile string
248+
expectedState utils.KongRawState
249+
envVars map[string]string
192250
}{
193251
{
194-
name: "diff with not existent workspace doesn't error out",
195-
stateFile: "testdata/diff/001-not-existing-workspace/kong3x.yaml",
196-
envVars: diffEnvVars,
252+
name: "env variable are unmasked",
253+
initialStateFile: "testdata/diff/003-unmask/initial3x.yaml",
254+
stateFile: "testdata/diff/003-unmask/kong3x.yaml",
255+
envVars: diffEnvVars,
197256
},
198257
}
199258
for _, tc := range tests {
@@ -208,9 +267,12 @@ func Test_Diff_Workspace_Masked_NewerThan3x(t *testing.T) {
208267
teardown := setup(t)
209268
defer teardown(t)
210269

211-
out, err := diff(tc.stateFile)
270+
// initialize state
271+
assert.NoError(t, sync(tc.initialStateFile))
272+
273+
out, err := diff(tc.stateFile, "--no-mask-deck-env-vars-value")
212274
assert.NoError(t, err)
213-
assert.Equal(t, out, expectedOutputMasked)
275+
assert.Equal(t, expectedOutputUnMasked, out)
214276
})
215277
}
216278
}

tests/integration/testdata/diff/001-not-existing-workspace/kong.yaml

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
_workspace: test
22
services:
33
- name: svc1
4-
id: 9ecf5708-f2f4-444e-a4c7-fcd3a57f9a6d
5-
host: ${{ env "DECK_SVC1_HOSTNAME" }}
6-
tags:
7-
- ${{ env "DECK_SVC1_HOSTNAME" }} is an external host. I like mockbin.org!
8-
- foo:foo
9-
- baz:${{ env "DECK_BAZZ" }}
10-
- another:${{ env "DECK_BAZZ" }}
11-
- bar:${{ env "DECK_BARR" }}
4+
host: mockbin.org
125
plugins:
136
- name: rate-limiting
147
id: a1368a28-cb5c-4eee-86d8-03a6bdf94b5e

tests/integration/testdata/diff/001-not-existing-workspace/kong3x.yaml

+1-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@ _format_version: "3.0"
22
_workspace: test
33
services:
44
- name: svc1
5-
id: 9ecf5708-f2f4-444e-a4c7-fcd3a57f9a6d
6-
host: ${{ env "DECK_SVC1_HOSTNAME" }}
7-
tags:
8-
- ${{ env "DECK_SVC1_HOSTNAME" }} is an external host. I like mockbin.org!
9-
- foo:foo
10-
- baz:${{ env "DECK_BAZZ" }}
11-
- another:${{ env "DECK_BAZZ" }}
12-
- bar:${{ env "DECK_BARR" }}
5+
host: mockbin.org
136
plugins:
147
- name: rate-limiting
158
id: a1368a28-cb5c-4eee-86d8-03a6bdf94b5e
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
_format_version: "1.1"
2+
services:
3+
- name: svc1
4+
id: 9ecf5708-f2f4-444e-a4c7-fcd3a57f9a6d
5+
host: ${{ env "DECK_SVC1_HOSTNAME" }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
_format_version: "3.0"
2+
services:
3+
- name: svc1
4+
id: 9ecf5708-f2f4-444e-a4c7-fcd3a57f9a6d
5+
host: ${{ env "DECK_SVC1_HOSTNAME" }}

0 commit comments

Comments
 (0)