]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/telemetry/telemetry.component.spec.ts
import 15.2.4
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / cluster / telemetry / telemetry.component.spec.ts
1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { Router } from '@angular/router';
5 import { RouterTestingModule } from '@angular/router/testing';
6
7 import * as _ from 'lodash';
8 import { ToastrModule } from 'ngx-toastr';
9 import { of as observableOf } from 'rxjs';
10
11 import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper';
12 import { MgrModuleService } from '../../../shared/api/mgr-module.service';
13 import { TelemetryService } from '../../../shared/api/telemetry.service';
14
15 import { TextToDownloadService } from '../../../shared/services/text-to-download.service';
16 import { SharedModule } from '../../../shared/shared.module';
17 import { TelemetryComponent } from './telemetry.component';
18
19 describe('TelemetryComponent', () => {
20 let component: TelemetryComponent;
21 let fixture: ComponentFixture<TelemetryComponent>;
22 let mgrModuleService: MgrModuleService;
23 let telemetryService: TelemetryService;
24 let options: any;
25 let configs: any;
26 let httpTesting: HttpTestingController;
27 let router: Router;
28
29 const optionsNames = [
30 'channel_basic',
31 'channel_crash',
32 'channel_device',
33 'channel_ident',
34 'contact',
35 'description',
36 'device_url',
37 'enabled',
38 'interval',
39 'last_opt_revision',
40 'leaderboard',
41 'log_level',
42 'log_to_cluster',
43 'log_to_cluster_level',
44 'log_to_file',
45 'organization',
46 'proxy',
47 'url'
48 ];
49
50 configureTestBed(
51 {
52 declarations: [TelemetryComponent],
53 imports: [
54 HttpClientTestingModule,
55 ReactiveFormsModule,
56 RouterTestingModule,
57 SharedModule,
58 ToastrModule.forRoot()
59 ],
60 providers: i18nProviders
61 },
62 true
63 );
64
65 describe('configForm', () => {
66 beforeEach(() => {
67 fixture = TestBed.createComponent(TelemetryComponent);
68 component = fixture.componentInstance;
69 mgrModuleService = TestBed.get(MgrModuleService);
70 options = {};
71 configs = {};
72 optionsNames.forEach((name) => (options[name] = { name }));
73 optionsNames.forEach((name) => (configs[name] = true));
74 spyOn(mgrModuleService, 'getOptions').and.callFake(() => observableOf(options));
75 spyOn(mgrModuleService, 'getConfig').and.callFake(() => observableOf(configs));
76 fixture.detectChanges();
77 httpTesting = TestBed.get(HttpTestingController);
78 router = TestBed.get(Router);
79 spyOn(router, 'navigate');
80 });
81
82 it('should create', () => {
83 expect(component).toBeTruthy();
84 });
85
86 it('should set module enability to true correctly', () => {
87 expect(component.moduleEnabled).toBeTruthy();
88 });
89
90 it('should set module enability to false correctly', () => {
91 configs['enabled'] = false;
92 component.ngOnInit();
93 expect(component.moduleEnabled).toBeFalsy();
94 });
95
96 it('should filter options list correctly', () => {
97 _.forEach(Object.keys(component.options), (option) => {
98 expect(component.requiredFields).toContain(option);
99 });
100 });
101
102 it('should update the Telemetry configuration', () => {
103 component.updateConfig();
104 const req = httpTesting.expectOne('api/mgr/module/telemetry');
105 expect(req.request.method).toBe('PUT');
106 expect(req.request.body).toEqual({
107 config: {}
108 });
109 req.flush({});
110 });
111
112 it('should disable the Telemetry module', () => {
113 const message = 'Module disabled message.';
114 const followUpFunc = function () {
115 return 'followUp';
116 };
117 component.disableModule(message, followUpFunc);
118 const req = httpTesting.expectOne('api/telemetry');
119 expect(req.request.method).toBe('PUT');
120 expect(req.request.body).toEqual({
121 enable: false
122 });
123 req.flush({});
124 });
125
126 it('should disable the Telemetry module with default parameters', () => {
127 component.disableModule();
128 const req = httpTesting.expectOne('api/telemetry');
129 expect(req.request.method).toBe('PUT');
130 expect(req.request.body).toEqual({
131 enable: false
132 });
133 req.flush({});
134 expect(router.navigate).toHaveBeenCalledWith(['']);
135 });
136 });
137
138 describe('previewForm', () => {
139 const reportText = {
140 testA: 'testA',
141 testB: 'testB'
142 };
143
144 beforeEach(() => {
145 fixture = TestBed.createComponent(TelemetryComponent);
146 component = fixture.componentInstance;
147 fixture.detectChanges();
148 telemetryService = TestBed.get(TelemetryService);
149 httpTesting = TestBed.get(HttpTestingController);
150 router = TestBed.get(Router);
151 spyOn(router, 'navigate');
152 });
153
154 it('should create', () => {
155 expect(component).toBeTruthy();
156 });
157
158 it('should call TextToDownloadService download function', () => {
159 spyOn(telemetryService, 'getReport').and.returnValue(observableOf(reportText));
160 component.ngOnInit();
161
162 const downloadSpy = spyOn(TestBed.get(TextToDownloadService), 'download');
163 const filename = 'reportText.json';
164 component.download(reportText, filename);
165 expect(downloadSpy).toHaveBeenCalledWith(JSON.stringify(reportText, null, 2), filename);
166 });
167
168 it('should submit', () => {
169 component.onSubmit();
170 const req = httpTesting.expectOne('api/telemetry');
171 expect(req.request.method).toBe('PUT');
172 expect(req.request.body).toEqual({
173 enable: true,
174 license_name: 'sharing-1-0'
175 });
176 req.flush({});
177 expect(router.navigate).toHaveBeenCalledWith(['']);
178 });
179 });
180 });