prometheus
202 строки · 5.4 Кб
1import * as React from 'react';
2import { mount, ReactWrapper } from 'enzyme';
3import { act } from 'react-dom/test-utils';
4import { Table } from 'reactstrap';
5
6import TSDBStatus from './TSDBStatus';
7import { TSDBMap } from './TSDBStatus';
8import { PathPrefixContext } from '../../contexts/PathPrefixContext';
9
10const fakeTSDBStatusResponse: {
11status: string;
12data: TSDBMap;
13} = {
14status: 'success',
15data: {
16headStats: {
17numSeries: 508,
18numLabelPairs: 1234,
19chunkCount: 937,
20minTime: 1591516800000,
21maxTime: 1598896800143,
22},
23labelValueCountByLabelName: [
24{
25name: '__name__',
26value: 5,
27},
28],
29seriesCountByMetricName: [
30{
31name: 'scrape_duration_seconds',
32value: 1,
33},
34{
35name: 'scrape_samples_scraped',
36value: 1,
37},
38],
39memoryInBytesByLabelName: [
40{
41name: '__name__',
42value: 103,
43},
44],
45seriesCountByLabelValuePair: [
46{
47name: 'instance=localhost:9100',
48value: 5,
49},
50],
51},
52};
53
54const fakeEmptyTSDBStatusResponse: {
55status: string;
56data: TSDBMap;
57} = {
58status: 'success',
59data: {
60headStats: {
61numSeries: 0,
62numLabelPairs: 0,
63chunkCount: 0,
64minTime: 9223372036854776000,
65maxTime: -9223372036854776000,
66},
67labelValueCountByLabelName: [],
68seriesCountByMetricName: [],
69memoryInBytesByLabelName: [],
70seriesCountByLabelValuePair: [],
71},
72};
73
74const fakeInvalidTimestampTSDBStatusResponse: {
75status: string;
76data: TSDBMap;
77} = {
78status: 'success',
79data: {
80headStats: {
81numSeries: 1,
82numLabelPairs: 0,
83chunkCount: 0,
84minTime: 9223372036854776000,
85maxTime: -9223372036854776000,
86},
87labelValueCountByLabelName: [],
88seriesCountByMetricName: [],
89memoryInBytesByLabelName: [],
90seriesCountByLabelValuePair: [],
91},
92};
93
94describe('TSDB Stats', () => {
95beforeEach(() => {
96fetchMock.resetMocks();
97});
98
99describe('Table Data Validation', () => {
100it('Table Test', async () => {
101const tables = [
102fakeTSDBStatusResponse.data.labelValueCountByLabelName,
103fakeTSDBStatusResponse.data.seriesCountByMetricName,
104fakeTSDBStatusResponse.data.memoryInBytesByLabelName,
105fakeTSDBStatusResponse.data.seriesCountByLabelValuePair,
106];
107
108const mock = fetchMock.mockResponse(JSON.stringify(fakeTSDBStatusResponse));
109let page: any;
110await act(async () => {
111page = mount(
112<PathPrefixContext.Provider value="/path/prefix">
113<TSDBStatus />
114</PathPrefixContext.Provider>
115);
116});
117page.update();
118
119expect(mock).toHaveBeenCalledWith('/path/prefix/api/v1/status/tsdb', {
120cache: 'no-store',
121credentials: 'same-origin',
122});
123
124const headStats = page.find(Table).at(0).find('tbody').find('td');
125['508', '937', '1234', '2020-06-07T08:00:00.000Z (1591516800000)', '2020-08-31T18:00:00.143Z (1598896800143)'].forEach(
126(value, i) => {
127expect(headStats.at(i).text()).toEqual(value);
128}
129);
130
131for (let i = 0; i < tables.length; i++) {
132const data = tables[i];
133const table = page
134.find(Table)
135.at(i + 1)
136.find('tbody');
137const rows = table.find('tr');
138for (let i = 0; i < data.length; i++) {
139const firstRowColumns = rows
140.at(i)
141.find('td')
142.map((column: ReactWrapper) => column.text());
143expect(rows.length).toBe(data.length);
144expect(firstRowColumns[0]).toBe(data[i].name);
145expect(firstRowColumns[1]).toBe(data[i].value.toString());
146}
147}
148});
149
150it('No Data', async () => {
151const mock = fetchMock.mockResponse(JSON.stringify(fakeEmptyTSDBStatusResponse));
152let page: any;
153await act(async () => {
154page = mount(
155<PathPrefixContext.Provider value="/path/prefix">
156<TSDBStatus />
157</PathPrefixContext.Provider>
158);
159});
160page.update();
161
162expect(mock).toHaveBeenCalledWith('/path/prefix/api/v1/status/tsdb', {
163cache: 'no-store',
164credentials: 'same-origin',
165});
166
167expect(page.find('h2').text()).toEqual('TSDB Status');
168
169const headStats = page.find(Table).at(0).find('tbody').find('td');
170['0', '0', '0', 'No datapoints yet', 'No datapoints yet'].forEach((value, i) => {
171expect(headStats.at(i).text()).toEqual(value);
172});
173});
174
175it('Invalid min/max Timestamp', async () => {
176const mock = fetchMock.mockResponse(JSON.stringify(fakeInvalidTimestampTSDBStatusResponse));
177let page: any;
178await act(async () => {
179page = mount(
180<PathPrefixContext.Provider value="/path/prefix">
181<TSDBStatus />
182</PathPrefixContext.Provider>
183);
184});
185page.update();
186
187expect(mock).toHaveBeenCalledWith('/path/prefix/api/v1/status/tsdb', {
188cache: 'no-store',
189credentials: 'same-origin',
190});
191
192expect(page.find('h2').text()).toEqual('TSDB Status');
193
194const headStats = page.find(Table).at(0).find('tbody').find('td');
195['1', '0', '0', 'Error parsing time (9223372036854776000)', 'Error parsing time (-9223372036854776000)'].forEach(
196(value, i) => {
197expect(headStats.at(i).text()).toEqual(value);
198}
199);
200});
201});
202});
203