github.com/hernad/nomad@v1.6.112/ui/tests/unit/serializers/volume-test.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 import { module, test } from 'qunit'; 7 import { setupTest } from 'ember-qunit'; 8 import VolumeModel from 'nomad-ui/models/volume'; 9 10 module('Unit | Serializer | Volume', function (hooks) { 11 setupTest(hooks); 12 hooks.beforeEach(function () { 13 this.store = this.owner.lookup('service:store'); 14 this.subject = () => this.store.serializerFor('volume'); 15 }); 16 17 // Set the milliseconds to avoid possible floating point precision 18 // issue that arises from converting to nanos and back. 19 const REF_DATE = new Date(); 20 REF_DATE.setMilliseconds(0); 21 22 const normalizationTestCases = [ 23 { 24 name: '`default` is used as the namespace in the volume ID when there is no namespace in the payload', 25 in: { 26 ID: 'volume-id', 27 Name: 'volume-id', 28 PluginID: 'plugin-1', 29 ExternalID: 'external-uuid', 30 Topologies: {}, 31 AccessMode: 'access-this-way', 32 AttachmentMode: 'attach-this-way', 33 Schedulable: true, 34 Provider: 'abc.123', 35 Version: '1.0.29', 36 ControllerRequired: true, 37 ControllersHealthy: 1, 38 ControllersExpected: 1, 39 NodesHealthy: 1, 40 NodesExpected: 2, 41 CreateIndex: 1, 42 ModifyIndex: 38, 43 WriteAllocs: {}, 44 ReadAllocs: {}, 45 }, 46 out: { 47 data: { 48 id: '["csi/volume-id","default"]', 49 type: 'volume', 50 attributes: { 51 plainId: 'volume-id', 52 name: 'volume-id', 53 externalId: 'external-uuid', 54 topologies: {}, 55 accessMode: 'access-this-way', 56 attachmentMode: 'attach-this-way', 57 schedulable: true, 58 provider: 'abc.123', 59 version: '1.0.29', 60 controllerRequired: true, 61 controllersHealthy: 1, 62 controllersExpected: 1, 63 nodesHealthy: 1, 64 nodesExpected: 2, 65 createIndex: 1, 66 modifyIndex: 38, 67 }, 68 relationships: { 69 plugin: { 70 data: { 71 id: 'csi/plugin-1', 72 type: 'plugin', 73 }, 74 }, 75 readAllocations: { 76 data: [], 77 }, 78 writeAllocations: { 79 data: [], 80 }, 81 }, 82 }, 83 included: [], 84 }, 85 }, 86 87 { 88 name: 'The ID of the record is a composite of both the name and the namespace', 89 in: { 90 ID: 'volume-id', 91 Name: 'volume-id', 92 Namespace: 'namespace-2', 93 PluginID: 'plugin-1', 94 ExternalID: 'external-uuid', 95 Topologies: {}, 96 AccessMode: 'access-this-way', 97 AttachmentMode: 'attach-this-way', 98 Schedulable: true, 99 Provider: 'abc.123', 100 Version: '1.0.29', 101 ControllerRequired: true, 102 ControllersHealthy: 1, 103 ControllersExpected: 1, 104 NodesHealthy: 1, 105 NodesExpected: 2, 106 CreateIndex: 1, 107 ModifyIndex: 38, 108 WriteAllocs: {}, 109 ReadAllocs: {}, 110 }, 111 out: { 112 data: { 113 id: '["csi/volume-id","namespace-2"]', 114 type: 'volume', 115 attributes: { 116 plainId: 'volume-id', 117 name: 'volume-id', 118 externalId: 'external-uuid', 119 topologies: {}, 120 accessMode: 'access-this-way', 121 attachmentMode: 'attach-this-way', 122 schedulable: true, 123 provider: 'abc.123', 124 version: '1.0.29', 125 controllerRequired: true, 126 controllersHealthy: 1, 127 controllersExpected: 1, 128 nodesHealthy: 1, 129 nodesExpected: 2, 130 createIndex: 1, 131 modifyIndex: 38, 132 }, 133 relationships: { 134 plugin: { 135 data: { 136 id: 'csi/plugin-1', 137 type: 'plugin', 138 }, 139 }, 140 namespace: { 141 data: { 142 id: 'namespace-2', 143 type: 'namespace', 144 }, 145 }, 146 readAllocations: { 147 data: [], 148 }, 149 writeAllocations: { 150 data: [], 151 }, 152 }, 153 }, 154 included: [], 155 }, 156 }, 157 158 { 159 name: 'Allocations are interpreted as embedded records and are properly normalized into included resources in a JSON API shape', 160 in: { 161 ID: 'volume-id', 162 Name: 'volume-id', 163 Namespace: 'namespace-2', 164 PluginID: 'plugin-1', 165 ExternalID: 'external-uuid', 166 Topologies: {}, 167 AccessMode: 'access-this-way', 168 AttachmentMode: 'attach-this-way', 169 Schedulable: true, 170 Provider: 'abc.123', 171 Version: '1.0.29', 172 ControllerRequired: true, 173 ControllersHealthy: 1, 174 ControllersExpected: 1, 175 NodesHealthy: 1, 176 NodesExpected: 2, 177 CreateIndex: 1, 178 ModifyIndex: 38, 179 Allocations: [ 180 { 181 ID: 'alloc-id-1', 182 TaskGroup: 'foobar', 183 CreateTime: +REF_DATE * 1000000, 184 ModifyTime: +REF_DATE * 1000000, 185 JobID: 'the-job', 186 Namespace: 'namespace-2', 187 }, 188 { 189 ID: 'alloc-id-2', 190 TaskGroup: 'write-here', 191 CreateTime: +REF_DATE * 1000000, 192 ModifyTime: +REF_DATE * 1000000, 193 JobID: 'the-job', 194 Namespace: 'namespace-2', 195 }, 196 { 197 ID: 'alloc-id-3', 198 TaskGroup: 'look-if-you-must', 199 CreateTime: +REF_DATE * 1000000, 200 ModifyTime: +REF_DATE * 1000000, 201 JobID: 'the-job', 202 Namespace: 'namespace-2', 203 }, 204 ], 205 WriteAllocs: { 206 'alloc-id-1': null, 207 'alloc-id-2': null, 208 }, 209 ReadAllocs: { 210 'alloc-id-3': null, 211 }, 212 }, 213 out: { 214 data: { 215 id: '["csi/volume-id","namespace-2"]', 216 type: 'volume', 217 attributes: { 218 plainId: 'volume-id', 219 name: 'volume-id', 220 externalId: 'external-uuid', 221 topologies: {}, 222 accessMode: 'access-this-way', 223 attachmentMode: 'attach-this-way', 224 schedulable: true, 225 provider: 'abc.123', 226 version: '1.0.29', 227 controllerRequired: true, 228 controllersHealthy: 1, 229 controllersExpected: 1, 230 nodesHealthy: 1, 231 nodesExpected: 2, 232 createIndex: 1, 233 modifyIndex: 38, 234 }, 235 relationships: { 236 plugin: { 237 data: { 238 id: 'csi/plugin-1', 239 type: 'plugin', 240 }, 241 }, 242 namespace: { 243 data: { 244 id: 'namespace-2', 245 type: 'namespace', 246 }, 247 }, 248 readAllocations: { 249 data: [{ type: 'allocation', id: 'alloc-id-3' }], 250 }, 251 writeAllocations: { 252 data: [ 253 { type: 'allocation', id: 'alloc-id-1' }, 254 { type: 'allocation', id: 'alloc-id-2' }, 255 ], 256 }, 257 }, 258 }, 259 included: [ 260 { 261 id: 'alloc-id-1', 262 type: 'allocation', 263 attributes: { 264 createTime: REF_DATE, 265 modifyTime: REF_DATE, 266 namespace: 'namespace-2', 267 taskGroupName: 'foobar', 268 wasPreempted: false, 269 states: [], 270 allocationTaskGroup: null, 271 }, 272 relationships: { 273 followUpEvaluation: { 274 data: null, 275 }, 276 job: { 277 data: { type: 'job', id: '["the-job","namespace-2"]' }, 278 }, 279 nextAllocation: { 280 data: null, 281 }, 282 previousAllocation: { 283 data: null, 284 }, 285 preemptedAllocations: { 286 data: [], 287 }, 288 preemptedByAllocation: { 289 data: null, 290 }, 291 }, 292 }, 293 { 294 id: 'alloc-id-2', 295 type: 'allocation', 296 attributes: { 297 createTime: REF_DATE, 298 modifyTime: REF_DATE, 299 namespace: 'namespace-2', 300 taskGroupName: 'write-here', 301 wasPreempted: false, 302 states: [], 303 allocationTaskGroup: null, 304 }, 305 relationships: { 306 followUpEvaluation: { 307 data: null, 308 }, 309 job: { 310 data: { type: 'job', id: '["the-job","namespace-2"]' }, 311 }, 312 nextAllocation: { 313 data: null, 314 }, 315 previousAllocation: { 316 data: null, 317 }, 318 preemptedAllocations: { 319 data: [], 320 }, 321 preemptedByAllocation: { 322 data: null, 323 }, 324 }, 325 }, 326 { 327 id: 'alloc-id-3', 328 type: 'allocation', 329 attributes: { 330 createTime: REF_DATE, 331 modifyTime: REF_DATE, 332 namespace: 'namespace-2', 333 taskGroupName: 'look-if-you-must', 334 wasPreempted: false, 335 states: [], 336 allocationTaskGroup: null, 337 }, 338 relationships: { 339 followUpEvaluation: { 340 data: null, 341 }, 342 job: { 343 data: { type: 'job', id: '["the-job","namespace-2"]' }, 344 }, 345 nextAllocation: { 346 data: null, 347 }, 348 previousAllocation: { 349 data: null, 350 }, 351 preemptedAllocations: { 352 data: [], 353 }, 354 preemptedByAllocation: { 355 data: null, 356 }, 357 }, 358 }, 359 ], 360 }, 361 }, 362 ]; 363 364 normalizationTestCases.forEach((testCase) => { 365 test(`normalization: ${testCase.name}`, async function (assert) { 366 assert.deepEqual( 367 this.subject().normalize(VolumeModel, testCase.in), 368 testCase.out 369 ); 370 }); 371 }); 372 });