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