This repository has been archived by the owner on May 5, 2023. It is now read-only.
/
cli.site-tests.js
353 lines (294 loc) · 11.5 KB
/
cli.site-tests.js
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
/**
* Copyright 2012 Microsoft Corporation
*
* 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.
*/
var should = require('should');
var url = require('url');
var GitHubApi = require('github');
var util = require('util');
var cli = require('../cli');
var capture = require('../util').capture;
var LinkedRevisionControlClient = require('../../../lib/cli/linkedrevisioncontrol').LinkedRevisionControlClient;
var githubUsername = process.env['AZURE_GITHUB_USERNAME'];
var githubPassword = process.env['AZURE_GITHUB_PASSWORD'];
var githubRepositoryFullName = process.env['AZURE_GITHUB_REPOSITORY'];
var githubClient = new GitHubApi({ version: "3.0.0" });
githubClient.authenticate({
type: "basic",
username: githubUsername,
password: githubPassword
});
suite('cli', function(){
suite('site', function() {
teardown(function (done) {
var repositoryName;
function deleteAllHooks (hooks, callback) {
if (hooks.length === 0) {
callback();
} else {
var hook = hooks.pop();
hook.user = githubUsername;
hook.repo = repositoryName;
githubClient.repos.deleteHook(hook, function () {
deleteAllHooks(hooks, callback);
});
}
};
// Remove any existing repository hooks
githubClient.repos.getFromUser({ user: githubUsername }, function (err, repositories) {
repositoryName = LinkedRevisionControlClient._getRepository(repositories, githubRepositoryFullName).name;
githubClient.repos.getHooks({
user: githubUsername,
repo: repositoryName
}, function (err, hooks) {
deleteAllHooks(hooks, done);
});
});
});
test('site create', function(done) {
var siteName = 'cliuttestsite1';
// Create site
var cmd = ('node cli.js site create ' + siteName + ' --json --location').split(' ');
cmd.push('West US');
capture(function() {
cli.parse(cmd);
}, function (result) {
result.text.should.equal('');
result.exitStatus.should.equal(0);
// List sites
cmd = 'node cli.js site list --json'.split(' ');
capture(function() {
cli.parse(cmd);
}, function (result) {
var siteList = JSON.parse(result.text);
var siteExists = siteList.some(function (site) {
return site.Name.toLowerCase() === siteName.toLowerCase()
});
siteExists.should.be.ok;
// Delete created site
cmd = ('node cli.js site delete ' + siteName + ' --json --quiet').split(' ');
capture(function() {
cli.parse(cmd);
}, function (result) {
result.text.should.equal('');
result.exitStatus.should.equal(0);
// List sites
cmd = 'node cli.js site list --json'.split(' ');
capture(function() {
cli.parse(cmd);
}, function (result) {
siteList = JSON.parse(result.text);
siteExists = siteList.some(function (site) {
return site.Name.toLowerCase() === siteName.toLowerCase()
});
siteExists.should.not.be.ok;
done();
});
});
});
});
});
test('site create github', function(done) {
var siteName = 'cliuttestsite2';
// Create site
var cmd = ('node cli.js site create ' + siteName + ' --github --json --location').split(' ');
cmd.push('East US');
cmd.push('--githubusername');
cmd.push(githubUsername);
cmd.push('--githubpassword');
cmd.push(githubPassword);
cmd.push('--githubrepository');
cmd.push(githubRepositoryFullName);
capture(function() {
cli.parse(cmd);
}, function (result) {
result.text.should.equal('');
result.exitStatus.should.equal(0);
// List sites
cmd = 'node cli.js site list --json'.split(' ');
capture(function() {
cli.parse(cmd);
}, function (result) {
var siteList = JSON.parse(result.text);
var siteExists = siteList.some(function (site) {
return site.Name.toLowerCase() === siteName.toLowerCase()
});
siteExists.should.be.ok;
// verify that the hook is in github
githubClient.repos.getFromUser({ user: githubUsername }, function (err, repositories) {
var repository = LinkedRevisionControlClient._getRepository(repositories, githubRepositoryFullName);
githubClient.repos.getHooks({
user: githubUsername,
repo: repository.name
}, function (err, hooks) {
var hookExists = hooks.some(function (hook) {
var parsedUrl = url.parse(hook.config.url);
return parsedUrl.hostname === (siteName + '.scm.azurewebsites.net');
});
hookExists.should.be.ok;
// Delete created site
cmd = ('node cli.js site delete ' + siteName + ' --json --quiet').split(' ');
capture(function() {
cli.parse(cmd);
}, function (result) {
result.text.should.equal('');
result.exitStatus.should.equal(0);
// List sites
cmd = 'node cli.js site list --json'.split(' ');
capture(function() {
cli.parse(cmd);
}, function (result) {
siteList = JSON.parse(result.text);
siteExists = siteList.some(function (site) {
return site.Name.toLowerCase() === siteName.toLowerCase()
});
siteExists.should.not.be.ok;
done();
});
});
});
});
});
});
});
test('site create github rerun scenario', function(done) {
var siteName = 'cliuttestsite3';
// Create site
var cmd = ('node cli.js site create ' + siteName + ' --json --location').split(' ');
cmd.push('East US');
capture(function() {
cli.parse(cmd);
}, function (result) {
result.text.should.equal('');
result.exitStatus.should.equal(0);
cmd.push('--github');
cmd.push('--githubusername');
cmd.push(githubUsername);
cmd.push('--githubpassword');
cmd.push(githubPassword);
cmd.push('--githubrepository');
cmd.push(githubRepositoryFullName);
// Rerun to make sure update hook works properly
capture(function() {
cli.parse(cmd);
}, function (result) {
result.text.should.equal('');
result.exitStatus.should.equal(0);
// List sites
cmd = 'node cli.js site list --json'.split(' ');
capture(function() {
cli.parse(cmd);
}, function (result) {
var siteList = JSON.parse(result.text);
var siteExists = siteList.some(function (site) {
return site.Name.toLowerCase() === siteName.toLowerCase()
});
siteExists.should.be.ok;
// verify that the hook is in github
githubClient.repos.getFromUser({ user: githubUsername }, function (err, repositories) {
var repository = LinkedRevisionControlClient._getRepository(repositories, githubRepositoryFullName);
githubClient.repos.getHooks({
user: githubUsername,
repo: repository.name
}, function (err, hooks) {
var hookExists = hooks.some(function (hook) {
var parsedUrl = url.parse(hook.config.url);
return parsedUrl.hostname === (siteName + '.scm.azurewebsites.net');
});
hookExists.should.be.ok;
// Delete created site
cmd = ('node cli.js site delete ' + siteName + ' --json --quiet').split(' ');
capture(function() {
cli.parse(cmd);
}, function (result) {
result.text.should.equal('');
result.exitStatus.should.equal(0);
// List sites
cmd = 'node cli.js site list --json'.split(' ');
capture(function() {
cli.parse(cmd);
}, function (result) {
siteList = JSON.parse(result.text);
siteExists = siteList.some(function (site) {
return site.Name.toLowerCase() === siteName.toLowerCase();
});
siteExists.should.not.be.ok;
done();
});
});
});
});
});
});
});
});
test('site restart running site', function (done) {
var siteName = 'cliuttestsite4';
// Create site for testing
var cmd = util.format('node cli.js site create %s --json --location', siteName).split(' ');
cmd.push('East US');
capture(function () {
cli.parse(cmd);
}, function (result) {
// Restart site, it's created running
cmd = util.format('node cli.js site restart %s', siteName).split(' ');
capture(function () {
cli.parse(cmd);
}, function (result) {
// Verify site stopped and restarted
result.text.should.match(new Regex('Site ' + siteName + ' has been stopped, restarting'));
result.text.should.match(new Regex('Site ' + siteName + ' has been restarted'));
// Delete test site
cmd = util.format('node cli.js site delete %s', siteName).split(' ');
capture(function () {
cli.parse(cmd);
}, function (result) {
done();
});
});
});
});
test('site restart stopped site', function (done) {
var siteName = 'cliuttestsite4';
// Create site for testing
var cmd = util.format('node cli.js site create %s --json --location', siteName).split(' ');
cmd.push('East US');
capture(function () {
cli.parse(cmd);
}, function (result) {
// Stop the site
cmd = util.format('node cli.js site stop %s', siteName).split(' ');
capture(function () {
cli.parse(cmd);
}, function () {
// Restart site
cmd = util.format('node cli.js site restart %s', siteName).split(' ');
capture(function () {
cli.parse(cmd);
}, function (result) {
// Verify site stopped and restarted
result.text.should.match(new Regex('Site ' + siteName + ' has been stopped, restarting'));
result.text.should.match(new Regex('Site ' + siteName + ' has been restarted'));
// Delete test site
cmd = util.format('node cli.js site delete %s', siteName).split(' ');
capture(function () {
cli.parse(cmd);
}, function (result) {
done();
});
});
});
});
});
});
});