]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-daemon.service.spec.ts
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / rgw-daemon.service.spec.ts
1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
3
4 import { of } from 'rxjs';
5
6 import { RgwDaemon } from '~/app/ceph/rgw/models/rgw-daemon';
7 import { configureTestBed, RgwHelper } from '~/testing/unit-test-helper';
8 import { RgwDaemonService } from './rgw-daemon.service';
9
10 describe('RgwDaemonService', () => {
11 let service: RgwDaemonService;
12 let httpTesting: HttpTestingController;
13 let selectDaemonSpy: jasmine.Spy;
14
15 const daemonList = RgwHelper.getDaemonList();
16 const retrieveDaemonList = (reqDaemonList: RgwDaemon[], daemon: RgwDaemon) => {
17 service
18 .request((params) => of(params))
19 .subscribe((params) => expect(params.get('daemon_name')).toBe(daemon.id));
20 const listReq = httpTesting.expectOne('api/rgw/daemon');
21 listReq.flush(reqDaemonList);
22 tick();
23 expect(service['selectedDaemon'].getValue()).toEqual(daemon);
24 };
25
26 configureTestBed({
27 providers: [RgwDaemonService],
28 imports: [HttpClientTestingModule]
29 });
30
31 beforeEach(() => {
32 service = TestBed.inject(RgwDaemonService);
33 selectDaemonSpy = spyOn(service, 'selectDaemon').and.callThrough();
34 httpTesting = TestBed.inject(HttpTestingController);
35 });
36
37 afterEach(() => {
38 httpTesting.verify();
39 });
40
41 it('should be created', () => {
42 expect(service).toBeTruthy();
43 });
44
45 it('should get daemon list', () => {
46 service.list().subscribe();
47 const req = httpTesting.expectOne('api/rgw/daemon');
48 req.flush(daemonList);
49 expect(req.request.method).toBe('GET');
50 expect(service['daemons'].getValue()).toEqual(daemonList);
51 });
52
53 it('should get daemon ', () => {
54 service.get('foo').subscribe();
55 const req = httpTesting.expectOne('api/rgw/daemon/foo');
56 expect(req.request.method).toBe('GET');
57 });
58
59 it('should call request and not select any daemon from empty daemon list', fakeAsync(() => {
60 expect(() => retrieveDaemonList([], null)).toThrowError('No RGW daemons found!');
61 expect(selectDaemonSpy).toHaveBeenCalledTimes(0);
62 }));
63
64 it('should call request and select default daemon from daemon list', fakeAsync(() => {
65 retrieveDaemonList(daemonList, daemonList[1]);
66 expect(selectDaemonSpy).toHaveBeenCalledTimes(1);
67 expect(selectDaemonSpy).toHaveBeenCalledWith(daemonList[1]);
68 }));
69
70 it('should call request and select first daemon from daemon list that has no default', fakeAsync(() => {
71 const noDefaultDaemonList = daemonList.map((daemon) => {
72 daemon.default = false;
73 return daemon;
74 });
75 retrieveDaemonList(noDefaultDaemonList, noDefaultDaemonList[0]);
76 expect(selectDaemonSpy).toHaveBeenCalledTimes(1);
77 expect(selectDaemonSpy).toHaveBeenCalledWith(noDefaultDaemonList[0]);
78 }));
79 });