github.com/hernad/nomad@v1.6.112/ui/tests/acceptance/jobs-list-test.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 /* eslint-disable qunit/require-expect */ 7 import { currentURL } from '@ember/test-helpers'; 8 import { module, test } from 'qunit'; 9 import { setupApplicationTest } from 'ember-qunit'; 10 import { setupMirage } from 'ember-cli-mirage/test-support'; 11 import a11yAudit from 'nomad-ui/tests/helpers/a11y-audit'; 12 import pageSizeSelect from './behaviors/page-size-select'; 13 import JobsList from 'nomad-ui/tests/pages/jobs/list'; 14 import percySnapshot from '@percy/ember'; 15 import faker from 'nomad-ui/mirage/faker'; 16 17 let managementToken, clientToken; 18 19 module('Acceptance | jobs list', function (hooks) { 20 setupApplicationTest(hooks); 21 setupMirage(hooks); 22 23 hooks.beforeEach(function () { 24 // Required for placing allocations (a result of creating jobs) 25 server.create('node-pool'); 26 server.create('node'); 27 28 managementToken = server.create('token'); 29 clientToken = server.create('token'); 30 31 window.localStorage.clear(); 32 window.localStorage.nomadTokenSecret = managementToken.secretId; 33 }); 34 35 test('it passes an accessibility audit', async function (assert) { 36 await JobsList.visit(); 37 await a11yAudit(assert); 38 }); 39 40 test('visiting /jobs', async function (assert) { 41 await JobsList.visit(); 42 43 assert.equal(currentURL(), '/jobs'); 44 assert.equal(document.title, 'Jobs - Nomad'); 45 }); 46 47 test('/jobs should list the first page of jobs sorted by modify index', async function (assert) { 48 faker.seed(1); 49 const jobsCount = JobsList.pageSize + 1; 50 server.createList('job', jobsCount, { createAllocations: true }); 51 52 await JobsList.visit(); 53 54 await percySnapshot(assert); 55 56 const sortedJobs = server.db.jobs.sortBy('modifyIndex').reverse(); 57 assert.equal(JobsList.jobs.length, JobsList.pageSize); 58 JobsList.jobs.forEach((job, index) => { 59 assert.equal(job.name, sortedJobs[index].name, 'Jobs are ordered'); 60 }); 61 }); 62 63 test('each job row should contain information about the job', async function (assert) { 64 server.createList('job', 2); 65 const job = server.db.jobs.sortBy('modifyIndex').reverse()[0]; 66 const taskGroups = server.db.taskGroups.where({ jobId: job.id }); 67 68 await JobsList.visit(); 69 70 const jobRow = JobsList.jobs.objectAt(0); 71 72 assert.equal(jobRow.name, job.name, 'Name'); 73 assert.notOk(jobRow.hasNamespace); 74 assert.equal(jobRow.nodePool, job.nodePool, 'Node Pool'); 75 assert.equal(jobRow.link, `/ui/jobs/${job.id}@default`, 'Detail Link'); 76 assert.equal(jobRow.status, job.status, 'Status'); 77 assert.equal(jobRow.type, typeForJob(job), 'Type'); 78 assert.equal(jobRow.priority, job.priority, 'Priority'); 79 assert.equal(jobRow.taskGroups, taskGroups.length, '# Groups'); 80 }); 81 82 test('each job row should link to the corresponding job', async function (assert) { 83 server.create('job'); 84 const job = server.db.jobs[0]; 85 86 await JobsList.visit(); 87 await JobsList.jobs.objectAt(0).clickName(); 88 89 assert.equal(currentURL(), `/jobs/${job.id}@default`); 90 }); 91 92 test('the new job button transitions to the new job page', async function (assert) { 93 await JobsList.visit(); 94 await JobsList.runJobButton.click(); 95 96 assert.equal(currentURL(), '/jobs/run'); 97 }); 98 99 test('the job run button is disabled when the token lacks permission', async function (assert) { 100 window.localStorage.nomadTokenSecret = clientToken.secretId; 101 102 await JobsList.visit(); 103 104 assert.ok(JobsList.runJobButton.isDisabled); 105 }); 106 107 test('the anonymous policy is fetched to check whether to show the job run button', async function (assert) { 108 window.localStorage.removeItem('nomadTokenSecret'); 109 110 server.create('policy', { 111 id: 'anonymous', 112 name: 'anonymous', 113 rulesJSON: { 114 Namespaces: [ 115 { 116 Name: 'default', 117 Capabilities: ['list-jobs', 'submit-job'], 118 }, 119 ], 120 }, 121 }); 122 123 await JobsList.visit(); 124 assert.notOk(JobsList.runJobButton.isDisabled); 125 }); 126 127 test('when there are no jobs, there is an empty message', async function (assert) { 128 faker.seed(1); 129 await JobsList.visit(); 130 131 await percySnapshot(assert); 132 133 assert.ok(JobsList.isEmpty, 'There is an empty message'); 134 assert.equal( 135 JobsList.emptyState.headline, 136 'No Jobs', 137 'The message is appropriate' 138 ); 139 }); 140 141 test('when there are jobs, but no matches for a search result, there is an empty message', async function (assert) { 142 server.create('job', { name: 'cat 1' }); 143 server.create('job', { name: 'cat 2' }); 144 145 await JobsList.visit(); 146 147 await JobsList.search.fillIn('dog'); 148 assert.ok(JobsList.isEmpty, 'The empty message is shown'); 149 assert.equal( 150 JobsList.emptyState.headline, 151 'No Matches', 152 'The message is appropriate' 153 ); 154 }); 155 156 test('searching resets the current page', async function (assert) { 157 server.createList('job', JobsList.pageSize + 1, { 158 createAllocations: false, 159 }); 160 161 await JobsList.visit(); 162 await JobsList.nextPage(); 163 164 assert.equal( 165 currentURL(), 166 '/jobs?page=2', 167 'Page query param captures page=2' 168 ); 169 170 await JobsList.search.fillIn('foobar'); 171 172 assert.equal(currentURL(), '/jobs?search=foobar', 'No page query param'); 173 }); 174 175 test('when a cluster has namespaces, each job row includes the job namespace', async function (assert) { 176 server.createList('namespace', 2); 177 server.createList('job', 2); 178 const job = server.db.jobs.sortBy('modifyIndex').reverse()[0]; 179 180 await JobsList.visit({ namespace: '*' }); 181 182 const jobRow = JobsList.jobs.objectAt(0); 183 assert.equal(jobRow.namespace, job.namespaceId); 184 }); 185 186 test('when the namespace query param is set, only matching jobs are shown', async function (assert) { 187 server.createList('namespace', 2); 188 const job1 = server.create('job', { 189 namespaceId: server.db.namespaces[0].id, 190 }); 191 const job2 = server.create('job', { 192 namespaceId: server.db.namespaces[1].id, 193 }); 194 195 await JobsList.visit(); 196 assert.equal(JobsList.jobs.length, 2, 'All jobs by default'); 197 198 const firstNamespace = server.db.namespaces[0]; 199 await JobsList.visit({ namespace: firstNamespace.id }); 200 assert.equal(JobsList.jobs.length, 1, 'One job in the default namespace'); 201 assert.equal( 202 JobsList.jobs.objectAt(0).name, 203 job1.name, 204 'The correct job is shown' 205 ); 206 207 const secondNamespace = server.db.namespaces[1]; 208 await JobsList.visit({ namespace: secondNamespace.id }); 209 210 assert.equal( 211 JobsList.jobs.length, 212 1, 213 `One job in the ${secondNamespace.name} namespace` 214 ); 215 assert.equal( 216 JobsList.jobs.objectAt(0).name, 217 job2.name, 218 'The correct job is shown' 219 ); 220 }); 221 222 test('when accessing jobs is forbidden, show a message with a link to the tokens page', async function (assert) { 223 server.pretender.get('/v1/jobs', () => [403, {}, null]); 224 225 await JobsList.visit(); 226 assert.equal(JobsList.error.title, 'Not Authorized'); 227 228 await JobsList.error.seekHelp(); 229 assert.equal(currentURL(), '/settings/tokens'); 230 }); 231 232 function typeForJob(job) { 233 return job.periodic 234 ? 'periodic' 235 : job.parameterized 236 ? 'parameterized' 237 : job.type; 238 } 239 240 test('the jobs list page has appropriate faceted search options', async function (assert) { 241 await JobsList.visit(); 242 243 assert.ok( 244 JobsList.facets.namespace.isHidden, 245 'Namespace facet not found (no namespaces)' 246 ); 247 assert.ok(JobsList.facets.type.isPresent, 'Type facet found'); 248 assert.ok(JobsList.facets.status.isPresent, 'Status facet found'); 249 assert.ok(JobsList.facets.datacenter.isPresent, 'Datacenter facet found'); 250 assert.ok(JobsList.facets.prefix.isPresent, 'Prefix facet found'); 251 }); 252 253 testSingleSelectFacet('Namespace', { 254 facet: JobsList.facets.namespace, 255 paramName: 'namespace', 256 expectedOptions: ['All (*)', 'default', 'namespace-2'], 257 optionToSelect: 'namespace-2', 258 async beforeEach() { 259 server.create('namespace', { id: 'default' }); 260 server.create('namespace', { id: 'namespace-2' }); 261 server.createList('job', 2, { namespaceId: 'default' }); 262 server.createList('job', 2, { namespaceId: 'namespace-2' }); 263 await JobsList.visit(); 264 }, 265 filter(job, selection) { 266 return job.namespaceId === selection; 267 }, 268 }); 269 270 testFacet('Type', { 271 facet: JobsList.facets.type, 272 paramName: 'type', 273 expectedOptions: [ 274 'Batch', 275 'Pack', 276 'Parameterized', 277 'Periodic', 278 'Service', 279 'System', 280 'System Batch', 281 ], 282 async beforeEach() { 283 server.createList('job', 2, { createAllocations: false, type: 'batch' }); 284 server.createList('job', 2, { 285 createAllocations: false, 286 type: 'batch', 287 periodic: true, 288 childrenCount: 0, 289 }); 290 server.createList('job', 2, { 291 createAllocations: false, 292 type: 'batch', 293 parameterized: true, 294 childrenCount: 0, 295 }); 296 server.createList('job', 2, { 297 createAllocations: false, 298 type: 'service', 299 }); 300 await JobsList.visit(); 301 }, 302 filter(job, selection) { 303 let displayType = job.type; 304 if (job.parameterized) displayType = 'parameterized'; 305 if (job.periodic) displayType = 'periodic'; 306 return selection.includes(displayType); 307 }, 308 }); 309 310 testFacet('Status', { 311 facet: JobsList.facets.status, 312 paramName: 'status', 313 expectedOptions: ['Pending', 'Running', 'Dead'], 314 async beforeEach() { 315 server.createList('job', 2, { 316 status: 'pending', 317 createAllocations: false, 318 childrenCount: 0, 319 }); 320 server.createList('job', 2, { 321 status: 'running', 322 createAllocations: false, 323 childrenCount: 0, 324 }); 325 server.createList('job', 2, { 326 status: 'dead', 327 createAllocations: false, 328 childrenCount: 0, 329 }); 330 await JobsList.visit(); 331 }, 332 filter: (job, selection) => selection.includes(job.status), 333 }); 334 335 testFacet('Datacenter', { 336 facet: JobsList.facets.datacenter, 337 paramName: 'dc', 338 expectedOptions(jobs) { 339 const allDatacenters = new Set( 340 jobs.mapBy('datacenters').reduce((acc, val) => acc.concat(val), []) 341 ); 342 return Array.from(allDatacenters).sort(); 343 }, 344 async beforeEach() { 345 server.create('job', { 346 datacenters: ['pdx', 'lax'], 347 createAllocations: false, 348 childrenCount: 0, 349 }); 350 server.create('job', { 351 datacenters: ['pdx', 'ord'], 352 createAllocations: false, 353 childrenCount: 0, 354 }); 355 server.create('job', { 356 datacenters: ['lax', 'jfk'], 357 createAllocations: false, 358 childrenCount: 0, 359 }); 360 server.create('job', { 361 datacenters: ['jfk', 'dfw'], 362 createAllocations: false, 363 childrenCount: 0, 364 }); 365 server.create('job', { 366 datacenters: ['pdx'], 367 createAllocations: false, 368 childrenCount: 0, 369 }); 370 await JobsList.visit(); 371 }, 372 filter: (job, selection) => 373 job.datacenters.find((dc) => selection.includes(dc)), 374 }); 375 376 testFacet('Prefix', { 377 facet: JobsList.facets.prefix, 378 paramName: 'prefix', 379 expectedOptions: ['hashi (3)', 'nmd (2)', 'pre (2)'], 380 async beforeEach() { 381 [ 382 'pre-one', 383 'hashi_one', 384 'nmd.one', 385 'one-alone', 386 'pre_two', 387 'hashi.two', 388 'hashi-three', 389 'nmd_two', 390 'noprefix', 391 ].forEach((name) => { 392 server.create('job', { 393 name, 394 createAllocations: false, 395 childrenCount: 0, 396 }); 397 }); 398 await JobsList.visit(); 399 }, 400 filter: (job, selection) => 401 selection.find((prefix) => job.name.startsWith(prefix)), 402 }); 403 404 test('when the facet selections result in no matches, the empty state states why', async function (assert) { 405 server.createList('job', 2, { 406 status: 'pending', 407 createAllocations: false, 408 childrenCount: 0, 409 }); 410 411 await JobsList.visit(); 412 413 await JobsList.facets.status.toggle(); 414 await JobsList.facets.status.options.objectAt(1).toggle(); 415 assert.ok(JobsList.isEmpty, 'There is an empty message'); 416 assert.equal( 417 JobsList.emptyState.headline, 418 'No Matches', 419 'The message is appropriate' 420 ); 421 }); 422 423 test('the jobs list is immediately filtered based on query params', async function (assert) { 424 server.create('job', { type: 'batch', createAllocations: false }); 425 server.create('job', { type: 'service', createAllocations: false }); 426 427 await JobsList.visit({ type: JSON.stringify(['batch']) }); 428 429 assert.equal( 430 JobsList.jobs.length, 431 1, 432 'Only one job shown due to query param' 433 ); 434 }); 435 436 test('when the user has a client token that has a namespace with a policy to run a job', async function (assert) { 437 const READ_AND_WRITE_NAMESPACE = 'read-and-write-namespace'; 438 const READ_ONLY_NAMESPACE = 'read-only-namespace'; 439 440 server.create('namespace', { id: READ_AND_WRITE_NAMESPACE }); 441 server.create('namespace', { id: READ_ONLY_NAMESPACE }); 442 443 const policy = server.create('policy', { 444 id: 'something', 445 name: 'something', 446 rulesJSON: { 447 Namespaces: [ 448 { 449 Name: READ_AND_WRITE_NAMESPACE, 450 Capabilities: ['submit-job'], 451 }, 452 { 453 Name: READ_ONLY_NAMESPACE, 454 Capabilities: ['list-job'], 455 }, 456 ], 457 }, 458 }); 459 460 clientToken.policyIds = [policy.id]; 461 clientToken.save(); 462 463 window.localStorage.nomadTokenSecret = clientToken.secretId; 464 465 await JobsList.visit({ namespace: READ_AND_WRITE_NAMESPACE }); 466 assert.notOk(JobsList.runJobButton.isDisabled); 467 468 await JobsList.visit({ namespace: READ_ONLY_NAMESPACE }); 469 assert.notOk(JobsList.runJobButton.isDisabled); 470 }); 471 472 test('when the user has no client tokens that allow them to run a job', async function (assert) { 473 const READ_AND_WRITE_NAMESPACE = 'read-and-write-namespace'; 474 const READ_ONLY_NAMESPACE = 'read-only-namespace'; 475 476 server.create('namespace', { id: READ_ONLY_NAMESPACE }); 477 478 const policy = server.create('policy', { 479 id: 'something', 480 name: 'something', 481 rulesJSON: { 482 Namespaces: [ 483 { 484 Name: READ_ONLY_NAMESPACE, 485 Capabilities: ['list-job'], 486 }, 487 ], 488 }, 489 }); 490 491 clientToken.policyIds = [policy.id]; 492 clientToken.save(); 493 494 window.localStorage.nomadTokenSecret = clientToken.secretId; 495 496 await JobsList.visit({ namespace: READ_AND_WRITE_NAMESPACE }); 497 assert.ok(JobsList.runJobButton.isDisabled); 498 499 await JobsList.visit({ namespace: READ_ONLY_NAMESPACE }); 500 assert.ok(JobsList.runJobButton.isDisabled); 501 }); 502 503 pageSizeSelect({ 504 resourceName: 'job', 505 pageObject: JobsList, 506 pageObjectList: JobsList.jobs, 507 async setup() { 508 server.createList('job', JobsList.pageSize, { 509 shallow: true, 510 createAllocations: false, 511 }); 512 await JobsList.visit(); 513 }, 514 }); 515 516 async function facetOptions(assert, beforeEach, facet, expectedOptions) { 517 await beforeEach(); 518 await facet.toggle(); 519 520 let expectation; 521 if (typeof expectedOptions === 'function') { 522 expectation = expectedOptions(server.db.jobs); 523 } else { 524 expectation = expectedOptions; 525 } 526 527 assert.deepEqual( 528 facet.options.map((option) => option.label.trim()), 529 expectation, 530 'Options for facet are as expected' 531 ); 532 } 533 534 function testSingleSelectFacet( 535 label, 536 { facet, paramName, beforeEach, filter, expectedOptions, optionToSelect } 537 ) { 538 test(`the ${label} facet has the correct options`, async function (assert) { 539 await facetOptions(assert, beforeEach, facet, expectedOptions); 540 }); 541 542 test(`the ${label} facet filters the jobs list by ${label}`, async function (assert) { 543 await beforeEach(); 544 await facet.toggle(); 545 546 const option = facet.options.findOneBy('label', optionToSelect); 547 const selection = option.key; 548 await option.select(); 549 550 const expectedJobs = server.db.jobs 551 .filter((job) => filter(job, selection)) 552 .sortBy('modifyIndex') 553 .reverse(); 554 555 JobsList.jobs.forEach((job, index) => { 556 assert.equal( 557 job.id, 558 expectedJobs[index].id, 559 `Job at ${index} is ${expectedJobs[index].id}` 560 ); 561 }); 562 }); 563 564 test(`selecting an option in the ${label} facet updates the ${paramName} query param`, async function (assert) { 565 await beforeEach(); 566 await facet.toggle(); 567 568 const option = facet.options.objectAt(1); 569 const selection = option.key; 570 await option.select(); 571 572 assert.ok( 573 currentURL().includes(`${paramName}=${selection}`), 574 'URL has the correct query param key and value' 575 ); 576 }); 577 } 578 579 function testFacet( 580 label, 581 { facet, paramName, beforeEach, filter, expectedOptions } 582 ) { 583 test(`the ${label} facet has the correct options`, async function (assert) { 584 await facetOptions(assert, beforeEach, facet, expectedOptions); 585 }); 586 587 test(`the ${label} facet filters the jobs list by ${label}`, async function (assert) { 588 let option; 589 590 await beforeEach(); 591 await facet.toggle(); 592 593 option = facet.options.objectAt(0); 594 await option.toggle(); 595 596 const selection = [option.key]; 597 const expectedJobs = server.db.jobs 598 .filter((job) => filter(job, selection)) 599 .sortBy('modifyIndex') 600 .reverse(); 601 602 JobsList.jobs.forEach((job, index) => { 603 assert.equal( 604 job.id, 605 expectedJobs[index].id, 606 `Job at ${index} is ${expectedJobs[index].id}` 607 ); 608 }); 609 }); 610 611 test(`selecting multiple options in the ${label} facet results in a broader search`, async function (assert) { 612 const selection = []; 613 614 await beforeEach(); 615 await facet.toggle(); 616 617 const option1 = facet.options.objectAt(0); 618 const option2 = facet.options.objectAt(1); 619 await option1.toggle(); 620 selection.push(option1.key); 621 await option2.toggle(); 622 selection.push(option2.key); 623 624 const expectedJobs = server.db.jobs 625 .filter((job) => filter(job, selection)) 626 .sortBy('modifyIndex') 627 .reverse(); 628 629 JobsList.jobs.forEach((job, index) => { 630 assert.equal( 631 job.id, 632 expectedJobs[index].id, 633 `Job at ${index} is ${expectedJobs[index].id}` 634 ); 635 }); 636 }); 637 638 test(`selecting options in the ${label} facet updates the ${paramName} query param`, async function (assert) { 639 const selection = []; 640 641 await beforeEach(); 642 await facet.toggle(); 643 644 const option1 = facet.options.objectAt(0); 645 const option2 = facet.options.objectAt(1); 646 await option1.toggle(); 647 selection.push(option1.key); 648 await option2.toggle(); 649 selection.push(option2.key); 650 651 assert.ok( 652 currentURL().includes(encodeURIComponent(JSON.stringify(selection))), 653 'URL has the correct query param key and value' 654 ); 655 }); 656 657 test('the run job button works when filters are set', async function (assert) { 658 ['pre-one', 'pre-two', 'pre-three'].forEach((name) => { 659 server.create('job', { 660 name, 661 createAllocations: false, 662 childrenCount: 0, 663 }); 664 }); 665 666 await JobsList.visit(); 667 668 await JobsList.facets.prefix.toggle(); 669 await JobsList.facets.prefix.options[0].toggle(); 670 671 await JobsList.runJobButton.click(); 672 assert.equal(currentURL(), '/jobs/run'); 673 }); 674 } 675 });