This repository was archived by the owner on Sep 1, 2023. It is now read-only.
forked from DataDog/integrations-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_openstack.py
More file actions
366 lines (304 loc) · 13.7 KB
/
Copy pathtest_openstack.py
File metadata and controls
366 lines (304 loc) · 13.7 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# (C) Datadog, Inc. 2010-2016
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
# stdlib
from nose.plugins.attrib import attr
from time import sleep
from unittest import TestCase
from mock import patch
import re
# 3p
# project
from tests.checks.common import AgentCheckTest, load_class
from checks import AgentCheck
OS_CHECK_NAME = 'openstack'
OpenStackProjectScope = load_class(OS_CHECK_NAME, "OpenStackProjectScope")
KeystoneCatalog = load_class(OS_CHECK_NAME, "KeystoneCatalog")
IncompleteConfig = load_class(OS_CHECK_NAME, "IncompleteConfig")
IncompleteAuthScope = load_class(OS_CHECK_NAME, "IncompleteAuthScope")
IncompleteIdentity = load_class(OS_CHECK_NAME, "IncompleteIdentity")
class MockHTTPResponse(object):
def __init__(self, response_dict, headers):
self.response_dict = response_dict
self.headers = headers
def json(self):
return self.response_dict
EXAMPLE_AUTH_RESPONSE = {
u'token': {
u'methods': [
u'password'
],
u'roles': [
{
u'id': u'f20c215f5a4d47b7a6e510bc65485ced',
u'name': u'datadog_monitoring'
},
{
u'id': u'9fe2ff9ee4384b1894a90878d3e92bab',
u'name': u'_member_'
}
],
u'expires_at': u'2015-11-02T15: 57: 43.911674Z',
u'project': {
u'domain': {
u'id': u'default',
u'name': u'Default'
},
u'id': u'0850707581fe4d738221a72db0182876',
u'name': u'admin'
},
u'catalog': [
{
u'endpoints': [
{
u'url': u'http://10.0.2.15:8773/',
u'interface': u'public',
u'region': u'RegionOne',
u'region_id': u'RegionOne',
u'id': u'541baeb9ab7542609d7ae307a7a9d5f0'
},
{
u'url': u'http: //10.0.2.15:8773/',
u'interface': u'admin',
u'region': u'RegionOne',
u'region_id': u'RegionOne',
u'id': u'5c648acaea9941659a5dc04fb3b18e49'
},
{
u'url': u'http: //10.0.2.15:8773/',
u'interface': u'internal',
u'region': u'RegionOne',
u'region_id': u'RegionOne',
u'id': u'cb70e610620542a1804522d365226981'
}
],
u'type': u'compute',
u'id': u'1398dc02f9b7474eb165106485033b48',
u'name': u'nova'
},
{
u'endpoints': [
{
u'url': u'http://10.0.2.15:8774/v2.1/0850707581fe4d738221a72db0182876',
u'interface': u'internal',
u'region': u'RegionOne',
u'region_id': u'RegionOne',
u'id': u'354e35ed19774e398f80dc2a90d07f4b'
},
{
u'url': u'http://10.0.2.15:8774/v2.1/0850707581fe4d738221a72db0182876',
u'interface': u'public',
u'region': u'RegionOne',
u'region_id': u'RegionOne',
u'id': u'36e8e2bf24384105b9d56a65b0900172'
},
{
u'url': u'http://10.0.2.15:8774/v2.1/0850707581fe4d738221a72db0182876',
u'interface': u'admin',
u'region': u'RegionOne',
u'region_id': u'RegionOne',
u'id': u'de93edcbf7f9446286687ec68423c36f'
}
],
u'type': u'computev21',
u'id': u'2023bd4f451849ba8abeaaf283cdde4f',
u'name': u'novav21'
},
{
u'endpoints': [
{
u'url': u'http://10.0.2.15:9292',
u'interface': u'internal',
u'region': u'RegionOne',
u'region_id': u'RegionOne',
u'id': u'7c1e318d8f7f42029fcb591598df2ef5'
},
{
u'url': u'http://10.0.2.15:9292',
u'interface': u'public',
u'region': u'RegionOne',
u'region_id': u'RegionOne',
u'id': u'afcc88b1572f48a38bb393305dc2b584'
},
{
u'url': u'http://10.0.2.15:9292',
u'interface': u'admin',
u'region': u'RegionOne',
u'region_id': u'RegionOne',
u'id': u'd9730dbdc07844d785913219da64a197'
}
],
u'type': u'network',
u'id': u'21ad241f26194bccb7d2e49ee033d5a2',
u'name': u'neutron'
},
],
u'extras': {
},
u'user': {
u'domain': {
u'id': u'default',
u'name': u'Default'
},
u'id': u'5f10e63fbd6b411186e561dc62a9a675',
u'name': u'datadog'
},
u'audit_ids': [
u'OMQQg9g3QmmxRHwKrfWxyQ'
],
u'issued_at': u'2015-11-02T14: 57: 43.911697Z'
}
}
MOCK_HTTP_RESPONSE = MockHTTPResponse(response_dict=EXAMPLE_AUTH_RESPONSE, headers={"X-Subject-Token": "fake_token"})
@attr(requires='openstack')
class OSProjectScopeTest(TestCase):
BAD_AUTH_SCOPES = [
{"auth_scope": {}},
{"auth_scope": {"project": {}}},
{"auth_scope": {"project": {"id": ""}}},
{"auth_scope": {"project": {"name": "test"}}},
{"auth_scope": {"project": {"name": "test", "domain": {}}}},
{"auth_scope": {"project": {"name": "test", "domain": {"id": ""}}}},
]
GOOD_AUTH_SCOPES = [
{"auth_scope": {"project": {"id": "test_project_id"}}},
{"auth_scope": {"project": {"name": "test", "domain": {"id": "test_id"}}}},
]
BAD_USERS = [
{"user": {}},
{"user": {"name": ""}},
{"user": {"name": "test_name", "password": ""}},
{"user": {"name": "test_name", "password": "test_pass", "domain": {}}},
{"user": {"name": "test_name", "password": "test_pass", "domain": {"id": ""}}},
]
GOOD_USERS = [
{"user": {"name": "test_name", "password": "test_pass", "domain": {"id": "test_id"}}},
]
def _test_bad_auth_scope(self, scope):
self.assertRaises(IncompleteAuthScope, OpenStackProjectScope.get_auth_scope, scope)
def test_get_auth_scope(self):
for scope in self.BAD_AUTH_SCOPES:
self._test_bad_auth_scope(scope)
for scope in self.GOOD_AUTH_SCOPES:
auth_scope = OpenStackProjectScope.get_auth_scope(scope)
# Should pass through unchanged
self.assertEqual(auth_scope, scope.get("auth_scope"))
def _test_bad_user(self, user):
self.assertRaises(IncompleteIdentity, OpenStackProjectScope.get_user_identity, user)
def test_get_user_identity(self):
for user in self.BAD_USERS:
self._test_bad_user(user)
for user in self.GOOD_USERS:
parsed_user = OpenStackProjectScope.get_user_identity(user)
self.assertEqual(parsed_user, {"methods": ["password"], "password": user})
def test_from_config(self):
init_config = {"keystone_server_url": "http://10.0.2.15:5000", "nova_api_version": "v2"}
bad_instance_config = {}
good_instance_config = {"user": self.GOOD_USERS[0]["user"], "auth_scope": self.GOOD_AUTH_SCOPES[0]["auth_scope"]}
self.assertRaises(IncompleteConfig, OpenStackProjectScope.from_config, init_config, bad_instance_config)
with patch("_openstack.OpenStackProjectScope.request_auth_token", return_value=MOCK_HTTP_RESPONSE):
append_config = good_instance_config.copy()
append_config["append_tenant_id"] = True
scope = OpenStackProjectScope.from_config(init_config, append_config)
self.assertTrue(isinstance(scope, OpenStackProjectScope))
self.assertEqual(scope.auth_token, "fake_token")
self.assertEqual(scope.tenant_id, "test_project_id")
# Test that append flag worked
self.assertEqual(scope.service_catalog.nova_endpoint, "http://10.0.2.15:8773/test_project_id")
@attr(requires='openstack')
class KeyStoneCatalogTest(TestCase):
def test_get_nova_endpoint(self):
self.assertEqual(KeystoneCatalog.get_nova_endpoint(EXAMPLE_AUTH_RESPONSE), u"http://10.0.2.15:8774/v2.1/0850707581fe4d738221a72db0182876")
self.assertEqual(KeystoneCatalog.get_nova_endpoint(EXAMPLE_AUTH_RESPONSE, nova_api_version="v2"), u"http://10.0.2.15:8773/")
def test_get_neutron_endpoint(self):
self.assertEqual(KeystoneCatalog.get_neutron_endpoint(EXAMPLE_AUTH_RESPONSE), u"http://10.0.2.15:9292")
def test_from_auth_response(self):
catalog = KeystoneCatalog.from_auth_response(EXAMPLE_AUTH_RESPONSE, "v2.1")
self.assertTrue(isinstance(catalog, KeystoneCatalog))
self.assertEqual(catalog.neutron_endpoint, u"http://10.0.2.15:9292")
self.assertEqual(catalog.nova_endpoint, u"http://10.0.2.15:8774/v2.1/0850707581fe4d738221a72db0182876")
@attr(requires='openstack')
class TestOpenstack(AgentCheckTest):
"""Test for openstack integration."""
CHECK_NAME = OS_CHECK_NAME
# Samples
# .. server/network
ALL_IDS = ["server-1", "server-2", "other-1", "other-2"]
EXCLUDED_NETWORK_IDS = ["server-1", "other-.*"]
EXCLUDED_SERVER_IDS = ["server-2", "other-.*"]
FILTERED_NETWORK_ID = "server-2"
FILTERED_SERVER_ID = "server-1"
# .. config
MOCK_CONFIG = {
"init_config": {
"keystone_server_url": "http://10.0.2.15:5000",
"ssl_verify": False,
"exclude_network_ids": EXCLUDED_NETWORK_IDS,
},
"instances": [
{
"name": "test_name", "user": {"name": "test_name", "password": "test_pass", "domain": {"id": "test_id"}},
"auth_scope": {"project": {"id": "test_project_id"}},
}
]
}
def setUp(self):
self.load_check(self.MOCK_CONFIG, self.DEFAULT_AGENT_CONFIG)
def test_ensure_auth_scope(self):
instance = self.MOCK_CONFIG["instances"][0]
self.assertRaises(KeyError, self.check.get_scope_for_instance, instance)
with patch("_openstack.OpenStackProjectScope.request_auth_token", return_value=MOCK_HTTP_RESPONSE):
scope = self.check.ensure_auth_scope(instance)
self.assertEqual(self.check.get_scope_for_instance(instance), scope)
self.check._send_api_service_checks(scope)
self.service_checks = self.check.get_service_checks()
# Expect OK, since we've mocked an API response
self.assertServiceCheck(self.check.IDENTITY_API_SC, status=AgentCheck.OK, count=1)
# Expect CRITICAL since URLs are non-existent
self.assertServiceCheck(self.check.COMPUTE_API_SC, status=AgentCheck.CRITICAL, count=1)
self.assertServiceCheck(self.check.NETWORK_API_SC, status=AgentCheck.CRITICAL, count=1)
self.check._current_scope = scope
self.check.delete_current_scope()
self.assertRaises(KeyError, self.check.get_scope_for_instance, instance)
def test_parse_uptime_string(self):
uptime_parsed = self.check._parse_uptime_string(u' 16:53:48 up 1 day, 21:34, 3 users, load average: 0.04, 0.14, 0.19\n')
self.assertEqual(uptime_parsed.get('loads'), [0.04, 0.14, 0.19])
def test_cache_utils(self):
self.check.CACHE_TTL["aggregates"] = 1
expected_aggregates = {"hyp_1": ["aggregate:staging", "availability_zone:test"]}
with patch("_openstack.OpenStackCheck.get_all_aggregate_hypervisors", return_value=expected_aggregates):
self.assertEqual(self.check._get_and_set_aggregate_list(), expected_aggregates)
sleep(1.5)
self.assertTrue(self.check._is_expired("aggregates"))
@patch("_openstack.OpenStackCheck.get_all_server_ids", return_value=ALL_IDS)
def test_server_exclusion(self, *args):
"""
Exclude networks using regular expressions.
"""
self.check.exclude_server_id_rules = set([re.compile(rule) for rule in self.EXCLUDED_SERVER_IDS])
# Retrieve servers
server_ids = self.check.get_servers_managed_by_hypervisor()
# Assert
# .. 1 out of 4 server ids filtered
self.assertEqual(len(server_ids), 1)
self.assertEqual(server_ids[0], self.FILTERED_SERVER_ID)
# cleanup
self.check.exclude_server_id_rules = set([])
@patch("_openstack.OpenStackCheck.get_all_network_ids", return_value=ALL_IDS)
def test_network_exclusion(self, *args):
"""
Exclude networks using regular expressions.
"""
with patch("_openstack.OpenStackCheck.get_stats_for_single_network") \
as mock_get_stats_single_network:
self.check.exclude_network_id_rules = set([re.compile(rule) for rule in self.EXCLUDED_NETWORK_IDS])
# Retrieve network stats
self.check.get_network_stats()
# Assert
# .. 1 out of 4 network filtered in
self.assertEqual(mock_get_stats_single_network.call_count, 1)
self.assertEqual(
mock_get_stats_single_network.call_args[0][0], self.FILTERED_NETWORK_ID
)
# cleanup
self.check.exclude_network_id_rules = set([])