-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathexec-test.js
More file actions
641 lines (508 loc) · 19.1 KB
/
exec-test.js
File metadata and controls
641 lines (508 loc) · 19.1 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/**
* Copyright IBM Corp. 2015, 2025
* SPDX-License-Identifier: BUSL-1.1
*/
/* eslint-disable qunit/require-expect */
import { module, skip, test } from 'qunit';
import { currentURL, settled } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
import a11yAudit from 'nomad-ui/tests/helpers/a11y-audit';
import Service from '@ember/service';
import Exec from 'nomad-ui/tests/pages/exec';
import KEYS from 'nomad-ui/utils/keys';
import faker from 'nomad-ui/mirage/faker';
module('Acceptance | exec', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
hooks.beforeEach(async function () {
window.localStorage.clear();
window.sessionStorage.clear();
faker.seed(1);
server.create('agent');
server.create('node-pool');
server.create('node');
this.job = server.create('job', {
groupsCount: 2,
groupAllocCount: 5,
createAllocations: false,
status: 'running',
});
this.job.taskGroups.models.forEach((taskGroup) => {
const alloc = server.create('allocation', {
jobId: this.job.id,
taskGroup: taskGroup.name,
forceRunningClientStatus: true,
});
server.db.taskStates.update(
{ allocationId: alloc.id },
{ state: 'running' }
);
});
});
test('it passes an accessibility audit', async function (assert) {
await Exec.visitJob({ job: this.job.id });
await a11yAudit(assert);
});
test('/exec/:job should show the region, namespace, and job name', async function (assert) {
server.create('namespace');
let namespace = server.create('namespace');
server.create('region', { id: 'global' });
server.create('region', { id: 'region-2' });
this.job = server.create('job', {
createAllocations: false,
namespaceId: namespace.id,
status: 'running',
});
await Exec.visitJob({
job: this.job.id,
namespace: namespace.id,
region: 'region-2',
});
assert.ok(document.title.includes('Exec - region-2'));
assert.equal(Exec.header.region.text, this.job.region);
assert.equal(Exec.header.namespace.text, this.job.namespace);
assert.equal(Exec.header.job, this.job.name);
assert.notOk(Exec.jobDead.isPresent);
});
test('/exec/:job should not show region and namespace when there are none', async function (assert) {
await Exec.visitJob({ job: this.job.id });
assert.ok(Exec.header.region.isHidden);
assert.ok(Exec.header.namespace.isHidden);
});
test('/exec/:job should show the task groups collapsed by default and allow the tasks to be shown', async function (assert) {
const firstTaskGroup = this.job.taskGroups.models.sortBy('name')[0];
await Exec.visitJob({ job: this.job.id });
assert.equal(Exec.taskGroups.length, this.job.taskGroups.length);
assert.equal(Exec.taskGroups[0].name, firstTaskGroup.name);
assert.equal(Exec.taskGroups[0].tasks.length, 0);
assert.ok(Exec.taskGroups[0].chevron.isRight);
assert.notOk(Exec.taskGroups[0].isLoading);
await Exec.taskGroups[0].click();
assert.equal(Exec.taskGroups[0].tasks.length, firstTaskGroup.tasks.length);
assert.notOk(Exec.taskGroups[0].tasks[0].isActive);
assert.ok(Exec.taskGroups[0].chevron.isDown);
await Exec.taskGroups[0].click();
assert.equal(Exec.taskGroups[0].tasks.length, 0);
});
test('/exec/:job should require selecting a task', async function (assert) {
await Exec.visitJob({ job: this.job.id });
assert.equal(
window.execTerminal.buffer.active.getLine(0).translateToString().trim(),
'Select a task to start your session.'
);
});
test('a task group with a pending allocation shows a loading spinner', async function (assert) {
let taskGroup = this.job.taskGroups.models.sortBy('name')[0];
this.server.db.allocations.update(
{ taskGroup: taskGroup.name },
{ clientStatus: 'pending' }
);
await Exec.visitJob({ job: this.job.id });
assert.ok(Exec.taskGroups[0].isLoading);
});
test('a task group with no running task states or pending allocations should not be shown', async function (assert) {
let taskGroup = this.job.taskGroups.models.sortBy('name')[0];
this.server.db.allocations.update(
{ taskGroup: taskGroup.name },
{ clientStatus: 'failed' }
);
await Exec.visitJob({ job: this.job.id });
assert.notEqual(Exec.taskGroups[0].name, taskGroup.name);
});
test('an inactive task should not be shown', async function (assert) {
let notRunningTaskGroup = this.job.taskGroups.models.sortBy('name')[0];
this.server.db.allocations.update(
{ taskGroup: notRunningTaskGroup.name },
{ clientStatus: 'failed' }
);
let runningTaskGroup = this.job.taskGroups.models.sortBy('name')[1];
runningTaskGroup.tasks.models.forEach((task, index) => {
let state = 'running';
if (index > 0) {
state = 'dead';
}
this.server.db.taskStates.update({ name: task.name }, { state });
});
await Exec.visitJob({ job: this.job.id });
await Exec.taskGroups[0].click();
assert.equal(Exec.taskGroups[0].tasks.length, 1);
});
test('a task that becomes active should appear', async function (assert) {
let notRunningTaskGroup = this.job.taskGroups.models.sortBy('name')[0];
this.server.db.allocations.update(
{ taskGroup: notRunningTaskGroup.name },
{ clientStatus: 'failed' }
);
let runningTaskGroup = this.job.taskGroups.models.sortBy('name')[1];
let changingTaskStateName;
runningTaskGroup.tasks.models.sortBy('name').forEach((task, index) => {
let state = 'running';
if (index > 0) {
state = 'dead';
}
this.server.db.taskStates.update({ name: task.name }, { state });
if (index === 1) {
changingTaskStateName = task.name;
}
});
await Exec.visitJob({ job: this.job.id });
await Exec.taskGroups[0].click();
assert.equal(Exec.taskGroups[0].tasks.length, 1);
// Approximate new task arrival via polling by changing a finished task state to be not finished
this.owner
.lookup('service:store')
.peekAll('allocation')
.forEach((allocation) => {
const changingTaskState = allocation.states.findBy(
'name',
changingTaskStateName
);
if (changingTaskState) {
changingTaskState.set('state', 'running');
}
});
await settled();
assert.equal(Exec.taskGroups[0].tasks.length, 2);
assert.equal(Exec.taskGroups[0].tasks[1].name, changingTaskStateName);
});
test('a dead job has an inert window', async function (assert) {
this.job.status = 'dead';
this.job.save();
let taskGroup = this.job.taskGroups.models.sortBy('name')[0];
let task = taskGroup.tasks.models.sortBy('name')[0];
this.server.db.taskStates.update({ finishedAt: new Date() });
await Exec.visitTask({
job: this.job.id,
task_group: taskGroup.name,
task_name: task.name,
});
assert.ok(Exec.jobDead.isPresent);
assert.equal(
Exec.jobDead.message,
`Job ${this.job.name} is dead and cannot host an exec session.`
);
});
test('when a job dies the exec window becomes inert', async function (assert) {
await Exec.visitJob({ job: this.job.id });
// Approximate live-polling job death
this.owner
.lookup('service:store')
.peekAll('job')
.forEach((job) => job.set('status', 'dead'));
await settled();
assert.ok(Exec.jobDead.isPresent);
});
test('visiting a path with a task group should open the group by default', async function (assert) {
let taskGroup = this.job.taskGroups.models.sortBy('name')[0];
await Exec.visitTaskGroup({ job: this.job.id, task_group: taskGroup.name });
assert.equal(Exec.taskGroups[0].tasks.length, taskGroup.tasks.length);
assert.ok(Exec.taskGroups[0].chevron.isDown);
let task = taskGroup.tasks.models.sortBy('name')[0];
await Exec.visitTask({
job: this.job.id,
task_group: taskGroup.name,
task_name: task.name,
});
assert.equal(Exec.taskGroups[0].tasks.length, taskGroup.tasks.length);
assert.ok(Exec.taskGroups[0].chevron.isDown);
});
test('navigating to a task adds its name to the route, chooses an allocation, and assigns a default command', async function (assert) {
await Exec.visitJob({ job: this.job.id, namespace: this.job.namespaceId });
await Exec.taskGroups[0].click();
await Exec.taskGroups[0].tasks[0].click();
let taskGroup = this.job.taskGroups.models.sortBy('name')[0];
let task = taskGroup.tasks.models.sortBy('name')[0];
let taskStates = this.server.db.taskStates.where({
name: task.name,
});
let allocationId = taskStates.find((ts) => ts.allocationId).allocationId;
await settled();
assert.equal(
currentURL(),
`/exec/${this.job.id}/${taskGroup.name}/${task.name}?namespace=${this.job.namespaceId}`
);
assert.ok(Exec.taskGroups[0].tasks[0].isActive);
assert.equal(
window.execTerminal.buffer.active.getLine(2).translateToString().trim(),
'Multiple instances of this task are running. The allocation below was selected by random draw.'
);
assert.equal(
window.execTerminal.buffer.active.getLine(4).translateToString().trim(),
'Customize your command, then hit ‘return’ to run.'
);
assert.equal(
window.execTerminal.buffer.active.getLine(6).translateToString().trim(),
`$ nomad alloc exec -i -t -task ${task.name} ${
allocationId.split('-')[0]
} /bin/bash`
);
const terminalTextRendered = assert.async();
setTimeout(async () => {
terminalTextRendered();
}, 1000);
});
test('an allocation can be specified', async function (assert) {
let taskGroup = this.job.taskGroups.models.sortBy('name')[0];
let task = taskGroup.tasks.models.sortBy('name')[0];
let allocations = this.server.db.allocations.where({
jobId: this.job.id,
taskGroup: taskGroup.name,
});
let allocation = allocations[allocations.length - 1];
this.server.db.taskStates.update(
{ name: task.name },
{ name: 'spaced name!' }
);
task.name = 'spaced name!';
task.save();
await Exec.visitTask({
job: this.job.id,
task_group: taskGroup.name,
task_name: task.name,
allocation: allocation.id.split('-')[0],
namespace: this.job.namespaceId,
});
await settled();
assert.equal(
window.execTerminal.buffer.active.getLine(4).translateToString().trim(),
`$ nomad alloc exec -i -t -task spaced\\ name\\! ${
allocation.id.split('-')[0]
} /bin/bash`
);
});
test('a namespace can be specified', async function (assert) {
server.create('namespace'); // default
let namespace = server.create('namespace', {
id: 'should-show-in-example-string',
});
let job = server.create('job', {
namespaceId: namespace.id,
createAllocations: true,
status: 'running',
});
let taskGroup = job.taskGroups.models.sortBy('name')[0];
let task = taskGroup.tasks.models.sortBy('name')[0];
let allocations = this.server.db.allocations.where({
jobId: job.id,
taskGroup: taskGroup.name,
});
let allocation = allocations[allocations.length - 1];
await Exec.visitTask({
job: job.id,
task_group: taskGroup.name,
task_name: task.name,
allocation: allocation.id.split('-')[0],
namespace: namespace.id,
});
await settled();
assert.equal(
window.execTerminal.buffer.active.getLine(4).translateToString().trim(),
`$ nomad alloc exec -i -t -namespace should-show-in-example-string -task ${
task.name
} ${allocation.id.split('-')[0]} /bin/bash`
);
});
test('running the command opens the socket for reading/writing and detects it closing', async function (assert) {
let mockSocket = new MockSocket();
let mockSockets = Service.extend({
getTaskStateSocket(taskState, command) {
assert.equal(taskState.name, task.name);
assert.equal(taskState.allocation.id, allocation.id);
assert.equal(command, '/bin/bash');
assert.step('Socket built');
return mockSocket;
},
});
this.owner.register('service:sockets', mockSockets);
let taskGroup = this.job.taskGroups.models.sortBy('name')[0];
let task = taskGroup.tasks.models.sortBy('name')[0];
let allocations = this.server.db.allocations.where({
jobId: this.job.id,
taskGroup: taskGroup.name,
});
let allocation = allocations[allocations.length - 1];
await Exec.visitTask({
job: this.job.id,
task_group: taskGroup.name,
task_name: task.name,
allocation: allocation.id.split('-')[0],
});
await settled();
await Exec.terminal.pressEnter();
await settled();
mockSocket.onopen();
assert.verifySteps(['Socket built']);
mockSocket.onmessage({
data: '{"stdout":{"data":"c2gtMy4yIPCfpbMk"}}',
});
await settled();
assert.equal(
window.execTerminal.buffer.active.getLine(5).translateToString().trim(),
'sh-3.2 🥳$'
);
await Exec.terminal.pressEnter();
await settled();
assert.deepEqual(mockSocket.sent, [
'{"version":1,"auth_token":""}',
`{"tty_size":{"width":${window.execTerminal.cols},"height":${window.execTerminal.rows}}}`,
'{"stdin":{"data":"DQ=="}}',
]);
await mockSocket.onclose();
await settled();
assert.equal(
window.execTerminal.buffer.active.getLine(6).translateToString().trim(),
'The connection has closed.'
);
});
test('the opening message includes the token if it exists', async function (assert) {
const { secretId } = server.create('token');
window.localStorage.nomadTokenSecret = secretId;
let mockSocket = new MockSocket();
let mockSockets = Service.extend({
getTaskStateSocket() {
return mockSocket;
},
});
this.owner.register('service:sockets', mockSockets);
let taskGroup = this.job.taskGroups.models[0];
let task = taskGroup.tasks.models[0];
let allocations = this.server.db.allocations.where({
jobId: this.job.id,
taskGroup: taskGroup.name,
});
let allocation = allocations[allocations.length - 1];
await Exec.visitTask({
job: this.job.id,
task_group: taskGroup.name,
task_name: task.name,
allocation: allocation.id.split('-')[0],
});
await Exec.terminal.pressEnter();
await settled();
mockSocket.onopen();
await Exec.terminal.pressEnter();
await settled();
assert.equal(
mockSocket.sent[0],
`{"version":1,"auth_token":"${secretId}"}`
);
});
test('only one socket is opened after switching between tasks', async function (assert) {
let mockSockets = Service.extend({
getTaskStateSocket() {
assert.step('Socket built');
return new MockSocket();
},
});
this.owner.register('service:sockets', mockSockets);
await Exec.visitJob({
job: this.job.id,
});
await settled();
await Exec.taskGroups[0].click();
await Exec.taskGroups[0].tasks[0].click();
await Exec.taskGroups[1].click();
await Exec.taskGroups[1].tasks[0].click();
await Exec.terminal.pressEnter();
assert.verifySteps(['Socket built']);
});
test('the command can be customised', async function (assert) {
let mockSockets = Service.extend({
getTaskStateSocket(taskState, command) {
assert.equal(command, '/sh');
window.localStorage.getItem('nomadExecCommand', JSON.stringify('/sh'));
assert.step('Socket built');
return new MockSocket();
},
});
this.owner.register('service:sockets', mockSockets);
await Exec.visitJob({ job: this.job.id, namespace: this.job.namespaceId });
await Exec.taskGroups[0].click();
await Exec.taskGroups[0].tasks[0].click();
let taskGroup = this.job.taskGroups.models.sortBy('name')[0];
let task = taskGroup.tasks.models.sortBy('name')[0];
let allocation = this.server.db.allocations.findBy({
jobId: this.job.id,
taskGroup: taskGroup.name,
});
await settled();
// Delete /bash
await window.execTerminal.simulateCommandDataEvent(KEYS.DELETE);
await window.execTerminal.simulateCommandDataEvent(KEYS.DELETE);
await window.execTerminal.simulateCommandDataEvent(KEYS.DELETE);
await window.execTerminal.simulateCommandDataEvent(KEYS.DELETE);
await window.execTerminal.simulateCommandDataEvent(KEYS.DELETE);
// Delete /bin and try to go beyond
await window.execTerminal.simulateCommandDataEvent(KEYS.DELETE);
await window.execTerminal.simulateCommandDataEvent(KEYS.DELETE);
await window.execTerminal.simulateCommandDataEvent(KEYS.DELETE);
await window.execTerminal.simulateCommandDataEvent(KEYS.DELETE);
await window.execTerminal.simulateCommandDataEvent(KEYS.DELETE);
await window.execTerminal.simulateCommandDataEvent(KEYS.DELETE);
await window.execTerminal.simulateCommandDataEvent(KEYS.DELETE);
await settled();
assert.equal(
window.execTerminal.buffer.active.getLine(6).translateToString().trim(),
`$ nomad alloc exec -i -t -task ${task.name} ${
allocation.id.split('-')[0]
}`
);
await window.execTerminal.simulateCommandDataEvent('/sh');
await Exec.terminal.pressEnter();
await settled();
assert.verifySteps(['Socket built']);
});
test('a persisted customised command is recalled', async function (assert) {
window.localStorage.setItem('nomadExecCommand', JSON.stringify('/bin/sh'));
let taskGroup = this.job.taskGroups.models[0];
let task = taskGroup.tasks.models[0];
let allocations = this.server.db.allocations.where({
jobId: this.job.id,
taskGroup: taskGroup.name,
});
let allocation = allocations[allocations.length - 1];
await Exec.visitTask({
job: this.job.id,
task_group: taskGroup.name,
task_name: task.name,
allocation: allocation.id.split('-')[0],
namespace: this.job.namespaceId,
});
await settled();
assert.equal(
window.execTerminal.buffer.active.getLine(4).translateToString().trim(),
`$ nomad alloc exec -i -t -task ${task.name} ${
allocation.id.split('-')[0]
} /bin/sh`
);
});
skip('when a task state finishes submitting a command displays an error', async function (assert) {
let taskGroup = this.job.taskGroups.models.sortBy('name')[0];
let task = taskGroup.tasks.models.sortBy('name')[0];
await Exec.visitTask({
job: this.job.id,
task_group: taskGroup.name,
task_name: task.name,
});
// Approximate allocation failure via polling
this.owner
.lookup('service:store')
.peekAll('allocation')
.forEach((allocation) => allocation.set('clientStatus', 'failed'));
await Exec.terminal.pressEnter();
await settled();
assert.equal(
window.execTerminal.buffer.active.getLine(7).translateToString().trim(),
`Failed to open a socket because task ${task.name} is not active.`
);
});
});
class MockSocket {
constructor() {
this.sent = [];
}
send(message) {
this.sent.push(message);
}
}