2021-12-28 15:52:15 +00:00
|
|
|
/* eslint-disable ember/no-string-prototype-extensions */
|
2020-06-15 07:10:36 +00:00
|
|
|
import { run } from '@ember/runloop';
|
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
|
|
|
import { find, render, settled } from '@ember/test-helpers';
|
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
|
|
|
import Pretender from 'pretender';
|
|
|
|
import sinon from 'sinon';
|
2020-08-25 15:56:02 +00:00
|
|
|
import { logEncode } from '../../../mirage/data/logs';
|
2021-12-28 16:08:12 +00:00
|
|
|
import {
|
|
|
|
selectOpen,
|
|
|
|
selectOpenChoose,
|
|
|
|
} from '../../utils/ember-power-select-extensions';
|
2020-08-25 15:56:02 +00:00
|
|
|
import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
|
2020-06-15 07:10:36 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
module('Integration | Component | agent-monitor', function (hooks) {
|
2020-06-15 07:10:36 +00:00
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
|
|
|
const LOG_MESSAGE = 'log message goes here';
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
hooks.beforeEach(function () {
|
2020-06-15 07:10:36 +00:00
|
|
|
// Normally this would be called server, but server is a prop of this component.
|
2021-12-28 14:45:20 +00:00
|
|
|
this.pretender = new Pretender(function () {
|
2020-06-15 07:10:36 +00:00
|
|
|
this.get('/v1/regions', () => [200, {}, '[]']);
|
|
|
|
this.get('/v1/agent/monitor', ({ queryParams }) => [
|
|
|
|
200,
|
|
|
|
{},
|
2021-12-28 16:08:12 +00:00
|
|
|
logEncode(
|
|
|
|
[
|
|
|
|
`[${(
|
|
|
|
queryParams.log_level || 'info'
|
|
|
|
).toUpperCase()}] ${LOG_MESSAGE}\n`,
|
|
|
|
],
|
|
|
|
0
|
|
|
|
),
|
2020-06-15 07:10:36 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
hooks.afterEach(function () {
|
2020-06-15 07:10:36 +00:00
|
|
|
this.pretender.shutdown();
|
|
|
|
});
|
|
|
|
|
|
|
|
const INTERVAL = 200;
|
|
|
|
|
|
|
|
const commonTemplate = hbs`
|
|
|
|
<AgentMonitor
|
2021-01-28 22:29:08 +00:00
|
|
|
@level={{this.level}}
|
|
|
|
@client={{this.client}}
|
|
|
|
@server={{this.server}}
|
|
|
|
@onLevelChange={{this.onLevelChange}} />
|
2020-06-15 07:10:36 +00:00
|
|
|
`;
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('basic appearance', async function (assert) {
|
2020-06-15 07:10:36 +00:00
|
|
|
this.setProperties({
|
|
|
|
level: 'info',
|
|
|
|
client: { id: 'client1' },
|
|
|
|
});
|
|
|
|
|
|
|
|
run.later(run, run.cancelTimers, INTERVAL);
|
|
|
|
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
2021-03-26 13:55:12 +00:00
|
|
|
assert.ok(find('[data-test-level-switcher-parent]'));
|
2020-06-15 07:10:36 +00:00
|
|
|
assert.ok(find('[data-test-toggle]'));
|
|
|
|
assert.ok(find('[data-test-log-box]'));
|
|
|
|
assert.ok(find('[data-test-log-box].is-full-bleed.is-dark'));
|
2020-08-25 15:56:02 +00:00
|
|
|
|
|
|
|
await componentA11yAudit(this.element, assert);
|
2020-06-15 07:10:36 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('when provided with a client, AgentMonitor streams logs for the client', async function (assert) {
|
2020-06-15 07:10:36 +00:00
|
|
|
this.setProperties({
|
|
|
|
level: 'info',
|
2021-01-28 22:29:08 +00:00
|
|
|
client: { id: 'client1', region: 'us-west-1' },
|
2020-06-15 07:10:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
run.later(run, run.cancelTimers, INTERVAL);
|
|
|
|
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
|
|
|
const logRequest = this.pretender.handledRequests[1];
|
|
|
|
assert.ok(logRequest.url.startsWith('/v1/agent/monitor'));
|
|
|
|
assert.ok(logRequest.url.includes('client_id=client1'));
|
|
|
|
assert.ok(logRequest.url.includes('log_level=info'));
|
|
|
|
assert.notOk(logRequest.url.includes('server_id'));
|
2021-01-28 22:29:08 +00:00
|
|
|
assert.notOk(logRequest.url.includes('region='));
|
2020-06-15 07:10:36 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('when provided with a server, AgentMonitor streams logs for the server', async function (assert) {
|
2020-06-15 07:10:36 +00:00
|
|
|
this.setProperties({
|
|
|
|
level: 'warn',
|
2021-01-28 22:29:08 +00:00
|
|
|
server: { id: 'server1', region: 'us-west-1' },
|
2020-06-15 07:10:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
run.later(run, run.cancelTimers, INTERVAL);
|
|
|
|
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
|
|
|
const logRequest = this.pretender.handledRequests[1];
|
|
|
|
assert.ok(logRequest.url.startsWith('/v1/agent/monitor'));
|
|
|
|
assert.ok(logRequest.url.includes('server_id=server1'));
|
|
|
|
assert.ok(logRequest.url.includes('log_level=warn'));
|
2021-01-28 22:29:08 +00:00
|
|
|
assert.ok(logRequest.url.includes('region=us-west-1'));
|
2020-06-15 07:10:36 +00:00
|
|
|
assert.notOk(logRequest.url.includes('client_id'));
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('switching levels calls onLevelChange and restarts the logger', async function (assert) {
|
2020-06-15 07:10:36 +00:00
|
|
|
const onLevelChange = sinon.spy();
|
|
|
|
const newLevel = 'trace';
|
|
|
|
|
|
|
|
this.setProperties({
|
|
|
|
level: 'info',
|
|
|
|
client: { id: 'client1' },
|
|
|
|
onLevelChange,
|
|
|
|
});
|
|
|
|
|
|
|
|
run.later(run, run.cancelTimers, INTERVAL);
|
|
|
|
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
2021-03-26 13:55:12 +00:00
|
|
|
const contentId = await selectOpen('[data-test-level-switcher-parent]');
|
2020-06-15 07:10:36 +00:00
|
|
|
run.later(run, run.cancelTimers, INTERVAL);
|
|
|
|
await selectOpenChoose(contentId, newLevel.capitalize());
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
assert.ok(onLevelChange.calledOnce);
|
|
|
|
assert.ok(onLevelChange.calledWith(newLevel));
|
|
|
|
|
|
|
|
const secondLogRequest = this.pretender.handledRequests[2];
|
|
|
|
assert.ok(secondLogRequest.url.includes(`log_level=${newLevel}`));
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('when switching levels, the scrollback is preserved and annotated with a switch message', async function (assert) {
|
2020-06-15 07:10:36 +00:00
|
|
|
const newLevel = 'trace';
|
|
|
|
const onLevelChange = sinon.spy();
|
|
|
|
|
|
|
|
this.setProperties({
|
|
|
|
level: 'info',
|
|
|
|
client: { id: 'client1' },
|
|
|
|
onLevelChange,
|
|
|
|
});
|
|
|
|
|
|
|
|
run.later(run, run.cancelTimers, INTERVAL);
|
|
|
|
|
|
|
|
await render(commonTemplate);
|
2021-12-28 14:45:20 +00:00
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.equal(
|
|
|
|
find('[data-test-log-cli]').textContent,
|
|
|
|
`[INFO] ${LOG_MESSAGE}\n`
|
|
|
|
);
|
2020-06-15 07:10:36 +00:00
|
|
|
|
2021-03-26 13:55:12 +00:00
|
|
|
const contentId = await selectOpen('[data-test-level-switcher-parent]');
|
2020-06-15 07:10:36 +00:00
|
|
|
run.later(run, run.cancelTimers, INTERVAL);
|
|
|
|
await selectOpenChoose(contentId, newLevel.capitalize());
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
find('[data-test-log-cli]').textContent,
|
|
|
|
`[INFO] ${LOG_MESSAGE}\n\n...changing log level to ${newLevel}...\n\n[TRACE] ${LOG_MESSAGE}\n`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('when switching levels and there is no scrollback, there is no appended switch message', async function (assert) {
|
2020-06-15 07:10:36 +00:00
|
|
|
const newLevel = 'trace';
|
|
|
|
const onLevelChange = sinon.spy();
|
|
|
|
|
|
|
|
// Emit nothing for the first request
|
|
|
|
this.pretender.get('/v1/agent/monitor', ({ queryParams }) => [
|
|
|
|
200,
|
|
|
|
{},
|
|
|
|
queryParams.log_level === 'info'
|
|
|
|
? logEncode([''], 0)
|
2021-12-28 16:08:12 +00:00
|
|
|
: logEncode(
|
|
|
|
[
|
|
|
|
`[${(
|
|
|
|
queryParams.log_level || 'info'
|
|
|
|
).toUpperCase()}] ${LOG_MESSAGE}\n`,
|
|
|
|
],
|
|
|
|
0
|
|
|
|
),
|
2020-06-15 07:10:36 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
this.setProperties({
|
|
|
|
level: 'info',
|
|
|
|
client: { id: 'client1' },
|
|
|
|
onLevelChange,
|
|
|
|
});
|
|
|
|
|
|
|
|
run.later(run, run.cancelTimers, INTERVAL);
|
|
|
|
|
|
|
|
await render(commonTemplate);
|
2021-12-28 14:45:20 +00:00
|
|
|
|
2020-06-15 07:10:36 +00:00
|
|
|
assert.equal(find('[data-test-log-cli]').textContent, '');
|
|
|
|
|
2021-03-26 13:55:12 +00:00
|
|
|
const contentId = await selectOpen('[data-test-level-switcher-parent]');
|
2020-06-15 07:10:36 +00:00
|
|
|
run.later(run, run.cancelTimers, INTERVAL);
|
|
|
|
await selectOpenChoose(contentId, newLevel.capitalize());
|
|
|
|
await settled();
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.equal(
|
|
|
|
find('[data-test-log-cli]').textContent,
|
|
|
|
`[TRACE] ${LOG_MESSAGE}\n`
|
|
|
|
);
|
2020-06-15 07:10:36 +00:00
|
|
|
});
|
|
|
|
});
|