prometheus

Форк
0
/
TSDBStatus.test.tsx 
202 строки · 5.4 Кб
1
import * as React from 'react';
2
import { mount, ReactWrapper } from 'enzyme';
3
import { act } from 'react-dom/test-utils';
4
import { Table } from 'reactstrap';
5

6
import TSDBStatus from './TSDBStatus';
7
import { TSDBMap } from './TSDBStatus';
8
import { PathPrefixContext } from '../../contexts/PathPrefixContext';
9

10
const fakeTSDBStatusResponse: {
11
  status: string;
12
  data: TSDBMap;
13
} = {
14
  status: 'success',
15
  data: {
16
    headStats: {
17
      numSeries: 508,
18
      numLabelPairs: 1234,
19
      chunkCount: 937,
20
      minTime: 1591516800000,
21
      maxTime: 1598896800143,
22
    },
23
    labelValueCountByLabelName: [
24
      {
25
        name: '__name__',
26
        value: 5,
27
      },
28
    ],
29
    seriesCountByMetricName: [
30
      {
31
        name: 'scrape_duration_seconds',
32
        value: 1,
33
      },
34
      {
35
        name: 'scrape_samples_scraped',
36
        value: 1,
37
      },
38
    ],
39
    memoryInBytesByLabelName: [
40
      {
41
        name: '__name__',
42
        value: 103,
43
      },
44
    ],
45
    seriesCountByLabelValuePair: [
46
      {
47
        name: 'instance=localhost:9100',
48
        value: 5,
49
      },
50
    ],
51
  },
52
};
53

54
const fakeEmptyTSDBStatusResponse: {
55
  status: string;
56
  data: TSDBMap;
57
} = {
58
  status: 'success',
59
  data: {
60
    headStats: {
61
      numSeries: 0,
62
      numLabelPairs: 0,
63
      chunkCount: 0,
64
      minTime: 9223372036854776000,
65
      maxTime: -9223372036854776000,
66
    },
67
    labelValueCountByLabelName: [],
68
    seriesCountByMetricName: [],
69
    memoryInBytesByLabelName: [],
70
    seriesCountByLabelValuePair: [],
71
  },
72
};
73

74
const fakeInvalidTimestampTSDBStatusResponse: {
75
  status: string;
76
  data: TSDBMap;
77
} = {
78
  status: 'success',
79
  data: {
80
    headStats: {
81
      numSeries: 1,
82
      numLabelPairs: 0,
83
      chunkCount: 0,
84
      minTime: 9223372036854776000,
85
      maxTime: -9223372036854776000,
86
    },
87
    labelValueCountByLabelName: [],
88
    seriesCountByMetricName: [],
89
    memoryInBytesByLabelName: [],
90
    seriesCountByLabelValuePair: [],
91
  },
92
};
93

94
describe('TSDB Stats', () => {
95
  beforeEach(() => {
96
    fetchMock.resetMocks();
97
  });
98

99
  describe('Table Data Validation', () => {
100
    it('Table Test', async () => {
101
      const tables = [
102
        fakeTSDBStatusResponse.data.labelValueCountByLabelName,
103
        fakeTSDBStatusResponse.data.seriesCountByMetricName,
104
        fakeTSDBStatusResponse.data.memoryInBytesByLabelName,
105
        fakeTSDBStatusResponse.data.seriesCountByLabelValuePair,
106
      ];
107

108
      const mock = fetchMock.mockResponse(JSON.stringify(fakeTSDBStatusResponse));
109
      let page: any;
110
      await act(async () => {
111
        page = mount(
112
          <PathPrefixContext.Provider value="/path/prefix">
113
            <TSDBStatus />
114
          </PathPrefixContext.Provider>
115
        );
116
      });
117
      page.update();
118

119
      expect(mock).toHaveBeenCalledWith('/path/prefix/api/v1/status/tsdb', {
120
        cache: 'no-store',
121
        credentials: 'same-origin',
122
      });
123

124
      const 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) => {
127
          expect(headStats.at(i).text()).toEqual(value);
128
        }
129
      );
130

131
      for (let i = 0; i < tables.length; i++) {
132
        const data = tables[i];
133
        const table = page
134
          .find(Table)
135
          .at(i + 1)
136
          .find('tbody');
137
        const rows = table.find('tr');
138
        for (let i = 0; i < data.length; i++) {
139
          const firstRowColumns = rows
140
            .at(i)
141
            .find('td')
142
            .map((column: ReactWrapper) => column.text());
143
          expect(rows.length).toBe(data.length);
144
          expect(firstRowColumns[0]).toBe(data[i].name);
145
          expect(firstRowColumns[1]).toBe(data[i].value.toString());
146
        }
147
      }
148
    });
149

150
    it('No Data', async () => {
151
      const mock = fetchMock.mockResponse(JSON.stringify(fakeEmptyTSDBStatusResponse));
152
      let page: any;
153
      await act(async () => {
154
        page = mount(
155
          <PathPrefixContext.Provider value="/path/prefix">
156
            <TSDBStatus />
157
          </PathPrefixContext.Provider>
158
        );
159
      });
160
      page.update();
161

162
      expect(mock).toHaveBeenCalledWith('/path/prefix/api/v1/status/tsdb', {
163
        cache: 'no-store',
164
        credentials: 'same-origin',
165
      });
166

167
      expect(page.find('h2').text()).toEqual('TSDB Status');
168

169
      const headStats = page.find(Table).at(0).find('tbody').find('td');
170
      ['0', '0', '0', 'No datapoints yet', 'No datapoints yet'].forEach((value, i) => {
171
        expect(headStats.at(i).text()).toEqual(value);
172
      });
173
    });
174

175
    it('Invalid min/max Timestamp', async () => {
176
      const mock = fetchMock.mockResponse(JSON.stringify(fakeInvalidTimestampTSDBStatusResponse));
177
      let page: any;
178
      await act(async () => {
179
        page = mount(
180
          <PathPrefixContext.Provider value="/path/prefix">
181
            <TSDBStatus />
182
          </PathPrefixContext.Provider>
183
        );
184
      });
185
      page.update();
186

187
      expect(mock).toHaveBeenCalledWith('/path/prefix/api/v1/status/tsdb', {
188
        cache: 'no-store',
189
        credentials: 'same-origin',
190
      });
191

192
      expect(page.find('h2').text()).toEqual('TSDB Status');
193

194
      const 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) => {
197
          expect(headStats.at(i).text()).toEqual(value);
198
        }
199
      );
200
    });
201
  });
202
});
203

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.