github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/integration/components/allocation-row-test.js (about) 1 import { module, test } from 'qunit'; 2 import { setupRenderingTest } from 'ember-qunit'; 3 import hbs from 'htmlbars-inline-precompile'; 4 import generateResources from '../../../mirage/data/generate-resources'; 5 import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage'; 6 import { find, render } from '@ember/test-helpers'; 7 import Response from 'ember-cli-mirage/response'; 8 import { initialize as fragmentSerializerInitializer } from 'nomad-ui/initializers/fragment-serializer'; 9 import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit'; 10 11 module('Integration | Component | allocation row', function(hooks) { 12 setupRenderingTest(hooks); 13 14 hooks.beforeEach(function() { 15 fragmentSerializerInitializer(this.owner); 16 this.store = this.owner.lookup('service:store'); 17 this.server = startMirage(); 18 this.server.create('namespace'); 19 this.server.create('node'); 20 this.server.create('job', { createAllocations: false }); 21 }); 22 23 hooks.afterEach(function() { 24 this.server.shutdown(); 25 }); 26 27 test('Allocation row polls for stats, even when it errors or has an invalid response', async function(assert) { 28 const component = this; 29 30 let currentFrame = 0; 31 let frames = [ 32 JSON.stringify({ ResourceUsage: generateResources() }), 33 JSON.stringify({ ResourceUsage: generateResources() }), 34 null, 35 '<Not>Valid JSON</Not>', 36 JSON.stringify({ ResourceUsage: generateResources() }), 37 ]; 38 39 this.server.get('/client/allocation/:id/stats', function() { 40 const response = frames[++currentFrame]; 41 42 // Disable polling to stop the EC task in the component 43 if (currentFrame >= frames.length) { 44 component.set('enablePolling', false); 45 } 46 47 if (response) { 48 return response; 49 } 50 return new Response(500, {}, ''); 51 }); 52 53 this.server.create('allocation', { clientStatus: 'running' }); 54 await this.store.findAll('allocation'); 55 56 const allocation = this.store.peekAll('allocation').get('firstObject'); 57 58 this.setProperties({ 59 allocation, 60 context: 'job', 61 enablePolling: true, 62 }); 63 64 await render(hbs` 65 <AllocationRow 66 @allocation={{allocation}} 67 @context={{context}} 68 @enablePolling={{enablePolling}} /> 69 `); 70 71 assert.equal( 72 this.server.pretender.handledRequests.filterBy( 73 'url', 74 `/v1/client/allocation/${allocation.get('id')}/stats` 75 ).length, 76 frames.length, 77 'Requests continue to be made after malformed responses and server errors' 78 ); 79 }); 80 81 test('Allocation row shows warning when it requires drivers that are unhealthy on the node it is running on', async function(assert) { 82 const node = this.server.schema.nodes.first(); 83 const drivers = node.drivers; 84 Object.values(drivers).forEach(driver => { 85 driver.Healthy = false; 86 driver.Detected = true; 87 }); 88 node.update({ drivers }); 89 90 this.server.create('allocation', { clientStatus: 'running' }); 91 await this.store.findAll('job'); 92 await this.store.findAll('node'); 93 await this.store.findAll('allocation'); 94 95 const allocation = this.store.peekAll('allocation').get('firstObject'); 96 97 this.setProperties({ 98 allocation, 99 context: 'job', 100 }); 101 102 await render(hbs` 103 <AllocationRow 104 @allocation={{allocation}} 105 @context={{context}} /> 106 `); 107 108 assert.ok(find('[data-test-icon="unhealthy-driver"]'), 'Unhealthy driver icon is shown'); 109 await componentA11yAudit(this.element, assert); 110 }); 111 112 test('Allocation row shows an icon indicator when it was preempted', async function(assert) { 113 const allocId = this.server.create('allocation', 'preempted').id; 114 const allocation = await this.store.findRecord('allocation', allocId); 115 116 this.setProperties({ allocation, context: 'job' }); 117 await render(hbs` 118 <AllocationRow 119 @allocation={{allocation}} 120 @context={{context}} /> 121 `); 122 123 assert.ok(find('[data-test-icon="preemption"]'), 'Preempted icon is shown'); 124 await componentA11yAudit(this.element, assert); 125 }); 126 127 test('when an allocation is not running, the utilization graphs are omitted', async function(assert) { 128 this.setProperties({ 129 context: 'job', 130 enablePolling: false, 131 }); 132 133 // All non-running statuses need to be tested 134 ['pending', 'complete', 'failed', 'lost'].forEach(clientStatus => 135 this.server.create('allocation', { clientStatus }) 136 ); 137 138 await this.store.findAll('allocation'); 139 140 const allocations = this.store.peekAll('allocation'); 141 142 for (const allocation of allocations.toArray()) { 143 this.set('allocation', allocation); 144 await this.render(hbs` 145 <AllocationRow 146 @allocation={{allocation}} 147 @context={{context}} 148 @enablePolling={{enablePolling}} /> 149 `); 150 151 const status = allocation.get('clientStatus'); 152 assert.notOk(find('[data-test-cpu] .inline-chart'), `No CPU chart for ${status}`); 153 assert.notOk(find('[data-test-mem] .inline-chart'), `No Mem chart for ${status}`); 154 } 155 }); 156 });