open-nomad/ui/tests/helpers/module-for-job.js

302 lines
9.3 KiB
JavaScript
Raw Normal View History

/* eslint-disable qunit/require-expect */
/* eslint-disable qunit/no-conditional-assertions */
import { currentURL } from '@ember/test-helpers';
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';
2018-07-11 02:20:02 +00:00
import JobDetail from 'nomad-ui/tests/pages/jobs/detail';
// eslint-disable-next-line ember/no-test-module-for
2021-12-28 16:08:12 +00:00
export default function moduleForJob(
title,
context,
jobFactory,
additionalTests
) {
let job;
2022-01-20 15:39:02 +00:00
module(title, function (hooks) {
setupApplicationTest(hooks);
2019-03-13 01:09:19 +00:00
setupMirage(hooks);
2022-01-20 15:39:02 +00:00
hooks.before(function () {
if (context !== 'allocations' && context !== 'children') {
throw new Error(
`Invalid context provided to moduleForJob, expected either "allocations" or "children", got ${context}`
);
}
});
2022-01-20 15:39:02 +00:00
hooks.beforeEach(async function () {
server.create('node');
job = jobFactory();
if (!job.namespace || job.namespace === 'default') {
await JobDetail.visit({ id: job.id });
} else {
await JobDetail.visit({ id: job.id, namespace: job.namespace });
}
});
2022-01-20 15:39:02 +00:00
test('visiting /jobs/:job_id', async function (assert) {
assert.equal(
currentURL(),
urlWithNamespace(`/jobs/${encodeURIComponent(job.id)}`, job.namespace)
);
assert.equal(document.title, `Job ${job.name} - Nomad`);
});
2022-01-20 15:39:02 +00:00
test('the subnav links to overview', async function (assert) {
await JobDetail.tabFor('overview').visit();
assert.equal(
currentURL(),
urlWithNamespace(`/jobs/${encodeURIComponent(job.id)}`, job.namespace)
);
});
2022-01-20 15:39:02 +00:00
test('the subnav links to definition', async function (assert) {
await JobDetail.tabFor('definition').visit();
assert.equal(
currentURL(),
2021-12-28 16:08:12 +00:00
urlWithNamespace(
`/jobs/${encodeURIComponent(job.id)}/definition`,
job.namespace
)
);
});
2022-01-20 15:39:02 +00:00
test('the subnav links to versions', async function (assert) {
await JobDetail.tabFor('versions').visit();
assert.equal(
currentURL(),
2021-12-28 16:08:12 +00:00
urlWithNamespace(
`/jobs/${encodeURIComponent(job.id)}/versions`,
job.namespace
)
);
2018-07-11 02:28:54 +00:00
});
2022-01-20 15:39:02 +00:00
test('the subnav links to evaluations', async function (assert) {
await JobDetail.tabFor('evaluations').visit();
assert.equal(
currentURL(),
2021-12-28 16:08:12 +00:00
urlWithNamespace(
`/jobs/${encodeURIComponent(job.id)}/evaluations`,
job.namespace
)
);
});
2022-01-20 15:39:02 +00:00
test('the title buttons are dependent on job status', async function (assert) {
if (job.status === 'dead') {
assert.ok(JobDetail.start.isPresent);
assert.notOk(JobDetail.stop.isPresent);
assert.notOk(JobDetail.execButton.isPresent);
} else {
assert.notOk(JobDetail.start.isPresent);
assert.ok(JobDetail.stop.isPresent);
assert.ok(JobDetail.execButton.isPresent);
}
});
if (context === 'allocations') {
2022-01-20 15:39:02 +00:00
test('allocations for the job are shown in the overview', async function (assert) {
assert.ok(
JobDetail.allocationsSummary.isPresent,
'Allocations are shown in the summary section'
);
assert.ok(
JobDetail.childrenSummary.isHidden,
'Children are not shown in the summary section'
);
});
2022-01-20 15:39:02 +00:00
test('clicking in an allocation row navigates to that allocation', async function (assert) {
const allocationRow = JobDetail.allocations[0];
const allocationId = allocationRow.id;
await allocationRow.visitRow();
assert.equal(
currentURL(),
`/allocations/${allocationId}`,
'Allocation row links to allocation detail'
);
});
2022-01-20 15:39:02 +00:00
test('clicking legend item navigates to a pre-filtered allocations table', async function (assert) {
2021-12-28 16:08:12 +00:00
const legendItem =
JobDetail.allocationsSummary.legend.clickableItems[1];
const status = legendItem.label;
await legendItem.click();
const encodedStatus = encodeURIComponent(JSON.stringify([status]));
const expectedURL = new URL(
2021-12-28 16:08:12 +00:00
urlWithNamespace(
`/jobs/${job.name}/clients?status=${encodedStatus}`,
job.namespace
),
window.location
);
const gotURL = new URL(currentURL(), window.location);
assert.deepEqual(gotURL.path, expectedURL.path);
assert.deepEqual(gotURL.searchParams, expectedURL.searchParams);
});
2022-01-20 15:39:02 +00:00
test('clicking in a slice takes you to a pre-filtered allocations table', async function (assert) {
const slice = JobDetail.allocationsSummary.slices[1];
const status = slice.label;
await slice.click();
const encodedStatus = encodeURIComponent(JSON.stringify([status]));
const expectedURL = new URL(
2022-01-18 15:36:10 +00:00
urlWithNamespace(
2021-12-28 16:08:12 +00:00
`/jobs/${encodeURIComponent(
job.name
)}/allocations?status=${encodedStatus}`,
2022-01-18 15:36:10 +00:00
job.namespace
),
window.location
);
const gotURL = new URL(currentURL(), window.location);
assert.deepEqual(gotURL.pathname, expectedURL.pathname);
// Sort and compare URL query params.
gotURL.searchParams.sort();
expectedURL.searchParams.sort();
2021-12-28 16:08:12 +00:00
assert.equal(
gotURL.searchParams.toString(),
expectedURL.searchParams.toString()
);
});
}
if (context === 'children') {
2022-01-20 15:39:02 +00:00
test('children for the job are shown in the overview', async function (assert) {
2021-12-28 16:08:12 +00:00
assert.ok(
JobDetail.childrenSummary.isPresent,
'Children are shown in the summary section'
);
assert.ok(
JobDetail.allocationsSummary.isHidden,
'Allocations are not shown in the summary section'
);
});
}
for (var testName in additionalTests) {
2022-01-20 15:39:02 +00:00
test(testName, async function (assert) {
await additionalTests[testName].call(this, job, assert);
});
}
});
}
// eslint-disable-next-line ember/no-test-module-for
2021-12-28 16:08:12 +00:00
export function moduleForJobWithClientStatus(
title,
jobFactory,
additionalTests
) {
let job;
2022-01-20 15:39:02 +00:00
module(title, function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
2022-01-20 15:39:02 +00:00
hooks.beforeEach(async function () {
const clients = server.createList('node', 3, {
datacenter: 'dc1',
2022-01-20 15:39:02 +00:00
status: 'ready',
});
job = jobFactory();
2022-01-20 15:39:02 +00:00
clients.forEach((c) => {
server.create('allocation', { jobId: job.id, nodeId: c.id });
});
if (!job.namespace || job.namespace === 'default') {
await JobDetail.visit({ id: job.id });
} else {
await JobDetail.visit({ id: job.id, namespace: job.namespace });
}
});
2022-01-20 15:39:02 +00:00
test('the subnav links to clients', async function (assert) {
await JobDetail.tabFor('clients').visit();
assert.equal(
currentURL(),
2021-12-28 16:08:12 +00:00
urlWithNamespace(
`/jobs/${encodeURIComponent(job.id)}/clients`,
job.namespace
)
);
});
2022-01-20 15:39:02 +00:00
test('job status summary is shown in the overview', async function (assert) {
assert.ok(
JobDetail.jobClientStatusSummary.isPresent,
'Summary bar is displayed in the Job Status in Client summary section'
);
});
2022-01-20 15:39:02 +00:00
test('clicking legend item navigates to a pre-filtered clients table', async function (assert) {
2021-12-28 16:08:12 +00:00
const legendItem =
JobDetail.jobClientStatusSummary.legend.clickableItems[0];
const status = legendItem.label;
await legendItem.click();
const encodedStatus = encodeURIComponent(JSON.stringify([status]));
const expectedURL = new URL(
2021-12-28 16:08:12 +00:00
urlWithNamespace(
`/jobs/${job.name}/clients?status=${encodedStatus}`,
job.namespace
),
window.location
);
const gotURL = new URL(currentURL(), window.location);
assert.deepEqual(gotURL.path, expectedURL.path);
assert.deepEqual(gotURL.searchParams, expectedURL.searchParams);
});
2022-01-20 15:39:02 +00:00
test('clicking in a slice takes you to a pre-filtered clients table', async function (assert) {
const slice = JobDetail.jobClientStatusSummary.slices[0];
const status = slice.label;
await slice.click();
const encodedStatus = encodeURIComponent(JSON.stringify([status]));
const expectedURL = new URL(
2021-12-28 16:08:12 +00:00
urlWithNamespace(
`/jobs/${job.name}/clients?status=${encodedStatus}`,
job.namespace
),
window.location
);
const gotURL = new URL(currentURL(), window.location);
assert.deepEqual(gotURL.pathname, expectedURL.pathname);
// Sort and compare URL query params.
gotURL.searchParams.sort();
expectedURL.searchParams.sort();
2021-12-28 16:08:12 +00:00
assert.equal(
gotURL.searchParams.toString(),
expectedURL.searchParams.toString()
);
});
for (var testName in additionalTests) {
2022-01-20 15:39:02 +00:00
test(testName, async function (assert) {
await additionalTests[testName].call(this, job, assert);
});
}
});
}
function urlWithNamespace(url, namespace) {
if (!namespace || namespace === 'default') {
return url;
}
const parts = url.split('?');
const params = new URLSearchParams(parts[1]);
params.set('namespace', namespace);
return `${parts[0]}?${params.toString()}`;
}