github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/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&plain=true`;
    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 truncationMessage =
    93        '\n\n---------- TRUNCATED: Click "tail" to view the bottom of the log ----------';
    94  
    95      const mocks = makeMocks(longLog);
    96      const log = Log.create(mocks);
    97  
    98      run(() => {
    99        log.get('gotoHead').perform();
   100      });
   101  
   102      await settled();
   103      assert.ok(log.get('output').toString().endsWith(truncationMessage), 'Truncation message is shown');
   104      assert.equal(
   105        log.get('output').toString().length,
   106        50000 + truncationMessage.length,
   107        'Output is truncated the appropriate amount'
   108      );
   109    });
   110  
   111    test('gotoTail builds the correct URL', async function(assert) {
   112      const mocks = makeMocks('');
   113      const expectedUrl = `${mocks.url}?a=param&another=one&offset=50000&origin=end&plain=true`;
   114      const log = Log.create(mocks);
   115  
   116      run(() => {
   117        log.get('gotoTail').perform();
   118        assert.ok(fetchSpy.calledWith(expectedUrl), `gotoTail URL was ${expectedUrl}`);
   119      });
   120    });
   121  
   122    test('startStreaming starts the log streamer', async function(assert) {
   123      const log = Log.create(makeMocks(''));
   124  
   125      log.startStreaming();
   126      assert.ok(startSpy.calledOnce, 'Streaming started');
   127      assert.equal(log.get('logPointer'), 'tail', 'Streaming points the log to the tail');
   128    });
   129  
   130    test('When the log streamer calls `write`, the output is appended', async function(assert) {
   131      const log = Log.create(makeMocks(''));
   132      const chunk1 = 'Hello';
   133      const chunk2 = ' World';
   134      const chunk3 = '\n\nEOF';
   135  
   136      log.startStreaming();
   137      assert.equal(log.get('output'), '', 'No output yet');
   138  
   139      log.get('logStreamer').step(chunk1);
   140      assert.equal(log.get('output'), chunk1, 'First chunk written');
   141  
   142      log.get('logStreamer').step(chunk2);
   143      assert.equal(log.get('output'), chunk1 + chunk2, 'Second chunk written');
   144  
   145      log.get('logStreamer').step(chunk3);
   146      assert.equal(log.get('output'), chunk1 + chunk2 + chunk3, 'Third chunk written');
   147    });
   148  
   149    test('stop stops the log streamer', async function(assert) {
   150      const log = Log.create(makeMocks(''));
   151  
   152      log.stop();
   153      assert.ok(stopSpy.calledOnce, 'Streaming stopped');
   154    });
   155  });