From 4e152a75f5edc65a9d02ba6ec004484649920a8c Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Mon, 20 Jul 2020 21:55:51 -0700 Subject: [PATCH] Test all allocation adapter actions with and without a region This involves a refactor to prevent immense verbosity. --- ui/tests/unit/adapters/allocation-test.js | 135 ++++++++++++++-------- 1 file changed, 87 insertions(+), 48 deletions(-) diff --git a/ui/tests/unit/adapters/allocation-test.js b/ui/tests/unit/adapters/allocation-test.js index c176e8453..e0e727ec8 100644 --- a/ui/tests/unit/adapters/allocation-test.js +++ b/ui/tests/unit/adapters/allocation-test.js @@ -9,69 +9,108 @@ module('Unit | Adapter | Allocation', function(hooks) { this.store = this.owner.lookup('service:store'); this.subject = () => this.store.adapterFor('allocation'); + window.localStorage.clear(); + this.server = startMirage(); - this.server.create('namespace'); - this.server.create('node'); - this.server.create('job', { createAllocations: false }); - this.server.create('allocation', { id: 'alloc-1' }); + this.initialize = async (allocationId, { region } = {}) => { + if (region) window.localStorage.nomadActiveRegion = region; + + this.server.create('namespace'); + this.server.create('region', { id: 'region-1' }); + this.server.create('region', { id: 'region-2' }); + + this.server.create('node'); + this.server.create('job', { createAllocations: false }); + this.server.create('allocation', { id: 'alloc-1' }); + this.system = this.owner.lookup('service:system'); + await this.system.get('namespaces'); + this.system.get('shouldIncludeRegion'); + await this.system.get('defaultRegion'); + + const allocation = await this.store.findRecord('allocation', allocationId); + this.server.pretender.handledRequests.length = 0; + + return allocation; + }; }); hooks.afterEach(function() { this.server.shutdown(); }); - test('`stop` makes the correct API call', async function(assert) { - const { pretender } = this.server; - const allocationId = 'alloc-1'; + const testCases = [ + { + variation: '', + id: 'alloc-1', + task: 'task-name', + region: null, + path: 'some/path', + ls: `GET /v1/client/fs/ls/alloc-1?path=${encodeURIComponent('some/path')}`, + stat: `GET /v1/client/fs/stat/alloc-1?path=${encodeURIComponent('some/path')}`, + stop: 'POST /v1/allocation/alloc-1/stop', + restart: 'PUT /v1/client/allocation/alloc-1/restart', + }, + { + variation: 'with non-default region', + id: 'alloc-1', + task: 'task-name', + region: 'region-2', + path: 'some/path', + ls: `GET /v1/client/fs/ls/alloc-1?path=${encodeURIComponent('some/path')}®ion=region-2`, + stat: `GET /v1/client/fs/stat/alloc-1?path=${encodeURIComponent( + 'some/path' + )}®ion=region-2`, + stop: 'POST /v1/allocation/alloc-1/stop?region=region-2', + restart: 'PUT /v1/client/allocation/alloc-1/restart?region=region-2', + }, + ]; - const allocation = await this.store.findRecord('allocation', allocationId); - pretender.handledRequests.length = 0; + testCases.forEach(testCase => { + test(`ls makes the correct API call ${testCase.variation}`, async function(assert) { + const { pretender } = this.server; + const allocation = await this.initialize(testCase.id, { region: testCase.region }); - await this.subject().stop(allocation); - const req = pretender.handledRequests[0]; - assert.equal( - `${req.method} ${req.url}`, - `POST /v1/allocation/${allocationId}/stop`, - `POST /v1/allocation/${allocationId}/stop` - ); - }); + await this.subject().ls(allocation, testCase.path); + const req = pretender.handledRequests[0]; + assert.equal(`${req.method} ${req.url}`, testCase.ls); + }); - test('`restart` makes the correct API call', async function(assert) { - const { pretender } = this.server; - const allocationId = 'alloc-1'; + test(`stat makes the correct API call ${testCase.variation}`, async function(assert) { + const { pretender } = this.server; + const allocation = await this.initialize(testCase.id, { region: testCase.region }); - const allocation = await this.store.findRecord('allocation', allocationId); - pretender.handledRequests.length = 0; + await this.subject().stat(allocation, testCase.path); + const req = pretender.handledRequests[0]; + assert.equal(`${req.method} ${req.url}`, testCase.stat); + }); - await this.subject().restart(allocation); - const req = pretender.handledRequests[0]; - assert.equal( - `${req.method} ${req.url}`, - `PUT /v1/client/allocation/${allocationId}/restart`, - `PUT /v1/client/allocation/${allocationId}/restart` - ); - }); + test(`stop makes the correct API call ${testCase.variation}`, async function(assert) { + const { pretender } = this.server; + const allocation = await this.initialize(testCase.id, { region: testCase.region }); - test('`restart` takes an optional task name and makes the correct API call', async function(assert) { - const { pretender } = this.server; - const allocationId = 'alloc-1'; - const taskName = 'task-name'; + await this.subject().stop(allocation); + const req = pretender.handledRequests[0]; + assert.equal(`${req.method} ${req.url}`, testCase.stop); + }); - const allocation = await this.store.findRecord('allocation', allocationId); - pretender.handledRequests.length = 0; + test(`restart makes the correct API call ${testCase.variation}`, async function(assert) { + const { pretender } = this.server; + const allocation = await this.initialize(testCase.id, { region: testCase.region }); - await this.subject().restart(allocation, taskName); - const req = pretender.handledRequests[0]; - assert.equal( - `${req.method} ${req.url}`, - `PUT /v1/client/allocation/${allocationId}/restart`, - `PUT /v1/client/allocation/${allocationId}/restart` - ); - assert.deepEqual( - JSON.parse(req.requestBody), - { TaskName: taskName }, - 'Request body is correct' - ); + await this.subject().restart(allocation); + const req = pretender.handledRequests[0]; + assert.equal(`${req.method} ${req.url}`, testCase.restart); + }); + + test(`restart with optional task name makes the correct API call ${testCase.variation}`, async function(assert) { + const { pretender } = this.server; + const allocation = await this.initialize(testCase.id, { region: testCase.region }); + + await this.subject().restart(allocation, testCase.task); + const req = pretender.handledRequests[0]; + assert.equal(`${req.method} ${req.url}`, testCase.restart); + assert.deepEqual(JSON.parse(req.requestBody), { TaskName: testCase.task }); + }); }); });