summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.spec.ts
blob: 5369a578d4c67e414b602430e39461747c541e0e (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { HttpClient } from '@angular/common/http';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import { of as observableOf, Subscriber, Subscription } from 'rxjs';

import { configureTestBed } from '~/testing/unit-test-helper';
import { ExecutingTask } from '../models/executing-task';
import { Summary } from '../models/summary.model';
import { AuthStorageService } from './auth-storage.service';
import { SummaryService } from './summary.service';

describe('SummaryService', () => {
  let summaryService: SummaryService;
  let authStorageService: AuthStorageService;
  let subs: Subscription;

  const summary: Summary = {
    executing_tasks: [],
    health_status: 'HEALTH_OK',
    mgr_id: 'x',
    rbd_mirroring: { errors: 0, warnings: 0 },
    rbd_pools: [],
    have_mon_connection: true,
    finished_tasks: [],
    filesystems: [{ id: 1, name: 'cephfs_a' }]
  };

  const httpClientSpy = {
    get: () => observableOf(summary)
  };

  const nextSummary = (newData: any) => summaryService['summaryDataSource'].next(newData);

  configureTestBed({
    imports: [RouterTestingModule],
    providers: [
      SummaryService,
      AuthStorageService,
      { provide: HttpClient, useValue: httpClientSpy }
    ]
  });

  beforeEach(() => {
    summaryService = TestBed.inject(SummaryService);
    authStorageService = TestBed.inject(AuthStorageService);
  });

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

  it('should call refresh', fakeAsync(() => {
    authStorageService.set('foobar', undefined, undefined);
    const calledWith: any[] = [];
    subs = new Subscription();
    subs.add(summaryService.startPolling());
    tick();
    subs.add(
      summaryService.subscribe((data) => {
        calledWith.push(data);
      })
    );
    expect(calledWith).toEqual([summary]);
    subs.add(summaryService.refresh());
    expect(calledWith).toEqual([summary, summary]);
    tick(summaryService.REFRESH_INTERVAL * 2);
    expect(calledWith.length).toEqual(4);
    subs.unsubscribe();
  }));

  describe('Should test subscribe without initial value', () => {
    let result: Summary;
    let i: number;

    const callback = (response: Summary) => {
      i++;
      result = response;
    };

    beforeEach(() => {
      i = 0;
      result = undefined;
      nextSummary(undefined);
    });

    it('should call subscribeOnce', () => {
      const subscriber = summaryService.subscribeOnce(callback);

      expect(subscriber).toEqual(jasmine.any(Subscriber));
      expect(i).toBe(0);
      expect(result).toEqual(undefined);

      nextSummary(undefined);
      expect(i).toBe(0);
      expect(result).toEqual(undefined);
      expect(subscriber.closed).toBe(false);

      nextSummary(summary);
      expect(result).toEqual(summary);
      expect(i).toBe(1);
      expect(subscriber.closed).toBe(true);

      nextSummary(summary);
      expect(result).toEqual(summary);
      expect(i).toBe(1);
    });

    it('should call subscribe', () => {
      const subscriber = summaryService.subscribe(callback);

      expect(subscriber).toEqual(jasmine.any(Subscriber));
      expect(i).toBe(0);
      expect(result).toEqual(undefined);

      nextSummary(undefined);
      expect(i).toBe(0);
      expect(result).toEqual(undefined);
      expect(subscriber.closed).toBe(false);

      nextSummary(summary);
      expect(result).toEqual(summary);
      expect(i).toBe(1);
      expect(subscriber.closed).toBe(false);

      nextSummary(summary);
      expect(result).toEqual(summary);
      expect(i).toBe(2);
      expect(subscriber.closed).toBe(false);
    });
  });

  describe('Should test methods after first refresh', () => {
    beforeEach(() => {
      authStorageService.set('foobar', undefined, undefined);
      summaryService.refresh();
    });

    it('should call addRunningTask', () => {
      summaryService.addRunningTask(
        new ExecutingTask('rbd/delete', {
          pool_name: 'somePool',
          image_name: 'someImage'
        })
      );
      let result: any;
      summaryService.subscribeOnce((response) => {
        result = response;
      });

      expect(result.executing_tasks.length).toBe(1);
      expect(result.executing_tasks[0]).toEqual({
        metadata: { image_name: 'someImage', pool_name: 'somePool' },
        name: 'rbd/delete'
      });
    });

    it('should call addRunningTask with duplicate task', () => {
      let result: any;
      summaryService.subscribe((response) => {
        result = response;
      });

      const exec_task = new ExecutingTask('rbd/delete', {
        pool_name: 'somePool',
        image_name: 'someImage'
      });

      result.executing_tasks = [exec_task];
      nextSummary(result);

      expect(result.executing_tasks.length).toBe(1);

      summaryService.addRunningTask(exec_task);

      expect(result.executing_tasks.length).toBe(1);
    });
  });
});