7f0768df3e77864d90994d27e9cc78ffe317d379
1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
25 from unittest
import mock
27 from osm_common
import dbmongo
29 from osm_mon
.core
.common_db
import CommonDbClient
30 from osm_mon
.core
.config
import Config
31 from osm_mon
.core
.models
import Alarm
34 class CommonDbClientTest(unittest
.TestCase
):
37 self
.config
= Config()
39 @mock.patch
.object(dbmongo
.DbMongo
, "db_connect", mock
.Mock())
40 @mock.patch
.object(CommonDbClient
, "get_vnfr")
41 def test_get_vim_id(self
, get_vnfr
):
42 get_vnfr
.return_value
= {
43 "_id": "a314c865-aee7-4d9b-9c9d-079d7f857f01",
45 "projects_read": ["admin"],
46 "created": 1526044312.102287,
47 "modified": 1526044312.102287,
48 "projects_write": ["admin"],
50 "vim-account-id": "c1740601-7287-48c8-a2c9-bce8fee459eb",
51 "nsr-id-ref": "5ec3f571-d540-4cb0-9992-971d1b08312e",
54 "internal-connection-point": [],
55 "vdu-id-ref": "ubuntuvnf_vnfd-VM",
56 "id": "ffd73f33-c8bb-4541-a977-44dcc3cbe28d",
57 "vim-id": "27042672-5190-4209-b844-95bbaeea7ea7",
58 "name": "ubuntuvnf_vnfd-VM",
61 "vnfd-ref": "ubuntuvnf_vnfd",
62 "member-vnf-index-ref": "1",
63 "created-time": 1526044312.0999322,
64 "vnfd-id": "a314c865-aee7-4d9b-9c9d-079d7f857f01",
65 "id": "a314c865-aee7-4d9b-9c9d-079d7f857f01",
67 common_db_client
= CommonDbClient(self
.config
)
68 vim_account_id
= common_db_client
.get_vim_account_id(
69 "5ec3f571-d540-4cb0-9992-971d1b08312e", 1
71 self
.assertEqual(vim_account_id
, "c1740601-7287-48c8-a2c9-bce8fee459eb")
73 @mock.patch
.object(dbmongo
.DbMongo
, "db_connect", mock
.Mock())
74 @mock.patch
.object(dbmongo
.DbMongo
, "get_one")
75 def test_get_vdur(self
, get_one
):
76 get_one
.return_value
= {
77 "_id": "a314c865-aee7-4d9b-9c9d-079d7f857f01",
79 "projects_read": ["admin"],
80 "created": 1526044312.102287,
81 "modified": 1526044312.102287,
82 "projects_write": ["admin"],
84 "vim-account-id": "c1740601-7287-48c8-a2c9-bce8fee459eb",
85 "nsr-id-ref": "5ec3f571-d540-4cb0-9992-971d1b08312e",
88 "internal-connection-point": [],
89 "vdu-id-ref": "ubuntuvnf_vnfd-VM",
90 "id": "ffd73f33-c8bb-4541-a977-44dcc3cbe28d",
91 "vim-id": "27042672-5190-4209-b844-95bbaeea7ea7",
92 "name": "ubuntuvnf_vnfd-VM",
95 "vnfd-ref": "ubuntuvnf_vnfd",
96 "member-vnf-index-ref": "1",
97 "created-time": 1526044312.0999322,
98 "vnfd-id": "a314c865-aee7-4d9b-9c9d-079d7f857f01",
99 "id": "a314c865-aee7-4d9b-9c9d-079d7f857f01",
101 common_db_client
= CommonDbClient(self
.config
)
102 vdur
= common_db_client
.get_vdur(
103 "5ec3f571-d540-4cb0-9992-971d1b08312e", "1", "ubuntuvnf_vnfd-VM"
106 "internal-connection-point": [],
107 "vdu-id-ref": "ubuntuvnf_vnfd-VM",
108 "id": "ffd73f33-c8bb-4541-a977-44dcc3cbe28d",
109 "vim-id": "27042672-5190-4209-b844-95bbaeea7ea7",
110 "name": "ubuntuvnf_vnfd-VM",
113 self
.assertDictEqual(vdur
, expected_vdur
)
115 @mock.patch
.object(dbmongo
.DbMongo
, "db_connect", mock
.Mock())
116 @mock.patch
.object(dbmongo
.DbMongo
, "get_one")
117 @mock.patch
.object(CommonDbClient
, "decrypt_vim_password")
118 def test_get_vim_account_default_schema(self
, decrypt_vim_password
, get_one
):
119 schema_version
= "10.0"
121 get_one
.return_value
= {
123 "vim_password": "vim_password",
124 "schema_version": schema_version
,
126 "admin_password": "admin_password",
127 "vrops_password": "vrops_password",
128 "nsx_password": "nsx_password",
129 "vcenter_password": "vcenter_password",
133 common_db_client
= CommonDbClient(self
.config
)
134 common_db_client
.get_vim_account("1")
136 decrypt_vim_password
.assert_any_call("vim_password", schema_version
, vim_id
)
137 decrypt_vim_password
.assert_any_call("vrops_password", schema_version
, vim_id
)
138 decrypt_vim_password
.assert_any_call("admin_password", schema_version
, vim_id
)
139 decrypt_vim_password
.assert_any_call("nsx_password", schema_version
, vim_id
)
140 decrypt_vim_password
.assert_any_call("vcenter_password", schema_version
, vim_id
)
142 @mock.patch
.object(dbmongo
.DbMongo
, "db_connect", mock
.Mock())
143 @mock.patch
.object(dbmongo
.DbMongo
, "get_one")
144 @mock.patch
.object(CommonDbClient
, "decrypt_vim_password")
145 def test_get_vim_account_1_1_schema(self
, decrypt_vim_password
, get_one
):
146 schema_version
= "1.1"
148 get_one
.return_value
= {
150 "vim_password": "vim_password",
151 "schema_version": schema_version
,
152 "config": {"vrops_password": "vrops_password"},
155 common_db_client
= CommonDbClient(self
.config
)
156 common_db_client
.get_vim_account("1")
158 decrypt_vim_password
.assert_any_call("vim_password", schema_version
, vim_id
)
161 decrypt_vim_password
.assert_any_call
,
167 @mock.patch
.object(dbmongo
.DbMongo
, "db_connect", mock
.Mock())
168 @mock.patch
.object(dbmongo
.DbMongo
, "get_list")
169 def test_get_alarms(self
, get_list
):
170 get_list
.return_value
= [
174 "severity": "severity",
176 "operation": "operation",
177 "statistic": "statistic",
182 common_db_client
= CommonDbClient(self
.config
)
183 alarms
= common_db_client
.get_alarms()
184 self
.assertEqual("1", alarms
[0].uuid
)
186 @mock.patch
.object(dbmongo
.DbMongo
, "db_connect", mock
.Mock())
187 @mock.patch
.object(dbmongo
.DbMongo
, "create")
188 def test_create_alarm(self
, create
):
201 common_db_client
= CommonDbClient(self
.config
)
202 common_db_client
.create_alarm(alarm
)
203 create
.assert_called_with(
209 "severity": "severity",
210 "statistic": "statistic",
212 "operation": "operation",
214 "alarm_status": "ok",
219 @mock.patch
.object(dbmongo
.DbMongo
, "db_connect", mock
.Mock())
220 @mock.patch
.object(dbmongo
.DbMongo
, "del_one")
221 def test_delete_alarm(self
, delete
):
222 common_db_client
= CommonDbClient(self
.config
)
223 common_db_client
.delete_alarm("1")
224 delete
.assert_called_with("alarms", {"uuid": "1"})