summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-tabs/cephfs-tabs.component.spec.ts
blob: 6a8a3991b108ea4dfa941a42be8a6d2db8b593c0 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { Component, Input } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { TreeModule } from '@circlon/angular-tree-component';
import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap';
import _ from 'lodash';
import { ToastrModule } from 'ngx-toastr';
import { of } from 'rxjs';

import { CephfsService } from '~/app/shared/api/cephfs.service';
import { TableStatusViewCache } from '~/app/shared/classes/table-status-view-cache';
import { ViewCacheStatus } from '~/app/shared/enum/view-cache-status.enum';
import { SharedModule } from '~/app/shared/shared.module';
import { configureTestBed } from '~/testing/unit-test-helper';
import { CephfsClientsComponent } from '../cephfs-clients/cephfs-clients.component';
import { CephfsDetailComponent } from '../cephfs-detail/cephfs-detail.component';
import { CephfsDirectoriesComponent } from '../cephfs-directories/cephfs-directories.component';
import { CephfsTabsComponent } from './cephfs-tabs.component';

describe('CephfsTabsComponent', () => {
  let component: CephfsTabsComponent;
  let fixture: ComponentFixture<CephfsTabsComponent>;
  let service: CephfsService;
  let data: {
    standbys: string;
    pools: any[];
    ranks: any[];
    mdsCounters: object;
    name: string;
    clients: { status: ViewCacheStatus; data: any[] };
  };

  let old: any;
  const getReload: any = () => component['reloadSubscriber'];
  const setReload = (sth?: any) => (component['reloadSubscriber'] = sth);
  const mockRunOutside = () => {
    component['subscribeInterval'] = () => {
      // It's mocked because the rxjs timer subscription isn't called through the use of 'tick'.
      setReload({
        unsubscribed: false,
        unsubscribe: () => {
          old = getReload();
          getReload().unsubscribed = true;
          setReload();
        }
      });
      component.refresh();
    };
  };

  const setSelection = (selection: any) => {
    component.selection = selection;
    component.ngOnChanges();
  };

  const selectFs = (id: number, name: string) => {
    setSelection({
      id,
      mdsmap: {
        info: {
          something: {
            name
          }
        }
      }
    });
  };

  const updateData = () => {
    component['data'] = _.cloneDeep(data);
    component.softRefresh();
  };

  @Component({ selector: 'cd-cephfs-chart', template: '' })
  class CephfsChartStubComponent {
    @Input()
    mdsCounter: any;
  }

  configureTestBed({
    imports: [
      SharedModule,
      NgbNavModule,
      HttpClientTestingModule,
      TreeModule,
      ToastrModule.forRoot()
    ],
    declarations: [
      CephfsTabsComponent,
      CephfsChartStubComponent,
      CephfsDetailComponent,
      CephfsDirectoriesComponent,
      CephfsClientsComponent
    ]
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(CephfsTabsComponent);
    component = fixture.componentInstance;
    component.selection = undefined;
    data = {
      standbys: 'b',
      pools: [{}, {}],
      ranks: [{}, {}, {}],
      mdsCounters: { a: { name: 'a', x: [], y: [] } },
      name: 'someFs',
      clients: {
        status: ViewCacheStatus.ValueOk,
        data: [{}, {}, {}, {}]
      }
    };
    service = TestBed.inject(CephfsService);
    spyOn(service, 'getTabs').and.callFake(() => of(data));

    fixture.detectChanges();
    mockRunOutside();
    setReload(); // Clears rxjs timer subscription
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should resist invalid mds info', () => {
    setSelection({
      id: 3,
      mdsmap: {
        info: {}
      }
    });
    expect(component.grafanaId).toBe(undefined);
  });

  it('should find out the grafana id', () => {
    selectFs(2, 'otherMds');
    expect(component.grafanaId).toBe('otherMds');
  });

  it('should set default values on id change before api request', () => {
    const defaultDetails: Record<string, any> = {
      standbys: '',
      pools: [],
      ranks: [],
      mdsCounters: {},
      name: ''
    };
    const defaultClients: Record<string, any> = {
      data: [],
      status: new TableStatusViewCache(ViewCacheStatus.ValueNone)
    };
    component['subscribeInterval'] = () => undefined;
    updateData();
    expect(component.clients).not.toEqual(defaultClients);
    expect(component.details).not.toEqual(defaultDetails);
    selectFs(2, 'otherMds');
    expect(component.clients).toEqual(defaultClients);
    expect(component.details).toEqual(defaultDetails);
  });

  it('should force data updates on tab change without api requests', () => {
    const oldClients = component.clients;
    const oldDetails = component.details;
    updateData();
    expect(service.getTabs).toHaveBeenCalledTimes(0);
    expect(component.details).not.toBe(oldDetails);
    expect(component.clients).not.toBe(oldClients);
  });

  describe('handling of id change', () => {
    beforeEach(() => {
      setReload(); // Clears rxjs timer subscription
      selectFs(2, 'otherMds');
      old = getReload(); // Gets current subscription
    });

    it('should have called getDetails once', () => {
      expect(component.details.pools.length).toBe(2);
      expect(service.getTabs).toHaveBeenCalledTimes(1);
    });

    it('should not subscribe to an new interval for the same selection', () => {
      expect(component.id).toBe(2);
      expect(component.grafanaId).toBe('otherMds');
      selectFs(2, 'otherMds');
      expect(component.id).toBe(2);
      expect(component.grafanaId).toBe('otherMds');
      expect(getReload()).toBe(old);
    });

    it('should subscribe to an new interval', () => {
      selectFs(3, 'anotherMds');
      expect(getReload()).not.toBe(old); // Holds an new object
    });

    it('should unsubscribe the old interval if it exists', () => {
      selectFs(3, 'anotherMds');
      expect(old.unsubscribed).toBe(true);
    });

    it('should not unsubscribe if no interval exists', () => {
      expect(() => component.ngOnDestroy()).not.toThrow();
    });

    it('should request the details of the new id', () => {
      expect(service.getTabs).toHaveBeenCalledWith(2);
    });

    it('should should unsubscribe on deselect', () => {
      setSelection(undefined);
      expect(old.unsubscribed).toBe(true);
      expect(getReload()).toBe(undefined); // Cleared timer subscription
    });
  });
});