2020-03-24 23:22:16 +00:00
|
|
|
import ExecSocketXtermAdapter from 'nomad-ui/utils/classes/exec-socket-xterm-adapter';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { render, settled } from '@ember/test-helpers';
|
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
2020-05-26 14:56:25 +00:00
|
|
|
import { Terminal } from 'xterm';
|
2020-08-28 15:13:33 +00:00
|
|
|
import { HEARTBEAT_INTERVAL } from 'nomad-ui/utils/classes/exec-socket-xterm-adapter';
|
|
|
|
import sinon from 'sinon';
|
2020-03-24 23:22:16 +00:00
|
|
|
|
|
|
|
module('Integration | Utility | exec-socket-xterm-adapter', function(hooks) {
|
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
2020-04-03 15:22:22 +00:00
|
|
|
test('initiating socket sends authentication handshake', async function(assert) {
|
|
|
|
let done = assert.async();
|
|
|
|
|
|
|
|
let terminal = new Terminal();
|
|
|
|
this.set('terminal', terminal);
|
|
|
|
|
|
|
|
await render(hbs`
|
2020-07-09 17:30:11 +00:00
|
|
|
<ExecTerminal @terminal={{terminal}} />
|
2020-04-03 15:22:22 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
let firstMessage = true;
|
|
|
|
let mockSocket = new Object({
|
|
|
|
send(message) {
|
|
|
|
if (firstMessage) {
|
|
|
|
firstMessage = false;
|
|
|
|
assert.deepEqual(message, JSON.stringify({ version: 1, auth_token: 'mysecrettoken' }));
|
2020-08-28 15:13:33 +00:00
|
|
|
mockSocket.onclose();
|
2020-04-03 15:22:22 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
new ExecSocketXtermAdapter(terminal, mockSocket, 'mysecrettoken');
|
|
|
|
|
|
|
|
mockSocket.onopen();
|
|
|
|
|
|
|
|
await settled();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('initiating socket sends authentication handshake even if unauthenticated', async function(assert) {
|
|
|
|
let done = assert.async();
|
|
|
|
|
|
|
|
let terminal = new Terminal();
|
|
|
|
this.set('terminal', terminal);
|
|
|
|
|
|
|
|
await render(hbs`
|
2020-07-09 17:30:11 +00:00
|
|
|
<ExecTerminal @terminal={{terminal}} />
|
2020-04-03 15:22:22 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
let firstMessage = true;
|
|
|
|
let mockSocket = new Object({
|
|
|
|
send(message) {
|
|
|
|
if (firstMessage) {
|
|
|
|
firstMessage = false;
|
|
|
|
assert.deepEqual(message, JSON.stringify({ version: 1, auth_token: '' }));
|
2020-08-28 15:13:33 +00:00
|
|
|
mockSocket.onclose();
|
2020-04-03 15:22:22 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
new ExecSocketXtermAdapter(terminal, mockSocket, null);
|
|
|
|
|
|
|
|
mockSocket.onopen();
|
|
|
|
|
|
|
|
await settled();
|
|
|
|
});
|
|
|
|
|
2020-08-28 15:13:33 +00:00
|
|
|
test('a heartbeat is sent periodically', async function(assert) {
|
|
|
|
let done = assert.async();
|
|
|
|
|
|
|
|
const clock = sinon.useFakeTimers({
|
|
|
|
now: new Date(),
|
|
|
|
shouldAdvanceTime: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
let terminal = new Terminal();
|
|
|
|
this.set('terminal', terminal);
|
|
|
|
|
|
|
|
await render(hbs`
|
|
|
|
<ExecTerminal @terminal={{terminal}} />
|
|
|
|
`);
|
|
|
|
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
let mockSocket = new Object({
|
|
|
|
send(message) {
|
|
|
|
if (!message.includes('version') && !message.includes('tty_size')) {
|
|
|
|
assert.deepEqual(message, JSON.stringify({}));
|
|
|
|
clock.restore();
|
|
|
|
mockSocket.onclose();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
new ExecSocketXtermAdapter(terminal, mockSocket, null);
|
|
|
|
mockSocket.onopen();
|
|
|
|
await settled();
|
|
|
|
clock.tick(HEARTBEAT_INTERVAL);
|
|
|
|
});
|
|
|
|
|
2020-03-24 23:22:16 +00:00
|
|
|
test('resizing the window passes a resize message through the socket', async function(assert) {
|
|
|
|
let done = assert.async();
|
|
|
|
|
|
|
|
let terminal = new Terminal();
|
|
|
|
this.set('terminal', terminal);
|
|
|
|
|
|
|
|
await render(hbs`
|
2020-07-09 17:30:11 +00:00
|
|
|
<ExecTerminal @terminal={{terminal}} />
|
2020-03-24 23:22:16 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
let mockSocket = new Object({
|
|
|
|
send(message) {
|
|
|
|
assert.deepEqual(
|
|
|
|
message,
|
|
|
|
JSON.stringify({ tty_size: { width: terminal.cols, height: terminal.rows } })
|
|
|
|
);
|
2020-08-28 15:13:33 +00:00
|
|
|
mockSocket.onclose();
|
2020-03-24 23:22:16 +00:00
|
|
|
done();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-04-03 15:22:22 +00:00
|
|
|
new ExecSocketXtermAdapter(terminal, mockSocket, '');
|
2020-03-24 23:22:16 +00:00
|
|
|
|
|
|
|
window.dispatchEvent(new Event('resize'));
|
|
|
|
|
|
|
|
await settled();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('stdout frames without data are ignored', async function(assert) {
|
|
|
|
assert.expect(0);
|
|
|
|
|
|
|
|
let terminal = new Terminal();
|
|
|
|
this.set('terminal', terminal);
|
|
|
|
|
|
|
|
await render(hbs`
|
2020-07-09 17:30:11 +00:00
|
|
|
<ExecTerminal @terminal={{terminal}} />
|
2020-03-24 23:22:16 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
let mockSocket = new Object({
|
|
|
|
send() {},
|
|
|
|
});
|
|
|
|
|
2020-04-03 15:22:22 +00:00
|
|
|
new ExecSocketXtermAdapter(terminal, mockSocket, '');
|
2020-03-24 23:22:16 +00:00
|
|
|
|
|
|
|
mockSocket.onmessage({
|
|
|
|
data: '{"stdout":{"exited":"true"}}',
|
|
|
|
});
|
|
|
|
|
|
|
|
await settled();
|
2020-08-28 15:13:33 +00:00
|
|
|
mockSocket.onclose();
|
2020-03-24 23:22:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('stderr frames are ignored', async function(assert) {
|
|
|
|
let terminal = new Terminal();
|
|
|
|
this.set('terminal', terminal);
|
|
|
|
|
|
|
|
await render(hbs`
|
2020-07-09 17:30:11 +00:00
|
|
|
<ExecTerminal @terminal={{terminal}} />
|
2020-03-24 23:22:16 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
let mockSocket = new Object({
|
|
|
|
send() {},
|
|
|
|
});
|
|
|
|
|
2020-04-03 15:22:22 +00:00
|
|
|
new ExecSocketXtermAdapter(terminal, mockSocket, '');
|
2020-03-24 23:22:16 +00:00
|
|
|
|
|
|
|
mockSocket.onmessage({
|
|
|
|
data: '{"stdout":{"data":"c2gtMy4yIPCfpbMk"}}',
|
|
|
|
});
|
|
|
|
|
|
|
|
mockSocket.onmessage({
|
|
|
|
data: '{"stderr":{"data":"c2gtMy4yIPCfpbMk"}}',
|
|
|
|
});
|
|
|
|
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
assert.equal(
|
2020-05-26 14:56:25 +00:00
|
|
|
terminal.buffer.active
|
2020-03-24 23:22:16 +00:00
|
|
|
.getLine(0)
|
|
|
|
.translateToString()
|
|
|
|
.trim(),
|
|
|
|
'sh-3.2 🥳$'
|
|
|
|
);
|
2020-08-28 15:13:33 +00:00
|
|
|
|
|
|
|
mockSocket.onclose();
|
2020-03-24 23:22:16 +00:00
|
|
|
});
|
|
|
|
});
|