open-nomad/ui/tests/acceptance/job-run-test.js

146 lines
4.1 KiB
JavaScript
Raw Normal View History

2019-03-13 00:08:16 +00:00
import { currentURL } from '@ember/test-helpers';
2018-08-17 00:22:58 +00:00
import { assign } from '@ember/polyfills';
2019-03-13 00:04:16 +00:00
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
2019-09-26 18:47:07 +00:00
import { setupMirage } from 'ember-cli-mirage/test-support';
import a11yAudit from 'nomad-ui/tests/helpers/a11y-audit';
import setupCodeMirror from 'nomad-ui/tests/helpers/codemirror';
2018-08-17 00:22:58 +00:00
import JobRun from 'nomad-ui/tests/pages/jobs/run';
const newJobName = 'new-job';
const newJobTaskGroupName = 'redis';
2018-08-17 00:22:58 +00:00
let managementToken, clientToken;
2021-12-28 14:45:20 +00:00
const jsonJob = (overrides) => {
2018-08-17 00:22:58 +00:00
return JSON.stringify(
assign(
{},
{
Name: newJobName,
Namespace: 'default',
Datacenters: ['dc1'],
Priority: 50,
TaskGroups: [
{
Name: newJobTaskGroupName,
Tasks: [
{
Name: 'redis',
2018-08-17 00:22:58 +00:00
Driver: 'docker',
},
],
2018-08-17 00:22:58 +00:00
},
],
2018-08-17 00:22:58 +00:00
},
overrides
),
null,
2
);
};
2021-12-28 14:45:20 +00:00
module('Acceptance | job run', function (hooks) {
2019-03-13 00:04:16 +00:00
setupApplicationTest(hooks);
2019-03-13 01:09:19 +00:00
setupMirage(hooks);
setupCodeMirror(hooks);
2019-03-13 00:04:16 +00:00
2021-12-28 14:45:20 +00:00
hooks.beforeEach(function () {
2018-08-17 00:22:58 +00:00
// Required for placing allocations (a result of creating jobs)
server.create('node');
managementToken = server.create('token');
clientToken = server.create('token');
window.localStorage.nomadTokenSecret = managementToken.secretId;
2019-03-13 00:04:16 +00:00
});
2018-08-17 00:22:58 +00:00
2021-12-28 14:45:20 +00:00
test('it passes an accessibility audit', async function (assert) {
assert.expect(1);
await JobRun.visit();
await a11yAudit(assert);
});
2021-12-28 14:45:20 +00:00
test('visiting /jobs/run', async function (assert) {
await JobRun.visit();
2018-08-17 00:22:58 +00:00
assert.equal(currentURL(), '/jobs/run');
assert.equal(document.title, 'Run a job - Nomad');
2018-08-17 00:22:58 +00:00
});
2021-12-28 14:45:20 +00:00
test('when submitting a job, the site redirects to the new job overview page', async function (assert) {
2019-03-13 00:04:16 +00:00
const spec = jsonJob();
2018-08-17 00:22:58 +00:00
await JobRun.visit();
2018-08-17 00:22:58 +00:00
await JobRun.editor.editor.fillIn(spec);
await JobRun.editor.plan();
await JobRun.editor.run();
2018-08-17 00:22:58 +00:00
assert.equal(
currentURL(),
`/jobs/${newJobName}`,
`Redirected to the job overview page for ${newJobName}`
);
});
2021-12-28 14:45:20 +00:00
test('when submitting a job to a different namespace, the redirect to the job overview page takes namespace into account', async function (assert) {
2019-03-13 00:04:16 +00:00
const newNamespace = 'second-namespace';
2018-08-17 00:22:58 +00:00
2019-03-13 00:04:16 +00:00
server.create('namespace', { id: newNamespace });
const spec = jsonJob({ Namespace: newNamespace });
2018-08-17 00:22:58 +00:00
await JobRun.visit();
2018-08-17 00:22:58 +00:00
await JobRun.editor.editor.fillIn(spec);
await JobRun.editor.plan();
await JobRun.editor.run();
2018-08-17 00:22:58 +00:00
assert.equal(
currentURL(),
`/jobs/${newJobName}?namespace=${newNamespace}`,
`Redirected to the job overview page for ${newJobName} and switched the namespace to ${newNamespace}`
);
});
2021-12-28 14:45:20 +00:00
test('when the user doesnt have permission to run a job, redirects to the job overview page', async function (assert) {
window.localStorage.nomadTokenSecret = clientToken.secretId;
await JobRun.visit();
assert.equal(currentURL(), '/jobs');
});
2021-12-28 14:45:20 +00:00
test('when using client token user can still go to job page if they have correct permissions', async function (assert) {
const clientTokenWithPolicy = server.create('token');
const newNamespace = 'second-namespace';
server.create('namespace', { id: newNamespace });
server.create('job', {
groupCount: 0,
createAllocations: false,
shallow: true,
noActiveDeployment: true,
namespaceId: newNamespace,
});
const policy = server.create('policy', {
id: 'something',
name: 'something',
rulesJSON: {
Namespaces: [
{
Name: newNamespace,
Capabilities: ['scale-job', 'submit-job', 'read-job', 'list-jobs'],
},
],
},
});
clientTokenWithPolicy.policyIds = [policy.id];
clientTokenWithPolicy.save();
window.localStorage.nomadTokenSecret = clientTokenWithPolicy.secretId;
await JobRun.visit({ namespace: newNamespace });
assert.equal(currentURL(), `/jobs/run?namespace=${newNamespace}`);
});
2018-08-17 00:22:58 +00:00
});