|
1
|
|
|
import json |
|
2
|
|
|
|
|
3
|
|
|
import mock |
|
4
|
|
|
import requests |
|
5
|
|
|
import responses |
|
6
|
|
|
import unittest2 |
|
7
|
|
|
|
|
8
|
|
|
# XXX: This file uses a lot of globals and shared state. |
|
9
|
|
|
# We should definitely refactor this at some |
|
10
|
|
|
# point since we have tests now. |
|
11
|
|
|
import st2_handler as sensu_handler |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
__all__ = [ |
|
15
|
|
|
'SensuHandlerTestCase' |
|
16
|
|
|
] |
|
17
|
|
|
|
|
18
|
|
|
class FakeResponse(object): |
|
19
|
|
|
|
|
20
|
|
|
def __init__(self, text, status_code, reason): |
|
21
|
|
|
self.text = text |
|
22
|
|
|
self.status_code = status_code |
|
23
|
|
|
self.reason = reason |
|
24
|
|
|
|
|
25
|
|
|
def json(self): |
|
26
|
|
|
return json.loads(self.text) |
|
27
|
|
|
|
|
28
|
|
|
def raise_for_status(self): |
|
29
|
|
|
raise Exception(self.reason) |
|
30
|
|
|
|
|
31
|
|
|
class SensuHandlerTestCase(unittest2.TestCase): |
|
32
|
|
|
|
|
33
|
|
|
def test_st2_headers_token_auth(self): |
|
34
|
|
|
sensu_handler.IS_API_KEY_AUTH = False |
|
35
|
|
|
self.assertEqual(sensu_handler.IS_API_KEY_AUTH, False, |
|
36
|
|
|
'API auth should be off for this test.') |
|
37
|
|
|
sensu_handler.ST2_AUTH_TOKEN = 'dummy-token' |
|
38
|
|
|
sensu_handler.ST2_API_KEY = 'dummy-api-key' |
|
39
|
|
|
headers = sensu_handler._get_st2_request_headers() |
|
40
|
|
|
self.assertTrue('X-Auth-Token' in headers) |
|
41
|
|
|
self.assertTrue('St2-Api-Key' not in headers) |
|
42
|
|
|
self.assertEqual(headers['X-Auth-Token'], 'dummy-token') |
|
43
|
|
|
|
|
44
|
|
|
def test_st2_headers_apikey_auth(self): |
|
45
|
|
|
sensu_handler.IS_API_KEY_AUTH = True |
|
46
|
|
|
self.assertEqual(sensu_handler.IS_API_KEY_AUTH, True, |
|
47
|
|
|
'API auth should be on for this test.') |
|
48
|
|
|
sensu_handler.ST2_AUTH_TOKEN = 'dummy-token' |
|
49
|
|
|
sensu_handler.ST2_API_KEY = 'dummy-api-key' |
|
50
|
|
|
headers = sensu_handler._get_st2_request_headers() |
|
51
|
|
|
self.assertTrue('X-Auth-Token' not in headers) |
|
52
|
|
|
self.assertTrue('St2-Api-Key' in headers) |
|
53
|
|
|
self.assertEqual(headers['St2-Api-Key'], 'dummy-api-key') |
|
54
|
|
|
|
|
55
|
|
|
def test_get_st2_auth_url(self): |
|
56
|
|
|
sensu_handler.ST2_AUTH_BASE_URL = 'https://localhost/auth/v1/' |
|
57
|
|
|
self.assertEqual(sensu_handler._get_auth_url(), |
|
58
|
|
|
'https://localhost/auth/v1/tokens') |
|
59
|
|
|
|
|
60
|
|
|
def test_get_st2_triggers_base_url(self): |
|
61
|
|
|
sensu_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/' |
|
62
|
|
|
self.assertEqual(sensu_handler._get_st2_triggers_base_url(), |
|
63
|
|
|
'https://localhost/api/v1/triggertypes') |
|
64
|
|
|
|
|
65
|
|
|
def test_get_st2_triggers_url(self): |
|
66
|
|
|
sensu_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/' |
|
67
|
|
|
self.assertEqual(sensu_handler._get_st2_triggers_url(), |
|
68
|
|
|
'https://localhost/api/v1/triggertypes/sensu.event_handler') |
|
69
|
|
|
|
|
70
|
|
|
def test_get_st2_webhooks_url(self): |
|
71
|
|
|
sensu_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/' |
|
72
|
|
|
self.assertEqual(sensu_handler._get_st2_webhooks_url(), |
|
73
|
|
|
'https://localhost/api/v1/webhooks/st2') |
|
74
|
|
|
|
|
75
|
|
|
@responses.activate |
|
76
|
|
|
def test_post_event_to_st2_bad_payload(self): |
|
77
|
|
|
sensu_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/' |
|
78
|
|
|
responses.add( |
|
79
|
|
|
responses.POST, 'https://localhost/api/v1/webhooks/st2', |
|
80
|
|
|
json={}, status=202 |
|
81
|
|
|
) |
|
82
|
|
|
payload = {'foo': 'bar'} |
|
83
|
|
|
with self.assertRaises(SystemExit) as cm: |
|
84
|
|
|
sensu_handler._post_event_to_st2(payload) |
|
85
|
|
|
self.assertTrue(cm.exception.code > 0) |
|
86
|
|
|
|
|
87
|
|
|
@responses.activate |
|
88
|
|
|
def test_get_auth_token(self): |
|
89
|
|
|
sensu_handler.ST2_AUTH_BASE_URL = 'https://localhost/auth/v1/' |
|
90
|
|
|
responses.add( |
|
91
|
|
|
responses.POST, 'https://localhost/auth/v1/tokens', |
|
92
|
|
|
json={'token': 'your_auth_token'}, status=202 |
|
93
|
|
|
) |
|
94
|
|
|
token = sensu_handler._get_auth_token() |
|
95
|
|
|
self.assertEqual(token, 'your_auth_token') |
|
96
|
|
|
|
|
97
|
|
|
@mock.patch('st2_handler._create_trigger_type') |
|
98
|
|
|
@responses.activate |
|
99
|
|
|
def test_get_trigger_type_trigger_exists(self, mock_method): |
|
100
|
|
|
sensu_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/' |
|
101
|
|
|
responses.add( |
|
102
|
|
|
responses.GET, 'https://localhost/api/v1/triggertypes/sensu.event_handler', |
|
103
|
|
|
json={'type': 'sensu.event_handler'}, status=200 |
|
104
|
|
|
) |
|
105
|
|
|
sensu_handler._register_trigger_with_st2() |
|
106
|
|
|
self.assertFalse(mock_method.called) |
|
107
|
|
|
|
|
108
|
|
|
@mock.patch('st2_handler._create_trigger_type') |
|
109
|
|
|
@responses.activate |
|
110
|
|
|
def test_trigger_creation_trigger_not_exists(self, mock_method): |
|
111
|
|
|
sensu_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/' |
|
112
|
|
|
responses.add( |
|
113
|
|
|
responses.GET, 'https://localhost/api/v1/triggertypes/sensu.event_handler', |
|
114
|
|
|
json={}, status=404 |
|
115
|
|
|
) |
|
116
|
|
|
sensu_handler._register_trigger_with_st2() |
|
117
|
|
|
self.assertTrue(mock_method.called) |
|
118
|
|
|
|
|
119
|
|
|
@mock.patch.object(requests, 'post', mock.MagicMock( |
|
120
|
|
|
return_value=FakeResponse(json.dumps({}), status_code=200, reason='blah'))) |
|
121
|
|
|
def test_create_trigger_type(self): |
|
122
|
|
|
sensu_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/' |
|
123
|
|
|
sensu_handler._create_trigger_type() |
|
124
|
|
|
requests.post.assert_called_once_with('https://localhost/api/v1/triggertypes', |
|
125
|
|
|
data='{"description": "Trigger type for sensu event handler.", ' + |
|
126
|
|
|
'"name": "event_handler", "pack": "sensu"}', |
|
127
|
|
|
headers={'Content-Type': 'application/json; charset=utf-8'}, verify=False) |
|
128
|
|
|
|
|
129
|
|
|
@mock.patch.object(requests, 'post', mock.MagicMock( |
|
130
|
|
|
return_value=FakeResponse(json.dumps({}), status_code=200, reason='blah'))) |
|
131
|
|
|
def test_ssl_verify_on(self): |
|
132
|
|
|
sensu_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/' |
|
133
|
|
|
sensu_handler.ST2_SSL_VERIFY = True |
|
134
|
|
|
sensu_handler._create_trigger_type() |
|
135
|
|
|
requests.post.assert_called_with('https://localhost/api/v1/triggertypes', |
|
136
|
|
|
data='{"description": "Trigger type for sensu event handler.", "name": "event_handler", "pack": "sensu"}', |
|
137
|
|
|
headers={'Content-Type': 'application/json; charset=utf-8'}, verify=True) |
|
138
|
|
|
|
|
139
|
|
|
@responses.activate |
|
140
|
|
|
def test_post_event_to_st2_bad_payload(self): |
|
141
|
|
|
sensu_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/' |
|
142
|
|
|
responses.add( |
|
143
|
|
|
responses.POST, 'https://localhost/api/v1/webhooks/st2', |
|
144
|
|
|
json={}, status=202 |
|
145
|
|
|
) |
|
146
|
|
|
responses.add( |
|
147
|
|
|
responses.GET, 'http://localhost:4567/stashes/silence/ma_client', |
|
148
|
|
|
json={}, status=404 |
|
149
|
|
|
) |
|
150
|
|
|
responses.add( |
|
151
|
|
|
responses.GET, 'http://localhost:4567/stashes/silence/ma_client/ma_check', |
|
152
|
|
|
json={}, status=404 |
|
153
|
|
|
) |
|
154
|
|
|
responses.add( |
|
155
|
|
|
responses.GET, 'http://localhost:4567/stashes/silence/all/ma_check', |
|
156
|
|
|
json={}, status=404 |
|
157
|
|
|
) |
|
158
|
|
|
trigger_payload = { |
|
159
|
|
|
'client': {'name': 'ma_client'}, |
|
160
|
|
|
'check': {'name': 'ma_check'}, |
|
161
|
|
|
'id': 'foo-check-id' |
|
162
|
|
|
} |
|
163
|
|
|
ret = sensu_handler._post_event_to_st2(json.dumps(trigger_payload)) |
|
164
|
|
|
self.assertEqual(ret, True) |
|
165
|
|
|
|
|
166
|
|
|
@responses.activate |
|
167
|
|
|
def test_post_event_to_st2_sensu_stashed(self): |
|
168
|
|
|
sensu_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/' |
|
169
|
|
|
responses.add( |
|
170
|
|
|
responses.POST, 'https://localhost/api/v1/webhooks/st2', |
|
171
|
|
|
json={}, status=202 |
|
172
|
|
|
) |
|
173
|
|
|
responses.add( |
|
174
|
|
|
responses.GET, 'http://localhost:4567/stashes/silence/ma_client', |
|
175
|
|
|
json={}, status=200 |
|
176
|
|
|
) |
|
177
|
|
|
responses.add( |
|
178
|
|
|
responses.GET, 'http://localhost:4567/stashes/silence/ma_client/ma_check', |
|
179
|
|
|
json={}, status=200 |
|
180
|
|
|
) |
|
181
|
|
|
responses.add( |
|
182
|
|
|
responses.GET, 'http://localhost:4567/stashes/silence/all/ma_check', |
|
183
|
|
|
json={}, status=200 |
|
184
|
|
|
) |
|
185
|
|
|
trigger_payload = { |
|
186
|
|
|
'client': {'name': 'ma_client'}, |
|
187
|
|
|
'check': {'name': 'ma_check'}, |
|
188
|
|
|
'id': 'foo-check-id' |
|
189
|
|
|
} |
|
190
|
|
|
with self.assertRaises(SystemExit) as cm: |
|
191
|
|
|
sensu_handler._post_event_to_st2(json.dumps(trigger_payload)) |
|
192
|
|
|
self.assertEqual(cm.exception.code, 0) |
|
193
|
|
|
|
|
194
|
|
|
|