github.com/hernad/nomad@v1.6.112/ui/tests/unit/adapters/allocation-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 | Allocation', function (hooks) { 11 setupTest(hooks); 12 13 hooks.beforeEach(async function () { 14 this.store = this.owner.lookup('service:store'); 15 this.subject = () => this.store.adapterFor('allocation'); 16 17 window.localStorage.clear(); 18 19 this.server = startMirage(); 20 21 this.initialize = async (allocationId, { region } = {}) => { 22 if (region) window.localStorage.nomadActiveRegion = region; 23 24 this.server.create('namespace'); 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 this.server.create('job', { createAllocations: false }); 31 this.server.create('allocation', { id: 'alloc-1' }); 32 this.system = this.owner.lookup('service:system'); 33 await this.system.get('namespaces'); 34 this.system.get('shouldIncludeRegion'); 35 await this.system.get('defaultRegion'); 36 37 const allocation = await this.store.findRecord( 38 'allocation', 39 allocationId 40 ); 41 this.server.pretender.handledRequests.length = 0; 42 43 return allocation; 44 }; 45 }); 46 47 hooks.afterEach(function () { 48 this.server.shutdown(); 49 }); 50 51 const testCases = [ 52 { 53 variation: '', 54 id: 'alloc-1', 55 task: 'task-name', 56 region: null, 57 path: 'some/path', 58 ls: `GET /v1/client/fs/ls/alloc-1?path=${encodeURIComponent( 59 'some/path' 60 )}`, 61 stat: `GET /v1/client/fs/stat/alloc-1?path=${encodeURIComponent( 62 'some/path' 63 )}`, 64 stop: 'POST /v1/allocation/alloc-1/stop', 65 restart: 'PUT /v1/client/allocation/alloc-1/restart', 66 }, 67 { 68 variation: 'with non-default region', 69 id: 'alloc-1', 70 task: 'task-name', 71 region: 'region-2', 72 path: 'some/path', 73 ls: `GET /v1/client/fs/ls/alloc-1?path=${encodeURIComponent( 74 'some/path' 75 )}®ion=region-2`, 76 stat: `GET /v1/client/fs/stat/alloc-1?path=${encodeURIComponent( 77 'some/path' 78 )}®ion=region-2`, 79 stop: 'POST /v1/allocation/alloc-1/stop?region=region-2', 80 restart: 'PUT /v1/client/allocation/alloc-1/restart?region=region-2', 81 }, 82 ]; 83 84 testCases.forEach((testCase) => { 85 test(`ls makes the correct API call ${testCase.variation}`, async function (assert) { 86 const { pretender } = this.server; 87 const allocation = await this.initialize(testCase.id, { 88 region: testCase.region, 89 }); 90 91 await this.subject().ls(allocation, testCase.path); 92 const req = pretender.handledRequests[0]; 93 assert.equal(`${req.method} ${req.url}`, testCase.ls); 94 }); 95 96 test(`stat makes the correct API call ${testCase.variation}`, async function (assert) { 97 const { pretender } = this.server; 98 const allocation = await this.initialize(testCase.id, { 99 region: testCase.region, 100 }); 101 102 await this.subject().stat(allocation, testCase.path); 103 const req = pretender.handledRequests[0]; 104 assert.equal(`${req.method} ${req.url}`, testCase.stat); 105 }); 106 107 test(`stop makes the correct API call ${testCase.variation}`, async function (assert) { 108 const { pretender } = this.server; 109 const allocation = await this.initialize(testCase.id, { 110 region: testCase.region, 111 }); 112 113 await this.subject().stop(allocation); 114 const req = pretender.handledRequests[0]; 115 assert.equal(`${req.method} ${req.url}`, testCase.stop); 116 }); 117 118 test(`restart makes the correct API call ${testCase.variation}`, async function (assert) { 119 const { pretender } = this.server; 120 const allocation = await this.initialize(testCase.id, { 121 region: testCase.region, 122 }); 123 124 await this.subject().restart(allocation); 125 const req = pretender.handledRequests[0]; 126 assert.equal(`${req.method} ${req.url}`, testCase.restart); 127 }); 128 129 test(`restart with optional task name makes the correct API call ${testCase.variation}`, async function (assert) { 130 const { pretender } = this.server; 131 const allocation = await this.initialize(testCase.id, { 132 region: testCase.region, 133 }); 134 135 await this.subject().restart(allocation, testCase.task); 136 const req = pretender.handledRequests[0]; 137 assert.equal(`${req.method} ${req.url}`, testCase.restart); 138 assert.deepEqual(JSON.parse(req.requestBody), { 139 TaskName: testCase.task, 140 }); 141 }); 142 }); 143 });