github.com/hernad/nomad@v1.6.112/ui/tests/unit/adapters/deployment-test.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 import { module, test } from 'qunit'; 7 import { setupTest } from 'ember-qunit'; 8 import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage'; 9 10 module('Unit | Adapter | Deployment', function (hooks) { 11 setupTest(hooks); 12 13 hooks.beforeEach(async function () { 14 this.store = this.owner.lookup('service:store'); 15 this.system = this.owner.lookup('service:system'); 16 this.subject = () => this.store.adapterFor('deployment'); 17 18 window.localStorage.clear(); 19 20 this.server = startMirage(); 21 22 this.initialize = async ({ region } = {}) => { 23 if (region) window.localStorage.nomadActiveRegion = region; 24 25 this.server.create('region', { id: 'region-1' }); 26 this.server.create('region', { id: 'region-2' }); 27 28 this.server.create('node-pool'); 29 this.server.create('node'); 30 const job = this.server.create('job', { createAllocations: false }); 31 const deploymentRecord = server.schema.deployments.where({ 32 jobId: job.id, 33 }).models[0]; 34 35 this.system.get('shouldIncludeRegion'); 36 await this.system.get('defaultRegion'); 37 38 const deployment = await this.store.findRecord( 39 'deployment', 40 deploymentRecord.id 41 ); 42 this.server.pretender.handledRequests.length = 0; 43 44 return deployment; 45 }; 46 }); 47 48 hooks.afterEach(function () { 49 this.server.shutdown(); 50 }); 51 52 const testCases = [ 53 { 54 variation: '', 55 region: null, 56 fail: (id) => `POST /v1/deployment/fail/${id}`, 57 promote: (id) => `POST /v1/deployment/promote/${id}`, 58 }, 59 { 60 variation: 'with non-default region', 61 region: 'region-2', 62 fail: (id) => `POST /v1/deployment/fail/${id}?region=region-2`, 63 promote: (id) => `POST /v1/deployment/promote/${id}?region=region-2`, 64 }, 65 ]; 66 67 testCases.forEach((testCase) => { 68 test(`promote makes the correct API call ${testCase.variation}`, async function (assert) { 69 const deployment = await this.initialize({ region: testCase.region }); 70 await this.subject().promote(deployment); 71 72 const request = this.server.pretender.handledRequests[0]; 73 74 assert.equal( 75 `${request.method} ${request.url}`, 76 testCase.promote(deployment.id) 77 ); 78 assert.deepEqual(JSON.parse(request.requestBody), { 79 DeploymentId: deployment.id, 80 All: true, 81 }); 82 }); 83 84 test(`fail makes the correct API call ${testCase.variation}`, async function (assert) { 85 const deployment = await this.initialize({ region: testCase.region }); 86 await this.subject().fail(deployment); 87 88 const request = this.server.pretender.handledRequests[0]; 89 90 assert.equal( 91 `${request.method} ${request.url}`, 92 testCase.fail(deployment.id) 93 ); 94 assert.deepEqual(JSON.parse(request.requestBody), { 95 DeploymentId: deployment.id, 96 }); 97 }); 98 }); 99 });