github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/unit/serializers/volume-test.js (about)

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