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
24 from unittest
import mock
26 from osm_common
import dbmongo
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
33 class CommonDbClientTest(unittest
.TestCase
):
36 self
.config
= Config()
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",
44 "projects_read": ["admin"],
45 "created": 1526044312.102287,
46 "modified": 1526044312.102287,
47 "projects_write": ["admin"],
49 "vim-account-id": "c1740601-7287-48c8-a2c9-bce8fee459eb",
50 "nsr-id-ref": "5ec3f571-d540-4cb0-9992-971d1b08312e",
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",
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",
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
70 self
.assertEqual(vim_account_id
, "c1740601-7287-48c8-a2c9-bce8fee459eb")
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",
78 "projects_read": ["admin"],
79 "created": 1526044312.102287,
80 "modified": 1526044312.102287,
81 "projects_write": ["admin"],
83 "vim-account-id": "c1740601-7287-48c8-a2c9-bce8fee459eb",
84 "nsr-id-ref": "5ec3f571-d540-4cb0-9992-971d1b08312e",
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",
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",
100 common_db_client
= CommonDbClient(self
.config
)
101 vdur
= common_db_client
.get_vdur(
102 "5ec3f571-d540-4cb0-9992-971d1b08312e", "1", "ubuntuvnf_vnfd-VM"
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",
112 self
.assertDictEqual(vdur
, expected_vdur
)
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"
120 get_one
.return_value
= {
122 "vim_password": "vim_password",
123 "schema_version": schema_version
,
125 "admin_password": "admin_password",
126 "vrops_password": "vrops_password",
127 "nsx_password": "nsx_password",
128 "vcenter_password": "vcenter_password",
132 common_db_client
= CommonDbClient(self
.config
)
133 common_db_client
.get_vim_account("1")
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
)
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"
147 get_one
.return_value
= {
149 "vim_password": "vim_password",
150 "schema_version": schema_version
,
151 "config": {"vrops_password": "vrops_password"},
154 common_db_client
= CommonDbClient(self
.config
)
155 common_db_client
.get_vim_account("1")
157 decrypt_vim_password
.assert_any_call("vim_password", schema_version
, vim_id
)
160 decrypt_vim_password
.assert_any_call
,
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
= [
173 "severity": "severity",
175 "operation": "operation",
176 "statistic": "statistic",
181 common_db_client
= CommonDbClient(self
.config
)
182 alarms
= common_db_client
.get_alarms()
183 self
.assertEqual("1", alarms
[0].uuid
)
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", {})
190 common_db_client
= CommonDbClient(self
.config
)
191 common_db_client
.create_alarm(alarm
)
192 create
.assert_called_with(
198 "severity": "severity",
199 "statistic": "statistic",
201 "operation": "operation",
206 @mock.patch
.object(dbmongo
.DbMongo
, "db_connect", mock
.Mock())
207 @mock.patch
.object(dbmongo
.DbMongo
, "del_one")
208 def test_delete_alarm(self
, delete
):
209 common_db_client
= CommonDbClient(self
.config
)
210 common_db_client
.delete_alarm("1")
211 delete
.assert_called_with("alarms", {"uuid": "1"})