github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/ui/tests/unit/utils/log-test.js (about)

     1  import Ember from 'ember';
     2  import sinon from 'sinon';
     3  import wait from 'ember-test-helpers/wait';
     4  import { module, test } from 'ember-qunit';
     5  import _Log from 'nomad-ui/utils/classes/log';
     6  
     7  const { Object: EmberObject, RSVP, run } = Ember;
     8  
     9  let startSpy, stopSpy, initSpy, fetchSpy;
    10  
    11  const MockStreamer = EmberObject.extend({
    12    poll: {
    13      isRunning: false,
    14    },
    15  
    16    init() {
    17      initSpy(...arguments);
    18    },
    19  
    20    start() {
    21      this.get('poll').isRunning = true;
    22      startSpy(...arguments);
    23    },
    24  
    25    stop() {
    26      this.get('poll').isRunning = true;
    27      stopSpy(...arguments);
    28    },
    29  
    30    step(chunk) {
    31      if (this.get('poll').isRunning) {
    32        this.get('write')(chunk);
    33      }
    34    },
    35  });
    36  
    37  const Log = _Log.extend({
    38    init() {
    39      this._super();
    40      const props = this.get('logStreamer').getProperties('url', 'params', 'logFetch', 'write');
    41      this.set('logStreamer', MockStreamer.create(props));
    42    },
    43  });
    44  
    45  module('Unit | Util | Log', {
    46    beforeEach() {
    47      initSpy = sinon.spy();
    48      startSpy = sinon.spy();
    49      stopSpy = sinon.spy();
    50      fetchSpy = sinon.spy();
    51    },
    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', 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', 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', 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    return wait().then(() => {
   103      assert.ok(log.get('output').endsWith(truncationMessage), 'Truncation message is shown');
   104      assert.equal(
   105        log.get('output').length,
   106        50000 + truncationMessage.length,
   107        'Output is truncated the appropriate amount'
   108      );
   109    });
   110  });
   111  
   112  test('gotoTail builds the correct URL', function(assert) {
   113    const mocks = makeMocks('');
   114    const expectedUrl = `${mocks.url}?a=param&another=one&offset=50000&origin=end&plain=true`;
   115    const log = Log.create(mocks);
   116  
   117    run(() => {
   118      log.get('gotoTail').perform();
   119      assert.ok(fetchSpy.calledWith(expectedUrl), `gotoTail URL was ${expectedUrl}`);
   120    });
   121  });
   122  
   123  test('startStreaming starts the log streamer', function(assert) {
   124    const log = Log.create(makeMocks(''));
   125  
   126    log.startStreaming();
   127    assert.ok(startSpy.calledOnce, 'Streaming started');
   128    assert.equal(log.get('logPointer'), 'tail', 'Streaming points the log to the tail');
   129  });
   130  
   131  test('When the log streamer calls `write`, the output is appended', function(assert) {
   132    const log = Log.create(makeMocks(''));
   133    const chunk1 = 'Hello';
   134    const chunk2 = ' World';
   135    const chunk3 = '\n\nEOF';
   136  
   137    log.startStreaming();
   138    assert.equal(log.get('output'), '', 'No output yet');
   139  
   140    log.get('logStreamer').step(chunk1);
   141    assert.equal(log.get('output'), chunk1, 'First chunk written');
   142  
   143    log.get('logStreamer').step(chunk2);
   144    assert.equal(log.get('output'), chunk1 + chunk2, 'Second chunk written');
   145  
   146    log.get('logStreamer').step(chunk3);
   147    assert.equal(log.get('output'), chunk1 + chunk2 + chunk3, 'Third chunk written');
   148  });
   149  
   150  test('stop stops the log streamer', function(assert) {
   151    const log = Log.create(makeMocks(''));
   152  
   153    log.stop();
   154    assert.ok(stopSpy.calledOnce, 'Streaming stopped');
   155  });