6c3df421af3584413f679fa4ffc89aa63904f38b
[osm/MON.git] / osm_mon / test / collector / test_collector.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 import multiprocessing
16 # Unless required by applicable law or agreed to in writing, software
17 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
19 # License for the specific language governing permissions and limitations
20 # under the License.
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 unittest
25 from unittest import mock
26
27 from osm_mon.collector.collector import Collector
28 from osm_mon.collector.collectors.juju import VCACollector
29 from osm_mon.collector.collectors.openstack import OpenstackCollector
30 from osm_mon.core.database import VimCredentials
31
32
33 class CollectorTest(unittest.TestCase):
34 def setUp(self):
35 super().setUp()
36
37 @mock.patch("osm_mon.collector.collector.CommonDbClient", autospec=True)
38 def test_get_vim_id(self, common_db):
39 common_db.return_value.get_vnfr.return_value = {'_id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01',
40 '_admin': {
41 'projects_read': ['admin'], 'created': 1526044312.102287,
42 'modified': 1526044312.102287, 'projects_write': ['admin']
43 },
44 'vim-account-id': 'c1740601-7287-48c8-a2c9-bce8fee459eb',
45 'nsr-id-ref': '5ec3f571-d540-4cb0-9992-971d1b08312e',
46 'vdur': [
47 {
48 'internal-connection-point': [],
49 'vdu-id-ref': 'ubuntuvnf_vnfd-VM',
50 'id': 'ffd73f33-c8bb-4541-a977-44dcc3cbe28d',
51 'vim-id': '27042672-5190-4209-b844-95bbaeea7ea7',
52 'name': 'ubuntuvnf_vnfd-VM'
53 }
54 ],
55 'vnfd-ref': 'ubuntuvnf_vnfd',
56 'member-vnf-index-ref': '1',
57 'created-time': 1526044312.0999322,
58 'vnfd-id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01',
59 'id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01'}
60 collector = Collector()
61 vim_account_id = collector._get_vim_account_id('5ec3f571-d540-4cb0-9992-971d1b08312e', 1)
62 self.assertEqual(vim_account_id, 'c1740601-7287-48c8-a2c9-bce8fee459eb')
63
64 @mock.patch("osm_mon.collector.collector.CommonDbClient", mock.Mock())
65 @mock.patch("osm_mon.collector.collector.DatabaseManager", autospec=True)
66 def test_get_vim_type(self, database_manager):
67 mock_creds = VimCredentials()
68 mock_creds.id = 'test_id'
69 mock_creds.user = 'user'
70 mock_creds.url = 'url'
71 mock_creds.password = 'password'
72 mock_creds.tenant_name = 'tenant_name'
73 mock_creds.type = 'openstack'
74
75 database_manager.return_value.get_credentials.return_value = mock_creds
76 collector = Collector()
77 vim_type = collector._get_vim_type('test_id')
78 self.assertEqual(vim_type, 'openstack')
79
80 @mock.patch("osm_mon.collector.collector.CommonDbClient", mock.Mock())
81 @mock.patch.object(OpenstackCollector, "__init__", lambda *args, **kwargs: None)
82 @mock.patch.object(OpenstackCollector, "collect")
83 @mock.patch.object(Collector, "_get_vim_type")
84 def test_init_vim_collector_and_collect_openstack(self, _get_vim_type, collect):
85 _get_vim_type.return_value = 'openstack'
86 collector = Collector()
87 queue = multiprocessing.Queue()
88 collector._init_vim_collector_and_collect({}, 'test_vim_account_id', queue)
89 collect.assert_called_once_with({}, queue)
90
91 @mock.patch("osm_mon.collector.collector.CommonDbClient", mock.Mock())
92 @mock.patch.object(OpenstackCollector, "collect")
93 @mock.patch.object(Collector, "_get_vim_type")
94 def test_init_vim_collector_and_collect_unknown(self, _get_vim_type, openstack_collect):
95 _get_vim_type.return_value = 'unknown'
96 collector = Collector()
97 queue = multiprocessing.Queue()
98 collector._init_vim_collector_and_collect({}, 'test_vim_account_id', queue)
99 openstack_collect.assert_not_called()
100
101 @mock.patch("osm_mon.collector.collector.CommonDbClient", mock.Mock())
102 @mock.patch("osm_mon.collector.collector.VCACollector", autospec=True)
103 def test_init_vca_collector_and_collect(self, vca_collector):
104 collector = Collector()
105 queue = multiprocessing.Queue()
106 collector._init_vca_collector_and_collect({}, queue)
107 vca_collector.assert_called_once_with()
108 vca_collector.return_value.collect.assert_called_once_with({}, queue)