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

     1  import Ember from 'ember';
     2  import { test, moduleForComponent } from 'ember-qunit';
     3  import wait from 'ember-test-helpers/wait';
     4  import { find, click } from 'ember-native-dom-helpers';
     5  import hbs from 'htmlbars-inline-precompile';
     6  import Pretender from 'pretender';
     7  import { logEncode } from '../../mirage/data/logs';
     8  
     9  const { run } = Ember;
    10  
    11  const HOST = '1.1.1.1:1111';
    12  const commonProps = {
    13    interval: 50,
    14    allocation: {
    15      id: 'alloc-1',
    16      node: {
    17        httpAddr: HOST,
    18      },
    19    },
    20    task: 'task-name',
    21  };
    22  
    23  const logHead = ['HEAD'];
    24  const logTail = ['TAIL'];
    25  const streamFrames = ['one\n', 'two\n', 'three\n', 'four\n', 'five\n'];
    26  let streamPointer = 0;
    27  
    28  moduleForComponent('task-log', 'Integration | Component | task log', {
    29    integration: true,
    30    beforeEach() {
    31      this.server = new Pretender(function() {
    32        this.get(`http://${HOST}/v1/client/fs/logs/:allocation_id`, ({ queryParams }) => {
    33          const { origin, offset, plain, follow } = queryParams;
    34  
    35          let frames;
    36          let data;
    37  
    38          if (origin === 'start' && offset === '0' && plain && !follow) {
    39            frames = logHead;
    40          } else if (origin === 'end' && plain && !follow) {
    41            frames = logTail;
    42          } else {
    43            frames = streamFrames;
    44          }
    45  
    46          if (frames === streamFrames) {
    47            data = queryParams.plain ? frames[streamPointer] : logEncode(frames, streamPointer);
    48            streamPointer++;
    49          } else {
    50            data = queryParams.plain ? frames.join('') : logEncode(frames, frames.length - 1);
    51          }
    52  
    53          return [200, {}, data];
    54        });
    55      });
    56    },
    57    afterEach() {
    58      this.server.shutdown();
    59      streamPointer = 0;
    60    },
    61  });
    62  
    63  test('Basic appearance', function(assert) {
    64    this.setProperties(commonProps);
    65    this.render(hbs`{{task-log allocation=allocation task=task}}`);
    66  
    67    assert.ok(find('.action-stdout'), 'Stdout button');
    68    assert.ok(find('.action-stderr'), 'Stderr button');
    69    assert.ok(find('.action-head'), 'Head button');
    70    assert.ok(find('.action-tail'), 'Tail button');
    71    assert.ok(find('.action-toggle-stream'), 'Stream toggle button');
    72  
    73    assert.ok(find('.boxed-section-body.is-full-bleed.is-dark'), 'Body is full-bleed and dark');
    74  
    75    assert.ok(find('pre.cli-window'), 'Cli is preformatted and using the cli-window component class');
    76  });
    77  
    78  test('Streaming starts on creation', function(assert) {
    79    run.later(run, run.cancelTimers, commonProps.interval);
    80  
    81    this.setProperties(commonProps);
    82    this.render(hbs`{{task-log allocation=allocation task=task}}`);
    83  
    84    const logUrlRegex = new RegExp(`${HOST}/v1/client/fs/logs/${commonProps.allocation.id}`);
    85    assert.ok(
    86      this.server.handledRequests.filter(req => logUrlRegex.test(req.url)).length,
    87      'Log requests were made'
    88    );
    89  
    90    return wait().then(() => {
    91      assert.equal(
    92        find('.cli-window').textContent,
    93        streamFrames[0],
    94        'First chunk of streaming log is shown'
    95      );
    96    });
    97  });
    98  
    99  test('Clicking Head loads the log head', function(assert) {
   100    this.setProperties(commonProps);
   101    this.render(hbs`{{task-log allocation=allocation task=task}}`);
   102  
   103    click('.action-head');
   104  
   105    return wait().then(() => {
   106      assert.ok(
   107        this.server.handledRequests.find(
   108          ({ queryParams: qp }) => qp.origin === 'start' && qp.plain === 'true' && qp.offset === '0'
   109        ),
   110        'Log head request was made'
   111      );
   112      assert.equal(find('.cli-window').textContent, logHead[0], 'Head of the log is shown');
   113    });
   114  });
   115  
   116  test('Clicking Tail loads the log tail', function(assert) {
   117    this.setProperties(commonProps);
   118    this.render(hbs`{{task-log allocation=allocation task=task}}`);
   119  
   120    click('.action-tail');
   121  
   122    return wait().then(() => {
   123      assert.ok(
   124        this.server.handledRequests.find(
   125          ({ queryParams: qp }) => qp.origin === 'end' && qp.plain === 'true'
   126        ),
   127        'Log tail request was made'
   128      );
   129      assert.equal(find('.cli-window').textContent, logTail[0], 'Tail of the log is shown');
   130    });
   131  });
   132  
   133  test('Clicking toggleStream starts and stops the log stream', function(assert) {
   134    const { interval } = commonProps;
   135    this.setProperties(commonProps);
   136    this.render(hbs`{{task-log allocation=allocation task=task interval=interval}}`);
   137  
   138    run.later(() => {
   139      click('.action-toggle-stream');
   140    }, interval);
   141  
   142    return wait().then(() => {
   143      assert.equal(find('.cli-window').textContent, streamFrames[0], 'First frame loaded');
   144  
   145      run.later(() => {
   146        assert.equal(find('.cli-window').textContent, streamFrames[0], 'Still only first frame');
   147        click('.action-toggle-stream');
   148        run.later(run, run.cancelTimers, interval * 2);
   149      }, interval * 2);
   150  
   151      return wait().then(() => {
   152        assert.equal(
   153          find('.cli-window').textContent,
   154          streamFrames[0] + streamFrames[0] + streamFrames[1],
   155          'Now includes second frame'
   156        );
   157      });
   158    });
   159  });
   160  
   161  test('Clicking stderr switches the log to standard error', function(assert) {
   162    this.setProperties(commonProps);
   163    this.render(hbs`{{task-log allocation=allocation task=task}}`);
   164  
   165    click('.action-stderr');
   166    run.later(run, run.cancelTimers, commonProps.interval);
   167  
   168    return wait().then(() => {
   169      assert.ok(
   170        this.server.handledRequests.filter(req => req.queryParams.type === 'stderr').length,
   171        'stderr log requests were made'
   172      );
   173    });
   174  });