-
Notifications
You must be signed in to change notification settings - Fork 89
/
deploy.test.ts
365 lines (353 loc) · 11.7 KB
/
deploy.test.ts
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
/*
* Copyright 2021 Salto Labs Ltd.
*
* 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.
*/
import semver from 'semver'
import moment from 'moment'
import { Plan, PlanItem } from '@salto-io/core'
import { Workspace, state, pathIndex } from '@salto-io/workspace'
import { CliExitCode } from '../../src/types'
import * as callbacks from '../../src/callbacks'
import * as mocks from '../mocks'
import { action } from '../../src/commands/deploy'
import { version as currentVersion } from '../../src/generated/version.json'
const mockDeploy = mocks.deploy
const mockPreview = mocks.preview
jest.mock('../../src/callbacks')
jest.mock('@salto-io/core', () => ({
...jest.requireActual<{}>('@salto-io/core'),
deploy: jest.fn().mockImplementation((
ws: Workspace,
actionPlan: Plan,
reportProgress: (action: PlanItem, step: string, details?: string) => void,
services = new Array<string>(),
) =>
// Deploy with Nacl files will fail, doing this trick as we cannot reference vars, we get error:
// "The module factory of `jest.mock()` is not allowed to reference any
// out-of-scope variables."
// Notice that Nacl files are ignored in mockDeploy.
mockDeploy(ws, actionPlan, reportProgress, services)),
preview: jest.fn().mockImplementation((
_workspace: Workspace,
_services: string[],
) => mockPreview()),
}))
const commandName = 'deploy'
describe('deploy command', () => {
let workspace: mocks.MockWorkspace
let output: mocks.MockCliOutput
let cliCommandArgs: mocks.MockCommandArgs
const services = ['salesforce']
const mockGetUserBooleanInput = callbacks.getUserBooleanInput as jest.Mock
const mockShouldCancel = callbacks.shouldCancelCommand as jest.Mock
beforeEach(() => {
const cliArgs = mocks.mockCliArgs()
cliCommandArgs = mocks.mockCliCommandArgs(commandName, cliArgs)
output = cliArgs.output
workspace = mocks.mockWorkspace({})
workspace.getStateRecency.mockImplementation(async serviceName => ({
serviceName, status: 'Valid', date: new Date(),
}))
mockGetUserBooleanInput.mockReset()
mockShouldCancel.mockReset()
})
describe('should deploy considering user input', () => {
it('should continue with deploy when user input is y', async () => {
mockGetUserBooleanInput.mockResolvedValueOnce(true)
await action({
...cliCommandArgs,
input: {
force: false,
dryRun: false,
detailedPlan: false,
services,
},
workspace,
})
expect(output.stdout.content).toContain('Starting the deployment plan')
expect(output.stdout.content).toContain('Deployment succeeded')
})
it('should not deploy when user input is n', async () => {
mockGetUserBooleanInput.mockResolvedValueOnce(false)
await action({
...cliCommandArgs,
input: {
force: false,
dryRun: false,
detailedPlan: false,
services,
},
workspace,
})
expect(output.stdout.content).toContain('Cancelling deploy')
expect(output.stdout.content).not.toContain('Deployment succeeded')
})
})
describe('should not deploy on dry-run', () => {
it('should not deploy when dry-run flag is set', async () => {
const result = await action({
...cliCommandArgs,
input: {
force: false,
dryRun: true,
detailedPlan: false,
services,
},
workspace,
})
expect(result).toBe(CliExitCode.Success)
// exit without attempting to deploy
expect(output.stdout.content).not.toContain('Cancelling deploy')
expect(output.stdout.content).not.toContain('Deployment succeeded')
})
})
describe('detailed plan', () => {
it('should include value changes when detailed-plan is set', async () => {
await action({
...cliCommandArgs,
input: {
force: false,
dryRun: false,
detailedPlan: true,
services,
},
workspace,
})
expect(output.stdout.content).toMatch(/M.*name: "FirstEmployee" => "PostChange"/)
})
})
describe('invalid deploy', () => {
it('should fail gracefully', async () => {
workspace.errors.mockResolvedValue(
mocks.mockErrors([{ severity: 'Error', message: 'some error' }])
)
const result = await action({
...cliCommandArgs,
input: {
force: false,
dryRun: false,
detailedPlan: false,
services,
},
workspace,
})
expect(result).toBe(CliExitCode.AppError)
})
it('should allow the user to cancel when there are warnings', async () => {
workspace.errors.mockResolvedValue(
mocks.mockErrors([{ severity: 'Warning', message: 'some warning' }])
)
mockGetUserBooleanInput.mockResolvedValue(false)
const result = await action({
...cliCommandArgs,
input: {
force: false,
dryRun: false,
detailedPlan: false,
services,
},
workspace,
})
expect(result).toBe(CliExitCode.AppError)
expect(callbacks.shouldContinueInCaseOfWarnings).toHaveBeenCalled()
})
})
describe('when deploy result makes the workspace invalid', () => {
beforeEach(() => {
workspace.updateNaclFiles.mockImplementationOnce(async () => {
// Make the workspace errored after the call to updateNaclFiles
workspace.errors.mockResolvedValueOnce(
mocks.mockErrors([{ severity: 'Error', message: '' }])
)
})
mockGetUserBooleanInput.mockReturnValue(true)
})
describe('when called without force', () => {
it('should fail after asking whether to write', async () => {
const result = await action({
...cliCommandArgs,
input: {
force: false,
dryRun: false,
detailedPlan: false,
services,
},
workspace,
})
expect(result).toBe(CliExitCode.AppError)
expect(callbacks.getUserBooleanInput).toHaveBeenCalled()
})
})
describe('when called with force', () => {
it('should fail without user interaction', async () => {
const result = await action({
...cliCommandArgs,
input: {
force: true,
dryRun: false,
detailedPlan: false,
services,
},
workspace,
})
expect(result).toBe(CliExitCode.AppError)
expect(callbacks.getUserBooleanInput).not.toHaveBeenCalled()
})
})
})
describe('Using environment variable', () => {
it('should use provided env', async () => {
await action({
...cliCommandArgs,
input: {
force: false,
dryRun: false,
detailedPlan: false,
services,
env: mocks.withEnvironmentParam,
},
workspace,
})
expect(workspace.setCurrentEnv).toHaveBeenCalledWith(mocks.withEnvironmentParam, false)
})
})
describe('recommend fetch flow', () => {
const mockState = ({
saltoVersion, servicesUpdateDate,
}: Partial<state.StateData>): state.State => (
state.buildInMemState(async () => ({
elements: {},
pathIndex: new pathIndex.PathIndex(),
servicesUpdateDate: servicesUpdateDate ?? {},
saltoVersion,
}))
)
const inputOptions = {
force: false,
dryRun: false,
detailedPlan: false,
}
describe('when state salto version does not exist', () => {
beforeEach(async () => {
mockShouldCancel.mockResolvedValue(true)
workspace.state.mockReturnValue(mockState({}))
await action({
...cliCommandArgs,
input: inputOptions,
workspace,
})
})
it('should recommend cancel', () => {
expect(callbacks.shouldCancelCommand).toHaveBeenCalledTimes(1)
})
})
describe('when state version is newer than the current version', () => {
beforeEach(async () => {
mockShouldCancel.mockResolvedValue(true)
workspace.state.mockReturnValue(mockState({
saltoVersion: semver.inc(currentVersion, 'patch') as string,
}))
await action({
...cliCommandArgs,
input: inputOptions,
workspace,
})
})
it('should recommend cancel', () => {
expect(callbacks.shouldCancelCommand).toHaveBeenCalledTimes(1)
})
})
describe('when state version is the current version', () => {
beforeEach(async () => {
// answer false so we do not continue with deploy
mockGetUserBooleanInput.mockResolvedValue(false)
workspace.state.mockReturnValue(mockState({
saltoVersion: currentVersion,
}))
})
describe('when all services are valid', () => {
beforeEach(async () => {
await action({
...cliCommandArgs,
input: inputOptions,
workspace,
})
})
it('should not recommend cancel', () => {
expect(callbacks.shouldCancelCommand).not.toHaveBeenCalled()
})
})
describe('when some services were never fetched', () => {
beforeEach(async () => {
workspace.getStateRecency.mockImplementationOnce(async serviceName => ({
serviceName, status: 'Nonexistent', date: undefined,
}))
await action({
...cliCommandArgs,
input: inputOptions,
workspace,
})
})
it('should recommend cancel', () => {
expect(callbacks.shouldCancelCommand).toHaveBeenCalledTimes(1)
})
})
describe('when some services are old', () => {
beforeEach(async () => {
workspace.getStateRecency.mockImplementationOnce(async serviceName => ({
serviceName, status: 'Old', date: moment(new Date()).subtract(1, 'month').toDate(),
}))
await action({
...cliCommandArgs,
input: inputOptions,
workspace,
})
})
it('should recommend cancel', () => {
expect(callbacks.shouldCancelCommand).toHaveBeenCalledTimes(1)
})
})
})
describe('when state version is older than the current version by more than one patch', () => {
const decreaseVersion = (version: semver.SemVer): semver.SemVer => {
const prev = new semver.SemVer(version)
if (prev.patch > 1) {
prev.patch -= 2
} else if (prev.minor > 0) {
prev.minor -= 1
} else if (prev.major > 0) {
prev.major -= 1
} else {
throw new Error(`Cannot decrease version ${version.format()}`)
}
return prev
}
beforeEach(async () => {
mockShouldCancel.mockResolvedValue(true)
const prevVersion = decreaseVersion(semver.parse(currentVersion) as semver.SemVer)
workspace.state.mockReturnValue(mockState({
saltoVersion: prevVersion.format(),
}))
await action({
...cliCommandArgs,
input: inputOptions,
workspace,
})
})
it('should recommend cancel', () => {
expect(callbacks.shouldCancelCommand).toHaveBeenCalledTimes(1)
})
})
})
})