Feature 10339 - Enhanced Alarm Mgmt. (SOL005 FM Interface)
[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 = {
42 "_id": "a314c865-aee7-4d9b-9c9d-079d7f857f01",
43 "_admin": {
44 "projects_read": ["admin"],
45 "created": 1526044312.102287,
46 "modified": 1526044312.102287,
47 "projects_write": ["admin"],
48 },
49 "vim-account-id": "c1740601-7287-48c8-a2c9-bce8fee459eb",
50 "nsr-id-ref": "5ec3f571-d540-4cb0-9992-971d1b08312e",
51 "vdur": [
52 {
53 "internal-connection-point": [],
54 "vdu-id-ref": "ubuntuvnf_vnfd-VM",
55 "id": "ffd73f33-c8bb-4541-a977-44dcc3cbe28d",
56 "vim-id": "27042672-5190-4209-b844-95bbaeea7ea7",
57 "name": "ubuntuvnf_vnfd-VM",
58 }
59 ],
60 "vnfd-ref": "ubuntuvnf_vnfd",
61 "member-vnf-index-ref": "1",
62 "created-time": 1526044312.0999322,
63 "vnfd-id": "a314c865-aee7-4d9b-9c9d-079d7f857f01",
64 "id": "a314c865-aee7-4d9b-9c9d-079d7f857f01",
65 }
66 common_db_client = CommonDbClient(self.config)
67 vim_account_id = common_db_client.get_vim_account_id(
68 "5ec3f571-d540-4cb0-9992-971d1b08312e", 1
69 )
70 self.assertEqual(vim_account_id, "c1740601-7287-48c8-a2c9-bce8fee459eb")
71
72 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
73 @mock.patch.object(dbmongo.DbMongo, "get_one")
74 def test_get_vdur(self, get_one):
75 get_one.return_value = {
76 "_id": "a314c865-aee7-4d9b-9c9d-079d7f857f01",
77 "_admin": {
78 "projects_read": ["admin"],
79 "created": 1526044312.102287,
80 "modified": 1526044312.102287,
81 "projects_write": ["admin"],
82 },
83 "vim-account-id": "c1740601-7287-48c8-a2c9-bce8fee459eb",
84 "nsr-id-ref": "5ec3f571-d540-4cb0-9992-971d1b08312e",
85 "vdur": [
86 {
87 "internal-connection-point": [],
88 "vdu-id-ref": "ubuntuvnf_vnfd-VM",
89 "id": "ffd73f33-c8bb-4541-a977-44dcc3cbe28d",
90 "vim-id": "27042672-5190-4209-b844-95bbaeea7ea7",
91 "name": "ubuntuvnf_vnfd-VM",
92 }
93 ],
94 "vnfd-ref": "ubuntuvnf_vnfd",
95 "member-vnf-index-ref": "1",
96 "created-time": 1526044312.0999322,
97 "vnfd-id": "a314c865-aee7-4d9b-9c9d-079d7f857f01",
98 "id": "a314c865-aee7-4d9b-9c9d-079d7f857f01",
99 }
100 common_db_client = CommonDbClient(self.config)
101 vdur = common_db_client.get_vdur(
102 "5ec3f571-d540-4cb0-9992-971d1b08312e", "1", "ubuntuvnf_vnfd-VM"
103 )
104 expected_vdur = {
105 "internal-connection-point": [],
106 "vdu-id-ref": "ubuntuvnf_vnfd-VM",
107 "id": "ffd73f33-c8bb-4541-a977-44dcc3cbe28d",
108 "vim-id": "27042672-5190-4209-b844-95bbaeea7ea7",
109 "name": "ubuntuvnf_vnfd-VM",
110 }
111
112 self.assertDictEqual(vdur, expected_vdur)
113
114 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
115 @mock.patch.object(dbmongo.DbMongo, "get_one")
116 @mock.patch.object(CommonDbClient, "decrypt_vim_password")
117 def test_get_vim_account_default_schema(self, decrypt_vim_password, get_one):
118 schema_version = "10.0"
119 vim_id = "1"
120 get_one.return_value = {
121 "_id": vim_id,
122 "vim_password": "vim_password",
123 "schema_version": schema_version,
124 "config": {
125 "admin_password": "admin_password",
126 "vrops_password": "vrops_password",
127 "nsx_password": "nsx_password",
128 "vcenter_password": "vcenter_password",
129 },
130 }
131
132 common_db_client = CommonDbClient(self.config)
133 common_db_client.get_vim_account("1")
134
135 decrypt_vim_password.assert_any_call("vim_password", schema_version, vim_id)
136 decrypt_vim_password.assert_any_call("vrops_password", schema_version, vim_id)
137 decrypt_vim_password.assert_any_call("admin_password", schema_version, vim_id)
138 decrypt_vim_password.assert_any_call("nsx_password", schema_version, vim_id)
139 decrypt_vim_password.assert_any_call("vcenter_password", schema_version, vim_id)
140
141 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
142 @mock.patch.object(dbmongo.DbMongo, "get_one")
143 @mock.patch.object(CommonDbClient, "decrypt_vim_password")
144 def test_get_vim_account_1_1_schema(self, decrypt_vim_password, get_one):
145 schema_version = "1.1"
146 vim_id = "1"
147 get_one.return_value = {
148 "_id": vim_id,
149 "vim_password": "vim_password",
150 "schema_version": schema_version,
151 "config": {"vrops_password": "vrops_password"},
152 }
153
154 common_db_client = CommonDbClient(self.config)
155 common_db_client.get_vim_account("1")
156
157 decrypt_vim_password.assert_any_call("vim_password", schema_version, vim_id)
158 self.assertRaises(
159 AssertionError,
160 decrypt_vim_password.assert_any_call,
161 "vrops_password",
162 schema_version,
163 vim_id,
164 )
165
166 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
167 @mock.patch.object(dbmongo.DbMongo, "get_list")
168 def test_get_alarms(self, get_list):
169 get_list.return_value = [
170 {
171 "uuid": "1",
172 "name": "name",
173 "severity": "severity",
174 "threshold": 50,
175 "operation": "operation",
176 "statistic": "statistic",
177 "tags": {},
178 }
179 ]
180
181 common_db_client = CommonDbClient(self.config)
182 alarms = common_db_client.get_alarms()
183 self.assertEqual("1", alarms[0].uuid)
184
185 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
186 @mock.patch.object(dbmongo.DbMongo, "create")
187 def test_create_alarm(self, create):
188 alarm = Alarm("name", "severity", 50.0, "operation", "statistic", "metric", "scale_out", {}, "ok")
189 alarm.uuid = "1"
190 common_db_client = CommonDbClient(self.config)
191 common_db_client.create_alarm(alarm)
192 create.assert_called_with(
193 "alarms",
194 {
195 "tags": {},
196 "threshold": 50.0,
197 "metric": "metric",
198 "severity": "severity",
199 "statistic": "statistic",
200 "name": "name",
201 "operation": "operation",
202 "uuid": "1",
203 "alarm_status": "ok",
204 },
205 )
206
207 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
208 @mock.patch.object(dbmongo.DbMongo, "del_one")
209 def test_delete_alarm(self, delete):
210 common_db_client = CommonDbClient(self.config)
211 common_db_client.delete_alarm("1")
212 delete.assert_called_with("alarms", {"uuid": "1"})