github.com/hernad/nomad@v1.6.112/ui/tests/unit/controllers/allocations/allocation/index-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  
     9  module('Unit | Controller | allocations/allocation/index', function (hooks) {
    10    setupTest(hooks);
    11  
    12    module('#serviceHealthStatuses', function () {
    13      test('it groups health service data by service name', function (assert) {
    14        let controller = this.owner.lookup(
    15          'controller:allocations/allocation/index'
    16        );
    17        controller.set('model', JSON.parse(JSON.stringify(Allocation)));
    18  
    19        const groupFakePy = {
    20          refID: 'fakepy-group-fake-py',
    21          statuses: {
    22            success: 1,
    23            failure: 1,
    24            pending: 0,
    25          },
    26        };
    27        const taskFakePy = {
    28          refID: 'http.server-task-fake-py',
    29          statuses: {
    30            success: 2,
    31            failure: 2,
    32            pending: 0,
    33          },
    34        };
    35        const pender = {
    36          refID: 'http.server-pender',
    37          statuses: {
    38            success: 0,
    39            failure: 0,
    40            pending: 1,
    41          },
    42        };
    43  
    44        assert.equal(
    45          controller.servicesWithHealthChecks
    46            .findBy('refID', groupFakePy.refID)
    47            .healthChecks.filter((check) => check.Status === 'success').length,
    48          groupFakePy.statuses['success']
    49        );
    50        assert.equal(
    51          controller.servicesWithHealthChecks
    52            .findBy('refID', groupFakePy.refID)
    53            .healthChecks.filter((check) => check.Status === 'failure').length,
    54          groupFakePy.statuses['failure']
    55        );
    56        assert.equal(
    57          controller.servicesWithHealthChecks
    58            .findBy('refID', groupFakePy.refID)
    59            .healthChecks.filter((check) => check.Status === 'pending').length,
    60          groupFakePy.statuses['pending']
    61        );
    62  
    63        assert.equal(
    64          controller.servicesWithHealthChecks
    65            .findBy('refID', taskFakePy.refID)
    66            .healthChecks.filter((check) => check.Status === 'success').length,
    67          taskFakePy.statuses['success']
    68        );
    69        assert.equal(
    70          controller.servicesWithHealthChecks
    71            .findBy('refID', taskFakePy.refID)
    72            .healthChecks.filter((check) => check.Status === 'failure').length,
    73          taskFakePy.statuses['failure']
    74        );
    75        assert.equal(
    76          controller.servicesWithHealthChecks
    77            .findBy('refID', taskFakePy.refID)
    78            .healthChecks.filter((check) => check.Status === 'pending').length,
    79          taskFakePy.statuses['pending']
    80        );
    81  
    82        assert.equal(
    83          controller.servicesWithHealthChecks
    84            .findBy('refID', pender.refID)
    85            .healthChecks.filter((check) => check.Status === 'success').length,
    86          pender.statuses['success']
    87        );
    88        assert.equal(
    89          controller.servicesWithHealthChecks
    90            .findBy('refID', pender.refID)
    91            .healthChecks.filter((check) => check.Status === 'failure').length,
    92          pender.statuses['failure']
    93        );
    94        assert.equal(
    95          controller.servicesWithHealthChecks
    96            .findBy('refID', pender.refID)
    97            .healthChecks.filter((check) => check.Status === 'pending').length,
    98          pender.statuses['pending']
    99        );
   100      });
   101  
   102      test('it handles duplicate names', async function (assert) {
   103        let controller = this.owner.lookup(
   104          'controller:allocations/allocation/index'
   105        );
   106        controller.set('model', JSON.parse(JSON.stringify(Allocation)));
   107  
   108        const groupDupe = {
   109          refID: 'fakepy-duper',
   110          statuses: {
   111            success: 1,
   112            failure: 0,
   113            pending: 0,
   114          },
   115        };
   116        const taskDupe = {
   117          refID: 'http.server-duper',
   118          statuses: {
   119            success: 0,
   120            failure: 1,
   121            pending: 0,
   122          },
   123        };
   124  
   125        assert.equal(
   126          controller.servicesWithHealthChecks
   127            .findBy('refID', groupDupe.refID)
   128            .healthChecks.filter((check) => check.Status === 'success').length,
   129          groupDupe.statuses['success']
   130        );
   131        assert.equal(
   132          controller.servicesWithHealthChecks
   133            .findBy('refID', groupDupe.refID)
   134            .healthChecks.filter((check) => check.Status === 'failure').length,
   135          groupDupe.statuses['failure']
   136        );
   137        assert.equal(
   138          controller.servicesWithHealthChecks
   139            .findBy('refID', groupDupe.refID)
   140            .healthChecks.filter((check) => check.Status === 'pending').length,
   141          groupDupe.statuses['pending']
   142        );
   143  
   144        assert.equal(
   145          controller.servicesWithHealthChecks
   146            .findBy('refID', taskDupe.refID)
   147            .healthChecks.filter((check) => check.Status === 'success').length,
   148          taskDupe.statuses['success']
   149        );
   150        assert.equal(
   151          controller.servicesWithHealthChecks
   152            .findBy('refID', taskDupe.refID)
   153            .healthChecks.filter((check) => check.Status === 'failure').length,
   154          taskDupe.statuses['failure']
   155        );
   156        assert.equal(
   157          controller.servicesWithHealthChecks
   158            .findBy('refID', taskDupe.refID)
   159            .healthChecks.filter((check) => check.Status === 'pending').length,
   160          taskDupe.statuses['pending']
   161        );
   162      });
   163    });
   164  });
   165  
   166  // Using var to hoist this variable to the top of the module
   167  var Allocation = {
   168    namespace: 'default',
   169    name: 'my-alloc',
   170    taskGroup: {
   171      name: 'fakepy',
   172      count: 3,
   173      services: [
   174        {
   175          Name: 'group-fake-py',
   176          refID: 'fakepy-group-fake-py',
   177          PortLabel: 'http',
   178          Tags: [],
   179          OnUpdate: 'require_healthy',
   180          Provider: 'nomad',
   181          Connect: null,
   182          GroupName: 'fakepy',
   183          TaskName: '',
   184          healthChecks: [],
   185        },
   186        {
   187          Name: 'duper',
   188          refID: 'fakepy-duper',
   189          PortLabel: 'http',
   190          Tags: [],
   191          OnUpdate: 'require_healthy',
   192          Provider: 'nomad',
   193          Connect: null,
   194          GroupName: 'fakepy',
   195          TaskName: '',
   196          healthChecks: [],
   197        },
   198      ],
   199    },
   200    allocatedResources: {
   201      Cpu: 100,
   202      Memory: 300,
   203      MemoryMax: null,
   204      Disk: 0,
   205      Iops: null,
   206      Networks: [
   207        {
   208          Device: '',
   209          CIDR: '',
   210          IP: '127.0.0.1',
   211          Mode: 'host',
   212          MBits: 0,
   213          Ports: [
   214            {
   215              name: 'http',
   216              port: 22308,
   217              to: 0,
   218              isDynamic: true,
   219            },
   220          ],
   221        },
   222      ],
   223      Ports: [
   224        {
   225          HostIP: '127.0.0.1',
   226          Label: 'http',
   227          To: 0,
   228          Value: 22308,
   229        },
   230      ],
   231    },
   232    healthChecks: {
   233      c97fda942e772b43a5a537e5b0c8544c: {
   234        Check: 'service: "task-fake-py" check',
   235        Alloc: 'my-alloc',
   236        Group: 'trying-multi-dupes.fakepy[1]',
   237        ID: 'c97fda942e772b43a5a537e5b0c8544c',
   238        Mode: 'healthiness',
   239        Output: 'nomad: http ok',
   240        Service: 'task-fake-py',
   241        Status: 'success',
   242        StatusCode: 200,
   243        Task: 'http.server',
   244        Timestamp: 1662131947,
   245      },
   246      '2e1bfc8ecc485ee86b972ae08e890152': {
   247        Check: 'task-happy',
   248        Alloc: 'my-alloc',
   249        Group: 'trying-multi-dupes.fakepy[1]',
   250        ID: '2e1bfc8ecc485ee86b972ae08e890152',
   251        Mode: 'healthiness',
   252        Output: 'nomad: http ok',
   253        Service: 'task-fake-py',
   254        Status: 'success',
   255        StatusCode: 200,
   256        Task: 'http.server',
   257        Timestamp: 1662131949,
   258      },
   259      '6162723ab20b268c25eda69b400dc9c6': {
   260        Check: 'task-sad',
   261        Alloc: 'my-alloc',
   262        Group: 'trying-multi-dupes.fakepy[1]',
   263        ID: '6162723ab20b268c25eda69b400dc9c6',
   264        Mode: 'healthiness',
   265        Output:
   266          '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"\n        "http://www.w3.org/TR/html4/strict.dtd">\n<html>\n    <head>\n        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">\n        <title>Error response</title>\n    </head>\n    <body>\n        <h1>Error response</h1>\n        <p>Error code: 404</p>\n        <p>Message: File not found.</p>\n        <p>Error code explanation: HTTPStatus.NOT_FOUND - Nothing matches the given URI.</p>\n    </body>\n</html>\n',
   267        Service: 'task-fake-py',
   268        Status: 'failure',
   269        StatusCode: 404,
   270        Task: 'http.server',
   271        Timestamp: 1662131936,
   272      },
   273      a4a7050175a2b236edcf613cb3563753: {
   274        Check: 'task-sad2',
   275        Alloc: 'my-alloc',
   276        Group: 'trying-multi-dupes.fakepy[1]',
   277        ID: 'a4a7050175a2b236edcf613cb3563753',
   278        Mode: 'healthiness',
   279        Output:
   280          '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"\n        "http://www.w3.org/TR/html4/strict.dtd">\n<html>\n    <head>\n        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">\n        <title>Error response</title>\n    </head>\n    <body>\n        <h1>Error response</h1>\n        <p>Error code: 404</p>\n        <p>Message: File not found.</p>\n        <p>Error code explanation: HTTPStatus.NOT_FOUND - Nothing matches the given URI.</p>\n    </body>\n</html>\n',
   281        Service: 'task-fake-py',
   282        Status: 'failure',
   283        StatusCode: 404,
   284        Task: 'http.server',
   285        Timestamp: 1662131936,
   286      },
   287      '2dfe58eb841bdfa704f0ae9ef5b5af5e': {
   288        Check: 'tcp_probe',
   289        Alloc: 'my-alloc',
   290        Group: 'trying-multi-dupes.fakepy[1]',
   291        ID: '2dfe58eb841bdfa704f0ae9ef5b5af5e',
   292        Mode: 'readiness',
   293        Output: 'nomad: tcp ok',
   294        Service: 'web',
   295        Status: 'success',
   296        Task: 'http.server',
   297        Timestamp: 1662131949,
   298      },
   299      '69021054964f4c461b3c4c4f456e16a8': {
   300        Check: 'happy',
   301        Alloc: 'my-alloc',
   302        Group: 'trying-multi-dupes.fakepy[1]',
   303        ID: '69021054964f4c461b3c4c4f456e16a8',
   304        Mode: 'healthiness',
   305        Output: 'nomad: http ok',
   306        Service: 'group-fake-py',
   307        Status: 'success',
   308        StatusCode: 200,
   309        Timestamp: 1662131949,
   310      },
   311      '913f5b725ceecdd5ff48a9a51ddf8513': {
   312        Check: 'sad',
   313        Alloc: 'my-alloc',
   314        Group: 'trying-multi-dupes.fakepy[1]',
   315        ID: '913f5b725ceecdd5ff48a9a51ddf8513',
   316        Mode: 'healthiness',
   317        Output:
   318          '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"\n        "http://www.w3.org/TR/html4/strict.dtd">\n<html>\n    <head>\n        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">\n        <title>Error response</title>\n    </head>\n    <body>\n        <h1>Error response</h1>\n        <p>Error code: 404</p>\n        <p>Message: File not found.</p>\n        <p>Error code explanation: HTTPStatus.NOT_FOUND - Nothing matches the given URI.</p>\n    </body>\n</html>\n',
   319        Service: 'group-fake-py',
   320        Status: 'failure',
   321        StatusCode: 404,
   322        Timestamp: 1662131936,
   323      },
   324      bloop: {
   325        Check: 'is-alive',
   326        Alloc: 'my-alloc',
   327        Group: 'trying-multi-dupes.fakepy[1]',
   328        ID: 'bloop',
   329        Mode: 'healthiness',
   330        Service: 'pender',
   331        Status: 'pending',
   332        Task: 'http.server',
   333        Timestamp: 1662131947,
   334      },
   335      'group-dupe': {
   336        Check: 'is-alive',
   337        Alloc: 'my-alloc',
   338        Group: 'trying-multi-dupes.fakepy[1]',
   339        ID: 'group-dupe',
   340        Mode: 'healthiness',
   341        Service: 'duper',
   342        Status: 'success',
   343        Task: '',
   344        Timestamp: 1662131947,
   345      },
   346      'task-dupe': {
   347        Check: 'is-alive',
   348        Alloc: 'my-alloc',
   349        Group: 'trying-multi-dupes.fakepy[1]',
   350        ID: 'task-dupe',
   351        Mode: 'healthiness',
   352        Service: 'duper',
   353        Status: 'failure',
   354        Task: 'http.server',
   355        Timestamp: 1662131947,
   356      },
   357    },
   358    id: 'my-alloc',
   359    states: [
   360      {
   361        Name: 'http.server',
   362        task: {
   363          name: 'http.server',
   364          driver: 'raw_exec',
   365          kind: '',
   366          meta: null,
   367          lifecycle: null,
   368          reservedMemory: 300,
   369          reservedMemoryMax: 0,
   370          reservedCPU: 100,
   371          reservedDisk: 0,
   372          reservedEphemeralDisk: 300,
   373          services: [
   374            {
   375              Name: 'task-fake-py',
   376              PortLabel: 'http',
   377              refID: 'http.server-task-fake-py',
   378              Tags: [
   379                'long',
   380                'and',
   381                'arbitrary',
   382                'list',
   383                'of',
   384                'tags',
   385                'arbitrary',
   386              ],
   387              OnUpdate: 'require_healthy',
   388              Provider: 'nomad',
   389              Connect: null,
   390              TaskName: 'http.server',
   391              healthChecks: [],
   392            },
   393            {
   394              Name: 'pender',
   395              refID: 'http.server-pender',
   396              PortLabel: 'http',
   397              Tags: ['lol', 'lmao'],
   398              OnUpdate: 'require_healthy',
   399              Provider: 'nomad',
   400              Connect: null,
   401              TaskName: 'http.server',
   402              healthChecks: [],
   403            },
   404            {
   405              Name: 'web',
   406              refID: 'http.server-web',
   407              PortLabel: 'http',
   408              Tags: ['lol', 'lmao'],
   409              OnUpdate: 'require_healthy',
   410              Provider: 'nomad',
   411              Connect: null,
   412              TaskName: 'http.server',
   413              healthChecks: [],
   414            },
   415            {
   416              Name: 'duper',
   417              refID: 'http.server-duper',
   418              PortLabel: 'http',
   419              Tags: ['lol', 'lmao'],
   420              OnUpdate: 'require_healthy',
   421              Provider: 'nomad',
   422              Connect: null,
   423              TaskName: 'http.server',
   424              healthChecks: [],
   425            },
   426          ],
   427          volumeMounts: null,
   428        },
   429      },
   430    ],
   431    rescheduleEvents: [],
   432    job: '["trying-multi-dupes","default"]',
   433    node: '5d33384d-8d0f-6a65-743c-2fcc1871b13e',
   434    previousAllocation: null,
   435    nextAllocation: null,
   436    preemptedByAllocation: null,
   437    followUpEvaluation: null,
   438  };