-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathplanpreview_test.go
More file actions
366 lines (342 loc) · 10.9 KB
/
planpreview_test.go
File metadata and controls
366 lines (342 loc) · 10.9 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright 2024 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package planpreview
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/pipe-cd/pipecd/pkg/model"
)
func TestReadableResultString(t *testing.T) {
testcases := []struct {
name string
results []*model.PlanPreviewCommandResult
expected string
}{
{
name: "empty",
results: []*model.PlanPreviewCommandResult{},
expected: `
There are no updated applications. It means no deployment will be triggered once this pull request got merged.
`,
},
{
name: "there is only a plannable application",
results: []*model.PlanPreviewCommandResult{
{
CommandId: "command-2",
PipedId: "piped-2",
PipedUrl: "https://pipecd.dev/piped-2",
Results: []*model.ApplicationPlanPreviewResult{
{
ApplicationId: "app-1",
ApplicationName: "app-1",
ApplicationUrl: "https://pipecd.dev/app-1",
ApplicationKind: model.ApplicationKind_KUBERNETES,
Labels: map[string]string{"env": "env-1"},
SyncStrategy: model.SyncStrategy_QUICK_SYNC,
PlanSummary: []byte("2 manifests will be added, 1 manifest will be deleted and 5 manifests will be changed"),
PlanDetails: []byte("changes-1"),
},
},
},
},
expected: `
Here are plan-preview for 1 application:
1. app: app-1, env: env-1, kind: KUBERNETES
sync strategy: QUICK_SYNC
summary: 2 manifests will be added, 1 manifest will be deleted and 5 manifests will be changed
details:
---DETAILS_BEGIN---
changes-1
---DETAILS_END---
`,
},
{
name: "there is only a failure application",
results: []*model.PlanPreviewCommandResult{
{
CommandId: "command-2",
PipedId: "piped-2",
PipedUrl: "https://pipecd.dev/piped-2",
Results: []*model.ApplicationPlanPreviewResult{
{
ApplicationId: "app-2",
ApplicationName: "app-2",
ApplicationUrl: "https://pipecd.dev/app-2",
ApplicationKind: model.ApplicationKind_TERRAFORM,
Labels: map[string]string{"env": "env-2"},
Error: "wrong application configuration",
},
},
},
},
expected: `
NOTE: An error occurred while building plan-preview for the following application:
1. app: app-2, env: env-2, kind: TERRAFORM
reason: wrong application configuration
`,
},
{
name: "there is only a failure piped",
results: []*model.PlanPreviewCommandResult{
{
CommandId: "command-1",
PipedId: "piped-1",
PipedName: "piped-name-1",
PipedUrl: "https://pipecd.dev/piped-1",
Error: "failed to clone",
},
},
expected: `
NOTE: An error occurred while building plan-preview for applications of the following Piped:
1. piped: piped-name-1 (piped-1)
reason: failed to clone
`,
},
{
name: "all kinds",
results: []*model.PlanPreviewCommandResult{
{
CommandId: "command-1",
PipedId: "piped-1",
PipedName: "piped-name-1",
PipedUrl: "https://pipecd.dev/piped-1",
Error: "failed to clone",
},
{
CommandId: "command-2",
PipedId: "piped-2",
PipedName: "piped-name-2",
PipedUrl: "https://pipecd.dev/piped-2",
Results: []*model.ApplicationPlanPreviewResult{
{
ApplicationId: "app-1",
ApplicationName: "app-1",
ApplicationUrl: "https://pipecd.dev/app-1",
ApplicationKind: model.ApplicationKind_KUBERNETES,
Labels: map[string]string{"env": "env-1"},
SyncStrategy: model.SyncStrategy_QUICK_SYNC,
PlanSummary: []byte("2 manifests will be added, 1 manifest will be deleted and 5 manifests will be changed"),
PlanDetails: []byte("changes-1"),
},
{
ApplicationId: "app-2",
ApplicationName: "app-2",
ApplicationUrl: "https://pipecd.dev/app-2",
ApplicationKind: model.ApplicationKind_TERRAFORM,
Labels: map[string]string{"env": "env-2"},
SyncStrategy: model.SyncStrategy_PIPELINE,
PlanSummary: []byte("1 to add, 2 to change, 0 to destroy"),
PlanDetails: []byte("changes-2"),
},
{
ApplicationId: "app-3",
ApplicationName: "app-3",
ApplicationUrl: "https://pipecd.dev/app-3",
ApplicationKind: model.ApplicationKind_TERRAFORM,
Labels: map[string]string{"env": "env-3"},
Error: "wrong application configuration",
},
{
ApplicationId: "app-4",
ApplicationName: "app-4",
ApplicationUrl: "https://pipecd.dev/app-4",
ApplicationKind: model.ApplicationKind_CLOUDRUN,
Error: "missing key",
},
{
ApplicationId: "app-5",
ApplicationName: "app-5",
ApplicationUrl: "https://pipecd.dev/app-5",
ApplicationKind: model.ApplicationKind_ECS,
Error: "wrong application configuration",
},
{
ApplicationId: "app-6",
ApplicationName: "app-6",
ApplicationUrl: "https://pipecd.dev/app-6",
ApplicationKind: model.ApplicationKind_LAMBDA,
Error: "wrong application configuration",
},
},
},
{
CommandId: "command-3",
PipedId: "piped-3",
PipedName: "piped-name-3",
PipedUrl: "https://pipecd.dev/piped-3",
Error: "failed to checkout branch",
},
},
expected: `
Here are plan-preview for 2 applications:
1. app: app-1, env: env-1, kind: KUBERNETES
sync strategy: QUICK_SYNC
summary: 2 manifests will be added, 1 manifest will be deleted and 5 manifests will be changed
details:
---DETAILS_BEGIN---
changes-1
---DETAILS_END---
2. app: app-2, env: env-2, kind: TERRAFORM
sync strategy: PIPELINE
summary: 1 to add, 2 to change, 0 to destroy
details:
---DETAILS_BEGIN---
changes-2
---DETAILS_END---
NOTE: An error occurred while building plan-preview for the following 4 applications:
1. app: app-3, env: env-3, kind: TERRAFORM
reason: wrong application configuration
2. app: app-4, kind: CLOUDRUN
reason: missing key
3. app: app-5, kind: ECS
reason: wrong application configuration
4. app: app-6, kind: LAMBDA
reason: wrong application configuration
NOTE: An error occurred while building plan-preview for applications of the following 2 Pipeds:
1. piped: piped-name-1 (piped-1)
reason: failed to clone
2. piped: piped-name-3 (piped-3)
reason: failed to checkout branch
`,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
printResults(tc.results, &buf, "")
assert.Equal(t, tc.expected, buf.String())
})
}
}
func TestSortResults(t *testing.T) {
t.Parallel()
testcases := []struct {
name string
results []*model.PlanPreviewCommandResult
sortLabelKeys []string
expected []*model.PlanPreviewCommandResult
}{
{
name: "sort by pipedID and application name",
results: []*model.PlanPreviewCommandResult{
{
PipedId: "piped-2",
Results: []*model.ApplicationPlanPreviewResult{
{ApplicationName: "app-2"},
{ApplicationName: "app-1"},
},
},
{
PipedId: "piped-1",
Results: []*model.ApplicationPlanPreviewResult{
{ApplicationName: "app-2"},
{ApplicationName: "app-1"},
},
},
},
sortLabelKeys: []string{},
expected: []*model.PlanPreviewCommandResult{
{
PipedId: "piped-1",
Results: []*model.ApplicationPlanPreviewResult{
{ApplicationName: "app-1"},
{ApplicationName: "app-2"},
},
},
{
PipedId: "piped-2",
Results: []*model.ApplicationPlanPreviewResult{
{ApplicationName: "app-1"},
{ApplicationName: "app-2"},
},
},
},
},
{
name: "sort by label keys",
results: []*model.PlanPreviewCommandResult{
{
PipedId: "piped-1",
Results: []*model.ApplicationPlanPreviewResult{
{ApplicationName: "app-1", Labels: map[string]string{"env": "staging"}},
{ApplicationName: "app-1", Labels: map[string]string{"env": "prod"}},
},
},
{
PipedId: "piped-2",
Results: []*model.ApplicationPlanPreviewResult{
{ApplicationName: "app-3", Labels: map[string]string{"env": "staging"}},
{ApplicationName: "app-3", Labels: map[string]string{"env": "prod"}},
{ApplicationName: "app-2", Labels: map[string]string{"env": "staging"}},
{ApplicationName: "app-2", Labels: map[string]string{"env": "prod"}},
},
},
},
sortLabelKeys: []string{"env"},
expected: []*model.PlanPreviewCommandResult{
{
PipedId: "piped-1",
Results: []*model.ApplicationPlanPreviewResult{
{ApplicationName: "app-1", Labels: map[string]string{"env": "prod"}},
{ApplicationName: "app-1", Labels: map[string]string{"env": "staging"}},
},
},
{
PipedId: "piped-2",
Results: []*model.ApplicationPlanPreviewResult{
{ApplicationName: "app-2", Labels: map[string]string{"env": "prod"}},
{ApplicationName: "app-3", Labels: map[string]string{"env": "prod"}},
{ApplicationName: "app-2", Labels: map[string]string{"env": "staging"}},
{ApplicationName: "app-3", Labels: map[string]string{"env": "staging"}},
},
},
},
},
{
name: "sort by multiple label keys",
results: []*model.PlanPreviewCommandResult{
{
PipedId: "piped-1",
Results: []*model.ApplicationPlanPreviewResult{
{ApplicationName: "app-1", Labels: map[string]string{"env": "prod", "team": "team-2"}},
{ApplicationName: "app-1", Labels: map[string]string{"env": "staging", "team": "team-1"}},
{ApplicationName: "app-1", Labels: map[string]string{"env": "prod", "team": "team-1"}},
{ApplicationName: "app-2", Labels: map[string]string{"env": "prod", "team": "team-2"}},
},
},
},
sortLabelKeys: []string{"env", "team"},
expected: []*model.PlanPreviewCommandResult{
{
PipedId: "piped-1",
Results: []*model.ApplicationPlanPreviewResult{
{ApplicationName: "app-1", Labels: map[string]string{"env": "prod", "team": "team-1"}},
{ApplicationName: "app-1", Labels: map[string]string{"env": "prod", "team": "team-2"}},
{ApplicationName: "app-2", Labels: map[string]string{"env": "prod", "team": "team-2"}},
{ApplicationName: "app-1", Labels: map[string]string{"env": "staging", "team": "team-1"}},
},
},
},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
sortResults(tc.results, tc.sortLabelKeys)
assert.Equal(t, tc.expected, tc.results)
})
}
}