github.com/hernad/nomad@v1.6.112/ui/tests/unit/serializers/node-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 { run } from '@ember/runloop';
     9  import NodeModel from 'nomad-ui/models/node';
    10  import pushPayloadToStore from '../../utils/push-payload-to-store';
    11  import { settled } from '@ember/test-helpers';
    12  
    13  module('Unit | Serializer | Node', function (hooks) {
    14    setupTest(hooks);
    15    hooks.beforeEach(function () {
    16      this.store = this.owner.lookup('service:store');
    17      this.subject = () => this.store.serializerFor('node');
    18    });
    19  
    20    test('local store is culled to reflect the state of findAll requests', async function (assert) {
    21      const findAllResponse = [
    22        makeNode('1', 'One', '127.0.0.1:4646'),
    23        makeNode('2', 'Two', '127.0.0.2:4646'),
    24        makeNode('3', 'Three', '127.0.0.3:4646'),
    25      ];
    26  
    27      const payload = this.subject().normalizeFindAllResponse(
    28        this.store,
    29        NodeModel,
    30        findAllResponse
    31      );
    32      pushPayloadToStore(this.store, payload, NodeModel.modelName);
    33  
    34      assert.equal(
    35        payload.data.length,
    36        findAllResponse.length,
    37        'Each original record is returned in the response'
    38      );
    39  
    40      assert.equal(
    41        this.store.peekAll('node').filterBy('id').get('length'),
    42        findAllResponse.length,
    43        'Each original record is now in the store'
    44      );
    45  
    46      const newFindAllResponse = [
    47        makeNode('2', 'Two', '127.0.0.2:4646'),
    48        makeNode('3', 'Three', '127.0.0.3:4646'),
    49        makeNode('4', 'Four', '127.0.0.4:4646'),
    50      ];
    51  
    52      let newPayload;
    53      run(() => {
    54        newPayload = this.subject().normalizeFindAllResponse(
    55          this.store,
    56          NodeModel,
    57          newFindAllResponse
    58        );
    59      });
    60      pushPayloadToStore(this.store, newPayload, NodeModel.modelName);
    61  
    62      await settled();
    63      assert.equal(
    64        newPayload.data.length,
    65        newFindAllResponse.length,
    66        'Each new record is returned in the response'
    67      );
    68  
    69      assert.equal(
    70        this.store.peekAll('node').filterBy('id').get('length'),
    71        newFindAllResponse.length,
    72        'The node length in the store reflects the new response'
    73      );
    74  
    75      assert.notOk(
    76        this.store.peekAll('node').findBy('id', '1'),
    77        'Record One is no longer found'
    78      );
    79    });
    80  
    81    function makeNode(id, name, ip) {
    82      return { ID: id, Name: name, HTTPAddr: ip };
    83    }
    84  
    85    const normalizationTestCases = [
    86      {
    87        name: 'Normal',
    88        in: {
    89          ID: 'test-node',
    90          HTTPAddr: '867.53.0.9:4646',
    91          Drain: false,
    92          Drivers: {
    93            docker: {
    94              Detected: true,
    95              Healthy: false,
    96            },
    97          },
    98          HostVolumes: {
    99            one: {
   100              Name: 'one',
   101              ReadOnly: true,
   102            },
   103            two: {
   104              Name: 'two',
   105              ReadOnly: false,
   106            },
   107          },
   108        },
   109        out: {
   110          data: {
   111            id: 'test-node',
   112            type: 'node',
   113            attributes: {
   114              isDraining: false,
   115              httpAddr: '867.53.0.9:4646',
   116              drivers: [
   117                {
   118                  name: 'docker',
   119                  detected: true,
   120                  healthy: false,
   121                },
   122              ],
   123              hostVolumes: [
   124                { name: 'one', readOnly: true },
   125                { name: 'two', readOnly: false },
   126              ],
   127            },
   128            relationships: {
   129              allocations: {
   130                links: {
   131                  related: '/v1/node/test-node/allocations',
   132                },
   133              },
   134            },
   135          },
   136        },
   137      },
   138  
   139      {
   140        name: 'Dots in driver names',
   141        in: {
   142          ID: 'test-node',
   143          HTTPAddr: '867.53.0.9:4646',
   144          Drain: false,
   145          Drivers: {
   146            'my.driver': {
   147              Detected: true,
   148              Healthy: false,
   149            },
   150            'my.other.driver': {
   151              Detected: false,
   152              Healthy: false,
   153            },
   154          },
   155        },
   156        out: {
   157          data: {
   158            id: 'test-node',
   159            type: 'node',
   160            attributes: {
   161              isDraining: false,
   162              httpAddr: '867.53.0.9:4646',
   163              drivers: [
   164                {
   165                  name: 'my.driver',
   166                  detected: true,
   167                  healthy: false,
   168                },
   169                {
   170                  name: 'my.other.driver',
   171                  detected: false,
   172                  healthy: false,
   173                },
   174              ],
   175              hostVolumes: [],
   176            },
   177            relationships: {
   178              allocations: {
   179                links: {
   180                  related: '/v1/node/test-node/allocations',
   181                },
   182              },
   183            },
   184          },
   185        },
   186      },
   187  
   188      {
   189        name: 'Null hash values',
   190        in: {
   191          ID: 'test-node',
   192          Drivers: null,
   193          HostVolumes: null,
   194        },
   195        out: {
   196          data: {
   197            id: 'test-node',
   198            type: 'node',
   199            attributes: {
   200              hostVolumes: [],
   201              drivers: [],
   202            },
   203            relationships: {
   204              allocations: {
   205                links: {
   206                  related: '/v1/node/test-node/allocations',
   207                },
   208              },
   209            },
   210          },
   211        },
   212      },
   213    ];
   214  
   215    normalizationTestCases.forEach((testCase) => {
   216      test(`normalization: ${testCase.name}`, async function (assert) {
   217        assert.deepEqual(
   218          this.subject().normalize(NodeModel, testCase.in),
   219          testCase.out
   220        );
   221      });
   222    });
   223  });