forked from hexionas/grafana-annotation-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
113 lines (100 loc) · 4.46 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const {run} = require('./index');
import axios from "axios";
// Mock axios and reset after each test
jest.mock("axios");
afterEach(() => {
jest.clearAllMocks();
});
describe('Grafana Annotation Action Tests', () => {
// ************************ Input Validation Tests ****************************************
// ****************************************************************************************
test('Input Validation: missing all required params', () => {
run();
expect(axios.post).toBeCalledTimes(0);
expect(axios.patch).toBeCalledTimes(0);
});
test('Input Validation: supplied grafanaHost', () => {
process.env.INPUT_GRAFANAHOST = 'something';
run();
expect(axios.post).toBeCalledTimes(0);
expect(axios.patch).toBeCalledTimes(0);
});
test('Input Validation: supplied grafanaToken', () => {
process.env.INPUT_GRAFANAHOST = 'something';
process.env.INPUT_GRAFANATOKEN = 'something';
run();
expect(axios.post).toBeCalledTimes(0);
expect(axios.patch).toBeCalledTimes(0);
});
test('Input Validation: supplied grafanaText, satisfied minimum required', () => {
process.env.INPUT_GRAFANAHOST = 'something';
process.env.INPUT_GRAFANATOKEN = 'something';
process.env.INPUT_GRAFANATEXT = 'something';
axios.post.mockImplementation(() => Promise.resolve({ status: 200, data: { id: 1} }));
run();
expect(axios.post).toBeCalledTimes(1);
expect(axios.post).toBeCalledWith(expect.anything(), {
tags: [],
text: 'something',
}, expect.anything());
expect(axios.patch).toBeCalledTimes(0);
});
// ************************ Create Annotation Tests ***************************************
// ****************************************************************************************
test('Create Annotation: global annotation with tags', () => {
process.env.INPUT_GRAFANATAGS = 'tag:something\ntag2:something'
axios.post.mockImplementation(() => Promise.resolve({ status: 200, data: { id: 1} }));
run();
expect(axios.post).toBeCalledTimes(1);
expect(axios.post).toBeCalledWith(expect.anything(), {
tags: [
'tag:something',
'tag2:something',
],
text: 'something',
}, expect.anything());
expect(axios.put).toBeCalledTimes(0);
});
test('Create Annotation: new annotation for dashboard but no panel specified', () => {
process.env.INPUT_GRAFANADASHBOARDID = '12345';
axios.post.mockImplementation(() => Promise.resolve({ status: 200, data: { id: 1} }));
run();
expect(axios.post).toBeCalledTimes(0);
expect(axios.put).toBeCalledTimes(0);
});
test('Create Annotation: new annotation for panel but no dashboard specified', () => {
process.env.INPUT_GRAFANAPANELID = '12345';
process.env.INPUT_GRAFANADASHBOARDID = undefined;
axios.post.mockImplementation(() => Promise.resolve({ status: 200, data: { id: 1} }));
run();
expect(axios.post).toBeCalledTimes(0);
expect(axios.put).toBeCalledTimes(0);
});
test('Create Annotation: new annotation for panel and dashboard', () => {
process.env.INPUT_GRAFANAPANELID = '12345';
process.env.INPUT_GRAFANADASHBOARDID = '12345';
axios.post.mockImplementation(() => Promise.resolve({ status: 200, data: { id: 1} }));
run();
expect(axios.post).toBeCalledTimes(1);
expect(axios.post).toBeCalledWith(expect.anything(), {
tags: [
'tag:something',
'tag2:something',
],
dashboardId: 12345,
panelId: 12345,
text: 'something',
}, expect.anything());
expect(axios.put).toBeCalledTimes(0);
});
// ************************ Annotation Update Tests ***************************************
// ****************************************************************************************
test('Update Annotation: update', () => {
process.env.INPUT_GRAFANAANNOTATIONID = "12345";
axios.patch.mockImplementation(() => Promise.resolve({ status: 200}));
run();
expect(axios.post).toBeCalledTimes(0);
expect(axios.patch).toBeCalledTimes(1);
expect(axios.patch).toBeCalledWith(expect.anything(), { timeEnd: expect.any(Number) }, expect.anything());
});
});