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