Revert "Migrates alarms to MongoDB"
[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
31
32 class CommonDbClientTest(unittest.TestCase):
33 def setUp(self):
34 super().setUp()
35 self.config = Config()
36
37 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
38 @mock.patch.object(CommonDbClient, "get_vnfr")
39 def test_get_vim_id(self, get_vnfr):
40 get_vnfr.return_value = {'_id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01',
41 '_admin': {
42 'projects_read': ['admin'], 'created': 1526044312.102287,
43 'modified': 1526044312.102287, 'projects_write': ['admin']
44 },
45 'vim-account-id': 'c1740601-7287-48c8-a2c9-bce8fee459eb',
46 'nsr-id-ref': '5ec3f571-d540-4cb0-9992-971d1b08312e',
47 'vdur': [
48 {
49 'internal-connection-point': [],
50 'vdu-id-ref': 'ubuntuvnf_vnfd-VM',
51 'id': 'ffd73f33-c8bb-4541-a977-44dcc3cbe28d',
52 'vim-id': '27042672-5190-4209-b844-95bbaeea7ea7',
53 'name': 'ubuntuvnf_vnfd-VM'
54 }
55 ],
56 'vnfd-ref': 'ubuntuvnf_vnfd',
57 'member-vnf-index-ref': '1',
58 'created-time': 1526044312.0999322,
59 'vnfd-id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01',
60 'id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01'}
61 common_db_client = CommonDbClient(self.config)
62 vim_account_id = common_db_client.get_vim_account_id('5ec3f571-d540-4cb0-9992-971d1b08312e', 1)
63 self.assertEqual(vim_account_id, 'c1740601-7287-48c8-a2c9-bce8fee459eb')
64
65 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
66 @mock.patch.object(dbmongo.DbMongo, "get_one")
67 def test_get_vdur(self, get_one):
68 get_one.return_value = {'_id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01',
69 '_admin': {
70 'projects_read': ['admin'], 'created': 1526044312.102287,
71 'modified': 1526044312.102287, 'projects_write': ['admin']
72 },
73 'vim-account-id': 'c1740601-7287-48c8-a2c9-bce8fee459eb',
74 'nsr-id-ref': '5ec3f571-d540-4cb0-9992-971d1b08312e',
75 'vdur': [
76 {
77 'internal-connection-point': [],
78 'vdu-id-ref': 'ubuntuvnf_vnfd-VM',
79 'id': 'ffd73f33-c8bb-4541-a977-44dcc3cbe28d',
80 'vim-id': '27042672-5190-4209-b844-95bbaeea7ea7',
81 'name': 'ubuntuvnf_vnfd-VM'
82 }
83 ],
84 'vnfd-ref': 'ubuntuvnf_vnfd',
85 'member-vnf-index-ref': '1',
86 'created-time': 1526044312.0999322,
87 'vnfd-id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01',
88 'id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01'}
89 common_db_client = CommonDbClient(self.config)
90 vdur = common_db_client.get_vdur('5ec3f571-d540-4cb0-9992-971d1b08312e', '1', 'ubuntuvnf_vnfd-VM')
91 expected_vdur = {
92 'internal-connection-point': [],
93 'vdu-id-ref': 'ubuntuvnf_vnfd-VM',
94 'id': 'ffd73f33-c8bb-4541-a977-44dcc3cbe28d',
95 'vim-id': '27042672-5190-4209-b844-95bbaeea7ea7',
96 'name': 'ubuntuvnf_vnfd-VM'
97 }
98
99 self.assertDictEqual(vdur, expected_vdur)
100
101 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
102 @mock.patch.object(dbmongo.DbMongo, "get_one")
103 @mock.patch.object(CommonDbClient, "decrypt_vim_password")
104 def test_get_vim_account_default_schema(self, decrypt_vim_password, get_one):
105 schema_version = '10.0'
106 vim_id = '1'
107 get_one.return_value = {
108 '_id': vim_id,
109 'vim_password': 'vim_password',
110 'schema_version': schema_version,
111 'config': {
112 'admin_password': 'admin_password',
113 'vrops_password': 'vrops_password',
114 'nsx_password': 'nsx_password',
115 'vcenter_password': 'vcenter_password'
116 }
117 }
118
119 common_db_client = CommonDbClient(self.config)
120 common_db_client.get_vim_account('1')
121
122 decrypt_vim_password.assert_any_call('vim_password', schema_version, vim_id)
123 decrypt_vim_password.assert_any_call('vrops_password', schema_version, vim_id)
124 decrypt_vim_password.assert_any_call('admin_password', schema_version, vim_id)
125 decrypt_vim_password.assert_any_call('nsx_password', schema_version, vim_id)
126 decrypt_vim_password.assert_any_call('vcenter_password', schema_version, vim_id)
127
128 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
129 @mock.patch.object(dbmongo.DbMongo, "get_one")
130 @mock.patch.object(CommonDbClient, "decrypt_vim_password")
131 def test_get_vim_account_1_1_schema(self, decrypt_vim_password, get_one):
132 schema_version = '1.1'
133 vim_id = '1'
134 get_one.return_value = {
135 '_id': vim_id,
136 'vim_password': 'vim_password',
137 'schema_version': schema_version,
138 'config': {
139 'vrops_password': 'vrops_password'
140 }
141 }
142
143 common_db_client = CommonDbClient(self.config)
144 common_db_client.get_vim_account('1')
145
146 decrypt_vim_password.assert_any_call('vim_password', schema_version, vim_id)
147 self.assertRaises(AssertionError, decrypt_vim_password.assert_any_call, 'vrops_password', schema_version,
148 vim_id)