]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/components/grafana/grafana.component.spec.ts
5052ef9ec6e822eac104b8b9c6f13077c0af1c78
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / components / grafana / grafana.component.spec.ts
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { FormsModule } from '@angular/forms';
4 import { RouterTestingModule } from '@angular/router/testing';
5
6 import { NgbAlertModule } from '@ng-bootstrap/ng-bootstrap';
7 import { of } from 'rxjs';
8
9 import { SettingsService } from '~/app/shared/api/settings.service';
10 import { CephReleaseNamePipe } from '~/app/shared/pipes/ceph-release-name.pipe';
11 import { SummaryService } from '~/app/shared/services/summary.service';
12 import { configureTestBed } from '~/testing/unit-test-helper';
13 import { AlertPanelComponent } from '../alert-panel/alert-panel.component';
14 import { DocComponent } from '../doc/doc.component';
15 import { LoadingPanelComponent } from '../loading-panel/loading-panel.component';
16 import { GrafanaComponent } from './grafana.component';
17
18 describe('GrafanaComponent', () => {
19 let component: GrafanaComponent;
20 let fixture: ComponentFixture<GrafanaComponent>;
21 const expected_url =
22 'http:localhost:3000/d/foo/somePath&refresh=2s&var-datasource=Dashboard1&kiosk&from=now-1h&to=now';
23 const expected_logs_url =
24 'http:localhost:3000/explore?orgId=1&left=["now-1h","now","Loki",{"refId":"A"}]&kiosk';
25
26 configureTestBed({
27 declarations: [GrafanaComponent, AlertPanelComponent, LoadingPanelComponent, DocComponent],
28 imports: [NgbAlertModule, HttpClientTestingModule, RouterTestingModule, FormsModule],
29 providers: [CephReleaseNamePipe, SettingsService, SummaryService]
30 });
31
32 beforeEach(() => {
33 fixture = TestBed.createComponent(GrafanaComponent);
34 component = fixture.componentInstance;
35 component.grafanaPath = 'somePath';
36 component.type = 'metrics';
37 component.uid = 'foo';
38 });
39
40 it('should create', () => {
41 expect(component).toBeTruthy();
42 });
43
44 it('should have found out that grafana does not exist', () => {
45 fixture.detectChanges();
46 expect(component.grafanaExist).toBe(false);
47 expect(component.baseUrl).toBe(undefined);
48 expect(component.loading).toBe(true);
49 expect(component.url).toBe(undefined);
50 expect(component.grafanaSrc).toEqual(undefined);
51 });
52
53 describe('with grafana initialized', () => {
54 beforeEach(() => {
55 TestBed.inject(SettingsService)['settings'] = { 'api/grafana/url': 'http:localhost:3000' };
56 component.type = 'metrics';
57 fixture.detectChanges();
58 });
59
60 it('should have found out that grafana exists and dashboard exists', () => {
61 expect(component.time).toBe('from=now-1h&to=now');
62 expect(component.grafanaExist).toBe(true);
63 expect(component.baseUrl).toBe('http:localhost:3000/d/');
64 expect(component.loading).toBe(false);
65 expect(component.url).toBe(expected_url);
66 expect(component.grafanaSrc).toEqual({
67 changingThisBreaksApplicationSecurity: expected_url
68 });
69 });
70
71 it('should reset the values', () => {
72 component.reset();
73 expect(component.time).toBe('from=now-1h&to=now');
74 expect(component.url).toBe(expected_url);
75 expect(component.grafanaSrc).toEqual({
76 changingThisBreaksApplicationSecurity: expected_url
77 });
78 });
79
80 it('should have Dashboard', () => {
81 TestBed.inject(SettingsService).validateGrafanaDashboardUrl = () => of({ uid: 200 });
82 expect(component.dashboardExist).toBe(true);
83 });
84 });
85
86 describe('with loki datasource', () => {
87 beforeEach(() => {
88 TestBed.inject(SettingsService)['settings'] = { 'api/grafana/url': 'http:localhost:3000' };
89 component.type = 'logs';
90 component.grafanaPath = 'explore?';
91 fixture.detectChanges();
92 });
93
94 it('should have found out that Loki Log Search exists', () => {
95 expect(component.grafanaExist).toBe(true);
96 expect(component.baseUrl).toBe('http:localhost:3000/d/');
97 expect(component.loading).toBe(false);
98 expect(component.url).toBe(expected_logs_url);
99 expect(component.grafanaSrc).toEqual({
100 changingThisBreaksApplicationSecurity: expected_logs_url
101 });
102 });
103 });
104 });