-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathmain.test.ts
More file actions
291 lines (252 loc) · 13.9 KB
/
main.test.ts
File metadata and controls
291 lines (252 loc) · 13.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
import * as core from "@actions/core";
import { AuthorizerFactory } from 'azure-actions-webclient/AuthorizerFactory';
import run from "../src/main";
import AzureSqlAction, { ActionType, IActionInputs, IBuildAndPublishInputs, IDacpacActionInputs, SqlPackageAction } from "../src/AzureSqlAction";
import FirewallManager from "../src/FirewallManager";
import AzureSqlActionHelper from '../src/AzureSqlActionHelper';
import SqlUtils from '../src/SqlUtils';
jest.mock('@actions/core');
jest.mock('azure-actions-webclient/AuthorizerFactory');
jest.mock('../src/AzureSqlAction');
jest.mock('../src/FirewallManager');
jest.mock('../src/AzureSqlResourceManager');
jest.mock('../src/Setup');
describe('main.ts tests', () => {
beforeEach(() => {
jest.restoreAllMocks();
jest.clearAllMocks();
});
it('gets inputs and executes build and publish action', async () => {
const resolveFilePathSpy = jest.spyOn(AzureSqlActionHelper, 'resolveFilePath').mockReturnValue('./TestProject.sqlproj');
const getInputSpy = jest.spyOn(core, 'getInput').mockImplementation((name, options) => {
switch(name) {
case 'connection-string': return 'Server=testServer.database.windows.net;Initial Catalog=testDB;User Id=testUser;Password=placeholder;';
case 'path': return './TestProject.sqlproj';
case 'action': return 'publish';
case 'arguments': return '/p:FakeSqlpackageArgument';
case 'build-arguments': return '-p FakeDotnetArgument';
default : return '';
}
});
const getAuthorizerSpy = jest.spyOn(AuthorizerFactory, 'getAuthorizer');
const addFirewallRuleSpy = jest.spyOn(FirewallManager.prototype, 'addFirewallRule');
const actionExecuteSpy = jest.spyOn(AzureSqlAction.prototype, 'execute');
const removeFirewallRuleSpy = jest.spyOn(FirewallManager.prototype, 'removeFirewallRule');
const setFailedSpy = jest.spyOn(core, 'setFailed');
const detectIPAddressSpy = SqlUtils.detectIPAddress = jest.fn().mockImplementationOnce(() => {
return "";
});
await run();
expect(AzureSqlAction).toHaveBeenCalled();
expect(AzureSqlAction).toHaveBeenCalledWith(
expect.objectContaining({
actionType: ActionType.BuildAndPublish,
filePath: './TestProject.sqlproj',
buildArguments: '-p FakeDotnetArgument',
sqlpackageAction: SqlPackageAction.Publish,
additionalArguments: '/p:FakeSqlpackageArgument'
} as IBuildAndPublishInputs)
);
expect(detectIPAddressSpy).toHaveBeenCalled();
expect(getAuthorizerSpy).not.toHaveBeenCalled();
expect(getInputSpy).toHaveBeenCalledTimes(6);
expect(resolveFilePathSpy).toHaveBeenCalled();
expect(addFirewallRuleSpy).not.toHaveBeenCalled();
expect(actionExecuteSpy).toHaveBeenCalled();
expect(removeFirewallRuleSpy).not.toHaveBeenCalled();
expect(setFailedSpy).not.toHaveBeenCalled();
});
it('gets inputs and executes dacpac action', async () => {
let resolveFilePathSpy = jest.spyOn(AzureSqlActionHelper, 'resolveFilePath').mockReturnValue('./TestDacpacPackage.dacpac');
let getInputSpy = jest.spyOn(core, 'getInput').mockImplementation((name, options) => {
switch(name) {
case 'connection-string': return 'Server=testServer.database.windows.net;Initial Catalog=testDB;User Id=testUser;Password=placeholder;';
case 'path': return './TestDacpacPackage.dacpac';
case 'action': return 'script';
case 'arguments': return '/p:FakeSqlpackageArgument';
}
return '';
});
let getAuthorizerSpy = jest.spyOn(AuthorizerFactory, 'getAuthorizer');
let addFirewallRuleSpy = jest.spyOn(FirewallManager.prototype, 'addFirewallRule');
let actionExecuteSpy = jest.spyOn(AzureSqlAction.prototype, 'execute');
let removeFirewallRuleSpy = jest.spyOn(FirewallManager.prototype, 'removeFirewallRule');
let setFailedSpy = jest.spyOn(core, 'setFailed');
let detectIPAddressSpy = SqlUtils.detectIPAddress = jest.fn().mockImplementationOnce(() => {
return "";
});
await run();
expect(AzureSqlAction).toHaveBeenCalled();
expect(AzureSqlAction).toHaveBeenCalledWith(
expect.objectContaining({
actionType: ActionType.DacpacAction,
filePath: './TestDacpacPackage.dacpac',
sqlpackageAction: SqlPackageAction.Script,
additionalArguments: '/p:FakeSqlpackageArgument'
} as IDacpacActionInputs)
);
expect(detectIPAddressSpy).toHaveBeenCalled();
expect(getAuthorizerSpy).not.toHaveBeenCalled();
expect(getInputSpy).toHaveBeenCalledTimes(5);
expect(resolveFilePathSpy).toHaveBeenCalled();
expect(addFirewallRuleSpy).not.toHaveBeenCalled();
expect(actionExecuteSpy).toHaveBeenCalled();
expect(removeFirewallRuleSpy).not.toHaveBeenCalled();
expect(setFailedSpy).not.toHaveBeenCalled();
})
it('gets inputs and executes dacpac action with optional sqlpackage path', async () => {
let resolveFilePathSpy = jest.spyOn(AzureSqlActionHelper, 'resolveFilePath').mockReturnValue('./TestDacpacPackage.dacpac');
let getInputSpy = jest.spyOn(core, 'getInput').mockImplementation((name, options) => {
switch(name) {
case 'connection-string': return 'Server=testServer.database.windows.net;Initial Catalog=testDB;User Id=testUser;Password=placeholder;';
case 'path': return './TestDacpacPackage.dacpac';
case 'action': return 'script';
case 'arguments': return '/p:FakeSqlpackageArgument';
case 'sqlpackage-path': return '/path/to/sqlpackage';
}
return '';
});
let getAuthorizerSpy = jest.spyOn(AuthorizerFactory, 'getAuthorizer');
let addFirewallRuleSpy = jest.spyOn(FirewallManager.prototype, 'addFirewallRule');
let actionExecuteSpy = jest.spyOn(AzureSqlAction.prototype, 'execute');
let removeFirewallRuleSpy = jest.spyOn(FirewallManager.prototype, 'removeFirewallRule');
let setFailedSpy = jest.spyOn(core, 'setFailed');
let detectIPAddressSpy = SqlUtils.detectIPAddress = jest.fn().mockImplementationOnce(() => {
return "";
});
await run();
expect(AzureSqlAction).toHaveBeenCalled();
expect(AzureSqlAction).toHaveBeenCalledWith(
expect.objectContaining({
actionType: ActionType.DacpacAction,
filePath: './TestDacpacPackage.dacpac',
sqlpackageAction: SqlPackageAction.Script,
additionalArguments: '/p:FakeSqlpackageArgument',
sqlpackagePath: '/path/to/sqlpackage'
} as IDacpacActionInputs)
);
expect(detectIPAddressSpy).toHaveBeenCalled();
expect(getAuthorizerSpy).not.toHaveBeenCalled();
expect(getInputSpy).toHaveBeenCalledTimes(5);
expect(resolveFilePathSpy).toHaveBeenCalled();
expect(addFirewallRuleSpy).not.toHaveBeenCalled();
expect(actionExecuteSpy).toHaveBeenCalled();
expect(removeFirewallRuleSpy).not.toHaveBeenCalled();
expect(setFailedSpy).not.toHaveBeenCalled();
})
it('gets inputs and executes sql action', async () => {
let resolveFilePathSpy = jest.spyOn(AzureSqlActionHelper, 'resolveFilePath').mockReturnValue('./TestSqlFile.sql');
let getInputSpy = jest.spyOn(core, 'getInput').mockImplementation((name, options) => {
switch(name) {
case 'connection-string': return 'Server=testServer.database.windows.net;Initial Catalog=testDB;User Id=testUser;Password=placeholder;';
case 'path': return './TestSqlFile.sql';
case 'arguments': return '-v FakeSqlcmdArgument';
default: return '';
}
});
let setFailedSpy = jest.spyOn(core, 'setFailed');
let getAuthorizerSpy = jest.spyOn(AuthorizerFactory, 'getAuthorizer');
let addFirewallRuleSpy = jest.spyOn(FirewallManager.prototype, 'addFirewallRule');
let actionExecuteSpy = jest.spyOn(AzureSqlAction.prototype, 'execute');
let removeFirewallRuleSpy = jest.spyOn(FirewallManager.prototype, 'removeFirewallRule');
let detectIPAddressSpy = SqlUtils.detectIPAddress = jest.fn().mockImplementationOnce(() => {
return "";
});
await run();
expect(AzureSqlAction).toHaveBeenCalled();
expect(AzureSqlAction).toHaveBeenCalledWith(
expect.objectContaining({
actionType: ActionType.SqlAction,
filePath: './TestSqlFile.sql',
additionalArguments: '-v FakeSqlcmdArgument'
} as IActionInputs)
);
expect(detectIPAddressSpy).toHaveBeenCalled();
expect(getAuthorizerSpy).not.toHaveBeenCalled();
expect(getInputSpy).toHaveBeenCalledTimes(4);
expect(resolveFilePathSpy).toHaveBeenCalled();
expect(addFirewallRuleSpy).not.toHaveBeenCalled();
expect(actionExecuteSpy).toHaveBeenCalled();
expect(removeFirewallRuleSpy).not.toHaveBeenCalled();
expect(setFailedSpy).not.toHaveBeenCalled();
})
it('throws if input file is not found', async() => {
jest.spyOn(AzureSqlActionHelper, 'resolveFilePath').mockImplementation(() => {
throw new Error(`Unable to find file at location`);
});
jest.spyOn(core, 'getInput').mockImplementation((name, options) => {
switch(name) {
case 'connection-string': return 'Server=testServer.database.windows.net;Initial Catalog=testDB;User Id=testUser;Password=placeholder;';
case 'path': return './TestSqlFile.sql';
case 'action': return 'publish';
default: return '';
}
});
let detectIPAddressSpy = SqlUtils.detectIPAddress = jest.fn().mockImplementationOnce(() => {
return "";
});
let setFailedSpy = jest.spyOn(core, 'setFailed');
await run();
expect(AzureSqlAction).not.toHaveBeenCalled();
expect(detectIPAddressSpy).not.toHaveBeenCalled();
expect(setFailedSpy).toHaveBeenCalledWith('Unable to find file at location');
})
describe('validate errors on unsupported sqlpackage action types', () => {
const inputs = [ ['Extract'], ['Export'], ['Import'], ['InvalidAction'] ];
it.each(inputs)('Throws for unsupported action %s', async (actionName) => {
let resolveFilePathSpy = jest.spyOn(AzureSqlActionHelper, 'resolveFilePath').mockReturnValue('./TestDacpacPackage.dacpac');
let setFailedSpy = jest.spyOn(core, 'setFailed');
let getInputSpy = jest.spyOn(core, 'getInput').mockImplementation((name, options) => {
switch(name) {
case 'connection-string': return 'Server=testServer.database.windows.net;Initial Catalog=testDB;User Id=testUser;Password=placeholder;';
case 'path': return './TestDacpacPackage.dacpac';
case 'action': return actionName;
default: return '';
}
});
await run();
expect(resolveFilePathSpy).toHaveBeenCalled();
expect(getInputSpy).toHaveBeenCalled();
expect(setFailedSpy).toHaveBeenCalled();
expect(setFailedSpy).toHaveBeenCalledWith(`Action ${actionName} is invalid. Supported action types are: Publish, Script, DriftReport, DeployReport, or BuildOnly.`);
});
});
describe('validate firewall rule check depending on skip-firewall-check value', () => {
const inputs = [
[ 'skip-firewall-check is false, firewall rule should be checked', false, true ],
[ 'skip-firewall-check is true, firewall rule should not be checked', true, false]
];
it.each(inputs)('%s', async (testName, skipFirewallCheckValue, firewallRuleCheckRan) => {
const resolveFilePathSpy = jest.spyOn(AzureSqlActionHelper, 'resolveFilePath').mockReturnValue('./TestSqlFile.sql');
const getInputSpy = jest.spyOn(core, 'getInput').mockImplementation((name, options) => {
switch (name) {
case 'connection-string': return 'Server=testServer.database.windows.net;Initial Catalog=testDB;User Id=testUser;Password=placeholder;';
case 'path': return './TestSqlFile.sql';
case 'arguments': return '-v FakeSqlcmdArgument';
default: return '';
}
});
const getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput').mockImplementation((name, options) => {
switch (name) {
case 'skip-firewall-check': return Boolean(skipFirewallCheckValue);
default: return false;
}
});
const detectIPAddressSpy = jest.spyOn(SqlUtils, 'detectIPAddress').mockResolvedValue('');
const executeSpy = jest.spyOn(AzureSqlAction.prototype, 'execute').mockResolvedValue();
const removeFirewallRuleSpy = jest.spyOn(FirewallManager.prototype, 'removeFirewallRule');
await run();
expect(AzureSqlAction).toHaveBeenCalled();
expect(getInputSpy).toHaveBeenCalledTimes(4);
expect(getBooleanInputSpy).toHaveBeenCalled();
expect(resolveFilePathSpy).toHaveBeenCalled();
expect(executeSpy).toHaveBeenCalled();
if (Boolean(firewallRuleCheckRan)) {
expect(detectIPAddressSpy).toHaveBeenCalled();
} else {
expect(detectIPAddressSpy).not.toHaveBeenCalled();
}
expect(removeFirewallRuleSpy).not.toHaveBeenCalled();
});
});
})