-
Notifications
You must be signed in to change notification settings - Fork 89
/
restore.test.ts
262 lines (245 loc) · 7.75 KB
/
restore.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
/*
* 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 { restore } from '@salto-io/core'
import { Workspace } from '@salto-io/workspace'
import { CliExitCode } from '../../src/types'
import { action } from '../../src/commands/restore'
import * as mocks from '../mocks'
import { buildEventName } from '../../src/telemetry'
const commandName = 'restore'
const eventsNames = {
success: buildEventName(commandName, 'success'),
start: buildEventName(commandName, 'start'),
failure: buildEventName(commandName, 'failure'),
changes: buildEventName(commandName, 'changes'),
changesToApply: buildEventName(commandName, 'changesToApply'),
workspaceSize: buildEventName(commandName, 'workspaceSize'),
}
jest.mock('@salto-io/core', () => ({
...jest.requireActual<{}>('@salto-io/core'),
restore: jest.fn().mockImplementation(() => Promise.resolve([])),
}))
describe('restore command', () => {
const services = ['salesforce']
let cliCommandArgs: mocks.MockCommandArgs
let telemetry: mocks.MockTelemetry
let output: mocks.MockCliOutput
let mockRestore: jest.MockedFunction<typeof restore>
beforeEach(() => {
const cliArgs = mocks.mockCliArgs()
cliCommandArgs = mocks.mockCliCommandArgs(commandName, cliArgs)
telemetry = cliArgs.telemetry
output = cliArgs.output
mockRestore = restore as typeof mockRestore
mockRestore.mockReset()
mockRestore.mockResolvedValue(
mocks.dummyChanges.map(change => ({ change, serviceChange: change }))
)
})
describe('with errored workspace', () => {
let result: number
beforeEach(async () => {
const workspace = mocks.mockWorkspace({})
workspace.errors.mockResolvedValue(
mocks.mockErrors([{ severity: 'Error', message: 'some error' }])
)
result = await action({
...cliCommandArgs,
input: {
force: true,
interactive: false,
dryRun: false,
detailedPlan: false,
listPlannedChanges: false,
mode: 'default',
services,
},
workspace,
})
})
it('should fail', async () => {
expect(result).toBe(CliExitCode.AppError)
expect(restore).not.toHaveBeenCalled()
})
})
describe('with valid workspace', () => {
let result: number
let workspace: Workspace
beforeEach(async () => {
workspace = mocks.mockWorkspace({})
result = await action({
...cliCommandArgs,
input: {
force: true,
interactive: false,
dryRun: false,
detailedPlan: false,
listPlannedChanges: false,
mode: 'default',
services,
},
workspace,
})
})
it('should return success code', () => {
expect(result).toBe(CliExitCode.Success)
})
it('should call restore', () => {
expect(restore).toHaveBeenCalled()
})
it('should update changes', () => {
expect(workspace.updateNaclFiles).toHaveBeenCalledWith(mocks.dummyChanges, 'default')
})
it('should send telemetry events', () => {
expect(telemetry.getEventsMap()[eventsNames.changesToApply]).toHaveLength(1)
expect(telemetry.getEventsMap()[eventsNames.workspaceSize]).toHaveLength(1)
})
it('should print deployment to console', () => {
expect(output.stdout.content).toContain('Finished calculating the difference')
expect(output.stdout.content).toContain('Applying 2 changes to the local workspace')
expect(output.stdout.content).toContain('Done!')
})
})
describe('Verify using env command', () => {
it('should use current env when env is not provided', async () => {
const workspace = mocks.mockWorkspace({})
await action({
...cliCommandArgs,
input: {
force: true,
interactive: false,
dryRun: false,
detailedPlan: false,
listPlannedChanges: false,
mode: 'default',
services,
},
workspace,
})
expect(workspace.setCurrentEnv).not.toHaveBeenCalled()
})
it('should use provided env', async () => {
const workspace = mocks.mockWorkspace({})
await action({
...cliCommandArgs,
input: {
force: true,
interactive: false,
dryRun: false,
detailedPlan: false,
listPlannedChanges: false,
mode: 'default',
services,
env: mocks.withEnvironmentParam,
},
workspace,
})
expect(workspace.setCurrentEnv).toHaveBeenCalledWith(mocks.withEnvironmentParam, false)
})
})
describe('dry-run', () => {
let result: number
let workspace: Workspace
beforeEach(async () => {
workspace = mocks.mockWorkspace({})
result = await action({
...cliCommandArgs,
input: {
force: true,
interactive: false,
dryRun: true,
detailedPlan: false,
listPlannedChanges: false,
mode: 'default',
services,
},
workspace,
})
})
it('should return success code', () => {
expect(result).toBe(CliExitCode.Success)
})
it('should call restore', () => {
expect(restore).toHaveBeenCalled()
})
it('should not update changes', () => {
expect(workspace.updateNaclFiles).not.toHaveBeenCalled()
})
it('should print plan to console, but not deploy', () => {
expect(output.stdout.content).toContain('The following changes')
expect(output.stdout.content).toContain('Finished calculating the difference')
expect(output.stdout.content).not.toContain('Done!')
})
})
it('should return error when update workspace fails', async () => {
const workspace = mocks.mockWorkspace({})
workspace.updateNaclFiles.mockImplementation(async () => {
workspace.errors.mockResolvedValue(
mocks.mockErrors([{ severity: 'Error', message: 'some error ' }])
)
})
const result = await action({
...cliCommandArgs,
input: {
force: true,
interactive: false,
dryRun: false,
detailedPlan: false,
listPlannedChanges: false,
mode: 'default',
services,
},
workspace,
})
expect(result).toBe(CliExitCode.AppError)
})
describe('using id filters', () => {
it('should fail when invalid filters are provided', async () => {
const result = await action({
...cliCommandArgs,
input: {
force: true,
interactive: false,
dryRun: false,
detailedPlan: false,
listPlannedChanges: false,
mode: 'default',
services,
elementSelectors: ['++'],
},
workspace: mocks.mockWorkspace({}),
})
expect(result).toBe(CliExitCode.UserInputError)
})
it('should succeed when valid filters are provided', async () => {
const result = await action({
...cliCommandArgs,
input: {
force: false,
interactive: false,
dryRun: false,
detailedPlan: false,
listPlannedChanges: false,
mode: 'default',
services,
elementSelectors: ['salto.*'],
},
workspace: mocks.mockWorkspace({}),
})
expect(result).toBe(CliExitCode.Success)
})
})
})