github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/unit/serializers/allocation-test.js (about) 1 import { module, test } from 'qunit'; 2 import { setupTest } from 'ember-qunit'; 3 import AllocationModel from 'nomad-ui/models/allocation'; 4 5 module('Unit | Serializer | Allocation', function (hooks) { 6 setupTest(hooks); 7 hooks.beforeEach(function () { 8 this.store = this.owner.lookup('service:store'); 9 this.subject = () => this.store.serializerFor('allocation'); 10 }); 11 12 const sampleDate = new Date('2018-12-12T00:00:00'); 13 const normalizationTestCases = [ 14 { 15 name: 'Normal', 16 in: { 17 ID: 'test-allocation', 18 JobID: 'test-summary', 19 Name: 'test-summary[1]', 20 Namespace: 'test-namespace', 21 TaskGroup: 'test-group', 22 CreateTime: +sampleDate * 1000000, 23 ModifyTime: +sampleDate * 1000000, 24 TaskStates: { 25 testTask: { 26 State: 'running', 27 Failed: false, 28 }, 29 }, 30 }, 31 out: { 32 data: { 33 id: 'test-allocation', 34 type: 'allocation', 35 attributes: { 36 taskGroupName: 'test-group', 37 name: 'test-summary[1]', 38 namespace: 'test-namespace', 39 modifyTime: sampleDate, 40 createTime: sampleDate, 41 states: [ 42 { 43 name: 'testTask', 44 state: 'running', 45 failed: false, 46 }, 47 ], 48 wasPreempted: false, 49 allocationTaskGroup: null, 50 }, 51 relationships: { 52 followUpEvaluation: { 53 data: null, 54 }, 55 nextAllocation: { 56 data: null, 57 }, 58 previousAllocation: { 59 data: null, 60 }, 61 preemptedAllocations: { 62 data: [], 63 }, 64 preemptedByAllocation: { 65 data: null, 66 }, 67 job: { 68 data: { 69 id: '["test-summary","test-namespace"]', 70 type: 'job', 71 }, 72 }, 73 }, 74 }, 75 }, 76 }, 77 78 { 79 name: 'Dots in task names', 80 in: { 81 ID: 'test-allocation', 82 JobID: 'test-summary', 83 Name: 'test-summary[1]', 84 Namespace: 'test-namespace', 85 TaskGroup: 'test-group', 86 CreateTime: +sampleDate * 1000000, 87 ModifyTime: +sampleDate * 1000000, 88 TaskStates: { 89 'one.two': { 90 State: 'running', 91 Failed: false, 92 }, 93 'three.four': { 94 State: 'pending', 95 Failed: true, 96 }, 97 }, 98 }, 99 out: { 100 data: { 101 id: 'test-allocation', 102 type: 'allocation', 103 attributes: { 104 taskGroupName: 'test-group', 105 name: 'test-summary[1]', 106 namespace: 'test-namespace', 107 modifyTime: sampleDate, 108 createTime: sampleDate, 109 states: [ 110 { 111 name: 'one.two', 112 state: 'running', 113 failed: false, 114 }, 115 { 116 name: 'three.four', 117 state: 'pending', 118 failed: true, 119 }, 120 ], 121 wasPreempted: false, 122 allocationTaskGroup: null, 123 }, 124 relationships: { 125 followUpEvaluation: { 126 data: null, 127 }, 128 nextAllocation: { 129 data: null, 130 }, 131 previousAllocation: { 132 data: null, 133 }, 134 preemptedAllocations: { 135 data: [], 136 }, 137 preemptedByAllocation: { 138 data: null, 139 }, 140 job: { 141 data: { 142 id: '["test-summary","test-namespace"]', 143 type: 'job', 144 }, 145 }, 146 }, 147 }, 148 }, 149 }, 150 151 { 152 name: 'With preemptions', 153 in: { 154 ID: 'test-allocation', 155 JobID: 'test-summary', 156 Name: 'test-summary[1]', 157 Namespace: 'test-namespace', 158 TaskGroup: 'test-group', 159 CreateTime: +sampleDate * 1000000, 160 ModifyTime: +sampleDate * 1000000, 161 TaskStates: { 162 task: { 163 State: 'running', 164 Failed: false, 165 }, 166 }, 167 PreemptedByAllocation: 'preempter-allocation', 168 PreemptedAllocations: [ 169 'preempted-one-allocation', 170 'preempted-two-allocation', 171 ], 172 }, 173 out: { 174 data: { 175 id: 'test-allocation', 176 type: 'allocation', 177 attributes: { 178 taskGroupName: 'test-group', 179 name: 'test-summary[1]', 180 namespace: 'test-namespace', 181 modifyTime: sampleDate, 182 createTime: sampleDate, 183 states: [ 184 { 185 name: 'task', 186 state: 'running', 187 failed: false, 188 }, 189 ], 190 wasPreempted: true, 191 allocationTaskGroup: null, 192 }, 193 relationships: { 194 followUpEvaluation: { 195 data: null, 196 }, 197 nextAllocation: { 198 data: null, 199 }, 200 previousAllocation: { 201 data: null, 202 }, 203 preemptedAllocations: { 204 data: [ 205 { id: 'preempted-one-allocation', type: 'allocation' }, 206 { id: 'preempted-two-allocation', type: 'allocation' }, 207 ], 208 }, 209 preemptedByAllocation: { 210 data: { 211 id: 'preempter-allocation', 212 type: 'allocation', 213 }, 214 }, 215 job: { 216 data: { 217 id: '["test-summary","test-namespace"]', 218 type: 'job', 219 }, 220 }, 221 }, 222 }, 223 }, 224 }, 225 226 { 227 name: 'Derives task group from embedded job when available', 228 in: { 229 ID: 'test-allocation', 230 JobID: 'test-summary', 231 Name: 'test-summary[1]', 232 Namespace: 'test-namespace', 233 TaskGroup: 'test-group', 234 CreateTime: +sampleDate * 1000000, 235 ModifyTime: +sampleDate * 1000000, 236 TaskStates: { 237 task: { 238 State: 'running', 239 Failed: false, 240 }, 241 }, 242 Job: { 243 ID: 'test-summary', 244 Name: 'test-summary', 245 TaskGroups: [ 246 { 247 Name: 'fake-group', 248 Count: 2, 249 Tasks: [], 250 EphemeralDisk: {}, 251 }, 252 { 253 Name: 'test-group', 254 Count: 3, 255 Tasks: [], 256 EphemeralDisk: {}, 257 }, 258 ], 259 }, 260 }, 261 out: { 262 data: { 263 id: 'test-allocation', 264 type: 'allocation', 265 attributes: { 266 taskGroupName: 'test-group', 267 name: 'test-summary[1]', 268 namespace: 'test-namespace', 269 modifyTime: sampleDate, 270 createTime: sampleDate, 271 states: [ 272 { 273 name: 'task', 274 state: 'running', 275 failed: false, 276 }, 277 ], 278 wasPreempted: false, 279 allocationTaskGroup: { 280 name: 'test-group', 281 count: 3, 282 tasks: [], 283 services: [], 284 volumes: [], 285 }, 286 }, 287 relationships: { 288 followUpEvaluation: { 289 data: null, 290 }, 291 nextAllocation: { 292 data: null, 293 }, 294 previousAllocation: { 295 data: null, 296 }, 297 preemptedAllocations: { 298 data: [], 299 }, 300 preemptedByAllocation: { 301 data: null, 302 }, 303 job: { 304 data: { 305 id: '["test-summary","test-namespace"]', 306 type: 'job', 307 }, 308 }, 309 }, 310 }, 311 }, 312 }, 313 314 { 315 name: 'TaskStates are sorted for stable fragments', 316 in: { 317 ID: 'test-allocation', 318 JobID: 'test-summary', 319 Name: 'test-summary[1]', 320 Namespace: 'test-namespace', 321 TaskGroup: 'test-group', 322 CreateTime: +sampleDate * 1000000, 323 ModifyTime: +sampleDate * 1000000, 324 TaskStates: { 325 xyz: { 326 State: 'running', 327 Failed: false, 328 }, 329 abc: { 330 State: 'running', 331 Failed: false, 332 }, 333 }, 334 }, 335 out: { 336 data: { 337 id: 'test-allocation', 338 type: 'allocation', 339 attributes: { 340 taskGroupName: 'test-group', 341 name: 'test-summary[1]', 342 namespace: 'test-namespace', 343 modifyTime: sampleDate, 344 createTime: sampleDate, 345 states: [ 346 { 347 name: 'abc', 348 state: 'running', 349 failed: false, 350 }, 351 { 352 name: 'xyz', 353 state: 'running', 354 failed: false, 355 }, 356 ], 357 wasPreempted: false, 358 allocationTaskGroup: null, 359 }, 360 relationships: { 361 followUpEvaluation: { 362 data: null, 363 }, 364 nextAllocation: { 365 data: null, 366 }, 367 previousAllocation: { 368 data: null, 369 }, 370 preemptedAllocations: { 371 data: [], 372 }, 373 preemptedByAllocation: { 374 data: null, 375 }, 376 job: { 377 data: { 378 id: '["test-summary","test-namespace"]', 379 type: 'job', 380 }, 381 }, 382 }, 383 }, 384 }, 385 }, 386 ]; 387 388 normalizationTestCases.forEach((testCase) => { 389 test(`normalization: ${testCase.name}`, async function (assert) { 390 assert.deepEqual( 391 this.subject().normalize(AllocationModel, testCase.in), 392 testCase.out 393 ); 394 }); 395 }); 396 });