2021-09-07 16:27:33 +00:00
|
|
|
/* eslint-disable ember/no-test-module-for */
|
2021-12-28 19:30:38 +00:00
|
|
|
/* eslint-disable qunit/require-expect */
|
|
|
|
/* eslint-disable qunit/no-conditional-assertions */
|
2021-07-20 22:27:41 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupApplicationTest } from 'ember-qunit';
|
|
|
|
import { setupMirage } from 'ember-cli-mirage/test-support';
|
|
|
|
import setupCodeMirror from 'nomad-ui/tests/helpers/codemirror';
|
|
|
|
import JobDispatch from 'nomad-ui/tests/pages/jobs/dispatch';
|
|
|
|
import JobDetail from 'nomad-ui/tests/pages/jobs/detail';
|
|
|
|
import a11yAudit from 'nomad-ui/tests/helpers/a11y-audit';
|
|
|
|
import { currentURL } from '@ember/test-helpers';
|
|
|
|
|
|
|
|
const REQUIRED_INDICATOR = '*';
|
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
moduleForJobDispatch('Acceptance | job dispatch', () => {
|
|
|
|
server.createList('namespace', 2);
|
|
|
|
const namespace = server.db.namespaces[0];
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
return server.create('job', 'parameterized', {
|
|
|
|
status: 'running',
|
|
|
|
namespaceId: namespace.name,
|
2021-07-20 22:27:41 +00:00
|
|
|
});
|
2021-09-07 16:27:33 +00:00
|
|
|
});
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
moduleForJobDispatch('Acceptance | job dispatch (with namespace)', () => {
|
|
|
|
server.createList('namespace', 2);
|
|
|
|
const namespace = server.db.namespaces[1];
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
return server.create('job', 'parameterized', {
|
|
|
|
status: 'running',
|
|
|
|
namespaceId: namespace.name,
|
2021-07-20 22:27:41 +00:00
|
|
|
});
|
2021-09-07 16:27:33 +00:00
|
|
|
});
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
function moduleForJobDispatch(title, jobFactory) {
|
|
|
|
let job, namespace, managementToken, clientToken;
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
module(title, function (hooks) {
|
2021-09-07 16:27:33 +00:00
|
|
|
setupApplicationTest(hooks);
|
|
|
|
setupCodeMirror(hooks);
|
|
|
|
setupMirage(hooks);
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
hooks.beforeEach(function () {
|
2021-09-07 16:27:33 +00:00
|
|
|
// Required for placing allocations (a result of dispatching jobs)
|
|
|
|
server.create('node');
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
job = jobFactory();
|
|
|
|
namespace = server.db.namespaces.find(job.namespaceId);
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
managementToken = server.create('token');
|
|
|
|
clientToken = server.create('token');
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
window.localStorage.nomadTokenSecret = managementToken.secretId;
|
|
|
|
});
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('it passes an accessibility audit', async function (assert) {
|
2022-02-16 14:22:41 +00:00
|
|
|
await JobDispatch.visit({ id: `${job.id}@${namespace.name}` });
|
2021-09-07 16:27:33 +00:00
|
|
|
await a11yAudit(assert);
|
|
|
|
});
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('the dispatch button is displayed with management token', async function (assert) {
|
2022-02-16 14:22:41 +00:00
|
|
|
await JobDetail.visit({ id: `${job.id}@${namespace.name}` });
|
2021-09-07 16:27:33 +00:00
|
|
|
assert.notOk(JobDetail.dispatchButton.isDisabled);
|
2021-07-20 22:27:41 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('the dispatch button is displayed when allowed', async function (assert) {
|
2021-09-07 16:27:33 +00:00
|
|
|
window.localStorage.nomadTokenSecret = clientToken.secretId;
|
|
|
|
|
|
|
|
const policy = server.create('policy', {
|
|
|
|
id: 'dispatch',
|
|
|
|
name: 'dispatch',
|
|
|
|
rulesJSON: {
|
|
|
|
Namespaces: [
|
|
|
|
{
|
|
|
|
Name: namespace.name,
|
|
|
|
Capabilities: ['list-jobs', 'dispatch-job'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
clientToken.policyIds = [policy.id];
|
|
|
|
clientToken.save();
|
|
|
|
|
2022-02-16 14:22:41 +00:00
|
|
|
await JobDetail.visit({ id: `${job.id}@${namespace.name}` });
|
2021-09-07 16:27:33 +00:00
|
|
|
assert.notOk(JobDetail.dispatchButton.isDisabled);
|
|
|
|
|
|
|
|
// Reset clientToken policies.
|
|
|
|
clientToken.policyIds = [];
|
|
|
|
clientToken.save();
|
2021-08-03 17:50:43 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('the dispatch button is disabled when not allowed', async function (assert) {
|
2021-09-07 16:27:33 +00:00
|
|
|
window.localStorage.nomadTokenSecret = clientToken.secretId;
|
2021-08-03 17:50:43 +00:00
|
|
|
|
2022-02-16 14:22:41 +00:00
|
|
|
await JobDetail.visit({ id: `${job.id}@${namespace.name}` });
|
2021-09-07 16:27:33 +00:00
|
|
|
assert.ok(JobDetail.dispatchButton.isDisabled);
|
|
|
|
});
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('all meta fields are displayed', async function (assert) {
|
2022-02-16 14:22:41 +00:00
|
|
|
await JobDispatch.visit({ id: `${job.id}@${namespace.name}` });
|
2021-09-07 16:27:33 +00:00
|
|
|
assert.equal(
|
|
|
|
JobDispatch.metaFields.length,
|
2021-12-28 16:08:12 +00:00
|
|
|
job.parameterizedJob.MetaOptional.length +
|
|
|
|
job.parameterizedJob.MetaRequired.length
|
2021-09-07 16:27:33 +00:00
|
|
|
);
|
|
|
|
});
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('required meta fields are properly indicated', async function (assert) {
|
2022-02-16 14:22:41 +00:00
|
|
|
await JobDispatch.visit({ id: `${job.id}@${namespace.name}` });
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
JobDispatch.metaFields.forEach((f) => {
|
2021-09-07 16:27:33 +00:00
|
|
|
const hasIndicator = f.label.includes(REQUIRED_INDICATOR);
|
2021-12-28 16:08:12 +00:00
|
|
|
const isRequired = job.parameterizedJob.MetaRequired.includes(
|
|
|
|
f.field.id
|
|
|
|
);
|
2021-09-07 16:27:33 +00:00
|
|
|
|
|
|
|
if (isRequired) {
|
|
|
|
assert.ok(hasIndicator, `${f.label} contains required indicator.`);
|
|
|
|
} else {
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.notOk(
|
|
|
|
hasIndicator,
|
|
|
|
`${f.label} doesn't contain required indicator.`
|
|
|
|
);
|
2021-09-07 16:27:33 +00:00
|
|
|
}
|
|
|
|
});
|
2021-07-20 22:27:41 +00:00
|
|
|
});
|
2021-09-07 16:27:33 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('job without meta fields', async function (assert) {
|
2021-09-07 16:27:33 +00:00
|
|
|
const jobWithoutMeta = server.create('job', 'parameterized', {
|
|
|
|
status: 'running',
|
|
|
|
namespaceId: namespace.name,
|
|
|
|
parameterizedJob: {
|
|
|
|
MetaRequired: null,
|
|
|
|
MetaOptional: null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-02-16 14:22:41 +00:00
|
|
|
await JobDispatch.visit({ id: `${jobWithoutMeta.id}@${namespace.name}` });
|
2021-09-07 16:27:33 +00:00
|
|
|
assert.ok(JobDispatch.dispatchButton.isPresent);
|
2021-07-20 22:27:41 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('payload text area is hidden when forbidden', async function (assert) {
|
2021-09-07 16:27:33 +00:00
|
|
|
job.parameterizedJob.Payload = 'forbidden';
|
|
|
|
job.save();
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2022-02-16 14:22:41 +00:00
|
|
|
await JobDispatch.visit({ id: `${job.id}@${namespace.name}` });
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
assert.ok(JobDispatch.payload.emptyMessage.isPresent);
|
|
|
|
assert.notOk(JobDispatch.payload.editor.isPresent);
|
|
|
|
});
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('payload is indicated as required', async function (assert) {
|
2021-09-07 16:27:33 +00:00
|
|
|
const jobPayloadRequired = server.create('job', 'parameterized', {
|
|
|
|
status: 'running',
|
|
|
|
namespaceId: namespace.name,
|
|
|
|
parameterizedJob: {
|
|
|
|
Payload: 'required',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const jobPayloadOptional = server.create('job', 'parameterized', {
|
|
|
|
status: 'running',
|
|
|
|
namespaceId: namespace.name,
|
|
|
|
parameterizedJob: {
|
|
|
|
Payload: 'optional',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
await JobDispatch.visit({
|
2022-02-16 14:22:41 +00:00
|
|
|
id: `${jobPayloadRequired.id}@${namespace.name}`,
|
2021-12-28 16:08:12 +00:00
|
|
|
});
|
2021-09-07 16:27:33 +00:00
|
|
|
|
|
|
|
let payloadTitle = JobDispatch.payload.title;
|
|
|
|
assert.ok(
|
|
|
|
payloadTitle.includes(REQUIRED_INDICATOR),
|
|
|
|
`${payloadTitle} contains required indicator.`
|
|
|
|
);
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
await JobDispatch.visit({
|
2022-02-16 14:22:41 +00:00
|
|
|
id: `${jobPayloadOptional.id}@${namespace.name}`,
|
2021-12-28 16:08:12 +00:00
|
|
|
});
|
2021-09-07 16:27:33 +00:00
|
|
|
|
|
|
|
payloadTitle = JobDispatch.payload.title;
|
|
|
|
assert.notOk(
|
|
|
|
payloadTitle.includes(REQUIRED_INDICATOR),
|
|
|
|
`${payloadTitle} doesn't contain required indicator.`
|
|
|
|
);
|
|
|
|
});
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('dispatch a job', async function (assert) {
|
2021-09-07 16:27:33 +00:00
|
|
|
function countDispatchChildren() {
|
2021-12-28 16:08:12 +00:00
|
|
|
return server.db.jobs.where((j) =>
|
|
|
|
j.id.startsWith(`${job.id}/`)
|
|
|
|
).length;
|
2021-09-07 16:27:33 +00:00
|
|
|
}
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2022-02-16 14:22:41 +00:00
|
|
|
await JobDispatch.visit({ id: `${job.id}@${namespace.name}` });
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
// Fill form.
|
2021-12-28 14:45:20 +00:00
|
|
|
JobDispatch.metaFields.map((f) => f.field.input('meta value'));
|
2021-09-07 16:27:33 +00:00
|
|
|
JobDispatch.payload.editor.fillIn('payload');
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
const childrenCountBefore = countDispatchChildren();
|
|
|
|
await JobDispatch.dispatchButton.click();
|
|
|
|
const childrenCountAfter = countDispatchChildren();
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
assert.equal(childrenCountAfter, childrenCountBefore + 1);
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.ok(
|
|
|
|
currentURL().startsWith(`/jobs/${encodeURIComponent(`${job.id}/`)}`)
|
|
|
|
);
|
2021-09-07 16:27:33 +00:00
|
|
|
assert.ok(JobDetail.jobName);
|
|
|
|
});
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('fail when required meta field is empty', async function (assert) {
|
2021-09-07 16:27:33 +00:00
|
|
|
// Make sure we have a required meta param.
|
|
|
|
job.parameterizedJob.MetaRequired = ['required'];
|
|
|
|
job.parameterizedJob.Payload = 'forbidden';
|
|
|
|
job.save();
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2022-02-16 14:22:41 +00:00
|
|
|
await JobDispatch.visit({ id: `${job.id}@${namespace.name}` });
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
// Fill only optional meta params.
|
2021-12-28 14:45:20 +00:00
|
|
|
JobDispatch.optionalMetaFields.map((f) => f.field.input('meta value'));
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
await JobDispatch.dispatchButton.click();
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
assert.ok(JobDispatch.hasError, 'Dispatch error message is shown');
|
|
|
|
});
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('fail when required payload is empty', async function (assert) {
|
2021-09-07 16:27:33 +00:00
|
|
|
job.parameterizedJob.MetaRequired = [];
|
|
|
|
job.parameterizedJob.Payload = 'required';
|
|
|
|
job.save();
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2022-02-16 14:22:41 +00:00
|
|
|
await JobDispatch.visit({ id: `${job.id}@${namespace.name}` });
|
2021-09-07 16:27:33 +00:00
|
|
|
await JobDispatch.dispatchButton.click();
|
2021-07-20 22:27:41 +00:00
|
|
|
|
2021-09-07 16:27:33 +00:00
|
|
|
assert.ok(JobDispatch.hasError, 'Dispatch error message is shown');
|
|
|
|
});
|
2021-07-20 22:27:41 +00:00
|
|
|
});
|
2021-09-07 16:27:33 +00:00
|
|
|
}
|