summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts
blob: 3f883d91fdfa174d329d51a3aeb30dceb54070ab (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { HttpRequest } from '@angular/common/http';
import {
  HttpClientTestingModule,
  HttpTestingController,
  TestRequest
} from '@angular/common/http/testing';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';

import { configureTestBed } from '~/testing/unit-test-helper';
import { RbdMirroringService } from './rbd-mirroring.service';

describe('RbdMirroringService', () => {
  let service: RbdMirroringService;
  let httpTesting: HttpTestingController;
  let getMirroringSummaryCalls: () => TestRequest[];
  let flushCalls: (call: TestRequest) => void;

  const summary: Record<string, any> = {
    status: 0,
    content_data: {
      daemons: [],
      pools: [],
      image_error: [],
      image_syncing: [],
      image_ready: []
    },
    executing_tasks: [{}]
  };

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

  beforeEach(() => {
    service = TestBed.inject(RbdMirroringService);
    httpTesting = TestBed.inject(HttpTestingController);
    getMirroringSummaryCalls = () => {
      return httpTesting.match((request: HttpRequest<any>) => {
        return request.url.match(/api\/block\/mirroring\/summary/) && request.method === 'GET';
      });
    };
    flushCalls = (call: TestRequest) => {
      if (!call.cancelled) {
        call.flush(summary);
      }
    };
  });

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

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

  it('should periodically poll summary', fakeAsync(() => {
    const subs = service.startPolling();
    tick();
    const calledWith: any[] = [];
    service.subscribeSummary((data) => {
      calledWith.push(data);
    });
    tick(service.REFRESH_INTERVAL * 2);
    const calls = getMirroringSummaryCalls();

    expect(calls.length).toEqual(3);
    calls.forEach((call: TestRequest) => flushCalls(call));
    expect(calledWith).toEqual([summary]);

    subs.unsubscribe();
  }));

  it('should get pool config', () => {
    service.getPool('poolName').subscribe();

    const req = httpTesting.expectOne('api/block/mirroring/pool/poolName');
    expect(req.request.method).toBe('GET');
  });

  it('should update pool config', () => {
    const request = {
      mirror_mode: 'pool'
    };
    service.updatePool('poolName', request).subscribe();

    const req = httpTesting.expectOne('api/block/mirroring/pool/poolName');
    expect(req.request.method).toBe('PUT');
    expect(req.request.body).toEqual(request);
  });

  it('should get site name', () => {
    service.getSiteName().subscribe();

    const req = httpTesting.expectOne('api/block/mirroring/site_name');
    expect(req.request.method).toBe('GET');
  });

  it('should set site name', () => {
    service.setSiteName('site-a').subscribe();

    const req = httpTesting.expectOne('api/block/mirroring/site_name');
    expect(req.request.method).toBe('PUT');
    expect(req.request.body).toEqual({ site_name: 'site-a' });
  });

  it('should create bootstrap token', () => {
    service.createBootstrapToken('poolName').subscribe();

    const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/bootstrap/token');
    expect(req.request.method).toBe('POST');
  });

  it('should import bootstrap token', () => {
    service.importBootstrapToken('poolName', 'rx', 'token-1234').subscribe();

    const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/bootstrap/peer');
    expect(req.request.method).toBe('POST');
    expect(req.request.body).toEqual({
      direction: 'rx',
      token: 'token-1234'
    });
  });

  it('should get peer config', () => {
    service.getPeer('poolName', 'peerUUID').subscribe();

    const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/peer/peerUUID');
    expect(req.request.method).toBe('GET');
  });

  it('should add peer config', () => {
    const request = {
      cluster_name: 'remote',
      client_id: 'admin',
      mon_host: 'localhost',
      key: '1234'
    };
    service.addPeer('poolName', request).subscribe();

    const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/peer');
    expect(req.request.method).toBe('POST');
    expect(req.request.body).toEqual(request);
  });

  it('should update peer config', () => {
    const request = {
      cluster_name: 'remote'
    };
    service.updatePeer('poolName', 'peerUUID', request).subscribe();

    const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/peer/peerUUID');
    expect(req.request.method).toBe('PUT');
    expect(req.request.body).toEqual(request);
  });

  it('should delete peer config', () => {
    service.deletePeer('poolName', 'peerUUID').subscribe();

    const req = httpTesting.expectOne('api/block/mirroring/pool/poolName/peer/peerUUID');
    expect(req.request.method).toBe('DELETE');
  });
});