github.com/hernad/nomad@v1.6.112/ui/tests/unit/models/job-test.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 import { run } from '@ember/runloop'; 7 import { module, test } from 'qunit'; 8 import { setupTest } from 'ember-qunit'; 9 import sinon from 'sinon'; 10 11 module('Unit | Model | job', function (hooks) { 12 setupTest(hooks); 13 14 test('should expose aggregate allocations derived from task groups', function (assert) { 15 const store = this.owner.lookup('service:store'); 16 let summary; 17 run(() => { 18 summary = store.createRecord('job-summary', { 19 taskGroupSummaries: [ 20 { 21 name: 'one', 22 queuedAllocs: 1, 23 startingAllocs: 2, 24 runningAllocs: 3, 25 completeAllocs: 4, 26 failedAllocs: 5, 27 lostAllocs: 6, 28 unknownAllocs: 7, 29 }, 30 { 31 name: 'two', 32 queuedAllocs: 2, 33 startingAllocs: 4, 34 runningAllocs: 6, 35 completeAllocs: 8, 36 failedAllocs: 10, 37 lostAllocs: 12, 38 unknownAllocs: 14, 39 }, 40 { 41 name: 'three', 42 queuedAllocs: 3, 43 startingAllocs: 6, 44 runningAllocs: 9, 45 completeAllocs: 12, 46 failedAllocs: 15, 47 lostAllocs: 18, 48 unknownAllocs: 21, 49 }, 50 ], 51 }); 52 }); 53 54 const job = run(() => 55 this.owner.lookup('service:store').createRecord('job', { 56 summary, 57 name: 'example', 58 taskGroups: [ 59 { 60 name: 'one', 61 count: 0, 62 tasks: [], 63 }, 64 { 65 name: 'two', 66 count: 0, 67 tasks: [], 68 }, 69 { 70 name: 'three', 71 count: 0, 72 tasks: [], 73 }, 74 ], 75 }) 76 ); 77 78 assert.equal( 79 job.get('totalAllocs'), 80 job 81 .get('taskGroups') 82 .mapBy('summary.totalAllocs') 83 .reduce((sum, allocs) => sum + allocs, 0), 84 'totalAllocs is the sum of all group totalAllocs' 85 ); 86 87 assert.equal( 88 job.get('queuedAllocs'), 89 job 90 .get('taskGroups') 91 .mapBy('summary.queuedAllocs') 92 .reduce((sum, allocs) => sum + allocs, 0), 93 'queuedAllocs is the sum of all group queuedAllocs' 94 ); 95 96 assert.equal( 97 job.get('startingAllocs'), 98 job 99 .get('taskGroups') 100 .mapBy('summary.startingAllocs') 101 .reduce((sum, allocs) => sum + allocs, 0), 102 'startingAllocs is the sum of all group startingAllocs' 103 ); 104 105 assert.equal( 106 job.get('runningAllocs'), 107 job 108 .get('taskGroups') 109 .mapBy('summary.runningAllocs') 110 .reduce((sum, allocs) => sum + allocs, 0), 111 'runningAllocs is the sum of all group runningAllocs' 112 ); 113 114 assert.equal( 115 job.get('completeAllocs'), 116 job 117 .get('taskGroups') 118 .mapBy('summary.completeAllocs') 119 .reduce((sum, allocs) => sum + allocs, 0), 120 'completeAllocs is the sum of all group completeAllocs' 121 ); 122 123 assert.equal( 124 job.get('failedAllocs'), 125 job 126 .get('taskGroups') 127 .mapBy('summary.failedAllocs') 128 .reduce((sum, allocs) => sum + allocs, 0), 129 'failedAllocs is the sum of all group failedAllocs' 130 ); 131 132 assert.equal( 133 job.get('lostAllocs'), 134 job 135 .get('taskGroups') 136 .mapBy('summary.lostAllocs') 137 .reduce((sum, allocs) => sum + allocs, 0), 138 'lostAllocs is the sum of all group lostAllocs' 139 ); 140 }); 141 142 module('#parse', function () { 143 test('it parses JSON', async function (assert) { 144 const store = this.owner.lookup('service:store'); 145 const model = store.createRecord('job'); 146 147 model.set('_newDefinition', '{"name": "Tomster"}'); 148 149 const setIdByPayloadSpy = sinon.spy(model, 'setIdByPayload'); 150 151 const result = await model.parse(); 152 153 assert.deepEqual( 154 model.get('_newDefinitionJSON'), 155 { name: 'Tomster' }, 156 'Sets _newDefinitionJSON correctly' 157 ); 158 assert.ok( 159 setIdByPayloadSpy.calledWith({ name: 'Tomster' }), 160 'setIdByPayload is called with the parsed JSON' 161 ); 162 assert.deepEqual(result, '{"name": "Tomster"}', 'Returns the JSON input'); 163 }); 164 165 test('it dispatches a POST request to the /parse endpoint (eagerly assumes HCL specification) if JSON parse method errors', async function (assert) { 166 assert.expect(2); 167 168 const store = this.owner.lookup('service:store'); 169 const model = store.createRecord('job'); 170 171 model.set('_newDefinition', 'invalidJSON'); 172 173 const adapter = store.adapterFor('job'); 174 adapter.parse = sinon.stub().resolves('invalidJSON'); 175 176 await model.parse(); 177 178 assert.ok( 179 adapter.parse.calledWith('invalidJSON', undefined), 180 'adapter parse method should be called' 181 ); 182 183 assert.deepEqual( 184 model.get('_newDefinitionJSON'), 185 'invalidJSON', 186 '_newDefinitionJSON is set' 187 ); 188 }); 189 }); 190 });