github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/unit/utils/job-client-status-test.js (about)

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