summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/api/pool.service.spec.ts
blob: 292da3c21a03b280f0b0e22ceab5c30d217bec07 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';

import { configureTestBed } from '~/testing/unit-test-helper';
import { RbdConfigurationSourceField } from '../models/configuration';
import { RbdConfigurationService } from '../services/rbd-configuration.service';
import { PoolService } from './pool.service';

describe('PoolService', () => {
  let service: PoolService;
  let httpTesting: HttpTestingController;
  const apiPath = 'api/pool';

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

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

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

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

  it('should call getList', () => {
    service.getList().subscribe();
    const req = httpTesting.expectOne(`${apiPath}?stats=true`);
    expect(req.request.method).toBe('GET');
  });

  it('should call getInfo', () => {
    service.getInfo().subscribe();
    const req = httpTesting.expectOne(`ui-${apiPath}/info`);
    expect(req.request.method).toBe('GET');
  });

  it('should call create', () => {
    const pool = { pool: 'somePool' };
    service.create(pool).subscribe();
    const req = httpTesting.expectOne(apiPath);
    expect(req.request.method).toBe('POST');
    expect(req.request.body).toEqual(pool);
  });

  it('should call update', () => {
    service.update({ pool: 'somePool', application_metadata: [] }).subscribe();
    const req = httpTesting.expectOne(`${apiPath}/somePool`);
    expect(req.request.method).toBe('PUT');
    expect(req.request.body).toEqual({ application_metadata: [] });
  });

  it('should call delete', () => {
    service.delete('somePool').subscribe();
    const req = httpTesting.expectOne(`${apiPath}/somePool`);
    expect(req.request.method).toBe('DELETE');
  });

  it('should call list without parameter', fakeAsync(() => {
    let result;
    service.list().then((resp) => (result = resp));
    const req = httpTesting.expectOne(`${apiPath}?attrs=`);
    expect(req.request.method).toBe('GET');
    req.flush(['foo', 'bar']);
    tick();
    expect(result).toEqual(['foo', 'bar']);
  }));

  it('should call list with a list', fakeAsync(() => {
    let result;
    service.list(['foo']).then((resp) => (result = resp));
    const req = httpTesting.expectOne(`${apiPath}?attrs=foo`);
    expect(req.request.method).toBe('GET');
    req.flush(['foo', 'bar']);
    tick();
    expect(result).toEqual(['foo', 'bar']);
  }));

  it('should test injection of data from getConfiguration()', fakeAsync(() => {
    const pool = 'foo';
    let value;
    service.getConfiguration(pool).subscribe((next) => (value = next));
    const req = httpTesting.expectOne(`${apiPath}/${pool}/configuration`);
    expect(req.request.method).toBe('GET');
    req.flush([
      {
        name: 'rbd_qos_bps_limit',
        value: '60',
        source: RbdConfigurationSourceField.global
      },
      {
        name: 'rbd_qos_iops_limit',
        value: '0',
        source: RbdConfigurationSourceField.global
      }
    ]);
    tick();
    expect(value).toEqual([
      {
        description: 'The desired limit of IO bytes per second.',
        displayName: 'BPS Limit',
        name: 'rbd_qos_bps_limit',
        source: RbdConfigurationSourceField.global,
        type: 0,
        value: '60'
      },
      {
        description: 'The desired limit of IO operations per second.',
        displayName: 'IOPS Limit',
        name: 'rbd_qos_iops_limit',
        source: RbdConfigurationSourceField.global,
        type: 1,
        value: '0'
      }
    ]);
  }));
});