github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/integration/components/agent-monitor-test.js (about) 1 /* eslint-disable ember/no-string-prototype-extensions */ 2 import { run } from '@ember/runloop'; 3 import { module, test } from 'qunit'; 4 import { setupRenderingTest } from 'ember-qunit'; 5 import { find, render, settled } from '@ember/test-helpers'; 6 import hbs from 'htmlbars-inline-precompile'; 7 import Pretender from 'pretender'; 8 import sinon from 'sinon'; 9 import { logEncode } from '../../../mirage/data/logs'; 10 import { 11 selectOpen, 12 selectOpenChoose, 13 } from '../../utils/ember-power-select-extensions'; 14 import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit'; 15 import { capitalize } from '@ember/string'; 16 17 module('Integration | Component | agent-monitor', function (hooks) { 18 setupRenderingTest(hooks); 19 20 const LOG_MESSAGE = 'log message goes here'; 21 22 hooks.beforeEach(function () { 23 // Normally this would be called server, but server is a prop of this component. 24 this.pretender = new Pretender(function () { 25 this.get('/v1/regions', () => [200, {}, '[]']); 26 this.get('/v1/agent/monitor', ({ queryParams }) => [ 27 200, 28 {}, 29 logEncode( 30 [ 31 `[${( 32 queryParams.log_level || 'info' 33 ).toUpperCase()}] ${LOG_MESSAGE}\n`, 34 ], 35 0 36 ), 37 ]); 38 }); 39 }); 40 41 hooks.afterEach(function () { 42 this.pretender.shutdown(); 43 }); 44 45 const INTERVAL = 200; 46 47 const commonTemplate = hbs` 48 <AgentMonitor 49 @level={{this.level}} 50 @client={{this.client}} 51 @server={{this.server}} 52 @onLevelChange={{this.onLevelChange}} /> 53 `; 54 55 test('basic appearance', async function (assert) { 56 assert.expect(5); 57 58 this.setProperties({ 59 level: 'info', 60 client: { id: 'client1' }, 61 }); 62 63 run.later(run, run.cancelTimers, INTERVAL); 64 65 await render(commonTemplate); 66 67 assert.ok(find('[data-test-level-switcher-parent]')); 68 assert.ok(find('[data-test-toggle]')); 69 assert.ok(find('[data-test-log-box]')); 70 assert.ok(find('[data-test-log-box].is-full-bleed.is-dark')); 71 72 await componentA11yAudit(this.element, assert); 73 }); 74 75 test('when provided with a client, AgentMonitor streams logs for the client', async function (assert) { 76 this.setProperties({ 77 level: 'info', 78 client: { id: 'client1', region: 'us-west-1' }, 79 }); 80 81 run.later(run, run.cancelTimers, INTERVAL); 82 83 await render(commonTemplate); 84 85 const logRequest = this.pretender.handledRequests[1]; 86 assert.ok(logRequest.url.startsWith('/v1/agent/monitor')); 87 assert.ok(logRequest.url.includes('client_id=client1')); 88 assert.ok(logRequest.url.includes('log_level=info')); 89 assert.notOk(logRequest.url.includes('server_id')); 90 assert.notOk(logRequest.url.includes('region=')); 91 }); 92 93 test('when provided with a server, AgentMonitor streams logs for the server', async function (assert) { 94 this.setProperties({ 95 level: 'warn', 96 server: { id: 'server1', region: 'us-west-1' }, 97 }); 98 99 run.later(run, run.cancelTimers, INTERVAL); 100 101 await render(commonTemplate); 102 103 const logRequest = this.pretender.handledRequests[1]; 104 assert.ok(logRequest.url.startsWith('/v1/agent/monitor')); 105 assert.ok(logRequest.url.includes('server_id=server1')); 106 assert.ok(logRequest.url.includes('log_level=warn')); 107 assert.ok(logRequest.url.includes('region=us-west-1')); 108 assert.notOk(logRequest.url.includes('client_id')); 109 }); 110 111 test('switching levels calls onLevelChange and restarts the logger', async function (assert) { 112 const onLevelChange = sinon.spy(); 113 const newLevel = 'trace'; 114 115 this.setProperties({ 116 level: 'info', 117 client: { id: 'client1' }, 118 onLevelChange, 119 }); 120 121 run.later(run, run.cancelTimers, INTERVAL); 122 123 await render(commonTemplate); 124 125 const contentId = await selectOpen('[data-test-level-switcher-parent]'); 126 run.later(run, run.cancelTimers, INTERVAL); 127 await selectOpenChoose(contentId, capitalize(newLevel)); 128 await settled(); 129 130 assert.ok(onLevelChange.calledOnce); 131 assert.ok(onLevelChange.calledWith(newLevel)); 132 133 const secondLogRequest = this.pretender.handledRequests[2]; 134 assert.ok(secondLogRequest.url.includes(`log_level=${newLevel}`)); 135 }); 136 137 test('when switching levels, the scrollback is preserved and annotated with a switch message', async function (assert) { 138 const newLevel = 'trace'; 139 const onLevelChange = sinon.spy(); 140 141 this.setProperties({ 142 level: 'info', 143 client: { id: 'client1' }, 144 onLevelChange, 145 }); 146 147 run.later(run, run.cancelTimers, INTERVAL); 148 149 await render(commonTemplate); 150 151 assert.equal( 152 find('[data-test-log-cli]').textContent, 153 `[INFO] ${LOG_MESSAGE}\n` 154 ); 155 156 const contentId = await selectOpen('[data-test-level-switcher-parent]'); 157 run.later(run, run.cancelTimers, INTERVAL); 158 await selectOpenChoose(contentId, capitalize(newLevel)); 159 await settled(); 160 161 assert.equal( 162 find('[data-test-log-cli]').textContent, 163 `[INFO] ${LOG_MESSAGE}\n\n...changing log level to ${newLevel}...\n\n[TRACE] ${LOG_MESSAGE}\n` 164 ); 165 }); 166 167 test('when switching levels and there is no scrollback, there is no appended switch message', async function (assert) { 168 const newLevel = 'trace'; 169 const onLevelChange = sinon.spy(); 170 171 // Emit nothing for the first request 172 this.pretender.get('/v1/agent/monitor', ({ queryParams }) => [ 173 200, 174 {}, 175 queryParams.log_level === 'info' 176 ? logEncode([''], 0) 177 : logEncode( 178 [ 179 `[${( 180 queryParams.log_level || 'info' 181 ).toUpperCase()}] ${LOG_MESSAGE}\n`, 182 ], 183 0 184 ), 185 ]); 186 187 this.setProperties({ 188 level: 'info', 189 client: { id: 'client1' }, 190 onLevelChange, 191 }); 192 193 run.later(run, run.cancelTimers, INTERVAL); 194 195 await render(commonTemplate); 196 197 assert.equal(find('[data-test-log-cli]').textContent, ''); 198 199 const contentId = await selectOpen('[data-test-level-switcher-parent]'); 200 run.later(run, run.cancelTimers, INTERVAL); 201 await selectOpenChoose(contentId, capitalize(newLevel)); 202 await settled(); 203 204 assert.equal( 205 find('[data-test-log-cli]').textContent, 206 `[TRACE] ${LOG_MESSAGE}\n` 207 ); 208 }); 209 });