github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/ui/tests/integration/job-page/service-test.js (about)

     1  import { assign } from '@ember/polyfills';
     2  import { module, test } from 'qunit';
     3  import { setupRenderingTest } from 'ember-qunit';
     4  import { click, find, render } from '@ember/test-helpers';
     5  import hbs from 'htmlbars-inline-precompile';
     6  import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage';
     7  import { startJob, stopJob, expectError, expectDeleteRequest, expectStartRequest } from './helpers';
     8  import Job from 'nomad-ui/tests/pages/jobs/detail';
     9  import { initialize as fragmentSerializerInitializer } from 'nomad-ui/initializers/fragment-serializer';
    10  
    11  module('Integration | Component | job-page/service', function(hooks) {
    12    setupRenderingTest(hooks);
    13  
    14    hooks.beforeEach(function() {
    15      fragmentSerializerInitializer(this.owner);
    16      window.localStorage.clear();
    17      this.store = this.owner.lookup('service:store');
    18      this.server = startMirage();
    19      this.server.create('namespace');
    20    });
    21  
    22    hooks.afterEach(function() {
    23      Job.removeContext();
    24      this.server.shutdown();
    25      window.localStorage.clear();
    26    });
    27  
    28    const commonTemplate = hbs`
    29      {{job-page/service
    30        job=job
    31        sortProperty=sortProperty
    32        sortDescending=sortDescending
    33        currentPage=currentPage
    34        gotoJob=gotoJob}}
    35    `;
    36  
    37    const commonProperties = job => ({
    38      job,
    39      sortProperty: 'name',
    40      sortDescending: true,
    41      currentPage: 1,
    42      gotoJob() {},
    43    });
    44  
    45    const makeMirageJob = (server, props = {}) =>
    46      server.create(
    47        'job',
    48        assign(
    49          {
    50            type: 'service',
    51            createAllocations: false,
    52            status: 'running',
    53          },
    54          props
    55        )
    56      );
    57  
    58    test('Stopping a job sends a delete request for the job', async function(assert) {
    59      const mirageJob = makeMirageJob(this.server);
    60      await this.store.findAll('job');
    61  
    62      const job = this.store.peekAll('job').findBy('plainId', mirageJob.id);
    63  
    64      this.setProperties(commonProperties(job));
    65      await render(commonTemplate);
    66  
    67      await stopJob();
    68      expectDeleteRequest(assert, this.server, job);
    69    });
    70  
    71    test('Stopping a job without proper permissions shows an error message', async function(assert) {
    72      this.server.pretender.delete('/v1/job/:id', () => [403, {}, null]);
    73  
    74      const mirageJob = makeMirageJob(this.server);
    75      await this.store.findAll('job');
    76  
    77      const job = this.store.peekAll('job').findBy('plainId', mirageJob.id);
    78  
    79      this.setProperties(commonProperties(job));
    80      await render(commonTemplate);
    81  
    82      await stopJob();
    83      expectError(assert, 'Could Not Stop Job');
    84    });
    85  
    86    test('Starting a job sends a post request for the job using the current definition', async function(assert) {
    87      const mirageJob = makeMirageJob(this.server, { status: 'dead' });
    88      await this.store.findAll('job');
    89  
    90      const job = this.store.peekAll('job').findBy('plainId', mirageJob.id);
    91  
    92      this.setProperties(commonProperties(job));
    93      await render(commonTemplate);
    94  
    95      await startJob();
    96      expectStartRequest(assert, this.server, job);
    97    });
    98  
    99    test('Starting a job without proper permissions shows an error message', async function(assert) {
   100      this.server.pretender.post('/v1/job/:id', () => [403, {}, null]);
   101  
   102      const mirageJob = makeMirageJob(this.server, { status: 'dead' });
   103      await this.store.findAll('job');
   104  
   105      const job = this.store.peekAll('job').findBy('plainId', mirageJob.id);
   106  
   107      this.setProperties(commonProperties(job));
   108      await render(commonTemplate);
   109  
   110      await startJob();
   111      expectError(assert, 'Could Not Start Job');
   112    });
   113  
   114    test('Recent allocations shows allocations in the job context', async function(assert) {
   115      this.server.create('node');
   116      const mirageJob = makeMirageJob(this.server, { createAllocations: true });
   117      await this.store.findAll('job');
   118  
   119      const job = this.store.peekAll('job').findBy('plainId', mirageJob.id);
   120  
   121      this.setProperties(commonProperties(job));
   122      await render(commonTemplate);
   123  
   124      const allocation = this.server.db.allocations.sortBy('modifyIndex').reverse()[0];
   125      const allocationRow = Job.allocations.objectAt(0);
   126  
   127      assert.equal(allocationRow.shortId, allocation.id.split('-')[0], 'ID');
   128      assert.equal(allocationRow.taskGroup, allocation.taskGroup, 'Task Group name');
   129    });
   130  
   131    test('Recent allocations caps out at five', async function(assert) {
   132      this.server.create('node');
   133      const mirageJob = makeMirageJob(this.server);
   134      this.server.createList('allocation', 10);
   135  
   136      await this.store.findAll('job');
   137  
   138      const job = this.store.peekAll('job').findBy('plainId', mirageJob.id);
   139  
   140      this.setProperties(commonProperties(job));
   141      await render(commonTemplate);
   142  
   143      assert.equal(Job.allocations.length, 5, 'Capped at 5 allocations');
   144      assert.ok(
   145        Job.viewAllAllocations.includes(job.get('allocations.length') + ''),
   146        `View link mentions ${job.get('allocations.length')} allocations`
   147      );
   148    });
   149  
   150    test('Recent allocations shows an empty message when the job has no allocations', async function(assert) {
   151      this.server.create('node');
   152      const mirageJob = makeMirageJob(this.server);
   153  
   154      await this.store.findAll('job');
   155  
   156      const job = this.store.peekAll('job').findBy('plainId', mirageJob.id);
   157  
   158      this.setProperties(commonProperties(job));
   159      await render(commonTemplate);
   160  
   161      assert.ok(
   162        Job.recentAllocationsEmptyState.headline.includes('No Allocations'),
   163        'No allocations empty message'
   164      );
   165    });
   166  
   167    test('Active deployment can be promoted', async function(assert) {
   168      this.server.create('node');
   169      const mirageJob = makeMirageJob(this.server, { activeDeployment: true });
   170  
   171      await this.store.findAll('job');
   172  
   173      const job = this.store.peekAll('job').findBy('plainId', mirageJob.id);
   174      const deployment = await job.get('latestDeployment');
   175  
   176      this.setProperties(commonProperties(job));
   177      await render(commonTemplate);
   178  
   179      await click('[data-test-promote-canary]');
   180  
   181      const requests = this.server.pretender.handledRequests;
   182  
   183      assert.ok(
   184        requests
   185          .filterBy('method', 'POST')
   186          .findBy('url', `/v1/deployment/promote/${deployment.get('id')}`),
   187        'A promote POST request was made'
   188      );
   189    });
   190  
   191    test('When promoting the active deployment fails, an error is shown', async function(assert) {
   192      this.server.pretender.post('/v1/deployment/promote/:id', () => [403, {}, null]);
   193  
   194      this.server.create('node');
   195      const mirageJob = makeMirageJob(this.server, { activeDeployment: true });
   196  
   197      await this.store.findAll('job');
   198  
   199      const job = this.store.peekAll('job').findBy('plainId', mirageJob.id);
   200  
   201      this.setProperties(commonProperties(job));
   202      await render(commonTemplate);
   203  
   204      await click('[data-test-promote-canary]');
   205  
   206      assert.equal(
   207        find('[data-test-job-error-title]').textContent,
   208        'Could Not Promote Deployment',
   209        'Appropriate error is shown'
   210      );
   211      assert.ok(
   212        find('[data-test-job-error-body]').textContent.includes('ACL'),
   213        'The error message mentions ACLs'
   214      );
   215  
   216      await click('[data-test-job-error-close]');
   217  
   218      assert.notOk(find('[data-test-job-error-title]'), 'Error message is dismissable');
   219    });
   220  });