github.com/hernad/nomad@v1.6.112/ui/tests/unit/utils/job-client-status-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 jobClientStatus from 'nomad-ui/utils/properties/job-client-status';
     8  import EmberObject from '@ember/object';
     9  
    10  class JobClientStatusMock extends EmberObject {
    11    constructor(job, nodes) {
    12      super(...arguments);
    13      this.job = job;
    14      this.nodes = nodes;
    15    }
    16  
    17    @jobClientStatus('nodes', 'job') jobClientStatus;
    18  
    19    get(key) {
    20      switch (key) {
    21        case 'job':
    22          return this.job;
    23        case 'nodes':
    24          return this.nodes;
    25      }
    26    }
    27  }
    28  
    29  class NodeMock {
    30    constructor(id, datacenter) {
    31      this.id = id;
    32      this.datacenter = datacenter;
    33    }
    34  
    35    get(key) {
    36      switch (key) {
    37        case 'id':
    38          return this.id;
    39      }
    40    }
    41  }
    42  
    43  class AllocationMock {
    44    constructor(node, clientStatus) {
    45      this.node = node;
    46      this.clientStatus = clientStatus;
    47    }
    48  
    49    belongsTo() {
    50      const self = this;
    51      return {
    52        id() {
    53          return self.node.id;
    54        },
    55      };
    56    }
    57  }
    58  
    59  module('Unit | Util | JobClientStatus', function () {
    60    test('it handles the case where all nodes are running', async function (assert) {
    61      const node = new NodeMock('node-1', 'dc1');
    62      const nodes = [node];
    63      const job = {
    64        datacenters: ['dc1'],
    65        status: 'running',
    66        allocations: [new AllocationMock(node, 'running')],
    67        taskGroups: [{}],
    68      };
    69      const expected = {
    70        byNode: {
    71          'node-1': 'running',
    72        },
    73        byStatus: {
    74          running: ['node-1'],
    75          complete: [],
    76          degraded: [],
    77          failed: [],
    78          lost: [],
    79          notScheduled: [],
    80          queued: [],
    81          starting: [],
    82          unknown: [],
    83        },
    84        totalNodes: 1,
    85      };
    86  
    87      const mock = new JobClientStatusMock(job, nodes);
    88      let result = mock.jobClientStatus;
    89  
    90      assert.deepEqual(result, expected);
    91    });
    92  
    93    test('it handles the degraded case where a node has a failing allocation', async function (assert) {
    94      const node = new NodeMock('node-2', 'dc1');
    95      const nodes = [node];
    96      const job = {
    97        datacenters: ['dc1'],
    98        status: 'running',
    99        allocations: [
   100          new AllocationMock(node, 'running'),
   101          new AllocationMock(node, 'failed'),
   102          new AllocationMock(node, 'running'),
   103        ],
   104        taskGroups: [{}, {}, {}],
   105      };
   106      const expected = {
   107        byNode: {
   108          'node-2': 'degraded',
   109        },
   110        byStatus: {
   111          running: [],
   112          complete: [],
   113          degraded: ['node-2'],
   114          failed: [],
   115          lost: [],
   116          notScheduled: [],
   117          queued: [],
   118          starting: [],
   119          unknown: [],
   120        },
   121        totalNodes: 1,
   122      };
   123  
   124      const mock = new JobClientStatusMock(job, nodes);
   125      let result = mock.jobClientStatus;
   126  
   127      assert.deepEqual(result, expected);
   128    });
   129  
   130    test('it handles the case where a node has all lost allocations', async function (assert) {
   131      const node = new NodeMock('node-1', 'dc1');
   132      const nodes = [node];
   133      const job = {
   134        datacenters: ['dc1'],
   135        status: 'running',
   136        allocations: [
   137          new AllocationMock(node, 'lost'),
   138          new AllocationMock(node, 'lost'),
   139          new AllocationMock(node, 'lost'),
   140        ],
   141        taskGroups: [{}, {}, {}],
   142      };
   143      const expected = {
   144        byNode: {
   145          'node-1': 'lost',
   146        },
   147        byStatus: {
   148          running: [],
   149          complete: [],
   150          degraded: [],
   151          failed: [],
   152          lost: ['node-1'],
   153          notScheduled: [],
   154          queued: [],
   155          starting: [],
   156          unknown: [],
   157        },
   158        totalNodes: 1,
   159      };
   160  
   161      const mock = new JobClientStatusMock(job, nodes);
   162      let result = mock.jobClientStatus;
   163  
   164      assert.deepEqual(result, expected);
   165    });
   166  
   167    test('it handles the case where a node has all failed allocations', async function (assert) {
   168      const node = new NodeMock('node-1', 'dc1');
   169      const nodes = [node];
   170      const job = {
   171        datacenters: ['dc1'],
   172        status: 'running',
   173        allocations: [
   174          new AllocationMock(node, 'failed'),
   175          new AllocationMock(node, 'failed'),
   176          new AllocationMock(node, 'failed'),
   177        ],
   178        taskGroups: [{}, {}, {}],
   179      };
   180      const expected = {
   181        byNode: {
   182          'node-1': 'failed',
   183        },
   184        byStatus: {
   185          running: [],
   186          complete: [],
   187          degraded: [],
   188          failed: ['node-1'],
   189          lost: [],
   190          notScheduled: [],
   191          queued: [],
   192          starting: [],
   193          unknown: [],
   194        },
   195        totalNodes: 1,
   196      };
   197  
   198      const mock = new JobClientStatusMock(job, nodes);
   199      let result = mock.jobClientStatus;
   200  
   201      assert.deepEqual(result, expected);
   202    });
   203  
   204    test('it handles the degraded case where the expected number of allocations doesnt match the actual number of allocations', async function (assert) {
   205      const node = new NodeMock('node-1', 'dc1');
   206      const nodes = [node];
   207      const job = {
   208        datacenters: ['dc1'],
   209        status: 'running',
   210        allocations: [
   211          new AllocationMock(node, 'running'),
   212          new AllocationMock(node, 'running'),
   213          new AllocationMock(node, 'running'),
   214        ],
   215        taskGroups: [{}, {}, {}, {}],
   216      };
   217      const expected = {
   218        byNode: {
   219          'node-1': 'degraded',
   220        },
   221        byStatus: {
   222          running: [],
   223          complete: [],
   224          degraded: ['node-1'],
   225          failed: [],
   226          lost: [],
   227          notScheduled: [],
   228          queued: [],
   229          starting: [],
   230          unknown: [],
   231        },
   232        totalNodes: 1,
   233      };
   234  
   235      const mock = new JobClientStatusMock(job, nodes);
   236      let result = mock.jobClientStatus;
   237  
   238      assert.deepEqual(result, expected);
   239    });
   240  
   241    test('it handles the not scheduled case where a node has no allocations', async function (assert) {
   242      const node = new NodeMock('node-1', 'dc1');
   243      const nodes = [node];
   244      const job = {
   245        datacenters: ['dc1'],
   246        status: 'running',
   247        allocations: [],
   248        taskGroups: [],
   249      };
   250      const expected = {
   251        byNode: {
   252          'node-1': 'notScheduled',
   253        },
   254        byStatus: {
   255          running: [],
   256          complete: [],
   257          degraded: [],
   258          failed: [],
   259          lost: [],
   260          notScheduled: ['node-1'],
   261          queued: [],
   262          starting: [],
   263          unknown: [],
   264        },
   265        totalNodes: 1,
   266      };
   267  
   268      const mock = new JobClientStatusMock(job, nodes);
   269      let result = mock.jobClientStatus;
   270  
   271      assert.deepEqual(result, expected);
   272    });
   273  
   274    test('it handles the queued case where the job is pending', async function (assert) {
   275      const node = new NodeMock('node-1', 'dc1');
   276      const nodes = [node];
   277      const job = {
   278        datacenters: ['dc1'],
   279        status: 'pending',
   280        allocations: [
   281          new AllocationMock(node, 'starting'),
   282          new AllocationMock(node, 'starting'),
   283          new AllocationMock(node, 'starting'),
   284        ],
   285        taskGroups: [{}, {}, {}, {}],
   286      };
   287      const expected = {
   288        byNode: {
   289          'node-1': 'queued',
   290        },
   291        byStatus: {
   292          running: [],
   293          complete: [],
   294          degraded: [],
   295          failed: [],
   296          lost: [],
   297          notScheduled: [],
   298          queued: ['node-1'],
   299          starting: [],
   300          unknown: [],
   301        },
   302        totalNodes: 1,
   303      };
   304  
   305      const mock = new JobClientStatusMock(job, nodes);
   306      let result = mock.jobClientStatus;
   307  
   308      assert.deepEqual(result, expected);
   309    });
   310  
   311    test('it filters nodes by the datacenter of the job', async function (assert) {
   312      const node1 = new NodeMock('node-1', 'dc1');
   313      const node2 = new NodeMock('node-2', 'dc2');
   314      const nodes = [node1, node2];
   315      const job = {
   316        datacenters: ['dc1'],
   317        status: 'running',
   318        allocations: [
   319          new AllocationMock(node1, 'running'),
   320          new AllocationMock(node2, 'failed'),
   321          new AllocationMock(node1, 'running'),
   322        ],
   323        taskGroups: [{}, {}],
   324      };
   325      const expected = {
   326        byNode: {
   327          'node-1': 'running',
   328        },
   329        byStatus: {
   330          running: ['node-1'],
   331          complete: [],
   332          degraded: [],
   333          failed: [],
   334          lost: [],
   335          notScheduled: [],
   336          queued: [],
   337          starting: [],
   338          unknown: [],
   339        },
   340        totalNodes: 1,
   341      };
   342  
   343      const mock = new JobClientStatusMock(job, nodes);
   344      let result = mock.jobClientStatus;
   345  
   346      assert.deepEqual(result, expected);
   347    });
   348  });