github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/ui/tests/unit/adapters/node-test.js (about) 1 import { run } from '@ember/runloop'; 2 import { module, test } from 'qunit'; 3 import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage'; 4 import { setupTest } from 'ember-qunit'; 5 import { settled } from '@ember/test-helpers'; 6 7 module('Unit | Adapter | Node', function(hooks) { 8 setupTest(hooks); 9 10 hooks.beforeEach(function() { 11 this.store = this.owner.lookup('service:store'); 12 this.subject = () => this.store.adapterFor('node'); 13 14 this.server = startMirage(); 15 this.server.create('node', { id: 'node-1' }); 16 this.server.create('node', { id: 'node-2' }); 17 this.server.create('job', { id: 'job-1', createAllocations: false }); 18 19 this.server.create('allocation', { id: 'node-1-1', nodeId: 'node-1' }); 20 this.server.create('allocation', { id: 'node-1-2', nodeId: 'node-1' }); 21 this.server.create('allocation', { id: 'node-2-1', nodeId: 'node-2' }); 22 this.server.create('allocation', { id: 'node-2-2', nodeId: 'node-2' }); 23 }); 24 25 hooks.afterEach(function() { 26 this.server.shutdown(); 27 }); 28 29 test('findHasMany removes old related models from the store', async function(assert) { 30 // Fetch the model and related allocations 31 let node = await run(() => this.store.findRecord('node', 'node-1')); 32 let allocations = await run(() => findHasMany(node, 'allocations')); 33 assert.equal( 34 allocations.get('length'), 35 this.server.db.allocations.where({ nodeId: node.get('id') }).length, 36 'Allocations returned from the findHasMany matches the db state' 37 ); 38 39 await settled(); 40 server.db.allocations.remove('node-1-1'); 41 42 allocations = await run(() => findHasMany(node, 'allocations')); 43 const dbAllocations = this.server.db.allocations.where({ nodeId: node.get('id') }); 44 assert.equal( 45 allocations.get('length'), 46 dbAllocations.length, 47 'Allocations returned from the findHasMany matches the db state' 48 ); 49 assert.equal( 50 this.store.peekAll('allocation').get('length'), 51 dbAllocations.length, 52 'Server-side deleted allocation was removed from the store' 53 ); 54 }); 55 56 test('findHasMany does not remove old unrelated models from the store', async function(assert) { 57 // Fetch the first node and related allocations 58 const node = await run(() => this.store.findRecord('node', 'node-1')); 59 await run(() => findHasMany(node, 'allocations')); 60 61 // Also fetch the second node and related allocations; 62 const node2 = await run(() => this.store.findRecord('node', 'node-2')); 63 await run(() => findHasMany(node2, 'allocations')); 64 65 await settled(); 66 assert.deepEqual( 67 this.store 68 .peekAll('allocation') 69 .mapBy('id') 70 .sort(), 71 ['node-1-1', 'node-1-2', 'node-2-1', 'node-2-2'], 72 'All allocations for the first and second node are in the store' 73 ); 74 75 server.db.allocations.remove('node-1-1'); 76 77 // Reload the related allocations now that one was removed server-side 78 await run(() => findHasMany(node, 'allocations')); 79 assert.deepEqual( 80 this.store 81 .peekAll('allocation') 82 .mapBy('id') 83 .sort(), 84 ['node-1-2', 'node-2-1', 'node-2-2'], 85 'The deleted allocation is removed from the store and the allocations associated with the other node are untouched' 86 ); 87 }); 88 89 test('setEligible makes the correct POST request to /:node_id/eligibility', async function(assert) { 90 const { pretender } = this.server; 91 const node = await run(() => this.store.findRecord('node', 'node-1')); 92 93 await this.subject().setEligible(node); 94 95 const request = pretender.handledRequests.lastObject; 96 assert.equal( 97 request.url, 98 `/v1/node/${node.id}/eligibility`, 99 'Request was made to /:node_id/eligibility' 100 ); 101 assert.equal(request.method, 'POST', 'Request was made with the POST method'); 102 assert.deepEqual( 103 JSON.parse(request.requestBody), 104 { 105 NodeID: node.id, 106 Eligibility: 'eligible', 107 }, 108 'POST request is made with the correct body arguments' 109 ); 110 }); 111 112 test('setIneligible makes the correct POST request to /:node_id/eligibility', async function(assert) { 113 const { pretender } = this.server; 114 const node = await run(() => this.store.findRecord('node', 'node-1')); 115 116 await this.subject().setIneligible(node); 117 118 const request = pretender.handledRequests.lastObject; 119 assert.equal( 120 request.url, 121 `/v1/node/${node.id}/eligibility`, 122 'Request was made to /:node_id/eligibility' 123 ); 124 assert.equal(request.method, 'POST', 'Request was made with the POST method'); 125 assert.deepEqual( 126 JSON.parse(request.requestBody), 127 { 128 NodeID: node.id, 129 Eligibility: 'ineligible', 130 }, 131 'POST request is made with the correct body arguments' 132 ); 133 }); 134 135 test('drain makes the correct POST request to /:node_id/drain with appropriate defaults', async function(assert) { 136 const { pretender } = this.server; 137 const node = await run(() => this.store.findRecord('node', 'node-1')); 138 139 await this.subject().drain(node); 140 141 const request = pretender.handledRequests.lastObject; 142 assert.equal(request.url, `/v1/node/${node.id}/drain`, 'Request was made to /:node_id/drain'); 143 assert.equal(request.method, 'POST', 'Request was made with the POST method'); 144 assert.deepEqual( 145 JSON.parse(request.requestBody), 146 { 147 NodeID: node.id, 148 DrainSpec: { 149 Deadline: 0, 150 IgnoreSystemJobs: true, 151 }, 152 }, 153 'POST request is made with the default body arguments' 154 ); 155 }); 156 157 test('drain makes the correct POST request to /:node_id/drain with the provided drain spec', async function(assert) { 158 const { pretender } = this.server; 159 const node = await run(() => this.store.findRecord('node', 'node-1')); 160 161 const spec = { Deadline: 123456789, IgnoreSystemJobs: false }; 162 await this.subject().drain(node, spec); 163 164 const request = pretender.handledRequests.lastObject; 165 assert.deepEqual( 166 JSON.parse(request.requestBody), 167 { 168 NodeID: node.id, 169 DrainSpec: { 170 Deadline: spec.Deadline, 171 IgnoreSystemJobs: spec.IgnoreSystemJobs, 172 }, 173 }, 174 'POST request is made with the drain spec as body arguments' 175 ); 176 }); 177 178 test('forceDrain makes the correct POST request to /:node_id/drain with appropriate defaults', async function(assert) { 179 const { pretender } = this.server; 180 const node = await run(() => this.store.findRecord('node', 'node-1')); 181 182 await this.subject().forceDrain(node); 183 184 const request = pretender.handledRequests.lastObject; 185 assert.equal(request.url, `/v1/node/${node.id}/drain`, 'Request was made to /:node_id/drain'); 186 assert.equal(request.method, 'POST', 'Request was made with the POST method'); 187 assert.deepEqual( 188 JSON.parse(request.requestBody), 189 { 190 NodeID: node.id, 191 DrainSpec: { 192 Deadline: -1, 193 IgnoreSystemJobs: true, 194 }, 195 }, 196 'POST request is made with the default body arguments' 197 ); 198 }); 199 200 test('forceDrain makes the correct POST request to /:node_id/drain with the provided drain spec', async function(assert) { 201 const { pretender } = this.server; 202 const node = await run(() => this.store.findRecord('node', 'node-1')); 203 204 const spec = { Deadline: 123456789, IgnoreSystemJobs: false }; 205 await this.subject().forceDrain(node, spec); 206 207 const request = pretender.handledRequests.lastObject; 208 assert.equal(request.url, `/v1/node/${node.id}/drain`, 'Request was made to /:node_id/drain'); 209 assert.equal(request.method, 'POST', 'Request was made with the POST method'); 210 assert.deepEqual( 211 JSON.parse(request.requestBody), 212 { 213 NodeID: node.id, 214 DrainSpec: { 215 Deadline: -1, 216 IgnoreSystemJobs: spec.IgnoreSystemJobs, 217 }, 218 }, 219 'POST request is made with the drain spec, except deadline is not overridden' 220 ); 221 }); 222 223 test('cancelDrain makes the correct POST request to /:node_id/drain', async function(assert) { 224 const { pretender } = this.server; 225 const node = await run(() => this.store.findRecord('node', 'node-1')); 226 227 await this.subject().cancelDrain(node); 228 229 const request = pretender.handledRequests.lastObject; 230 assert.equal(request.url, `/v1/node/${node.id}/drain`, 'Request was made to /:node_id/drain'); 231 assert.equal(request.method, 'POST', 'Request was made with the POST method'); 232 assert.deepEqual( 233 JSON.parse(request.requestBody), 234 { 235 NodeID: node.id, 236 DrainSpec: null, 237 }, 238 'POST request is made with a null drain spec' 239 ); 240 }); 241 }); 242 243 // Using fetchLink on a model's hasMany relationship exercises the adapter's 244 // findHasMany method as well normalizing the response and pushing it to the store 245 function findHasMany(model, relationshipName) { 246 const relationship = model.relationshipFor(relationshipName); 247 return model.hasMany(relationship.key).reload(); 248 }