summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/services/task-manager.service.spec.ts
blob: 117b60c7edcd09281bbe91280ef9e82e4d4b7b97 (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
import { fakeAsync, TestBed, tick } from '@angular/core/testing';

import _ from 'lodash';
import { BehaviorSubject } from 'rxjs';

import { configureTestBed } from '~/testing/unit-test-helper';
import { SummaryService } from './summary.service';
import { TaskManagerService } from './task-manager.service';

const summary: Record<string, any> = {
  executing_tasks: [],
  health_status: 'HEALTH_OK',
  mgr_id: 'x',
  rbd_mirroring: { errors: 0, warnings: 0 },
  rbd_pools: [],
  have_mon_connection: true,
  finished_tasks: [{ name: 'foo', metadata: {} }],
  filesystems: [{ id: 1, name: 'cephfs_a' }]
};

export class SummaryServiceMock {
  summaryDataSource = new BehaviorSubject(summary);
  summaryData$ = this.summaryDataSource.asObservable();

  refresh() {
    this.summaryDataSource.next(summary);
  }
  subscribe(call: any) {
    return this.summaryData$.subscribe(call);
  }
}

describe('TaskManagerService', () => {
  let taskManagerService: TaskManagerService;
  let summaryService: any;
  let called: boolean;

  configureTestBed({
    providers: [TaskManagerService, { provide: SummaryService, useClass: SummaryServiceMock }]
  });

  beforeEach(() => {
    taskManagerService = TestBed.inject(TaskManagerService);
    summaryService = TestBed.inject(SummaryService);
    called = false;
    taskManagerService.subscribe('foo', {}, () => (called = true));
  });

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

  it('should subscribe and be notified when task is finished', fakeAsync(() => {
    expect(taskManagerService.subscriptions.length).toBe(1);
    summaryService.refresh();
    tick();
    taskManagerService.init(summaryService);
    expect(called).toEqual(true);
    expect(taskManagerService.subscriptions).toEqual([]);
  }));

  it('should subscribe and process executing taks', fakeAsync(() => {
    const original_subscriptions = _.cloneDeep(taskManagerService.subscriptions);
    _.assign(summary, {
      executing_tasks: [{ name: 'foo', metadata: {} }],
      finished_tasks: []
    });
    summaryService.refresh();
    tick();
    expect(taskManagerService.subscriptions).toEqual(original_subscriptions);
  }));
});