Adds migration engine for peewee ORM
[osm/POL.git] / osm_policy_module / tests / integration / test_policy_agent.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
23 ##
24 import asyncio
25 import logging
26 import sys
27 import unittest
28 import uuid
29 from unittest.mock import patch, Mock
30
31 from kafka import KafkaProducer
32 from osm_common.dbmongo import DbMongo
33 from playhouse.db_url import connect
34
35 from osm_policy_module.common.common_db_client import CommonDbClient
36 from osm_policy_module.common.mon_client import MonClient
37 from osm_policy_module.core import database
38 from osm_policy_module.core.agent import PolicyModuleAgent
39 from osm_policy_module.core.config import Config
40 from osm_policy_module.core.database import ScalingGroup, ScalingAlarm, ScalingPolicy, ScalingCriteria
41
42 log = logging.getLogger()
43 log.level = logging.INFO
44 stream_handler = logging.StreamHandler(sys.stdout)
45 log.addHandler(stream_handler)
46
47 nsr_record_mock = {
48 "_id": "87776f33-b67c-417a-8119-cb08e4098951",
49 "crete-time": 1535392482.0044956,
50 "operational-status": "running",
51 "ssh-authorized-key": None,
52 "name-ref": "cirros_ns",
53 "nsd": {
54 "name": "cirros_vdu_scaling_ns",
55 "_id": "d7c8bd3c-eb39-4514-8847-19f01345524f",
56 "vld": [
57 {
58 "id": "cirros_nsd_vld1",
59 "name": "cirros_nsd_vld1",
60 "type": "ELAN",
61 "mgmt-network": "true",
62 "vnfd-connection-point-ref": [
63 {
64 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
65 "member-vnf-index-ref": 1,
66 "vnfd-connection-point-ref": "eth0"
67 },
68 {
69 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
70 "member-vnf-index-ref": 2,
71 "vnfd-connection-point-ref": "eth0"
72 }
73 ]
74 }
75 ],
76 "vendor": "OSM",
77 "constituent-vnfd": [
78 {
79 "member-vnf-index": "1",
80 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
81 },
82 {
83 "member-vnf-index": "2",
84 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
85 }
86 ],
87 "version": "1.0",
88 "id": "cirros_vdu_scaling_ns",
89 "description": "Simple NS example with a cirros_vdu_scaling_vnf",
90 "logo": "osm.png",
91 "_admin": {
92 "created": 1535392246.499733,
93 "userDefinedData": {
94
95 },
96 "usageSate": "NOT_IN_USE",
97 "storage": {
98 "zipfile": "package.tar.gz",
99 "fs": "local",
100 "path": "/app/storage/",
101 "folder": "d7c8bd3c-eb39-4514-8847-19f01345524f",
102 "pkg-dir": "cirros_nsd",
103 "descriptor": "cirros_nsd/cirros_vdu_scaling_nsd.yaml"
104 },
105 "onboardingState": "ONBOARDED",
106 "modified": 1535392246.499733,
107 "projects_read": [
108 "admin"
109 ],
110 "operationalState": "ENABLED",
111 "projects_write": [
112 "admin"
113 ]
114 },
115 "short-name": "cirros_vdu_scaling_ns"
116 },
117 "id": "87776f33-b67c-417a-8119-cb08e4098951",
118 "config-status": "configured",
119 "operational-events": [],
120 "_admin": {
121 "created": 1535392482.0084584,
122 "projects_read": [
123 "admin"
124 ],
125 "nsState": "INSTANTIATED",
126 "modified": 1535392482.0084584,
127 "projects_write": [
128 "admin"
129 ],
130 "deployed": {
131 "RO": {
132 "vnfd_id": {
133 "cirros_vdu_scaling_vnf": "7445e347-fe2f-431a-abc2-8b9be3d093c6"
134 },
135 "nsd_id": "92c56cf0-f8fa-488c-9afb-9f3d78ae6bbb",
136 "nsr_id": "637e12cd-c201-4c44-8ebd-70fb57a4dcee",
137 "nsr_status": "BUILD"
138 }
139 }
140 },
141 "nsd-ref": "cirros_vdu_scaling_ns",
142 "name": "cirros_ns",
143 "resource-orchestrator": "osmopenmano",
144 "instantiate_params": {
145 "nsDescription": "default description",
146 "nsdId": "d7c8bd3c-eb39-4514-8847-19f01345524f",
147 "nsr_id": "87776f33-b67c-417a-8119-cb08e4098951",
148 "nsName": "cirros_ns",
149 "vimAccountId": "be48ae31-1d46-4892-a4b4-d69abd55714b"
150 },
151 "description": "default description",
152 "constituent-vnfr-ref": [
153 "0d9d06ad-3fc2-418c-9934-465e815fafe2",
154 "3336eb44-77df-4c4f-9881-d2828d259864"
155 ],
156 "admin-status": "ENABLED",
157 "detailed-status": "done",
158 "datacenter": "be48ae31-1d46-4892-a4b4-d69abd55714b",
159 "orchestration-progress": {
160
161 },
162 "short-name": "cirros_ns",
163 "ns-instance-config-ref": "87776f33-b67c-417a-8119-cb08e4098951",
164 "nsd-name-ref": "cirros_vdu_scaling_ns",
165 "admin": {
166 "deployed": {
167 "RO": {
168 "nsr_status": "ACTIVE"
169 }
170 }
171 }
172 }
173
174 vnfr_record_mocks = [
175 {
176 "_id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
177 "ip-address": "192.168.160.2",
178 "created-time": 1535392482.0044956,
179 "vim-account-id": "be48ae31-1d46-4892-a4b4-d69abd55714b",
180 "vdur": [
181 {
182 "interfaces": [
183 {
184 "mac-address": "fa:16:3e:71:fd:b8",
185 "name": "eth0",
186 "ip-address": "192.168.160.2"
187 }
188 ],
189 "status": "ACTIVE",
190 "vim-id": "63a65636-9fc8-4022-b070-980823e6266a",
191 "name": "cirros_ns-1-cirros_vnfd-VM-1",
192 "status-detailed": None,
193 "ip-address": "192.168.160.2",
194 "vdu-id-ref": "cirros_vnfd-VM"
195 }
196 ],
197 "id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
198 "vnfd-ref": "cirros_vdu_scaling_vnf",
199 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
200 "_admin": {
201 "created": 1535392482.0067868,
202 "projects_read": [
203 "admin"
204 ],
205 "modified": 1535392482.0067868,
206 "projects_write": [
207 "admin"
208 ]
209 },
210 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
211 "member-vnf-index-ref": "1",
212 "connection-point": [
213 {
214 "name": "eth0",
215 "id": None,
216 "connection-point-id": None
217 }
218 ]
219 },
220 {
221 "_id": "3336eb44-77df-4c4f-9881-d2828d259864",
222 "ip-address": "192.168.160.10",
223 "created-time": 1535392482.0044956,
224 "vim-account-id": "be48ae31-1d46-4892-a4b4-d69abd55714b",
225 "vdur": [
226 {
227 "interfaces": [
228 {
229 "mac-address": "fa:16:3e:1e:76:e8",
230 "name": "eth0",
231 "ip-address": "192.168.160.10"
232 }
233 ],
234 "status": "ACTIVE",
235 "vim-id": "a154b8d3-2b10-421a-a51d-4b391d9bd366",
236 "name": "cirros_ns-2-cirros_vnfd-VM-1",
237 "status-detailed": None,
238 "ip-address": "192.168.160.10",
239 "vdu-id-ref": "cirros_vnfd-VM"
240 }
241 ],
242 "id": "3336eb44-77df-4c4f-9881-d2828d259864",
243 "vnfd-ref": "cirros_vdu_scaling_vnf",
244 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
245 "_admin": {
246 "created": 1535392482.0076294,
247 "projects_read": [
248 "admin"
249 ],
250 "modified": 1535392482.0076294,
251 "projects_write": [
252 "admin"
253 ]
254 },
255 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
256 "member-vnf-index-ref": "2",
257 "connection-point": [
258 {
259 "name": "eth0",
260 "id": None,
261 "connection-point-id": None
262 }
263 ]}]
264
265 nsd_record_mock = {'name': 'cirros_vdu_scaling_ns',
266 'version': '1.0',
267 'short-name': 'cirros_vdu_scaling_ns',
268 'logo': 'osm.png',
269 'id': 'cirros_vdu_scaling_ns',
270 'description': 'Simple NS example with a cirros_vdu_scaling_vnf',
271 'vendor': 'OSM',
272 'vld': [
273 {'name': 'cirros_nsd_vld1',
274 'type': 'ELAN',
275 'id': 'cirros_nsd_vld1',
276 'mgmt-network': 'true',
277 'vnfd-connection-point-ref': [
278 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
279 'vnfd-connection-point-ref': 'eth0',
280 'member-vnf-index-ref': 1},
281 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
282 'vnfd-connection-point-ref': 'eth0',
283 'member-vnf-index-ref': 2}]}],
284 'constituent-vnfd': [{'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
285 'member-vnf-index': '1'},
286 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
287 'member-vnf-index': '2'}]}
288
289 vnfd_record_mock = {
290 "_id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
291 "name": "cirros_vdu_scaling_vnf",
292 "vendor": "OSM",
293 "vdu": [
294 {
295 "name": "cirros_vnfd-VM",
296 "monitoring-param": [
297 {
298 "id": "cirros_vnfd-VM_memory_util",
299 "nfvi-metric": "average_memory_utilization"
300 }
301 ],
302 "vm-flavor": {
303 "vcpu-count": 1,
304 "memory-mb": 256,
305 "storage-gb": 2
306 },
307 "description": "cirros_vnfd-VM",
308 "count": 1,
309 "id": "cirros_vnfd-VM",
310 "interface": [
311 {
312 "name": "eth0",
313 "external-connection-point-ref": "eth0",
314 "type": "EXTERNAL",
315 "virtual-interface": {
316 "bandwidth": "0",
317 "type": "VIRTIO",
318 "vpci": "0000:00:0a.0"
319 }
320 }
321 ],
322 "image": "cirros034"
323 }
324 ],
325 "monitoring-param": [
326 {
327 "id": "cirros_vnf_memory_util",
328 "name": "cirros_vnf_memory_util",
329 "aggregation-type": "AVERAGE",
330 "vdu-monitoring-param": {
331 "vdu-monitoring-param-ref": "cirros_vnfd-VM_memory_util",
332 "vdu-ref": "cirros_vnfd-VM"
333 }
334 },
335 {
336 "id": "haproxy_users",
337 "name": "haproxy_users",
338 "aggregation-type": "AVERAGE",
339 "vnf-metric":
340 {"vnf-metric-name-ref": "users"}
341 }
342 ],
343 "vdu-configuration": {
344 "juju": {
345 "charm": "testmetrics",
346 "proxy": True
347 },
348 "metrics": {
349 "name": "users"
350 }
351 },
352 "description": "Simple VNF example with a cirros and a scaling group descriptor",
353 "id": "cirros_vdu_scaling_vnf",
354 "logo": "cirros-64.png",
355 "version": "1.0",
356 "connection-point": [
357 {
358 "name": "eth0",
359 "type": "VPORT"
360 }
361 ],
362 "mgmt-interface": {
363 "cp": "eth0"
364 },
365 "scaling-group-descriptor": [
366 {
367 "name": "scale_cirros_vnfd-VM",
368 "min-instance-count": 1,
369 "vdu": [
370 {
371 "count": 1,
372 "vdu-id-ref": "cirros_vnfd-VM"
373 }
374 ],
375 "max-instance-count": 10,
376 "scaling-policy": [
377 {
378 "name": "auto_memory_util_above_threshold",
379 "scaling-type": "automatic",
380 "cooldown-time": 60,
381 "threshold-time": 10,
382 "scaling-criteria": [
383 {
384 "name": "group1_memory_util_above_threshold",
385 "vnf-monitoring-param-ref": "cirros_vnf_memory_util",
386 "scale-out-threshold": 80,
387 "scale-out-relational-operation": "GT",
388 "scale-in-relational-operation": "LT",
389 "scale-in-threshold": 20
390 }
391 ]
392 }
393 ]
394 }
395 ],
396 "short-name": "cirros_vdu_scaling_vnf",
397 "_admin": {
398 "created": 1535392242.6281035,
399 "modified": 1535392242.6281035,
400 "storage": {
401 "zipfile": "package.tar.gz",
402 "pkg-dir": "cirros_vnf",
403 "path": "/app/storage/",
404 "folder": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
405 "fs": "local",
406 "descriptor": "cirros_vnf/cirros_vdu_scaling_vnfd.yaml"
407 },
408 "usageSate": "NOT_IN_USE",
409 "onboardingState": "ONBOARDED",
410 "userDefinedData": {
411
412 },
413 "projects_read": [
414 "admin"
415 ],
416 "operationalState": "ENABLED",
417 "projects_write": [
418 "admin"
419 ]
420 }
421 }
422
423 MODELS = [ScalingGroup, ScalingPolicy, ScalingCriteria, ScalingAlarm]
424
425
426 class PolicyModuleAgentTest(unittest.TestCase):
427 def setUp(self):
428 super()
429 database.db.initialize(connect('sqlite://'))
430 database.db.bind(MODELS)
431 database.db.connect()
432 database.db.drop_tables(MODELS)
433 database.db.create_tables(MODELS)
434 self.loop = asyncio.new_event_loop()
435
436 def tearDown(self):
437 super()
438
439 @patch.object(DbMongo, 'db_connect', Mock())
440 @patch.object(KafkaProducer, '__init__')
441 @patch.object(MonClient, 'create_alarm')
442 @patch.object(CommonDbClient, 'get_vnfd')
443 @patch.object(CommonDbClient, 'get_nsr')
444 @patch.object(CommonDbClient, 'get_vnfr')
445 def test_configure_scaling_groups(self, get_vnfr, get_nsr, get_vnfd, create_alarm, kafka_producer_init):
446 def _test_configure_scaling_groups_get_vnfr(*args, **kwargs):
447 if '1' in args[1]:
448 return vnfr_record_mocks[0]
449 if '2' in args[1]:
450 return vnfr_record_mocks[1]
451
452 async def _test_configure_scaling_groups_create_alarm(*args, **kwargs):
453 return uuid.uuid4()
454
455 kafka_producer_init.return_value = None
456 get_vnfr.side_effect = _test_configure_scaling_groups_get_vnfr
457 get_nsr.return_value = nsr_record_mock
458 get_vnfd.return_value = vnfd_record_mock
459 create_alarm.side_effect = _test_configure_scaling_groups_create_alarm
460 config = Config()
461 agent = PolicyModuleAgent(config, self.loop)
462 self.loop.run_until_complete(agent._configure_scaling_groups("test_nsr_id"))
463 create_alarm.assert_any_call(metric_name='cirros_vnf_memory_util',
464 ns_id='test_nsr_id',
465 operation='GT',
466 statistic='AVERAGE',
467 threshold=80,
468 vdu_name='cirros_ns-1-cirros_vnfd-VM-1',
469 vnf_member_index='1')
470 create_alarm.assert_any_call(metric_name='cirros_vnf_memory_util',
471 ns_id='test_nsr_id',
472 operation='LT',
473 statistic='AVERAGE',
474 threshold=20,
475 vdu_name='cirros_ns-1-cirros_vnfd-VM-1',
476 vnf_member_index='1')
477 create_alarm.assert_any_call(metric_name='cirros_vnf_memory_util',
478 ns_id='test_nsr_id',
479 operation='GT',
480 statistic='AVERAGE',
481 threshold=80,
482 vdu_name='cirros_ns-2-cirros_vnfd-VM-1',
483 vnf_member_index='2')
484 create_alarm.assert_any_call(metric_name='cirros_vnf_memory_util',
485 ns_id='test_nsr_id',
486 operation='LT',
487 statistic='AVERAGE',
488 threshold=20,
489 vdu_name='cirros_ns-2-cirros_vnfd-VM-1',
490 vnf_member_index='2')
491 scaling_record = ScalingGroup.get()
492 self.assertEqual(scaling_record.name, 'scale_cirros_vnfd-VM')
493 self.assertEqual(scaling_record.nsr_id, 'test_nsr_id')
494
495
496 if __name__ == '__main__':
497 unittest.main()