2019-05-20 22:27:20 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupTest } from 'ember-qunit';
|
|
|
|
import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage';
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
module('Unit | Adapter | Allocation', function (hooks) {
|
2019-05-20 22:27:20 +00:00
|
|
|
setupTest(hooks);
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
hooks.beforeEach(async function () {
|
2019-05-20 22:27:20 +00:00
|
|
|
this.store = this.owner.lookup('service:store');
|
|
|
|
this.subject = () => this.store.adapterFor('allocation');
|
|
|
|
|
2020-07-21 04:55:51 +00:00
|
|
|
window.localStorage.clear();
|
|
|
|
|
2019-05-20 22:27:20 +00:00
|
|
|
this.server = startMirage();
|
|
|
|
|
2020-07-21 04:55:51 +00:00
|
|
|
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');
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
const allocation = await this.store.findRecord(
|
|
|
|
'allocation',
|
|
|
|
allocationId
|
|
|
|
);
|
2020-07-21 04:55:51 +00:00
|
|
|
this.server.pretender.handledRequests.length = 0;
|
|
|
|
|
|
|
|
return allocation;
|
|
|
|
};
|
2019-05-20 22:27:20 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
hooks.afterEach(function () {
|
2019-05-20 22:27:20 +00:00
|
|
|
this.server.shutdown();
|
|
|
|
});
|
|
|
|
|
2020-07-21 04:55:51 +00:00
|
|
|
const testCases = [
|
|
|
|
{
|
|
|
|
variation: '',
|
|
|
|
id: 'alloc-1',
|
|
|
|
task: 'task-name',
|
|
|
|
region: null,
|
|
|
|
path: 'some/path',
|
2021-12-28 16:08:12 +00:00
|
|
|
ls: `GET /v1/client/fs/ls/alloc-1?path=${encodeURIComponent(
|
|
|
|
'some/path'
|
|
|
|
)}`,
|
|
|
|
stat: `GET /v1/client/fs/stat/alloc-1?path=${encodeURIComponent(
|
|
|
|
'some/path'
|
|
|
|
)}`,
|
2020-07-21 04:55:51 +00:00
|
|
|
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',
|
2021-12-28 16:08:12 +00:00
|
|
|
ls: `GET /v1/client/fs/ls/alloc-1?path=${encodeURIComponent(
|
|
|
|
'some/path'
|
|
|
|
)}®ion=region-2`,
|
2020-07-21 04:55:51 +00:00
|
|
|
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',
|
|
|
|
},
|
|
|
|
];
|
2019-05-20 22:27:20 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
testCases.forEach((testCase) => {
|
|
|
|
test(`ls makes the correct API call ${testCase.variation}`, async function (assert) {
|
2020-07-21 04:55:51 +00:00
|
|
|
const { pretender } = this.server;
|
2021-12-28 16:08:12 +00:00
|
|
|
const allocation = await this.initialize(testCase.id, {
|
|
|
|
region: testCase.region,
|
|
|
|
});
|
2019-05-20 22:27:20 +00:00
|
|
|
|
2020-07-21 04:55:51 +00:00
|
|
|
await this.subject().ls(allocation, testCase.path);
|
|
|
|
const req = pretender.handledRequests[0];
|
|
|
|
assert.equal(`${req.method} ${req.url}`, testCase.ls);
|
|
|
|
});
|
2019-05-20 22:27:20 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test(`stat makes the correct API call ${testCase.variation}`, async function (assert) {
|
2020-07-21 04:55:51 +00:00
|
|
|
const { pretender } = this.server;
|
2021-12-28 16:08:12 +00:00
|
|
|
const allocation = await this.initialize(testCase.id, {
|
|
|
|
region: testCase.region,
|
|
|
|
});
|
2019-05-20 22:27:20 +00:00
|
|
|
|
2020-07-21 04:55:51 +00:00
|
|
|
await this.subject().stat(allocation, testCase.path);
|
|
|
|
const req = pretender.handledRequests[0];
|
|
|
|
assert.equal(`${req.method} ${req.url}`, testCase.stat);
|
|
|
|
});
|
2019-05-20 22:27:20 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test(`stop makes the correct API call ${testCase.variation}`, async function (assert) {
|
2020-07-21 04:55:51 +00:00
|
|
|
const { pretender } = this.server;
|
2021-12-28 16:08:12 +00:00
|
|
|
const allocation = await this.initialize(testCase.id, {
|
|
|
|
region: testCase.region,
|
|
|
|
});
|
2020-07-21 04:55:51 +00:00
|
|
|
|
|
|
|
await this.subject().stop(allocation);
|
|
|
|
const req = pretender.handledRequests[0];
|
|
|
|
assert.equal(`${req.method} ${req.url}`, testCase.stop);
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test(`restart makes the correct API call ${testCase.variation}`, async function (assert) {
|
2020-07-21 04:55:51 +00:00
|
|
|
const { pretender } = this.server;
|
2021-12-28 16:08:12 +00:00
|
|
|
const allocation = await this.initialize(testCase.id, {
|
|
|
|
region: testCase.region,
|
|
|
|
});
|
2020-07-21 04:55:51 +00:00
|
|
|
|
|
|
|
await this.subject().restart(allocation);
|
|
|
|
const req = pretender.handledRequests[0];
|
|
|
|
assert.equal(`${req.method} ${req.url}`, testCase.restart);
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test(`restart with optional task name makes the correct API call ${testCase.variation}`, async function (assert) {
|
2020-07-21 04:55:51 +00:00
|
|
|
const { pretender } = this.server;
|
2021-12-28 16:08:12 +00:00
|
|
|
const allocation = await this.initialize(testCase.id, {
|
|
|
|
region: testCase.region,
|
|
|
|
});
|
2019-05-20 22:27:20 +00:00
|
|
|
|
2020-07-21 04:55:51 +00:00
|
|
|
await this.subject().restart(allocation, testCase.task);
|
|
|
|
const req = pretender.handledRequests[0];
|
|
|
|
assert.equal(`${req.method} ${req.url}`, testCase.restart);
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.deepEqual(JSON.parse(req.requestBody), {
|
|
|
|
TaskName: testCase.task,
|
|
|
|
});
|
2020-07-21 04:55:51 +00:00
|
|
|
});
|
2019-05-20 22:27:20 +00:00
|
|
|
});
|
|
|
|
});
|