github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/unit/adapters/allocation-test.js (about)

     1  import { module, test } from 'qunit';
     2  import { setupTest } from 'ember-qunit';
     3  import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage';
     4  
     5  module('Unit | Adapter | Allocation', function(hooks) {
     6    setupTest(hooks);
     7  
     8    hooks.beforeEach(async function() {
     9      this.store = this.owner.lookup('service:store');
    10      this.subject = () => this.store.adapterFor('allocation');
    11  
    12      window.localStorage.clear();
    13  
    14      this.server = startMirage();
    15  
    16      this.initialize = async (allocationId, { region } = {}) => {
    17        if (region) window.localStorage.nomadActiveRegion = region;
    18  
    19        this.server.create('namespace');
    20        this.server.create('region', { id: 'region-1' });
    21        this.server.create('region', { id: 'region-2' });
    22  
    23        this.server.create('node');
    24        this.server.create('job', { createAllocations: false });
    25        this.server.create('allocation', { id: 'alloc-1' });
    26        this.system = this.owner.lookup('service:system');
    27        await this.system.get('namespaces');
    28        this.system.get('shouldIncludeRegion');
    29        await this.system.get('defaultRegion');
    30  
    31        const allocation = await this.store.findRecord('allocation', allocationId);
    32        this.server.pretender.handledRequests.length = 0;
    33  
    34        return allocation;
    35      };
    36    });
    37  
    38    hooks.afterEach(function() {
    39      this.server.shutdown();
    40    });
    41  
    42    const testCases = [
    43      {
    44        variation: '',
    45        id: 'alloc-1',
    46        task: 'task-name',
    47        region: null,
    48        path: 'some/path',
    49        ls: `GET /v1/client/fs/ls/alloc-1?path=${encodeURIComponent('some/path')}`,
    50        stat: `GET /v1/client/fs/stat/alloc-1?path=${encodeURIComponent('some/path')}`,
    51        stop: 'POST /v1/allocation/alloc-1/stop',
    52        restart: 'PUT /v1/client/allocation/alloc-1/restart',
    53      },
    54      {
    55        variation: 'with non-default region',
    56        id: 'alloc-1',
    57        task: 'task-name',
    58        region: 'region-2',
    59        path: 'some/path',
    60        ls: `GET /v1/client/fs/ls/alloc-1?path=${encodeURIComponent('some/path')}&region=region-2`,
    61        stat: `GET /v1/client/fs/stat/alloc-1?path=${encodeURIComponent(
    62          'some/path'
    63        )}&region=region-2`,
    64        stop: 'POST /v1/allocation/alloc-1/stop?region=region-2',
    65        restart: 'PUT /v1/client/allocation/alloc-1/restart?region=region-2',
    66      },
    67    ];
    68  
    69    testCases.forEach(testCase => {
    70      test(`ls makes the correct API call ${testCase.variation}`, async function(assert) {
    71        const { pretender } = this.server;
    72        const allocation = await this.initialize(testCase.id, { region: testCase.region });
    73  
    74        await this.subject().ls(allocation, testCase.path);
    75        const req = pretender.handledRequests[0];
    76        assert.equal(`${req.method} ${req.url}`, testCase.ls);
    77      });
    78  
    79      test(`stat makes the correct API call ${testCase.variation}`, async function(assert) {
    80        const { pretender } = this.server;
    81        const allocation = await this.initialize(testCase.id, { region: testCase.region });
    82  
    83        await this.subject().stat(allocation, testCase.path);
    84        const req = pretender.handledRequests[0];
    85        assert.equal(`${req.method} ${req.url}`, testCase.stat);
    86      });
    87  
    88      test(`stop makes the correct API call ${testCase.variation}`, async function(assert) {
    89        const { pretender } = this.server;
    90        const allocation = await this.initialize(testCase.id, { region: testCase.region });
    91  
    92        await this.subject().stop(allocation);
    93        const req = pretender.handledRequests[0];
    94        assert.equal(`${req.method} ${req.url}`, testCase.stop);
    95      });
    96  
    97      test(`restart makes the correct API call ${testCase.variation}`, async function(assert) {
    98        const { pretender } = this.server;
    99        const allocation = await this.initialize(testCase.id, { region: testCase.region });
   100  
   101        await this.subject().restart(allocation);
   102        const req = pretender.handledRequests[0];
   103        assert.equal(`${req.method} ${req.url}`, testCase.restart);
   104      });
   105  
   106      test(`restart with optional task name makes the correct API call ${testCase.variation}`, async function(assert) {
   107        const { pretender } = this.server;
   108        const allocation = await this.initialize(testCase.id, { region: testCase.region });
   109  
   110        await this.subject().restart(allocation, testCase.task);
   111        const req = pretender.handledRequests[0];
   112        assert.equal(`${req.method} ${req.url}`, testCase.restart);
   113        assert.deepEqual(JSON.parse(req.requestBody), { TaskName: testCase.task });
   114      });
   115    });
   116  });