Re-Revert "Revert "Migrates alarms to MongoDB"" approved by TSC
[osm/MON.git] / osm_mon / tests / unit / core / test_common_db_client.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 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
22 ##
23 import unittest
24 from unittest import mock
25
26 from osm_common import dbmongo
27
28 from osm_mon.core.common_db import CommonDbClient
29 from osm_mon.core.config import Config
30 from osm_mon.core.models import Alarm
31
32
33 class CommonDbClientTest(unittest.TestCase):
34 def setUp(self):
35 super().setUp()
36 self.config = Config()
37
38 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
39 @mock.patch.object(CommonDbClient, "get_vnfr")
40 def test_get_vim_id(self, get_vnfr):
41 get_vnfr.return_value = {'_id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01',
42 '_admin': {
43 'projects_read': ['admin'], 'created': 1526044312.102287,
44 'modified': 1526044312.102287, 'projects_write': ['admin']
45 },
46 'vim-account-id': 'c1740601-7287-48c8-a2c9-bce8fee459eb',
47 'nsr-id-ref': '5ec3f571-d540-4cb0-9992-971d1b08312e',
48 'vdur': [
49 {
50 'internal-connection-point': [],
51 'vdu-id-ref': 'ubuntuvnf_vnfd-VM',
52 'id': 'ffd73f33-c8bb-4541-a977-44dcc3cbe28d',
53 'vim-id': '27042672-5190-4209-b844-95bbaeea7ea7',
54 'name': 'ubuntuvnf_vnfd-VM'
55 }
56 ],
57 'vnfd-ref': 'ubuntuvnf_vnfd',
58 'member-vnf-index-ref': '1',
59 'created-time': 1526044312.0999322,
60 'vnfd-id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01',
61 'id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01'}
62 common_db_client = CommonDbClient(self.config)
63 vim_account_id = common_db_client.get_vim_account_id('5ec3f571-d540-4cb0-9992-971d1b08312e', 1)
64 self.assertEqual(vim_account_id, 'c1740601-7287-48c8-a2c9-bce8fee459eb')
65
66 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
67 @mock.patch.object(dbmongo.DbMongo, "get_one")
68 def test_get_vdur(self, get_one):
69 get_one.return_value = {'_id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01',
70 '_admin': {
71 'projects_read': ['admin'], 'created': 1526044312.102287,
72 'modified': 1526044312.102287, 'projects_write': ['admin']
73 },
74 'vim-account-id': 'c1740601-7287-48c8-a2c9-bce8fee459eb',
75 'nsr-id-ref': '5ec3f571-d540-4cb0-9992-971d1b08312e',
76 'vdur': [
77 {
78 'internal-connection-point': [],
79 'vdu-id-ref': 'ubuntuvnf_vnfd-VM',
80 'id': 'ffd73f33-c8bb-4541-a977-44dcc3cbe28d',
81 'vim-id': '27042672-5190-4209-b844-95bbaeea7ea7',
82 'name': 'ubuntuvnf_vnfd-VM'
83 }
84 ],
85 'vnfd-ref': 'ubuntuvnf_vnfd',
86 'member-vnf-index-ref': '1',
87 'created-time': 1526044312.0999322,
88 'vnfd-id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01',
89 'id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01'}
90 common_db_client = CommonDbClient(self.config)
91 vdur = common_db_client.get_vdur('5ec3f571-d540-4cb0-9992-971d1b08312e', '1', 'ubuntuvnf_vnfd-VM')
92 expected_vdur = {
93 'internal-connection-point': [],
94 'vdu-id-ref': 'ubuntuvnf_vnfd-VM',
95 'id': 'ffd73f33-c8bb-4541-a977-44dcc3cbe28d',
96 'vim-id': '27042672-5190-4209-b844-95bbaeea7ea7',
97 'name': 'ubuntuvnf_vnfd-VM'
98 }
99
100 self.assertDictEqual(vdur, expected_vdur)
101
102 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
103 @mock.patch.object(dbmongo.DbMongo, "get_one")
104 @mock.patch.object(CommonDbClient, "decrypt_vim_password")
105 def test_get_vim_account_default_schema(self, decrypt_vim_password, get_one):
106 schema_version = '10.0'
107 vim_id = '1'
108 get_one.return_value = {
109 '_id': vim_id,
110 'vim_password': 'vim_password',
111 'schema_version': schema_version,
112 'config': {
113 'admin_password': 'admin_password',
114 'vrops_password': 'vrops_password',
115 'nsx_password': 'nsx_password',
116 'vcenter_password': 'vcenter_password'
117 }
118 }
119
120 common_db_client = CommonDbClient(self.config)
121 common_db_client.get_vim_account('1')
122
123 decrypt_vim_password.assert_any_call('vim_password', schema_version, vim_id)
124 decrypt_vim_password.assert_any_call('vrops_password', schema_version, vim_id)
125 decrypt_vim_password.assert_any_call('admin_password', schema_version, vim_id)
126 decrypt_vim_password.assert_any_call('nsx_password', schema_version, vim_id)
127 decrypt_vim_password.assert_any_call('vcenter_password', schema_version, vim_id)
128
129 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
130 @mock.patch.object(dbmongo.DbMongo, "get_one")
131 @mock.patch.object(CommonDbClient, "decrypt_vim_password")
132 def test_get_vim_account_1_1_schema(self, decrypt_vim_password, get_one):
133 schema_version = '1.1'
134 vim_id = '1'
135 get_one.return_value = {
136 '_id': vim_id,
137 'vim_password': 'vim_password',
138 'schema_version': schema_version,
139 'config': {
140 'vrops_password': 'vrops_password'
141 }
142 }
143
144 common_db_client = CommonDbClient(self.config)
145 common_db_client.get_vim_account('1')
146
147 decrypt_vim_password.assert_any_call('vim_password', schema_version, vim_id)
148 self.assertRaises(AssertionError, decrypt_vim_password.assert_any_call, 'vrops_password', schema_version,
149 vim_id)
150
151 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
152 @mock.patch.object(dbmongo.DbMongo, "get_list")
153 def test_get_alarms(self, get_list):
154 get_list.return_value = [{
155 'uuid': '1',
156 'name': 'name',
157 'severity': 'severity',
158 'threshold': 50,
159 'operation': 'operation',
160 'statistic': 'statistic',
161 'tags': {},
162 }]
163
164 common_db_client = CommonDbClient(self.config)
165 alarms = common_db_client.get_alarms()
166 self.assertEqual('1', alarms[0].uuid)
167
168 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
169 @mock.patch.object(dbmongo.DbMongo, "create")
170 def test_create_alarm(self, create):
171 alarm = Alarm('name', 'severity', 50.0, 'operation', 'statistic', 'metric', {})
172 alarm.uuid = '1'
173 common_db_client = CommonDbClient(self.config)
174 common_db_client.create_alarm(alarm)
175 create.assert_called_with('alarms', {'tags': {}, 'threshold': 50.0, 'metric': 'metric', 'severity': 'severity',
176 'statistic': 'statistic', 'name': 'name', 'uuid': '1'})
177
178 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
179 @mock.patch.object(dbmongo.DbMongo, "del_one")
180 def test_delete_alarm(self, delete):
181 common_db_client = CommonDbClient(self.config)
182 common_db_client.delete_alarm('1')
183 delete.assert_called_with('alarms', {'uuid': '1'})