-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtests.js
More file actions
437 lines (382 loc) · 20.7 KB
/
Copy pathtests.js
File metadata and controls
437 lines (382 loc) · 20.7 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
'use strict';
const ava = require('ava');
const fs = require('fs');
require('../../scope/h1');
const tests = require('../../lib/tests');
const ssh = require('../../lib/ssh');
const commonCreateParams = '--type website --image h1cr.io/website/php-apache:7.2';
ava.serial('website life cycle', tests.resourceLifeCycle('website', {
createParams: `--name ${tests.getName('website-life-cycle')} ${commonCreateParams} `,
stateCreated: 'Running',
}));
const putFileWebsite = (website, auth, path, content) => {
console.log(new Date(), `Upload content to website ${website.id} at '${path}'`);
return ssh.putFile(path, content, Object.assign({
host: website.fqdn,
username: website.id,
}, auth));
};
const mkDirWebsite = (website, auth, path) => {
console.log(new Date(), `Make directory in website ${website.id} at '${path}'`);
return ssh.mkDir(path, Object.assign({
host: website.fqdn,
username: website.id,
}, auth));
};
const rmFileWebsite = (website, auth, path) => {
console.log(new Date(), `Remove file of website ${website.id} at '${path}'`);
return ssh.rmFile(path, Object.assign({
host: website.fqdn,
username: website.id,
}, auth));
};
const lsWebsite = (website, auth, path) => {
console.log(new Date(), `List files of website ${website.id} at '${path}'`);
return ssh.lsFile(path, Object.assign({
host: website.fqdn,
username: website.id,
}, auth));
};
ava.serial('website empty page results', async t => {
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams}`);
// TODO: Validate default page according scope
await tests.delay(tests.DELAY.website_start);
const resp = await tests.get(`http://${website.fqdn}/`).ok(res => [403, 200].includes(res.status));
t.true(resp.text.includes("You don't have permission to access"));
await tests.remove('website', website);
});
ava.serial('website put index via SFTP & password', async t => {
const password = await tests.getToken();
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams} --password ${password}`);
await tests.delay(tests.DELAY.website_start);
// Upload file
const token = await tests.getToken();
await putFileWebsite(website, { password }, 'public/index.html', token);
const resp = await tests.get(`http://${website.fqdn}/`);
t.true(resp.text === token);
await tests.remove('website', website);
});
ava.serial('website management domain', async t => {
const rset_cname = 'website-cname';
const rset_txt = 'website-txt';
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams}`);
await tests.run(`website stop --website ${website.id}`);
const zone = await tests.run(`dns zone show --zone ${tests.test_zone}`);
try {
const rrset_txt = await tests.run(`dns record-set txt upsert --name ${rset_txt} --zone ${zone.id} --value ${website.fqdn} --ttl 1`);
const rrset_cname = await tests.run(`dns record-set cname upsert --name ${rset_cname} --zone ${zone.id} --value '${website.fqdn}' --ttl 1`);
await tests.delay(tests.DELAY.dns_propagate);
const txt_response = (await tests.dnsResolve(rrset_txt.name, 'TXT')).flat(2);
t.true(txt_response.includes(website.fqdn));
const cname_response = await tests.dnsResolve(rrset_cname.name, 'CNAME');
t.true(cname_response.includes(website.fqdn));
await tests.run(`website domain add --website ${website.id} --domain ${rrset_txt.name}`);
let domains = await tests.run(`website domain list --website ${website.id}`);
t.true(domains.includes(`${rrset_txt.name}`));
t.true(!domains.includes(`${rrset_cname.name}`));
t.true(domains.length === 1);
await tests.run(`website domain add --website ${website.id} --domain ${rrset_cname.name}`);
domains = await tests.run(`website domain list --website ${website.id}`);
t.true(domains.includes(`${rrset_txt.name}`));
t.true(domains.includes(`${rrset_cname.name}`));
t.true(domains.length === 2);
await tests.run(`website domain delete --website ${website.id} --domain ${rset_txt}.${zone.dnsName}`);
domains = await tests.run(`website domain list --website ${website.id}`);
t.true(!domains.includes(`${rrset_txt.name}`));
t.true(domains.includes(`${rrset_cname.name}`));
t.true(domains.length === 1);
} finally {
await tests.remove('website', website);
}
});
ava.serial('website reachable through custom domain', async t => {
const rset = 'website-reachable';
const password = await tests.getToken();
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams} --password ${password}`);
await tests.run(`website stop --website ${website.id}`);
const zone = await tests.run(`dns zone show --zone ${tests.test_zone}`);
try {
const rrset = await tests.run(`dns record-set cname upsert --name ${rset} --zone ${zone.id} --value ${website.fqdn}. --ttl 1`);
await tests.delay(tests.DELAY.dns_propagate);
const host = rrset.name.slice(0, rrset.name.length - 1);
const dns_response = await tests.dnsResolve(rrset.name, 'CNAME');
t.true(dns_response.includes(website.fqdn));
await tests.run(`website domain add --website ${website.id} --domain ${host}`);
await tests.run(`website start --website ${website.id}`);
await tests.delay(tests.DELAY.website_start);
// Upload file
const token = await tests.getToken();
const files = await lsWebsite(website, { password }, '/data');
t.true(files.some(f => f.filename === 'public'));
await ssh.execResource(website, { password }, 'ls -lah /data/public');
await putFileWebsite(website, { password }, 'public/index.html', token);
await tests.delay(tests.DELAY.website_start);
// Test content
for (const proto of ['http', 'https']) {
const resp = await tests.get(`${proto}://${rrset.name.slice(0, rrset.name.length - 1)}/`);
t.true(resp.text === token);
}
} finally {
await tests.remove('website', website);
}
});
ava.serial('website reachable through apex record', async t => {
const password = await tests.getToken();
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams} --password ${password}`);
await tests.run(`website stop --website ${website.id}`);
const zone = await tests.run(`dns zone show --zone ${tests.test_zone}`);
try {
const rrset = await tests.run(`dns record-set cname upsert --name '@' --zone ${zone.id} --value ${website.fqdn}. --ttl 1`);
await tests.run(`dns record-set txt upsert --name '@' --zone ${zone.id} --value ${website.fqdn} --ttl 1`);
await tests.delay(tests.DELAY.dns_propagate);
const host = rrset.name.slice(0, rrset.name.length - 1);
const dns_response = await tests.dnsResolve(rrset.name, 'TXT');
t.true(dns_response.includes(website.fqdn));
await tests.run(`website domain add --website ${website.id} --domain ${host}`);
await tests.run(`website start --website ${website.id}`);
// Upload file
const token = await tests.getToken();
const files = await lsWebsite(website, { password }, '/data');
t.true(files.some(f => f.filename === 'public'));
await ssh.execResource(website, { password }, 'ls -lah /data/public');
await putFileWebsite(website, { password }, 'public/index.html', token);
await tests.delay(5 * 1000);
// Test content
for (const proto of ['http', 'https']) {
const resp = await tests.get(`${proto}://${rrset.name.slice(0, rrset.name.length - 1)}/`);
t.true(resp.text === token);
}
} finally {
await tests.remove('website', website);
}
});
['h1cr.io/website/nginx-static:latest', 'h1cr.io/website/php-apache:7.2', 'h1cr.io/website/php-apache:5.6'].forEach(image => {
ava.serial(`website reachable index.html - ${image}`, async t => {
const sshKeyPair = await ssh.generateKey();
const sshFilename = tests.getRandomFile(sshKeyPair.publicKey);
const website = await tests.run(`website create --name ${tests.getName(t.title)} --type website --image '${image}' --ssh-file ${sshFilename}`);
// Upload file
const token = await tests.getToken();
await putFileWebsite(website, { privateKey: sshKeyPair.privateKey }, 'public/index.html', token);
// Test content
const resp = await tests.get(`http://${website.fqdn}/`);
t.true(resp.text === token);
await tests.remove('website', website);
});
});
ava.serial('website credential cert life cycle', async t => {
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams}`);
await tests.credentialsLifeCycle('website credential cert', {
showParams: `--website ${website.id}`,
createParams: `--website ${website.id}`,
listParams: `--website ${website.id}`,
deleteParams: `--website ${website.id}`,
renameParams: `--website ${website.id}`,
})(t);
await tests.remove('website', website);
});
ava.serial('website credential password life cycle', async t => {
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams}`);
await tests.passwordLifeCycle(t, 'website', website);
await tests.remove('website', website);
});
ava.serial('website stop & start', async t => {
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams}`);
await tests.run(`website stop --website ${website.id}`);
const stopped_website = await tests.run(`website show --website ${website.id}`);
t.true(stopped_website.state === 'Off');
await tests.run(`website start --website ${website.id}`);
const started_website = await tests.run(`website show --website ${website.id}`);
t.true(started_website.state === 'Running');
await tests.remove('website', website);
});
const languages = {
php: '<?php error_reporting(E_ALL); file_put_contents("/data/public/test.txt", "TOKEN"); ?>',
node: `const rand = Math.random();
require('http').createServer(
(req, res) => res.end(rand.toString())
).listen(process.env.PORT);`,
// Warning: Python is tab-sensitive
python: `
import time
start=str(time.time());
def application(environ, start_response):
start_response('200 OK', [('Content-type', 'text/plain')])
return [start]
`.trim(),
};
const images = {
'h1cr.io/website/php-apache:7.2': {
code: languages.php,
},
'h1cr.io/website/php-apache:5.6': {
code: languages.php,
},
'h1cr.io/website/node:10': {
code: languages.node,
},
'h1cr.io/website/node:12': {
code: languages.node,
},
'h1cr.io/website/python-passenger:3': {
code: languages.python,
},
'h1cr.io/website/python-passenger:3.7': {
code: languages.python,
},
};
ava.serial('website runtime access rights match of sftp', async t => {
const image = 'h1cr.io/website/php-apache:7.2';
const password = await tests.getToken();
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams} --password ${password}`);
await tests.delay(tests.DELAY.website_start);
// Put code on website
const token = await tests.getToken();
const content = images[image].code.replace('TOKEN', token);
await putFileWebsite(website, { password }, 'public/test.php', content);
// Call script
const resp_call = await tests.get(`http://${website.fqdn}/test.php`);
t.true(resp_call.text === '');
// Verify content
const resp_test = await tests.get(`http://${website.fqdn}/test.txt`);
t.true(resp_test.text === token);
// Verify permission to remove
await rmFileWebsite(website, { password }, 'public/test.txt');
await tests.remove('website', website);
});
ava.todo('website sftp');
ava.todo('website ssh');
ava.serial('website connect via ssh', async t => {
const password = await tests.getToken();
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams} --password ${password}`);
try {
const hostname = await ssh.execResource(website, { password }, 'hostname');
t.true(hostname.trim() === website.id);
} finally {
await tests.remove('website', website);
}
});
ava.serial('website snapshot management', async t => {
const password = await tests.getToken();
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams} --password ${password}`);
try {
const snapshotName = 'my-snapshot';
await tests.run(`website snapshot create --website ${website.name} --name ${snapshotName}`);
const snapshot = await tests.run(`website snapshot show --website ${website.name} --snapshot ${snapshotName}`);
t.true(snapshot.id === snapshotName);
let snapshots = await tests.run(`website snapshot list --website ${website.name}`);
t.true(snapshots.some(x => x.id === snapshotName));
await tests.run(`website snapshot delete --yes --website ${website.name} --snapshot ${snapshotName}`);
t.true(snapshots.some(x => x.id === snapshotName));
snapshots = await tests.run(`website snapshot list --website ${website.name}`);
t.true(!snapshots.some(x => x.id === snapshotName));
} finally {
await tests.remove('website', website);
}
});
ava.serial('website snapshot restore', async t => {
const password = await tests.getToken();
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams} --password ${password}`);
const content = await tests.getToken();
await putFileWebsite(website, { password }, 'public/test.txt', content);
const snapshotName = tests.getName('snapshot', t.title);
await tests.run(`website snapshot create --website ${website.name} --name ${snapshotName}`);
const websiteRestored = await tests.run(`website create --name ${tests.getName('restored', t.title)} ${commonCreateParams} --password ${password} --source-website ${website.id} --source-snapshot ${snapshotName}`);
await tests.run(`website snapshot delete --yes --website ${website.name} --snapshot ${snapshotName}`);
const contentRestored = await ssh.execResource(websiteRestored, { password }, 'cat public/test.txt');
t.true(contentRestored.trim() === content);
await tests.remove('website', website); // remove source of snapshot first
await tests.remove('website', websiteRestored);
});
ava.serial('website snapshot access via .zfs', async t => {
const password = await tests.getToken();
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams} --password ${password}`);
const content = await tests.getToken();
await putFileWebsite(website, { password }, 'public/test.txt', content);
const snapshotName = tests.getName('snapshot', t.title);
await tests.run(`website snapshot create --website ${website.name} --name ${snapshotName}`);
const contentRestored = await ssh.execResource(website, { password }, `cat /data/.zfs/snapshot/${snapshotName}/public/test.txt`);
t.true(contentRestored.trim() === content);
await tests.run(`website snapshot delete --yes --website ${website.name} --snapshot ${snapshotName}`);
await tests.remove('website', website);
});
ava.serial('website log', async t => {
const password = await tests.getToken();
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams} --password ${password}`);
try {
await putFileWebsite(website, { password }, 'public/test.txt', await tests.getToken());
await tests.logStreamProcess(t, 'website', website,
(id_request) => tests.get(`http://${website.fqdn}/test.txt?${id_request}`)
);
} finally {
await tests.remove('website', website);
}
});
ava.serial('website snapshot download', async t => {
const password = await tests.getToken();
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams} --password ${password}`);
const content = await tests.getToken();
await putFileWebsite(website, { password }, 'public/test.txt', content);
const snapshot = await tests.run(`website snapshot create --website ${website.id} --name ${tests.getName('snapshot', t.title)}`);
const content_append = await tests.getToken();
await putFileWebsite(website, { password }, 'public/append.txt', content_append);
const second_snapshot = await tests.run(`website snapshot create --website ${website.id} --name ${tests.getName('diff', t.title)}`);
const output_file = await tests.getRandomFile();
await tests.run(`website snapshot download --website ${website.name} --snapshot ${snapshot.id} --destination-file ${output_file}`);
const output_diff_file = await tests.getRandomFile();
await tests.run(`website snapshot download --website ${website.name} --snapshot ${second_snapshot.id} --difference ${snapshot.id} --destination-file ${output_diff_file}`);
await tests.run(`website snapshot delete --yes --website ${website.name} --snapshot ${snapshot.id}`);
await tests.run(`website snapshot delete --yes --website ${website.name} --snapshot ${second_snapshot.id}`);
t.true(fs.readFileSync(output_file).length > 0);
t.true(fs.readFileSync(output_diff_file).length > 0);
await tests.remove('website', website);
});
ava.serial('website snapshot receive to zfs', async t => {
const zfs_list = await tests.runProcess('zfs list');
t.true(zfs_list.includes('h1_cli_test'), 'Missing dataset. Run: zpool create h1_cli_test /dev/sdb');
await tests.runProcess('zfs destroy -r h1_cli_test');
const dataset = await tests.getName(t.title);
const password = await tests.getToken();
const website = await tests.run(`website create --name ${tests.getName(t.title)} ${commonCreateParams} --password ${password}`);
const content = await tests.getToken();
await putFileWebsite(website, { password }, 'public/test.txt', content);
const snapshot = await tests.run(`website snapshot create --website ${website.id} --name ${tests.getName('snapshot', t.title)}`);
const output_file = await tests.getRandomFile();
await tests.run(`website snapshot download --website ${website.name} --snapshot ${snapshot.id} --destination-file ${output_file}`);
await tests.runProcess(`sh -c "cat ${output_file} | zfs receive -F h1_cli_test/${dataset}"`);
const readed = fs.readFileSync(`/h1_cli_test/${dataset}/public/test.txt`);
t.true(readed.includes(content));
await tests.runProcess('zfs destroy -r h1_cli_test');
});
ava.serial('website restart nodejs app', async t => {
const password = await tests.getToken();
const image = 'h1cr.io/website/node:12';
const website = await tests.run(`website create --name ${tests.getName(t.title)} --type website --image '${image}' --password ${password}`);
const content = images[image].code;
await mkDirWebsite(website, { password}, 'app');
await putFileWebsite(website, { password }, 'app/index.js', content);
await tests.run(`website restart --website ${website.id}`);
await tests.delay(tests.DELAY.website_start);
const response_before = await tests.get(`http://${website.fqdn}/`);
await tests.run(`website restart --website ${website.id}`);
await tests.delay(tests.DELAY.website_start);
const response_after = await tests.get(`http://${website.fqdn}/`);
t.true(response_after.body != response_before.body);
await tests.remove('website', website);
});
ava.serial('website serve python app', async t => {
const password = await tests.getToken();
const image = 'h1cr.io/website/python-passenger:3.7';
const website = await tests.run(`website create --name ${tests.getName(t.title)} --type website --image '${image}' --password ${password}`);
const content = images[image].code;
await mkDirWebsite(website, { password}, 'app');
await putFileWebsite(website, { password }, 'app/passenger_wsgi.py', content);
await tests.run(`website restart --website ${website.id}`);
await tests.delay(tests.DELAY.website_start);
const response_before = await tests.get(`http://${website.fqdn}/`);
await tests.run(`website restart --website ${website.id}`);
await tests.delay(tests.DELAY.website_start);
const response_after = await tests.get(`http://${website.fqdn}/`);
t.true(response_after.body != response_before.body);
await tests.remove('website', website);
});