summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/api/host.service.spec.ts
blob: e4b6476f2c08b49379d9db292542fba40b680e98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';

import { configureTestBed } from '~/testing/unit-test-helper';
import { HostService } from './host.service';

describe('HostService', () => {
  let service: HostService;
  let httpTesting: HttpTestingController;

  configureTestBed({
    providers: [HostService],
    imports: [HttpClientTestingModule]
  });

  beforeEach(() => {
    service = TestBed.inject(HostService);
    httpTesting = TestBed.inject(HttpTestingController);
  });

  afterEach(() => {
    httpTesting.verify();
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('should call list', fakeAsync(() => {
    let result;
    service.list('true').subscribe((resp) => (result = resp));
    const req = httpTesting.expectOne('api/host?facts=true');
    expect(req.request.method).toBe('GET');
    req.flush(['foo', 'bar']);
    tick();
    expect(result).toEqual(['foo', 'bar']);
  }));

  it('should make a GET request on the devices endpoint when requesting devices', () => {
    const hostname = 'hostname';
    service.getDevices(hostname).subscribe();
    const req = httpTesting.expectOne(`api/host/${hostname}/devices`);
    expect(req.request.method).toBe('GET');
  });

  it('should update host', fakeAsync(() => {
    service.update('mon0', true, ['foo', 'bar'], true, false).subscribe();
    const req = httpTesting.expectOne('api/host/mon0');
    expect(req.request.method).toBe('PUT');
    expect(req.request.body).toEqual({
      force: false,
      labels: ['foo', 'bar'],
      maintenance: true,
      update_labels: true,
      drain: false
    });
  }));

  it('should test host drain call', fakeAsync(() => {
    service.update('host0', false, null, false, false, true).subscribe();
    const req = httpTesting.expectOne('api/host/host0');
    expect(req.request.method).toBe('PUT');
    expect(req.request.body).toEqual({
      force: false,
      labels: null,
      maintenance: false,
      update_labels: false,
      drain: true
    });
  }));

  it('should call getInventory', () => {
    service.getInventory('host-0').subscribe();
    let req = httpTesting.expectOne('api/host/host-0/inventory');
    expect(req.request.method).toBe('GET');

    service.getInventory('host-0', true).subscribe();
    req = httpTesting.expectOne('api/host/host-0/inventory?refresh=true');
    expect(req.request.method).toBe('GET');
  });

  it('should call inventoryList', () => {
    service.inventoryList().subscribe();
    let req = httpTesting.expectOne('ui-api/host/inventory');
    expect(req.request.method).toBe('GET');

    service.inventoryList(true).subscribe();
    req = httpTesting.expectOne('ui-api/host/inventory?refresh=true');
    expect(req.request.method).toBe('GET');
  });
});