open-nomad/ui/tests/acceptance/job-run-test.js
Phil Renaud 3db9f11c37
[feat] Nomad Job Templates (#15746)
* Extend variables under the nomad path prefix to allow for job-templates (#15570)

* Extend variables under the nomad path prefix to allow for job-templates

* Add job-templates to error message hinting

* RadioCard component for Job Templates (#15582)

* chore: add

* test: component API

* ui: component template

* refact: remove  bc naming collission

* styles: remove SASS var causing conflicts

* Disallow specific variable at nomad/job-templates (#15681)

* Disallows variables at exactly nomad/job-templates

* idiomatic refactor

* Expanding nomad job init to accept a template flag (#15571)

* Adding a string flag for templates on job init

* data-down actions-up version of a custom template editor within variable

* Dont force grid on job template editor

* list-templates flag started

* Correctly slice from end of path name

* Pre-review cleanup

* Variable form acceptance test for job template editing

* Some review cleanup

* List Job templates test

* Example from template test

* Using must.assertions instead of require etc

* ui: add choose template button (#15596)

* ui: add new routes

* chore: update file directory

* ui: add choose template button

* test: button and page navigation

* refact: update var name

* ui: use `Button` component from `HDS` (#15607)

* ui: integrate  buttons

* refact: remove  helper

* ui: remove icons on non-tertiary buttons

* refact: update normalize method for key/value pairs (#15612)

* `revert`: `onCancel` for `JobDefinition`

The `onCancel` method isn't included in the component API for `JobEditor` and the primary cancel behavior exists outside of the component. With the exception of the `JobDefinition` page where we include this button in the top right of the component instead of next to the `Plan` button.

* style: increase button size

* style: keep lime green

* ui: select template (#15613)

* ui: deprecate unused component

* ui: deprecate tests

* ui: jobs.run.templates.index

* ui: update logic to handle templates

* refact: revert key/value changes

* style: padding for cards + buttons

* temp: fixtures for mirage testing

* Revert "refact: revert key/value changes"

This reverts commit 124e95d12140be38fc921f7e15243034092c4063.

* ui: guard template for unsaved job

* ui: handle reading template variable

* Revert "refact: update normalize method for key/value pairs (#15612)"

This reverts commit 6f5ffc9b610702aee7c47fbff742cc81f819ab74.

* revert: remove test fixtures

* revert: prettier problems

* refact: test doesnt need filter expression

* styling: button sizes and responsive cards

* refact: remove route guarding

* ui: update variable adapter

* refact: remove model editing behavior

* refact: model should query variables to populate editor

* ui: clear qp on exit

* refact: cleanup deprecated API

* refact: query all namespaces

* refact: deprecate action

* ui: rely on  collection

* refact: patch deprecate transition API

* refact: patch test to expect namespace qp

* styling: padding, conditionals

* ui: flashMessage on 404

* test: update for o(n+1) query

* ui: create new job template (#15744)

* refact: remove unused code

* refact: add type safety

* test: select template flow

* test: add data-test attrs

* chore: remove dead code

* test: create new job flow

* ui: add create button

* ui: create job template

* refact: no need for wildcard

* refact:  record instead of delete

* styling: spacing

* ui: add error handling and form validation to job create template (#15767)

* ui: handle server side errors

* ui: show error to prevent duplicate

* refact: conditional namespace

* ui: save as template flow (#15787)

* bug:  patches failing tests associated with `pretender` (#15812)

* refact: update assertion

* refact: test set-up

* ui: job templates manager view (#15815)

* ui: manager list view

* test: edit flow

* refact: deprecate column-helper

* ui: template edit and delete flow (#15823)

* ui: manager list view

* refact: update title

* refact: update permissions

* ui: template edit page

* bug: typo

* refact: update toast messages

* bug:  clear selections on exit (#15827)

* bug:  clear controllers on exit

* test: mirage config changes (#15828)

* refact: deprecate column-helper

* style: update z-index for HDS

* Revert "style: update z-index for HDS"

This reverts commit d3d87ceab6d083f7164941587448607838944fc1.

* refact: update delete button

* refact: edit redirect

* refact: patch reactivity issues

* styling: fixed width

* refact: override defaults

* styling: edit text causing overflow

* styling:  add inline text

Co-authored-by: Phil Renaud <phil.renaud@hashicorp.com>

* bug: edit `text` to `template`

Co-authored-by: Phil Renaud <phil.renaud@hashicorp.com>

Co-authored-by: Phil Renaud <phil.renaud@hashicorp.com>

* test:  delete flow job templates (#15896)

* refact: edit names

* bug:  set correct ref to store

* chore: trim whitespace:

* test: delete flow

* bug: reactively update view (#15904)

* Initialized default jobs (#15856)

* Initialized default jobs

* More jobs scaffolded

* Better commenting on a couple example job specs

* Adapter doing the work

* fall back to epic config

* Label format helper and custom serialization logic

* Test updates to account for a never-empty state

* Test suite uses settled and maintain RecordArray in adapter return

* Updates to hello-world and variables example jobspecs

* Parameterized job gets optional payload output

* Formatting changes for param and service discovery job templates

* Multi-group service discovery job

* Basic test for default templates (#15965)

* Basic test for default templates

* Percy snapshot for manage page

* Some late-breaking design changes

* Some copy edits to the header paragraphs for job templates (#15967)

* Added some init options for job templates (#15994)

* Async method for populating default job templates from the variable adapter

---------

Co-authored-by: Jai <41024828+ChaiWithJai@users.noreply.github.com>
2023-02-02 10:37:40 -05:00

595 lines
18 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import AdapterError from '@ember-data/adapter/error';
import {
click,
currentRouteName,
currentURL,
fillIn,
visit,
settled,
} from '@ember/test-helpers';
import { assign } from '@ember/polyfills';
import { module, test } from 'qunit';
import {
selectChoose,
clickTrigger,
} from 'ember-power-select/test-support/helpers';
import { setupApplicationTest } from 'ember-qunit';
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';
import JobRun from 'nomad-ui/tests/pages/jobs/run';
import percySnapshot from '@percy/ember';
const newJobName = 'new-job';
const newJobTaskGroupName = 'redis';
const newJobNamespace = 'default';
let managementToken, clientToken;
const jsonJob = (overrides) => {
return JSON.stringify(
assign(
{},
{
Name: newJobName,
Namespace: newJobNamespace,
Datacenters: ['dc1'],
Priority: 50,
TaskGroups: [
{
Name: newJobTaskGroupName,
Tasks: [
{
Name: 'redis',
Driver: 'docker',
},
],
},
],
},
overrides
),
null,
2
);
};
module('Acceptance | job run', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
setupCodeMirror(hooks);
hooks.beforeEach(function () {
// 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;
});
test('it passes an accessibility audit', async function (assert) {
assert.expect(1);
await JobRun.visit();
await a11yAudit(assert);
});
test('visiting /jobs/run', async function (assert) {
await JobRun.visit();
assert.equal(currentURL(), '/jobs/run');
assert.equal(document.title, 'Run a job - Nomad');
});
test('when submitting a job, the site redirects to the new job overview page', async function (assert) {
const spec = jsonJob();
await JobRun.visit();
await JobRun.editor.editor.fillIn(spec);
await JobRun.editor.plan();
await JobRun.editor.run();
assert.equal(
currentURL(),
`/jobs/${newJobName}@${newJobNamespace}`,
`Redirected to the job overview page for ${newJobName}`
);
});
test('when submitting a job to a different namespace, the redirect to the job overview page takes namespace into account', async function (assert) {
const newNamespace = 'second-namespace';
server.create('namespace', { id: newNamespace });
const spec = jsonJob({ Namespace: newNamespace });
await JobRun.visit();
await JobRun.editor.editor.fillIn(spec);
await JobRun.editor.plan();
await JobRun.editor.run();
assert.equal(
currentURL(),
`/jobs/${newJobName}@${newNamespace}`,
`Redirected to the job overview page for ${newJobName} and switched the namespace to ${newNamespace}`
);
});
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');
});
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}`);
});
module('job template flow', function () {
test('allows user with the correct permissions to fill in the editor using a job template', async function (assert) {
assert.expect(10);
// Arrange
await JobRun.visit();
assert
.dom('[data-test-choose-template]')
.exists('A button allowing a user to select a template appears.');
server.get('/vars', function (_server, fakeRequest) {
assert.deepEqual(
fakeRequest.queryParams,
{
prefix: 'nomad/job-templates',
namespace: '*',
},
'It makes a request to the /vars endpoint with the appropriate query parameters for job templates.'
);
return [
{
ID: 'nomad/job-templates/foo',
Namespace: 'default',
Path: 'nomad/job-templates/foo',
},
];
});
server.get(
'/var/nomad%2Fjob-templates%2Ffoo',
function (_server, fakeRequest) {
assert.deepEqual(
fakeRequest.queryParams,
{
namespace: 'default',
},
'Dispatches O(n+1) query to retrive items.'
);
return {
ID: 'nomad/job-templates/foo',
Namespace: 'default',
Path: 'nomad/job-templates/foo',
Items: {
template: 'Hello World!',
label: 'foo',
},
};
}
);
// Act
await click('[data-test-choose-template]');
assert.equal(currentRouteName(), 'jobs.run.templates.index');
// Assert
assert
.dom('[data-test-template-list]')
.exists('A list of available job templates is rendered.');
assert
.dom('[data-test-apply]')
.exists('A button to apply the selected templated is displayed.');
assert
.dom('[data-test-cancel]')
.exists('A button to cancel the template selection is displayed.');
await click('[data-test-template-card=Foo]');
await click('[data-test-apply]');
assert.equal(
currentURL(),
'/jobs/run?template=nomad%2Fjob-templates%2Ffoo%40default'
);
assert.dom('[data-test-editor]').containsText('Hello World!');
});
test('a user can create their own job template', async function (assert) {
assert.expect(7);
// Arrange
await JobRun.visit();
await click('[data-test-choose-template]');
// Assert
assert
.dom('[data-test-template-card]')
.exists({ count: 4 }, 'A list of default job templates is rendered.');
await click('[data-test-create-new-button]');
assert.equal(currentRouteName(), 'jobs.run.templates.new');
await fillIn('[data-test-template-name]', 'foo');
await fillIn('[data-test-template-description]', 'foo-bar-baz');
const codeMirror = getCodeMirrorInstance('[data-test-template-json]');
codeMirror.setValue(jsonJob());
server.put('/var/:varId', function (_server, fakeRequest) {
assert.deepEqual(
JSON.parse(fakeRequest.requestBody),
{
Path: 'nomad/job-templates/foo',
CreateIndex: null,
ModifyIndex: null,
Namespace: 'default',
ID: 'nomad/job-templates/foo',
Items: { description: 'foo-bar-baz', template: jsonJob() },
},
'It makes a PUT request to the /vars/:varId endpoint with the appropriate request body for job templates.'
);
return {
Items: { description: 'foo-bar-baz', template: jsonJob() },
Namespace: 'default',
Path: 'nomad/job-templates/foo',
};
});
server.get('/vars', function (_server, fakeRequest) {
assert.deepEqual(
fakeRequest.queryParams,
{
prefix: 'nomad/job-templates',
namespace: '*',
},
'It makes a request to the /vars endpoint with the appropriate query parameters for job templates.'
);
return [
{
ID: 'nomad/job-templates/foo',
Namespace: 'default',
Path: 'nomad/job-templates/foo',
},
];
});
server.get(
'/var/nomad%2Fjob-templates%2Ffoo',
function (_server, fakeRequest) {
assert.deepEqual(
fakeRequest.queryParams,
{
namespace: 'default',
},
'Dispatches O(n+1) query to retrive items.'
);
return {
ID: 'nomad/job-templates/foo',
Namespace: 'default',
Path: 'nomad/job-templates/foo',
Items: {
template: 'qud',
label: 'foo',
},
};
}
);
await click('[data-test-save-template]');
assert.equal(currentRouteName(), 'jobs.run.templates.index');
assert
.dom('[data-test-template-card=Foo]')
.exists('The newly created template appears in the list.');
});
test('a toast notification alerts the user if there is an error saving the newly created job template', async function (assert) {
assert.expect(5);
// Arrange
await JobRun.visit();
await click('[data-test-choose-template]');
// Assert
assert
.dom('[data-test-template-card]')
.exists({ count: 4 }, 'A list of default job templates is rendered.');
await click('[data-test-create-new-button]');
assert.equal(currentRouteName(), 'jobs.run.templates.new');
assert
.dom('[data-test-save-template]')
.isDisabled('the save button should be disabled if no path is set');
await fillIn('[data-test-template-name]', 'try@');
await fillIn('[data-test-template-description]', 'foo-bar-baz');
const codeMirror = getCodeMirrorInstance('[data-test-template-json]');
codeMirror.setValue(jsonJob());
server.put('/var/:varId?cas=0', function () {
return new AdapterError({
detail: `invalid path "nomad/job-templates/try@"`,
status: 500,
});
});
await click('[data-test-save-template]');
assert.equal(
currentRouteName(),
'jobs.run.templates.new',
'We do not navigate away from the page if an error is returned by the API.'
);
assert
.dom('.flash-message.alert-error')
.exists('A toast error message pops up.');
});
test('a user cannot create a job template if one with the same name and namespace already exists', async function (assert) {
assert.expect(4);
// Arrange
await JobRun.visit();
await click('[data-test-choose-template]');
server.create('variable', {
path: 'nomad/job-templates/foo',
namespace: 'default',
id: 'nomad/job-templates/foo',
});
server.create('namespace', { id: 'test' });
this.system = this.owner.lookup('service:system');
this.system.shouldShowNamespaces = true;
// Assert
assert
.dom('[data-test-template-card]')
.exists({ count: 4 }, 'A list of default job templates is rendered.');
await click('[data-test-create-new-button]');
assert.equal(currentRouteName(), 'jobs.run.templates.new');
await fillIn('[data-test-template-name]', 'foo');
assert
.dom('[data-test-duplicate-error]')
.exists('an error message alerts the user');
await clickTrigger('[data-test-namespace-facet]');
await selectChoose('[data-test-namespace-facet]', 'test');
assert
.dom('[data-test-duplicate-error]')
.doesNotExist(
'an error disappears when name or namespace combination is unique'
);
// Clean-up
this.system.shouldShowNamespaces = false;
});
test('a user can save code from the editor as a template', async function (assert) {
assert.expect(4);
// Arrange
await JobRun.visit();
await JobRun.editor.editor.fillIn(jsonJob());
await click('[data-test-save-as-template]');
assert.equal(
currentRouteName(),
'jobs.run.templates.new',
'We navigate template creation page.'
);
// Assert
assert
.dom('[data-test-template-name]')
.hasNoText('No template name is prefilled.');
assert
.dom('[data-test-template-description]')
.hasNoText('No template description is prefilled.');
const codeMirror = getCodeMirrorInstance('[data-test-template-json]');
const json = codeMirror.getValue();
assert.equal(
json,
jsonJob(),
'Template is filled out with text from the editor.'
);
});
test('a user can edit a template', async function (assert) {
assert.expect(5);
// Arrange
server.create('variable', {
path: 'nomad/job-templates/foo',
namespace: 'default',
id: 'nomad/job-templates/foo',
Items: {},
});
await visit('/jobs/run/templates/manage');
assert.equal(currentRouteName(), 'jobs.run.templates.manage');
assert
.dom('[data-test-template-list]')
.exists('A list of templates is visible');
await percySnapshot(assert);
await click('[data-test-edit-template="nomad/job-templates/foo"]');
assert.equal(
currentRouteName(),
'jobs.run.templates.template',
'Navigates to edit template view'
);
server.put('/var/:varId', function (_server, fakeRequest) {
assert.deepEqual(
JSON.parse(fakeRequest.requestBody),
{
Path: 'nomad/job-templates/foo',
CreateIndex: null,
ModifyIndex: null,
Namespace: 'default',
ID: 'nomad/job-templates/foo',
Items: { description: 'baz qud thud' },
},
'It makes a PUT request to the /vars/:varId endpoint with the appropriate request body for job templates.'
);
return {
Items: { description: 'baz qud thud' },
Namespace: 'default',
Path: 'nomad/job-templates/foo',
};
});
await fillIn('[data-test-template-description]', 'baz qud thud');
await click('[data-test-edit-template]');
assert.equal(
currentRouteName(),
'jobs.run.templates.index',
'We navigate back to the templates view.'
);
});
test('a user can delete a template', async function (assert) {
assert.expect(5);
// Arrange
server.create('variable', {
path: 'nomad/job-templates/foo',
namespace: 'default',
id: 'nomad/job-templates/foo',
Items: {},
});
server.create('variable', {
path: 'nomad/job-templates/bar',
namespace: 'default',
id: 'nomad/job-templates/bar',
Items: {},
});
server.create('variable', {
path: 'nomad/job-templates/baz',
namespace: 'default',
id: 'nomad/job-templates/baz',
Items: {},
});
await visit('/jobs/run/templates/manage');
assert.equal(currentRouteName(), 'jobs.run.templates.manage');
assert
.dom('[data-test-template-list]')
.exists('A list of templates is visible');
await click('[data-test-idle-button]');
await click('[data-test-confirm-button]');
assert
.dom('[data-test-edit-template="nomad/job-templates/foo"]')
.doesNotExist('The template is removed from the list.');
await click('[data-test-edit-template="nomad/job-templates/bar"]');
await click('[data-test-idle-button]');
await click('[data-test-confirm-button]');
assert.equal(
currentRouteName(),
'jobs.run.templates.manage',
'We navigate back to the templates manager view.'
);
assert
.dom('[data-test-edit-template="nomad/job-templates/bar"]')
.doesNotExist('The template is removed from the list.');
});
test('a user sees accurate template information', async function (assert) {
assert.expect(3);
// Arrange
server.create('variable', {
path: 'nomad/job-templates/foo',
namespace: 'default',
id: 'nomad/job-templates/foo',
Items: {
template: 'qud',
label: 'foo',
description: 'bar baz',
},
});
await visit('/jobs/run/templates');
assert.equal(currentRouteName(), 'jobs.run.templates.index');
assert.dom('[data-test-template-card="Foo"]').exists();
this.store = this.owner.lookup('service:store');
this.store.unloadAll();
await settled();
assert
.dom('[data-test-template-card="Foo"]')
.doesNotExist(
'The template reactively updates to changes in the Ember Data Store.'
);
});
test('default templates', async function (assert) {
assert.expect(4);
const NUMBER_OF_DEFAULT_TEMPLATES = 4;
await visit('/jobs/run/templates');
assert.equal(currentRouteName(), 'jobs.run.templates.index');
assert
.dom('[data-test-template-card]')
.exists({ count: NUMBER_OF_DEFAULT_TEMPLATES });
await percySnapshot(assert);
await click('[data-test-template-card="Hello world"]');
await click('[data-test-apply]');
assert.equal(
currentURL(),
'/jobs/run?template=nomad%2Fjob-templates%2Fdefault%2Fhello-world'
);
assert.dom('[data-test-editor]').includesText('job "hello-world"');
});
});
});