github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/ui/tests/integration/job-page/periodic-test.js (about)

     1  import { module, test } from 'qunit';
     2  import { setupRenderingTest } from 'ember-qunit';
     3  import { render, settled } from '@ember/test-helpers';
     4  import { click, find, findAll } from 'ember-native-dom-helpers';
     5  import hbs from 'htmlbars-inline-precompile';
     6  import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage';
     7  import {
     8    jobURL,
     9    stopJob,
    10    startJob,
    11    expectError,
    12    expectDeleteRequest,
    13    expectStartRequest,
    14  } from './helpers';
    15  
    16  module('Integration | Component | job-page/periodic', function(hooks) {
    17    setupRenderingTest(hooks);
    18  
    19    hooks.beforeEach(function() {
    20      window.localStorage.clear();
    21      this.store = this.owner.lookup('service:store');
    22      this.server = startMirage();
    23      this.server.create('namespace');
    24    });
    25  
    26    hooks.afterEach(function() {
    27      this.server.shutdown();
    28      window.localStorage.clear();
    29    });
    30  
    31    const commonTemplate = hbs`
    32      {{job-page/periodic
    33        job=job
    34        sortProperty=sortProperty
    35        sortDescending=sortDescending
    36        currentPage=currentPage
    37        gotoJob=gotoJob}}
    38    `;
    39  
    40    const commonProperties = job => ({
    41      job,
    42      sortProperty: 'name',
    43      sortDescending: true,
    44      currentPage: 1,
    45      gotoJob: () => {},
    46    });
    47  
    48    test('Clicking Force Launch launches a new periodic child job', function(assert) {
    49      const childrenCount = 3;
    50  
    51      this.server.create('job', 'periodic', {
    52        id: 'parent',
    53        childrenCount,
    54        createAllocations: false,
    55      });
    56  
    57      this.store.findAll('job');
    58  
    59      return settled().then(async () => {
    60        const job = this.store.peekAll('job').findBy('plainId', 'parent');
    61  
    62        this.setProperties(commonProperties(job));
    63        await render(commonTemplate);
    64  
    65        return settled().then(() => {
    66          const currentJobCount = server.db.jobs.length;
    67  
    68          assert.equal(
    69            findAll('[data-test-job-name]').length,
    70            childrenCount,
    71            'The new periodic job launch is in the children list'
    72          );
    73  
    74          click('[data-test-force-launch]');
    75  
    76          return settled().then(() => {
    77            const expectedURL = jobURL(job, '/periodic/force');
    78  
    79            assert.ok(
    80              this.server.pretender.handledRequests
    81                .filterBy('method', 'POST')
    82                .find(req => req.url === expectedURL),
    83              'POST URL was correct'
    84            );
    85  
    86            assert.equal(server.db.jobs.length, currentJobCount + 1, 'POST request was made');
    87          });
    88        });
    89      });
    90    });
    91  
    92    test('Clicking force launch without proper permissions shows an error message', function(assert) {
    93      this.server.pretender.post('/v1/job/:id/periodic/force', () => [403, {}, null]);
    94  
    95      this.server.create('job', 'periodic', {
    96        id: 'parent',
    97        childrenCount: 1,
    98        createAllocations: false,
    99        status: 'running',
   100      });
   101  
   102      this.store.findAll('job');
   103  
   104      return settled().then(async () => {
   105        const job = this.store.peekAll('job').findBy('plainId', 'parent');
   106  
   107        this.setProperties(commonProperties(job));
   108        await render(commonTemplate);
   109  
   110        return settled().then(() => {
   111          assert.notOk(find('[data-test-job-error-title]'), 'No error message yet');
   112  
   113          click('[data-test-force-launch]');
   114  
   115          return settled().then(() => {
   116            assert.equal(
   117              find('[data-test-job-error-title]').textContent,
   118              'Could Not Force Launch',
   119              'Appropriate error is shown'
   120            );
   121            assert.ok(
   122              find('[data-test-job-error-body]').textContent.includes('ACL'),
   123              'The error message mentions ACLs'
   124            );
   125  
   126            click('[data-test-job-error-close]');
   127  
   128            assert.notOk(find('[data-test-job-error-title]'), 'Error message is dismissable');
   129          });
   130        });
   131      });
   132    });
   133  
   134    test('Stopping a job sends a delete request for the job', function(assert) {
   135      const mirageJob = this.server.create('job', 'periodic', {
   136        childrenCount: 0,
   137        createAllocations: false,
   138        status: 'running',
   139      });
   140  
   141      let job;
   142      this.store.findAll('job');
   143  
   144      return settled()
   145        .then(async () => {
   146          job = this.store.peekAll('job').findBy('plainId', mirageJob.id);
   147  
   148          this.setProperties(commonProperties(job));
   149          await render(commonTemplate);
   150  
   151          return settled();
   152        })
   153        .then(stopJob)
   154        .then(() => expectDeleteRequest(assert, this.server, job));
   155    });
   156  
   157    test('Stopping a job without proper permissions shows an error message', function(assert) {
   158      this.server.pretender.delete('/v1/job/:id', () => [403, {}, null]);
   159  
   160      const mirageJob = this.server.create('job', 'periodic', {
   161        childrenCount: 0,
   162        createAllocations: false,
   163        status: 'running',
   164      });
   165  
   166      this.store.findAll('job');
   167  
   168      return settled()
   169        .then(async () => {
   170          const job = this.store.peekAll('job').findBy('plainId', mirageJob.id);
   171  
   172          this.setProperties(commonProperties(job));
   173          await render(commonTemplate);
   174  
   175          return settled();
   176        })
   177        .then(stopJob)
   178        .then(expectError(assert, 'Could Not Stop Job'));
   179    });
   180  
   181    test('Starting a job sends a post request for the job using the current definition', function(assert) {
   182      let job;
   183  
   184      const mirageJob = this.server.create('job', 'periodic', {
   185        childrenCount: 0,
   186        createAllocations: false,
   187        status: 'dead',
   188      });
   189      this.store.findAll('job');
   190  
   191      return settled()
   192        .then(async () => {
   193          job = this.store.peekAll('job').findBy('plainId', mirageJob.id);
   194  
   195          this.setProperties(commonProperties(job));
   196          await render(commonTemplate);
   197  
   198          return settled();
   199        })
   200        .then(startJob)
   201        .then(() => expectStartRequest(assert, this.server, job));
   202    });
   203  
   204    test('Starting a job without proper permissions shows an error message', function(assert) {
   205      this.server.pretender.post('/v1/job/:id', () => [403, {}, null]);
   206  
   207      const mirageJob = this.server.create('job', 'periodic', {
   208        childrenCount: 0,
   209        createAllocations: false,
   210        status: 'dead',
   211      });
   212      this.store.findAll('job');
   213  
   214      return settled()
   215        .then(async () => {
   216          const job = this.store.peekAll('job').findBy('plainId', mirageJob.id);
   217  
   218          this.setProperties(commonProperties(job));
   219          await render(commonTemplate);
   220  
   221          return settled();
   222        })
   223        .then(startJob)
   224        .then(expectError(assert, 'Could Not Start Job'));
   225    });
   226  });