@centreon/e2e-config 1.0.0 → 26.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,227 @@
1
+ /* eslint-disable @typescript-eslint/no-namespace */
2
+
3
+ const apiBase = '/centreon/api';
4
+ const apiActionV1 = `${apiBase}/index.php`;
5
+
6
+ const getStatusNumberFromString = (status: string): number => {
7
+ const statuses = {
8
+ critical: '2',
9
+ down: '1',
10
+ ok: '0',
11
+ unknown: '3',
12
+ unreachable: '2',
13
+ up: '0',
14
+ warning: '1'
15
+ };
16
+
17
+ if (status in statuses) {
18
+ return statuses[status];
19
+ }
20
+
21
+ throw new Error(`Status ${status} does not exist`);
22
+ };
23
+
24
+ interface HostCheck {
25
+ host: string;
26
+ isForced?: boolean;
27
+ }
28
+
29
+ Cypress.Commands.add(
30
+ 'scheduleHostCheck',
31
+ ({
32
+ host,
33
+ isForced = true
34
+ }: ServiceCheck): Cypress.Chainable => {
35
+ return cy
36
+ .requestOnDatabase({
37
+ database: 'centreon_storage',
38
+ query: 'SELECT id FROM resources WHERE name = ? AND type = 1',
39
+ params: [host]
40
+ })
41
+ .then(([rows]) => {
42
+ if (rows.length === 0) {
43
+ throw new Error(`Cannot find host ${host}`);
44
+ }
45
+
46
+ const hostId = rows[0].id;
47
+
48
+ return cy.request({
49
+ body: {
50
+ check: {
51
+ is_forced: isForced
52
+ },
53
+ resources: [
54
+ {
55
+ id: hostId,
56
+ parent: null,
57
+ type: 'host'
58
+ }
59
+ ]
60
+ },
61
+ method: 'POST',
62
+ timeout: 30000,
63
+ url: '/centreon/api/latest/monitoring/resources/check'
64
+ }).then((response) => {
65
+ expect(response.status).to.eq(204);
66
+
67
+ return cy.wrap(null);
68
+ });
69
+ });
70
+ }
71
+ );
72
+
73
+ interface ServiceCheck {
74
+ host: string;
75
+ isForced?: boolean;
76
+ service: string;
77
+ }
78
+
79
+ Cypress.Commands.add(
80
+ 'scheduleServiceCheck',
81
+ ({
82
+ host,
83
+ isForced = true,
84
+ service
85
+ }: ServiceCheck): Cypress.Chainable => {
86
+ return cy
87
+ .requestOnDatabase({
88
+ database: 'centreon_storage',
89
+ query: 'SELECT parent_id, id FROM resources WHERE parent_name = ? AND name = ?',
90
+ params: [host, service]
91
+ })
92
+ .then(([rows]) => {
93
+ if (rows.length === 0) {
94
+ throw new Error(`Cannot find service ${host} / ${service}`);
95
+ }
96
+
97
+ const hostId = rows[0].parent_id;
98
+ const serviceId = rows[0].id;
99
+
100
+ return cy.request({
101
+ body: {
102
+ check: {
103
+ is_forced: isForced
104
+ },
105
+ resources: [
106
+ {
107
+ id: serviceId,
108
+ parent: {
109
+ id: hostId
110
+ },
111
+ type: 'service'
112
+ }
113
+ ]
114
+ },
115
+ method: 'POST',
116
+ timeout: 30000,
117
+ url: '/centreon/api/latest/monitoring/resources/check'
118
+ }).then((response) => {
119
+ expect(response.status).to.eq(204);
120
+
121
+ return cy.wrap(null);
122
+ });
123
+ });
124
+ }
125
+ );
126
+
127
+
128
+ interface SubmitResult {
129
+ host: string;
130
+ output: string;
131
+ perfdata?: string | null;
132
+ service?: string | null;
133
+ status: string;
134
+ }
135
+
136
+ Cypress.Commands.add(
137
+ 'submitResults',
138
+ (results: Array<SubmitResult>): Cypress.Chainable => {
139
+ results.forEach(
140
+ ({ host, output, perfdata = '', service = null, status }) => {
141
+ const timestampNow = Math.floor(Date.now() / 1000) - 15;
142
+ const updatetime = timestampNow.toString();
143
+
144
+ const result = {
145
+ host,
146
+ output,
147
+ perfdata,
148
+ service,
149
+ status: getStatusNumberFromString(status),
150
+ updatetime
151
+ };
152
+
153
+ cy.ensureUserTokenApiV1().then(() =>
154
+ cy.request({
155
+ body: {
156
+ results: [result]
157
+ },
158
+ headers: {
159
+ 'Content-Type': 'application/json',
160
+ 'centreon-auth-token':
161
+ window.localStorage.getItem('userTokenApiV1')
162
+ },
163
+ method: 'POST',
164
+ url: `${apiActionV1}?action=submit&object=centreon_submit_results`
165
+ })
166
+ );
167
+ }
168
+ );
169
+
170
+ return cy.wrap(null);
171
+ }
172
+ );
173
+
174
+ interface Downtime {
175
+ host: string;
176
+ service?: string | null;
177
+ }
178
+
179
+ Cypress.Commands.add(
180
+ 'waitForDowntime',
181
+ (downtime: Downtime): Cypress.Chainable => {
182
+ cy.log('Checking hosts in database');
183
+
184
+ let query = `SELECT COUNT(d.downtime_id) AS count_downtimes FROM downtimes as d
185
+ INNER JOIN hosts as h ON h.host_id = d.host_id AND h.name = ?`;
186
+ const params: Array<string> = [downtime.host];
187
+ if (downtime.service) {
188
+ query += ` INNER JOIN services as s ON s.service_id = d.service_id AND s.description = ?`;
189
+ params.push(downtime.service);
190
+ }
191
+ query += ` WHERE d.started=1`;
192
+ if (!downtime.service) {
193
+ query += ` AND d.service_id = 0`;
194
+ }
195
+
196
+ cy.waitUntil(() => {
197
+ return cy
198
+ .requestOnDatabase({
199
+ database: 'centreon_storage',
200
+ query,
201
+ params
202
+ })
203
+ .then(([rows]) => {
204
+ const foundDowntimesCount = rows.length ? rows[0].count_downtimes : 0;
205
+
206
+ cy.log('Downtime count in database', foundDowntimesCount);
207
+
208
+ return cy.wrap(foundDowntimesCount > 0);
209
+ });
210
+ });
211
+
212
+ return cy.wrap(null);
213
+ }
214
+ );
215
+
216
+ declare global {
217
+ namespace Cypress {
218
+ interface Chainable {
219
+ scheduleHostCheck: (hostCheck) => Cypress.Chainable;
220
+ scheduleServiceCheck: (serviceCheck) => Cypress.Chainable;
221
+ submitResults: (props: Array<SubmitResult>) => Cypress.Chainable;
222
+ waitForDowntime: (downtime: Downtime) => Cypress.Chainable;
223
+ }
224
+ }
225
+ }
226
+
227
+ export {};