github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/ui/tests/unit/utils/log-test.js (about) 1 import EmberObject from '@ember/object'; 2 import RSVP from 'rsvp'; 3 import { run } from '@ember/runloop'; 4 import sinon from 'sinon'; 5 import { module, test } from 'qunit'; 6 import _Log from 'nomad-ui/utils/classes/log'; 7 8 import { settled } from '@ember/test-helpers'; 9 10 let startSpy, stopSpy, initSpy, fetchSpy; 11 12 const MockStreamer = EmberObject.extend({ 13 poll: { 14 isRunning: false, 15 }, 16 17 init() { 18 initSpy(...arguments); 19 }, 20 21 start() { 22 this.get('poll').isRunning = true; 23 startSpy(...arguments); 24 }, 25 26 stop() { 27 this.get('poll').isRunning = true; 28 stopSpy(...arguments); 29 }, 30 31 step(chunk) { 32 if (this.get('poll').isRunning) { 33 this.get('write')(chunk); 34 } 35 }, 36 }); 37 38 const Log = _Log.extend({ 39 init() { 40 this._super(); 41 const props = this.get('logStreamer').getProperties('url', 'params', 'logFetch', 'write'); 42 this.set('logStreamer', MockStreamer.create(props)); 43 }, 44 }); 45 46 module('Unit | Util | Log', function(hooks) { 47 hooks.beforeEach(function() { 48 initSpy = sinon.spy(); 49 startSpy = sinon.spy(); 50 stopSpy = sinon.spy(); 51 fetchSpy = sinon.spy(); 52 }); 53 54 const makeMocks = output => ({ 55 url: '/test-url/', 56 params: { 57 a: 'param', 58 another: 'one', 59 }, 60 logFetch: function() { 61 fetchSpy(...arguments); 62 return RSVP.Promise.resolve({ 63 text() { 64 return output; 65 }, 66 }); 67 }, 68 }); 69 70 test('logStreamer is created on init', async function(assert) { 71 const log = Log.create(makeMocks('')); 72 73 assert.ok(log.get('logStreamer'), 'logStreamer property is defined'); 74 assert.ok(initSpy.calledOnce, 'logStreamer init was called'); 75 }); 76 77 test('gotoHead builds the correct URL', async function(assert) { 78 const mocks = makeMocks(''); 79 const expectedUrl = `${mocks.url}?a=param&another=one&offset=0&origin=start`; 80 const log = Log.create(mocks); 81 82 run(() => { 83 log.get('gotoHead').perform(); 84 assert.ok(fetchSpy.calledWith(expectedUrl), `gotoHead URL was ${expectedUrl}`); 85 }); 86 }); 87 88 test('When gotoHead returns too large of a log, the log is truncated', async function(assert) { 89 const longLog = Array(50001) 90 .fill('a') 91 .join(''); 92 const encodedLongLog = `{"Offset":0,"Data":"${window.btoa(longLog)}"}`; 93 const truncationMessage = 94 '\n\n---------- TRUNCATED: Click "tail" to view the bottom of the log ----------'; 95 96 const mocks = makeMocks(encodedLongLog); 97 const log = Log.create(mocks); 98 99 run(() => { 100 log.get('gotoHead').perform(); 101 }); 102 103 await settled(); 104 assert.ok( 105 log 106 .get('output') 107 .toString() 108 .endsWith(truncationMessage), 109 'Truncation message is shown' 110 ); 111 assert.equal( 112 log.get('output').toString().length, 113 50000 + truncationMessage.length, 114 'Output is truncated the appropriate amount' 115 ); 116 }); 117 118 test('gotoTail builds the correct URL', async function(assert) { 119 const mocks = makeMocks(''); 120 const expectedUrl = `${mocks.url}?a=param&another=one&offset=50000&origin=end`; 121 const log = Log.create(mocks); 122 123 run(() => { 124 log.get('gotoTail').perform(); 125 assert.ok(fetchSpy.calledWith(expectedUrl), `gotoTail URL was ${expectedUrl}`); 126 }); 127 }); 128 129 test('startStreaming starts the log streamer', async function(assert) { 130 const log = Log.create(makeMocks('')); 131 132 log.startStreaming(); 133 assert.ok(startSpy.calledOnce, 'Streaming started'); 134 assert.equal(log.get('logPointer'), 'tail', 'Streaming points the log to the tail'); 135 }); 136 137 test('When the log streamer calls `write`, the output is appended', async function(assert) { 138 const log = Log.create(makeMocks('')); 139 const chunk1 = 'Hello'; 140 const chunk2 = ' World'; 141 const chunk3 = '\n\nEOF'; 142 143 log.startStreaming(); 144 assert.equal(log.get('output'), '', 'No output yet'); 145 146 log.get('logStreamer').step(chunk1); 147 assert.equal(log.get('output'), chunk1, 'First chunk written'); 148 149 log.get('logStreamer').step(chunk2); 150 assert.equal(log.get('output'), chunk1 + chunk2, 'Second chunk written'); 151 152 log.get('logStreamer').step(chunk3); 153 assert.equal(log.get('output'), chunk1 + chunk2 + chunk3, 'Third chunk written'); 154 }); 155 156 test('stop stops the log streamer', async function(assert) { 157 const log = Log.create(makeMocks('')); 158 159 log.stop(); 160 assert.ok(stopSpy.calledOnce, 'Streaming stopped'); 161 }); 162 });