2020-06-18 05:45:43 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
|
|
|
import { click, find, render, settled, waitUntil } from '@ember/test-helpers';
|
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
|
|
|
import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage';
|
|
|
|
import { initialize as fragmentSerializerInitializer } from 'nomad-ui/initializers/fragment-serializer';
|
2020-08-25 15:56:02 +00:00
|
|
|
import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
|
2020-06-18 05:45:43 +00:00
|
|
|
|
|
|
|
const jobName = 'test-job';
|
|
|
|
const jobId = JSON.stringify([jobName, 'default']);
|
|
|
|
|
2020-06-20 14:48:32 +00:00
|
|
|
const countChange = () => {
|
|
|
|
const initial = find('[data-test-task-group-count]').textContent;
|
|
|
|
return () => find('[data-test-task-group-count]').textContent !== initial;
|
|
|
|
};
|
|
|
|
|
2020-06-18 05:45:43 +00:00
|
|
|
let managementToken;
|
|
|
|
let clientToken;
|
|
|
|
|
|
|
|
const makeJob = (server, props = {}) => {
|
|
|
|
// These tests require a job with particular task groups. This requires
|
|
|
|
// mild Mirage surgery.
|
|
|
|
const job = server.create('job', {
|
|
|
|
id: jobName,
|
|
|
|
groupCount: 0,
|
|
|
|
createAllocations: false,
|
|
|
|
shallow: true,
|
|
|
|
...props,
|
|
|
|
});
|
|
|
|
const noScalingGroup = server.create('task-group', {
|
|
|
|
job,
|
|
|
|
name: 'no-scaling',
|
|
|
|
shallow: true,
|
|
|
|
withScaling: false,
|
|
|
|
});
|
|
|
|
const scalingGroup = server.create('task-group', {
|
|
|
|
job,
|
|
|
|
count: 2,
|
|
|
|
name: 'scaling',
|
|
|
|
shallow: true,
|
|
|
|
withScaling: true,
|
|
|
|
});
|
|
|
|
job.update({
|
|
|
|
taskGroupIds: [noScalingGroup.id, scalingGroup.id],
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
module('Integration | Component | task group row', function (hooks) {
|
2020-06-18 05:45:43 +00:00
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
hooks.beforeEach(async function () {
|
2020-06-18 05:45:43 +00:00
|
|
|
fragmentSerializerInitializer(this.owner);
|
|
|
|
this.store = this.owner.lookup('service:store');
|
|
|
|
this.token = this.owner.lookup('service:token');
|
|
|
|
this.server = startMirage();
|
|
|
|
this.server.create('node');
|
|
|
|
|
|
|
|
managementToken = this.server.create('token');
|
|
|
|
clientToken = this.server.create('token');
|
|
|
|
window.localStorage.nomadTokenSecret = managementToken.secretId;
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
hooks.afterEach(function () {
|
2020-06-18 05:45:43 +00:00
|
|
|
this.server.shutdown();
|
|
|
|
window.localStorage.clear();
|
|
|
|
});
|
|
|
|
|
|
|
|
const commonTemplate = hbs`
|
|
|
|
<TaskGroupRow @taskGroup={{group}} />
|
|
|
|
`;
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('Task group row conditionally shows scaling buttons based on the presence of the scaling attr on the task group', async function (assert) {
|
2020-06-20 14:48:32 +00:00
|
|
|
makeJob(this.server, { noActiveDeployment: true });
|
2020-06-18 05:45:43 +00:00
|
|
|
this.token.fetchSelfTokenAndPolicies.perform();
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
const job = await this.store.find('job', jobId);
|
|
|
|
this.set('group', job.taskGroups.findBy('name', 'no-scaling'));
|
|
|
|
|
|
|
|
await render(commonTemplate);
|
|
|
|
assert.notOk(find('[data-test-scale]'));
|
|
|
|
|
|
|
|
this.set('group', job.taskGroups.findBy('name', 'scaling'));
|
|
|
|
|
|
|
|
await settled();
|
|
|
|
assert.ok(find('[data-test-scale]'));
|
2020-08-25 15:56:02 +00:00
|
|
|
|
|
|
|
await componentA11yAudit(this.element, assert);
|
2020-06-18 05:45:43 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('Clicking scaling buttons immediately updates the rendered count but debounces the scaling API request', async function (assert) {
|
2020-06-20 14:48:32 +00:00
|
|
|
makeJob(this.server, { noActiveDeployment: true });
|
2020-06-18 05:45:43 +00:00
|
|
|
this.token.fetchSelfTokenAndPolicies.perform();
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
const job = await this.store.find('job', jobId);
|
|
|
|
this.set('group', job.taskGroups.findBy('name', 'scaling'));
|
|
|
|
|
|
|
|
await render(commonTemplate);
|
|
|
|
assert.equal(find('[data-test-task-group-count]').textContent, 2);
|
|
|
|
|
|
|
|
click('[data-test-scale="increment"]');
|
2020-06-20 14:48:32 +00:00
|
|
|
await waitUntil(countChange());
|
2020-06-18 05:45:43 +00:00
|
|
|
assert.equal(find('[data-test-task-group-count]').textContent, 3);
|
|
|
|
|
|
|
|
click('[data-test-scale="increment"]');
|
2020-06-20 14:48:32 +00:00
|
|
|
await waitUntil(countChange());
|
2020-06-18 05:45:43 +00:00
|
|
|
assert.equal(find('[data-test-task-group-count]').textContent, 4);
|
|
|
|
|
|
|
|
assert.notOk(
|
|
|
|
server.pretender.handledRequests.find(
|
2021-12-28 14:45:20 +00:00
|
|
|
(req) => req.method === 'POST' && req.url.endsWith('/scale')
|
2020-06-18 05:45:43 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
await settled();
|
|
|
|
const scaleRequests = server.pretender.handledRequests.filter(
|
2021-12-28 14:45:20 +00:00
|
|
|
(req) => req.method === 'POST' && req.url.endsWith('/scale')
|
2020-06-18 05:45:43 +00:00
|
|
|
);
|
|
|
|
assert.equal(scaleRequests.length, 1);
|
|
|
|
assert.equal(JSON.parse(scaleRequests[0].requestBody).Count, 4);
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('When the current count is equal to the max count, the increment count button is disabled', async function (assert) {
|
2020-06-20 14:48:32 +00:00
|
|
|
makeJob(this.server, { noActiveDeployment: true });
|
2020-06-18 05:45:43 +00:00
|
|
|
this.token.fetchSelfTokenAndPolicies.perform();
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
const job = await this.store.find('job', jobId);
|
|
|
|
const group = job.taskGroups.findBy('name', 'scaling');
|
|
|
|
group.set('count', group.scaling.max);
|
|
|
|
this.set('group', group);
|
|
|
|
|
|
|
|
await render(commonTemplate);
|
|
|
|
assert.ok(find('[data-test-scale="increment"]:disabled'));
|
2020-08-25 15:56:02 +00:00
|
|
|
|
|
|
|
await componentA11yAudit(this.element, assert);
|
2020-06-18 05:45:43 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('When the current count is equal to the min count, the decrement count button is disabled', async function (assert) {
|
2020-06-20 14:48:32 +00:00
|
|
|
makeJob(this.server, { noActiveDeployment: true });
|
2020-06-18 05:45:43 +00:00
|
|
|
this.token.fetchSelfTokenAndPolicies.perform();
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
const job = await this.store.find('job', jobId);
|
|
|
|
const group = job.taskGroups.findBy('name', 'scaling');
|
|
|
|
group.set('count', group.scaling.min);
|
|
|
|
this.set('group', group);
|
|
|
|
|
|
|
|
await render(commonTemplate);
|
|
|
|
assert.ok(find('[data-test-scale="decrement"]:disabled'));
|
2020-08-25 15:56:02 +00:00
|
|
|
|
|
|
|
await componentA11yAudit(this.element, assert);
|
2020-06-18 05:45:43 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('When there is an active deployment, both scale buttons are disabled', async function (assert) {
|
2020-06-18 05:45:43 +00:00
|
|
|
makeJob(this.server, { activeDeployment: true });
|
|
|
|
this.token.fetchSelfTokenAndPolicies.perform();
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
const job = await this.store.find('job', jobId);
|
|
|
|
this.set('group', job.taskGroups.findBy('name', 'scaling'));
|
|
|
|
|
|
|
|
await render(commonTemplate);
|
|
|
|
assert.ok(find('[data-test-scale="increment"]:disabled'));
|
|
|
|
assert.ok(find('[data-test-scale="decrement"]:disabled'));
|
2020-08-25 15:56:02 +00:00
|
|
|
|
|
|
|
await componentA11yAudit(this.element, assert);
|
2020-06-18 05:45:43 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('When the current ACL token does not have the namespace:scale-job or namespace:submit-job policy rule', async function (assert) {
|
2020-06-20 14:48:32 +00:00
|
|
|
makeJob(this.server, { noActiveDeployment: true });
|
2020-06-18 05:45:43 +00:00
|
|
|
window.localStorage.nomadTokenSecret = clientToken.secretId;
|
|
|
|
this.token.fetchSelfTokenAndPolicies.perform();
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
const job = await this.store.find('job', jobId);
|
|
|
|
this.set('group', job.taskGroups.findBy('name', 'scaling'));
|
|
|
|
|
|
|
|
await render(commonTemplate);
|
|
|
|
assert.ok(find('[data-test-scale="increment"]:disabled'));
|
|
|
|
assert.ok(find('[data-test-scale="decrement"]:disabled'));
|
|
|
|
assert.ok(
|
2021-12-28 14:45:20 +00:00
|
|
|
find('[data-test-scale-controls]').getAttribute('aria-label').includes("You aren't allowed")
|
2020-06-18 05:45:43 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|