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
= {'_id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01',
43 'projects_read': ['admin'], 'created': 1526044312.102287,
44 'modified': 1526044312.102287, 'projects_write': ['admin']
46 'vim-account-id': 'c1740601-7287-48c8-a2c9-bce8fee459eb',
47 'nsr-id-ref': '5ec3f571-d540-4cb0-9992-971d1b08312e',
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'
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')
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',
71 'projects_read': ['admin'], 'created': 1526044312.102287,
72 'modified': 1526044312.102287, 'projects_write': ['admin']
74 'vim-account-id': 'c1740601-7287-48c8-a2c9-bce8fee459eb',
75 'nsr-id-ref': '5ec3f571-d540-4cb0-9992-971d1b08312e',
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'
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')
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'
100 self
.assertDictEqual(vdur
, expected_vdur
)
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'
108 get_one
.return_value
= {
110 'vim_password': 'vim_password',
111 'schema_version': schema_version
,
113 'admin_password': 'admin_password',
114 'vrops_password': 'vrops_password',
115 'nsx_password': 'nsx_password',
116 'vcenter_password': 'vcenter_password'
120 common_db_client
= CommonDbClient(self
.config
)
121 common_db_client
.get_vim_account('1')
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
)
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'
135 get_one
.return_value
= {
137 'vim_password': 'vim_password',
138 'schema_version': schema_version
,
140 'vrops_password': 'vrops_password'
144 common_db_client
= CommonDbClient(self
.config
)
145 common_db_client
.get_vim_account('1')
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
,
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
= [{
157 'severity': 'severity',
159 'operation': 'operation',
160 'statistic': 'statistic',
164 common_db_client
= CommonDbClient(self
.config
)
165 alarms
= common_db_client
.get_alarms()
166 self
.assertEqual('1', alarms
[0].uuid
)
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', {})
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',
176 'severity': 'severity', 'statistic': 'statistic',
177 'name': 'name', 'operation': 'operation', 'uuid': '1'})
179 @mock.patch
.object(dbmongo
.DbMongo
, "db_connect", mock
.Mock())
180 @mock.patch
.object(dbmongo
.DbMongo
, "del_one")
181 def test_delete_alarm(self
, delete
):
182 common_db_client
= CommonDbClient(self
.config
)
183 common_db_client
.delete_alarm('1')
184 delete
.assert_called_with('alarms', {'uuid': '1'})