]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/dashboard: Add unit test for frontend api services
authorTiago Melo <tmelo@suse.com>
Sun, 10 Jun 2018 00:42:35 +0000 (01:42 +0100)
committerTiago Melo <tmelo@suse.com>
Tue, 12 Jun 2018 13:57:44 +0000 (14:57 +0100)
Signed-off-by: Tiago Melo <tmelo@suse.com>
17 files changed:
src/pybind/mgr/dashboard/frontend/src/app/shared/api/auth.service.spec.ts [new file with mode: 0644]
src/pybind/mgr/dashboard/frontend/src/app/shared/api/cephfs.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/api/configuration.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/api/dashboard.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/api/host.service.spec.ts [new file with mode: 0644]
src/pybind/mgr/dashboard/frontend/src/app/shared/api/logging.service.spec.ts [new file with mode: 0644]
src/pybind/mgr/dashboard/frontend/src/app/shared/api/logging.service.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/api/monitor.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/api/osd.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/api/performance-counter.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/api/pool.service.spec.ts [new file with mode: 0644]
src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd.service.spec.ts [new file with mode: 0644]
src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-bucket.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-daemon.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/api/tcmu-iscsi.service.spec.ts [new file with mode: 0644]

diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/auth.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/auth.service.spec.ts
new file mode 100644 (file)
index 0000000..f8d71b3
--- /dev/null
@@ -0,0 +1,56 @@
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { fakeAsync, TestBed, tick } from '@angular/core/testing';
+
+import { AuthStorageService } from '../services/auth-storage.service';
+
+import { configureTestBed } from '../unit-test-helper';
+import { AuthService } from './auth.service';
+
+describe('AuthService', () => {
+  let service: AuthService;
+  let httpTesting: HttpTestingController;
+
+  configureTestBed({
+    providers: [AuthService, AuthStorageService],
+    imports: [HttpClientTestingModule]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(AuthService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+
+  it(
+    'should login and save the user',
+    fakeAsync(() => {
+      const fakeCredentials = { username: 'foo', password: 'bar' };
+      service.login(<any>fakeCredentials);
+      const req = httpTesting.expectOne('api/auth');
+      expect(req.request.method).toBe('POST');
+      expect(req.request.body).toEqual(fakeCredentials);
+      req.flush(fakeCredentials);
+      tick();
+      expect(localStorage.getItem('dashboard_username')).toBe('foo');
+    })
+  );
+
+  it(
+    'should logout and remove the user',
+    fakeAsync(() => {
+      service.logout();
+      const req = httpTesting.expectOne('api/auth');
+      expect(req.request.method).toBe('DELETE');
+      req.flush({ username: 'foo' });
+      tick();
+      expect(localStorage.getItem('dashboard_username')).toBe(null);
+    })
+  );
+});
index 91fc8df7ecc85557d07848c6765f07695ed27449..1c8c14fee4fb55fbf865e5814ca099d3e1405634 100644 (file)
@@ -1,19 +1,52 @@
-import { HttpClientModule } from '@angular/common/http';
-import { inject } from '@angular/core/testing';
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { TestBed } from '@angular/core/testing';
 
 import { configureTestBed } from '../unit-test-helper';
 import { CephfsService } from './cephfs.service';
 
 describe('CephfsService', () => {
+  let service: CephfsService;
+  let httpTesting: HttpTestingController;
+
   configureTestBed({
-    imports: [HttpClientModule],
+    imports: [HttpClientTestingModule],
     providers: [CephfsService]
   });
 
-  it(
-    'should be created',
-    inject([CephfsService], (service: CephfsService) => {
-      expect(service).toBeTruthy();
-    })
-  );
+  beforeEach(() => {
+    service = TestBed.get(CephfsService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+
+  it('should call list', () => {
+    service.list().subscribe();
+    const req = httpTesting.expectOne('api/cephfs');
+    expect(req.request.method).toBe('GET');
+  });
+
+  it('should call getCephfs', () => {
+    service.getCephfs(1).subscribe();
+    const req = httpTesting.expectOne('api/cephfs/1');
+    expect(req.request.method).toBe('GET');
+  });
+
+  it('should call getClients', () => {
+    service.getClients(1).subscribe();
+    const req = httpTesting.expectOne('api/cephfs/1/clients');
+    expect(req.request.method).toBe('GET');
+  });
+
+  it('should call getMdsCounters', () => {
+    service.getMdsCounters(1).subscribe();
+    const req = httpTesting.expectOne('api/cephfs/1/mds_counters');
+    expect(req.request.method).toBe('GET');
+  });
 });
index be451104083213cbc7be9e109ad2f7f20ca34887..fa07822b508ed708d315c10fe38c4c99b3cc3fcc 100644 (file)
@@ -1,20 +1,34 @@
-import { HttpClientModule } from '@angular/common/http';
-import { HttpClientTestingModule } from '@angular/common/http/testing';
-import { inject } from '@angular/core/testing';
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { TestBed } from '@angular/core/testing';
 
 import { configureTestBed } from '../unit-test-helper';
 import { ConfigurationService } from './configuration.service';
 
 describe('ConfigurationService', () => {
+  let service: ConfigurationService;
+  let httpTesting: HttpTestingController;
+
   configureTestBed({
     providers: [ConfigurationService],
-    imports: [HttpClientTestingModule, HttpClientModule]
+    imports: [HttpClientTestingModule]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(ConfigurationService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
   });
 
-  it(
-    'should be created',
-    inject([ConfigurationService], (service: ConfigurationService) => {
-      expect(service).toBeTruthy();
-    })
-  );
+  it('should call getConfigData', () => {
+    service.getConfigData().subscribe();
+    const req = httpTesting.expectOne('api/cluster_conf/');
+    expect(req.request.method).toBe('GET');
+  });
 });
index d11d2b22447e875fe4c53465f12ecdd9685f6119..9062ba501b823f0c91afba6714b58c424a63a9c0 100644 (file)
@@ -1,20 +1,34 @@
-import { HttpClientModule } from '@angular/common/http';
-import { HttpClientTestingModule } from '@angular/common/http/testing';
-import { inject } from '@angular/core/testing';
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { TestBed } from '@angular/core/testing';
 
 import { configureTestBed } from '../unit-test-helper';
 import { DashboardService } from './dashboard.service';
 
 describe('DashboardService', () => {
+  let service: DashboardService;
+  let httpTesting: HttpTestingController;
+
   configureTestBed({
     providers: [DashboardService],
-    imports: [HttpClientTestingModule, HttpClientModule]
+    imports: [HttpClientTestingModule]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(DashboardService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
   });
 
-  it(
-    'should be created',
-    inject([DashboardService], (service: DashboardService) => {
-      expect(service).toBeTruthy();
-    })
-  );
+  it('should call getHealth', () => {
+    service.getHealth().subscribe();
+    const req = httpTesting.expectOne('api/dashboard/health');
+    expect(req.request.method).toBe('GET');
+  });
 });
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/host.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/host.service.spec.ts
new file mode 100644 (file)
index 0000000..445386f
--- /dev/null
@@ -0,0 +1,41 @@
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { fakeAsync, TestBed, tick } from '@angular/core/testing';
+
+import { configureTestBed } from '../unit-test-helper';
+import { HostService } from './host.service';
+
+describe('HostService', () => {
+  let service: HostService;
+  let httpTesting: HttpTestingController;
+
+  configureTestBed({
+    providers: [HostService],
+    imports: [HttpClientTestingModule]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(HostService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+
+  it(
+    'should call list',
+    fakeAsync(() => {
+      let result;
+      service.list().then((resp) => (result = resp));
+      const req = httpTesting.expectOne('api/host');
+      expect(req.request.method).toBe('GET');
+      req.flush(['foo', 'bar']);
+      tick();
+      expect(result).toEqual(['foo', 'bar']);
+    })
+  );
+});
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/logging.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/logging.service.spec.ts
new file mode 100644 (file)
index 0000000..fa8f0e9
--- /dev/null
@@ -0,0 +1,39 @@
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { TestBed } from '@angular/core/testing';
+
+import { configureTestBed } from '../unit-test-helper';
+import { LoggingService } from './logging.service';
+
+describe('LoggingService', () => {
+  let service: LoggingService;
+  let httpTesting: HttpTestingController;
+
+  configureTestBed({
+    providers: [LoggingService],
+    imports: [HttpClientTestingModule]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(LoggingService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+
+  it('should call jsError', () => {
+    service.jsError('foo', 'bar', 'baz').subscribe();
+    const req = httpTesting.expectOne('ui-api/logging/js-error');
+    expect(req.request.method).toBe('POST');
+    expect(req.request.body).toEqual({
+      url: 'foo',
+      message: 'bar',
+      stack: 'baz'
+    });
+  });
+});
index 097f86edfe82249646190cba0713db51f6a05275..3ed2a172643064d65059cdf57c4b1dfd34312ebb 100644 (file)
@@ -7,9 +7,7 @@ import { ApiModule } from './api.module';
   providedIn: ApiModule
 })
 export class LoggingService {
-
-  constructor(private http: HttpClient) {
-  }
+  constructor(private http: HttpClient) {}
 
   jsError(url, message, stack) {
     const request = {
@@ -19,5 +17,4 @@ export class LoggingService {
     };
     return this.http.post('ui-api/logging/js-error', request);
   }
-
 }
index fd59f30cab7e88dd61789f74e4462c8785d2bbe5..e1813b983cdb10697f15fbdd16cfbbb1ef6c2d2c 100644 (file)
@@ -1,20 +1,34 @@
-import { HttpClientModule } from '@angular/common/http';
-import { HttpClientTestingModule } from '@angular/common/http/testing';
-import { inject } from '@angular/core/testing';
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { TestBed } from '@angular/core/testing';
 
 import { configureTestBed } from '../unit-test-helper';
 import { MonitorService } from './monitor.service';
 
 describe('MonitorService', () => {
+  let service: MonitorService;
+  let httpTesting: HttpTestingController;
+
   configureTestBed({
     providers: [MonitorService],
-    imports: [HttpClientTestingModule, HttpClientModule]
+    imports: [HttpClientTestingModule]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(MonitorService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
   });
 
-  it(
-    'should be created',
-    inject([MonitorService], (service: MonitorService) => {
-      expect(service).toBeTruthy();
-    })
-  );
+  it('should call getMonitor', () => {
+    service.getMonitor().subscribe();
+    const req = httpTesting.expectOne('api/monitor');
+    expect(req.request.method).toBe('GET');
+  });
 });
index 848296c6eb6958600ff2429fffe30da18c9c4c65..d8237e714678ba8ff131f150c85a7d02461fcfe4 100644 (file)
@@ -1,19 +1,52 @@
-import { HttpClientModule } from '@angular/common/http';
-import { inject } from '@angular/core/testing';
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { TestBed } from '@angular/core/testing';
 
 import { configureTestBed } from '../unit-test-helper';
 import { OsdService } from './osd.service';
 
 describe('OsdService', () => {
+  let service: OsdService;
+  let httpTesting: HttpTestingController;
+
   configureTestBed({
     providers: [OsdService],
-    imports: [HttpClientModule]
+    imports: [HttpClientTestingModule]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(OsdService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
   });
 
-  it(
-    'should be created',
-    inject([OsdService], (service: OsdService) => {
-      expect(service).toBeTruthy();
-    })
-  );
+  it('should call getList', () => {
+    service.getList().subscribe();
+    const req = httpTesting.expectOne('api/osd');
+    expect(req.request.method).toBe('GET');
+  });
+
+  it('should call getDetails', () => {
+    service.getDetails(1).subscribe();
+    const req = httpTesting.expectOne('api/osd/1');
+    expect(req.request.method).toBe('GET');
+  });
+
+  it('should call scrub, with deep=true', () => {
+    service.scrub('foo', true).subscribe();
+    const req = httpTesting.expectOne('api/osd/foo/scrub?deep=true');
+    expect(req.request.method).toBe('POST');
+  });
+
+  it('should call scrub, with deep=false', () => {
+    service.scrub('foo', false).subscribe();
+    const req = httpTesting.expectOne('api/osd/foo/scrub?deep=false');
+    expect(req.request.method).toBe('POST');
+  });
 });
index 5b616ffd21e2e805ea7206fd086b468ac8894bc0..ec87677237b7ebb7013b4c05aecfff82650cd542 100644 (file)
@@ -1,22 +1,45 @@
-import { HttpClientModule } from '@angular/common/http';
-import { HttpClientTestingModule } from '@angular/common/http/testing';
-import { inject } from '@angular/core/testing';
-
-import { BsDropdownModule } from 'ngx-bootstrap';
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { TestBed } from '@angular/core/testing';
 
 import { configureTestBed } from '../unit-test-helper';
 import { PerformanceCounterService } from './performance-counter.service';
 
 describe('PerformanceCounterService', () => {
+  let service: PerformanceCounterService;
+  let httpTesting: HttpTestingController;
+
   configureTestBed({
     providers: [PerformanceCounterService],
-    imports: [HttpClientTestingModule, BsDropdownModule.forRoot(), HttpClientModule]
+    imports: [HttpClientTestingModule]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(PerformanceCounterService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
   });
 
-  it(
-    'should be created',
-    inject([PerformanceCounterService], (service: PerformanceCounterService) => {
-      expect(service).toBeTruthy();
-    })
-  );
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+
+  it('should call list', () => {
+    service.list().subscribe();
+    const req = httpTesting.expectOne('api/perf_counters');
+    expect(req.request.method).toBe('GET');
+  });
+
+  it('should call get', () => {
+    let result;
+    service.get('foo', '1').subscribe((resp) => {
+      result = resp;
+    });
+    const req = httpTesting.expectOne('api/perf_counters/foo/1');
+    expect(req.request.method).toBe('GET');
+    req.flush({ counters: [{ foo: 'bar' }] });
+    expect(result).toEqual([{ foo: 'bar' }]);
+  });
 });
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/pool.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/pool.service.spec.ts
new file mode 100644 (file)
index 0000000..693fd89
--- /dev/null
@@ -0,0 +1,60 @@
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { fakeAsync, TestBed, tick } from '@angular/core/testing';
+
+import { configureTestBed } from '../unit-test-helper';
+import { PoolService } from './pool.service';
+
+describe('PoolService', () => {
+  let service: PoolService;
+  let httpTesting: HttpTestingController;
+
+  configureTestBed({
+    providers: [PoolService],
+    imports: [HttpClientTestingModule]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(PoolService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+
+  it('should call getList', () => {
+    service.getList().subscribe();
+    const req = httpTesting.expectOne('api/pool');
+    expect(req.request.method).toBe('GET');
+  });
+
+  it(
+    'should call list without parameter',
+    fakeAsync(() => {
+      let result;
+      service.list().then((resp) => (result = resp));
+      const req = httpTesting.expectOne('api/pool?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('api/pool?attrs=foo');
+      expect(req.request.method).toBe('GET');
+      req.flush(['foo', 'bar']);
+      tick();
+      expect(result).toEqual(['foo', 'bar']);
+    })
+  );
+});
index 0a6321f9bceea09d1fcd840e7019941f6131daf0..827a185c9ba1b2f6ddb1d34199f279cf0362c039 100644 (file)
@@ -1,20 +1,34 @@
-import { HttpClientModule } from '@angular/common/http';
-import { HttpClientTestingModule } from '@angular/common/http/testing';
-import { inject } from '@angular/core/testing';
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { TestBed } from '@angular/core/testing';
 
 import { configureTestBed } from '../unit-test-helper';
 import { RbdMirroringService } from './rbd-mirroring.service';
 
 describe('RbdMirroringService', () => {
+  let service: RbdMirroringService;
+  let httpTesting: HttpTestingController;
+
   configureTestBed({
     providers: [RbdMirroringService],
-    imports: [HttpClientTestingModule, HttpClientModule]
+    imports: [HttpClientTestingModule]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(RbdMirroringService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
   });
 
-  it(
-    'should be created',
-    inject([RbdMirroringService], (service: RbdMirroringService) => {
-      expect(service).toBeTruthy();
-    })
-  );
+  it('should call get', () => {
+    service.get().subscribe();
+    const req = httpTesting.expectOne('api/rbdmirror');
+    expect(req.request.method).toBe('GET');
+  });
 });
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd.service.spec.ts
new file mode 100644 (file)
index 0000000..4d311c8
--- /dev/null
@@ -0,0 +1,129 @@
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { TestBed } from '@angular/core/testing';
+
+import { configureTestBed } from '../unit-test-helper';
+import { RbdService } from './rbd.service';
+
+describe('RbdService', () => {
+  let service: RbdService;
+  let httpTesting: HttpTestingController;
+
+  configureTestBed({
+    providers: [RbdService],
+    imports: [HttpClientTestingModule]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(RbdService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+
+  it('should call create', () => {
+    service.create('foo').subscribe();
+    const req = httpTesting.expectOne('api/block/image');
+    expect(req.request.method).toBe('POST');
+    expect(req.request.body).toEqual('foo');
+  });
+
+  it('should call delete', () => {
+    service.delete('poolName', 'rbdName').subscribe();
+    const req = httpTesting.expectOne('api/block/image/poolName/rbdName');
+    expect(req.request.method).toBe('DELETE');
+  });
+
+  it('should call update', () => {
+    service.update('poolName', 'rbdName', 'foo').subscribe();
+    const req = httpTesting.expectOne('api/block/image/poolName/rbdName');
+    expect(req.request.body).toEqual('foo');
+    expect(req.request.method).toBe('PUT');
+  });
+
+  it('should call get', () => {
+    service.get('poolName', 'rbdName').subscribe();
+    const req = httpTesting.expectOne('api/block/image/poolName/rbdName');
+    expect(req.request.method).toBe('GET');
+  });
+
+  it('should call list', () => {
+    service.list().subscribe();
+    const req = httpTesting.expectOne('api/block/image');
+    expect(req.request.method).toBe('GET');
+  });
+
+  it('should call copy', () => {
+    service.copy('poolName', 'rbdName', 'foo').subscribe();
+    const req = httpTesting.expectOne('api/block/image/poolName/rbdName/copy');
+    expect(req.request.body).toEqual('foo');
+    expect(req.request.method).toBe('POST');
+  });
+
+  it('should call flatten', () => {
+    service.flatten('poolName', 'rbdName').subscribe();
+    const req = httpTesting.expectOne('api/block/image/poolName/rbdName/flatten');
+    expect(req.request.body).toEqual(null);
+    expect(req.request.method).toBe('POST');
+  });
+
+  it('should call defaultFeatures', () => {
+    service.defaultFeatures().subscribe();
+    const req = httpTesting.expectOne('api/block/image/default_features');
+    expect(req.request.method).toBe('GET');
+  });
+
+  it('should call createSnapshot', () => {
+    service.createSnapshot('poolName', 'rbdName', 'snapshotName').subscribe();
+    const req = httpTesting.expectOne('api/block/image/poolName/rbdName/snap');
+    expect(req.request.body).toEqual({
+      snapshot_name: 'snapshotName'
+    });
+    expect(req.request.method).toBe('POST');
+  });
+
+  it('should call renameSnapshot', () => {
+    service.renameSnapshot('poolName', 'rbdName', 'snapshotName', 'foo').subscribe();
+    const req = httpTesting.expectOne('api/block/image/poolName/rbdName/snap/snapshotName');
+    expect(req.request.body).toEqual({
+      new_snap_name: 'foo'
+    });
+    expect(req.request.method).toBe('PUT');
+  });
+
+  it('should call protectSnapshot', () => {
+    service.protectSnapshot('poolName', 'rbdName', 'snapshotName', true).subscribe();
+    const req = httpTesting.expectOne('api/block/image/poolName/rbdName/snap/snapshotName');
+    expect(req.request.body).toEqual({
+      is_protected: true
+    });
+    expect(req.request.method).toBe('PUT');
+  });
+
+  it('should call rollbackSnapshot', () => {
+    service.rollbackSnapshot('poolName', 'rbdName', 'snapshotName').subscribe();
+    const req = httpTesting.expectOne(
+      'api/block/image/poolName/rbdName/snap/snapshotName/rollback'
+    );
+    expect(req.request.body).toEqual(null);
+    expect(req.request.method).toBe('POST');
+  });
+
+  it('should call cloneSnapshot', () => {
+    service.cloneSnapshot('poolName', 'rbdName', 'snapshotName', null).subscribe();
+    const req = httpTesting.expectOne('api/block/image/poolName/rbdName/snap/snapshotName/clone');
+    expect(req.request.body).toEqual(null);
+    expect(req.request.method).toBe('POST');
+  });
+
+  it('should call deleteSnapshot', () => {
+    service.deleteSnapshot('poolName', 'rbdName', 'snapshotName').subscribe();
+    const req = httpTesting.expectOne('api/block/image/poolName/rbdName/snap/snapshotName');
+    expect(req.request.method).toBe('DELETE');
+  });
+});
index 50890f1bed3cb20f25e6294c4a9a2e7251cf95c6..1a7c7c73173dcce5141dd593047c2d00e5e459d2 100644 (file)
-import { HttpClientModule } from '@angular/common/http';
-import { HttpClientTestingModule } from '@angular/common/http/testing';
-import { inject } from '@angular/core/testing';
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { TestBed } from '@angular/core/testing';
 
 import { configureTestBed } from '../unit-test-helper';
 import { RgwBucketService } from './rgw-bucket.service';
 
 describe('RgwBucketService', () => {
+  let service: RgwBucketService;
+  let httpTesting: HttpTestingController;
+
   configureTestBed({
     providers: [RgwBucketService],
-    imports: [HttpClientTestingModule, HttpClientModule]
+    imports: [HttpClientTestingModule]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(RgwBucketService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
   });
 
-  it(
-    'should be created',
-    inject([RgwBucketService], (service: RgwBucketService) => {
-      expect(service).toBeTruthy();
-    })
-  );
+  it('should call list, with enumerate returning empty', () => {
+    let result;
+    service.list().subscribe((resp) => {
+      result = resp;
+    });
+    const req = httpTesting.expectOne('/api/rgw/proxy/bucket');
+    req.flush([]);
+    expect(req.request.method).toBe('GET');
+    expect(result).toEqual([]);
+  });
+
+  it('should call list, with enumerate returning 2 elements', () => {
+    let result;
+    service.list().subscribe((resp) => {
+      result = resp;
+    });
+    let req = httpTesting.expectOne('/api/rgw/proxy/bucket');
+    req.flush(['foo', 'bar']);
+
+    req = httpTesting.expectOne('/api/rgw/proxy/bucket?bucket=foo');
+    req.flush({ name: 'foo' });
+
+    req = httpTesting.expectOne('/api/rgw/proxy/bucket?bucket=bar');
+    req.flush({ name: 'bar' });
+
+    expect(req.request.method).toBe('GET');
+    expect(result).toEqual([{ name: 'foo' }, { name: 'bar' }]);
+  });
+
+  it('should call get', () => {
+    service.get('foo').subscribe();
+    const req = httpTesting.expectOne('/api/rgw/proxy/bucket?bucket=foo');
+    expect(req.request.method).toBe('GET');
+  });
+
+  it('should call create', () => {
+    service.create('foo', 'bar').subscribe();
+    const req = httpTesting.expectOne('/api/rgw/bucket');
+    expect(req.request.method).toBe('POST');
+    expect(req.request.body).toEqual({
+      bucket: 'foo',
+      uid: 'bar'
+    });
+  });
+
+  it('should call update', () => {
+    service.update('foo', 'bar', 'baz').subscribe();
+    const req = httpTesting.expectOne('/api/rgw/proxy/bucket?bucket=bar&bucket-id=foo&uid=baz');
+    expect(req.request.method).toBe('PUT');
+  });
+
+  it('should call delete, with purgeObjects = true', () => {
+    service.delete('foo').subscribe();
+    const req = httpTesting.expectOne('/api/rgw/proxy/bucket?bucket=foo&purge-objects=true');
+    expect(req.request.method).toBe('DELETE');
+  });
+
+  it('should call delete, with purgeObjects = false', () => {
+    service.delete('foo', false).subscribe();
+    const req = httpTesting.expectOne('/api/rgw/proxy/bucket?bucket=foo&purge-objects=false');
+    expect(req.request.method).toBe('DELETE');
+  });
+
+  it('should call exists', () => {
+    let result;
+    service.exists('foo').subscribe((resp) => {
+      result = resp;
+    });
+    const req = httpTesting.expectOne('/api/rgw/proxy/bucket');
+    expect(req.request.method).toBe('GET');
+    req.flush(['foo', 'bar']);
+    expect(result).toBe(true);
+  });
 });
index aa3e52219e7900338ee662a6a0b4bdd9f66648ac..362e474fe58a6eea7ad296bc1daecd781c5d3944 100644 (file)
@@ -1,20 +1,40 @@
-import { HttpClientModule } from '@angular/common/http';
-import { HttpClientTestingModule } from '@angular/common/http/testing';
-import { inject } from '@angular/core/testing';
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { TestBed } from '@angular/core/testing';
 
 import { configureTestBed } from '../unit-test-helper';
 import { RgwDaemonService } from './rgw-daemon.service';
 
 describe('RgwDaemonService', () => {
+  let service: RgwDaemonService;
+  let httpTesting: HttpTestingController;
+
   configureTestBed({
     providers: [RgwDaemonService],
-    imports: [HttpClientTestingModule, HttpClientModule]
+    imports: [HttpClientTestingModule]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(RgwDaemonService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
   });
 
-  it(
-    'should be created',
-    inject([RgwDaemonService], (service: RgwDaemonService) => {
-      expect(service).toBeTruthy();
-    })
-  );
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+
+  it('should call list', () => {
+    service.list().subscribe();
+    const req = httpTesting.expectOne('api/rgw/daemon');
+    expect(req.request.method).toBe('GET');
+  });
+
+  it('should call get', () => {
+    service.get('foo').subscribe();
+    const req = httpTesting.expectOne('api/rgw/daemon/foo');
+    expect(req.request.method).toBe('GET');
+  });
 });
index 59f1b22f44881053c8ccb959abcb1084f907bb6a..e9569ef19ec2a2d3dca707da4eb58ad9005e62e3 100644 (file)
-import { HttpClientModule } from '@angular/common/http';
-import { HttpClientTestingModule } from '@angular/common/http/testing';
-import { inject } from '@angular/core/testing';
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { TestBed } from '@angular/core/testing';
+
+import { of as observableOf } from 'rxjs';
 
 import { configureTestBed } from '../unit-test-helper';
 import { RgwUserService } from './rgw-user.service';
 
 describe('RgwUserService', () => {
+  let service: RgwUserService;
+  let httpTesting: HttpTestingController;
+
   configureTestBed({
-    providers: [RgwUserService],
-    imports: [HttpClientTestingModule, HttpClientModule]
+    imports: [HttpClientTestingModule],
+    providers: [RgwUserService]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(RgwUserService);
+    httpTesting = TestBed.get(HttpTestingController);
   });
 
-  it(
-    'should be created',
-    inject([RgwUserService], (service: RgwUserService) => {
-      expect(service).toBeTruthy();
-    })
-  );
+  afterEach(() => {
+    httpTesting.verify();
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+
+  it('should call list with empty result', () => {
+    let result;
+    service.list().subscribe((resp) => {
+      result = resp;
+    });
+    const req = httpTesting.expectOne('/api/rgw/proxy/metadata/user');
+    expect(req.request.method).toBe('GET');
+    req.flush([]);
+    expect(result).toEqual([]);
+  });
+
+  it('should call list with result', () => {
+    let result;
+    service.list().subscribe((resp) => {
+      result = resp;
+    });
+
+    let req = httpTesting.expectOne('/api/rgw/proxy/metadata/user');
+    expect(req.request.method).toBe('GET');
+    req.flush(['foo', 'bar']);
+
+    req = httpTesting.expectOne('/api/rgw/proxy/user?uid=foo');
+    expect(req.request.method).toBe('GET');
+    req.flush({ name: 'foo' });
+
+    req = httpTesting.expectOne('/api/rgw/proxy/user?uid=bar');
+    expect(req.request.method).toBe('GET');
+    req.flush({ name: 'bar' });
+
+    expect(result).toEqual([{ name: 'foo' }, { name: 'bar' }]);
+  });
+
+  it('should call enumerate', () => {
+    service.enumerate().subscribe();
+    const req = httpTesting.expectOne('/api/rgw/proxy/metadata/user');
+    expect(req.request.method).toBe('GET');
+  });
+
+  it('should call get', () => {
+    service.get('foo').subscribe();
+    const req = httpTesting.expectOne('/api/rgw/proxy/user?uid=foo');
+    expect(req.request.method).toBe('GET');
+  });
+
+  it('should call getQuota', () => {
+    service.getQuota('foo').subscribe();
+    const req = httpTesting.expectOne('/api/rgw/proxy/user?quota&uid=foo');
+    expect(req.request.method).toBe('GET');
+  });
+
+  it('should call put', () => {
+    service.put({ foo: 'bar' }).subscribe();
+    const req = httpTesting.expectOne('/api/rgw/proxy/user?foo=bar');
+    expect(req.request.method).toBe('PUT');
+  });
+
+  it('should call putQuota', () => {
+    service.putQuota({ foo: 'bar' }).subscribe();
+    const req = httpTesting.expectOne('/api/rgw/proxy/user?quota&foo=bar');
+    expect(req.request.method).toBe('PUT');
+  });
+
+  it('should call post', () => {
+    service.post({ foo: 'bar' }).subscribe();
+    const req = httpTesting.expectOne('/api/rgw/proxy/user?foo=bar');
+    expect(req.request.method).toBe('POST');
+  });
+
+  it('should call delete', () => {
+    service.delete('foo').subscribe();
+    const req = httpTesting.expectOne('/api/rgw/proxy/user?uid=foo');
+    expect(req.request.method).toBe('DELETE');
+  });
+
+  it('should call addSubuser with unrecognized permission', () => {
+    service.addSubuser('foo', 'bar', 'baz', null, true).subscribe();
+    const req = httpTesting.expectOne(
+      '/api/rgw/proxy/user?uid=foo&subuser=bar&key-type=swift&access=baz&generate-secret=true'
+    );
+    expect(req.request.method).toBe('PUT');
+  });
+
+  it('should call addSubuser with mapped permission', () => {
+    service.addSubuser('foo', 'bar', 'full-control', 'baz', false).subscribe();
+    const req = httpTesting.expectOne(
+      '/api/rgw/proxy/user?uid=foo&subuser=bar&key-type=swift&access=full&secret-key=baz'
+    );
+    expect(req.request.method).toBe('PUT');
+  });
+
+  it('should call deleteSubuser', () => {
+    service.deleteSubuser('foo', 'bar').subscribe();
+    const req = httpTesting.expectOne('/api/rgw/proxy/user?uid=foo&subuser=bar&purge-keys=true');
+    expect(req.request.method).toBe('DELETE');
+  });
+
+  it('should call addCapability', () => {
+    service.addCapability('foo', 'bar', 'baz').subscribe();
+    const req = httpTesting.expectOne('/api/rgw/proxy/user?caps&uid=foo&user-caps=bar=baz');
+    expect(req.request.method).toBe('PUT');
+  });
+
+  it('should call deleteCapability', () => {
+    service.deleteCapability('foo', 'bar', 'baz').subscribe();
+    const req = httpTesting.expectOne('/api/rgw/proxy/user?caps&uid=foo&user-caps=bar=baz');
+    expect(req.request.method).toBe('DELETE');
+  });
+
+  it('should call addS3Key, with generateKey true', () => {
+    service.addS3Key('1', 'foo', 'bar', 'baz', true).subscribe();
+    const req = httpTesting.expectOne(
+      '/api/rgw/proxy/user?key&uid=1&key-type=s3&generate-key=true&subuser=foo'
+    );
+    expect(req.request.method).toBe('PUT');
+  });
+
+  it('should call addS3Key, with generateKey false', () => {
+    service.addS3Key('1', 'foo', 'bar', 'baz', false).subscribe();
+    const req = httpTesting.expectOne(
+      '/api/rgw/proxy/user?key' +
+        '&uid=1&key-type=s3&generate-key=false&access-key=bar&secret-key=baz&subuser=foo'
+    );
+    expect(req.request.method).toBe('PUT');
+  });
+
+  it('should call deleteS3Key', () => {
+    service.deleteS3Key('foo', 'bar').subscribe();
+    const req = httpTesting.expectOne('/api/rgw/proxy/user?key&uid=foo&key-type=s3&access-key=bar');
+    expect(req.request.method).toBe('DELETE');
+  });
+
+  it('should call exists with an existent uid', () => {
+    spyOn(service, 'enumerate').and.returnValue(observableOf(['foo', 'bar']));
+    let result;
+    service.exists('foo').subscribe((res) => {
+      result = res;
+    });
+    expect(result).toBe(true);
+  });
+
+  it('should call exists with a non existent uid', () => {
+    spyOn(service, 'enumerate').and.returnValue(observableOf(['foo', 'bar']));
+    let result;
+    service.exists('baz').subscribe((res) => {
+      result = res;
+    });
+    expect(result).toBe(false);
+  });
 });
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/tcmu-iscsi.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/tcmu-iscsi.service.spec.ts
new file mode 100644 (file)
index 0000000..b917923
--- /dev/null
@@ -0,0 +1,43 @@
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { fakeAsync, TestBed, tick } from '@angular/core/testing';
+
+import { configureTestBed } from '../unit-test-helper';
+import { TcmuIscsiService } from './tcmu-iscsi.service';
+
+describe('TcmuIscsiService', () => {
+  let service: TcmuIscsiService;
+  let httpTesting: HttpTestingController;
+
+  configureTestBed({
+    providers: [TcmuIscsiService],
+    imports: [HttpClientTestingModule]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(TcmuIscsiService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+
+  it(
+    'should call tcmuiscsi',
+    fakeAsync(() => {
+      let result;
+      service.tcmuiscsi().then((resp) => {
+        result = resp;
+      });
+      const req = httpTesting.expectOne('api/tcmuiscsi');
+      expect(req.request.method).toBe('GET');
+      req.flush(['foo']);
+      tick();
+      expect(result).toEqual(['foo']);
+    })
+  );
+});