Coverage for osm_mon/tests/unit/core/test_common_db_client.py: 100%

71 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-05-06 19:04 +0000

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 

24import unittest 

25from unittest import mock 

26 

27from osm_common import dbmongo 

28 

29from osm_mon.core.common_db import CommonDbClient 

30from osm_mon.core.config import Config 

31from osm_mon.core.models import Alarm 

32 

33 

34class CommonDbClientTest(unittest.TestCase): 

35 def setUp(self): 

36 super().setUp() 

37 self.config = Config() 

38 

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", 

44 "_admin": { 

45 "projects_read": ["admin"], 

46 "created": 1526044312.102287, 

47 "modified": 1526044312.102287, 

48 "projects_write": ["admin"], 

49 }, 

50 "vim-account-id": "c1740601-7287-48c8-a2c9-bce8fee459eb", 

51 "nsr-id-ref": "5ec3f571-d540-4cb0-9992-971d1b08312e", 

52 "vdur": [ 

53 { 

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", 

59 } 

60 ], 

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", 

66 } 

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 

70 ) 

71 self.assertEqual(vim_account_id, "c1740601-7287-48c8-a2c9-bce8fee459eb") 

72 

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", 

78 "_admin": { 

79 "projects_read": ["admin"], 

80 "created": 1526044312.102287, 

81 "modified": 1526044312.102287, 

82 "projects_write": ["admin"], 

83 }, 

84 "vim-account-id": "c1740601-7287-48c8-a2c9-bce8fee459eb", 

85 "nsr-id-ref": "5ec3f571-d540-4cb0-9992-971d1b08312e", 

86 "vdur": [ 

87 { 

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", 

93 } 

94 ], 

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", 

100 } 

101 common_db_client = CommonDbClient(self.config) 

102 vdur = common_db_client.get_vdur( 

103 "5ec3f571-d540-4cb0-9992-971d1b08312e", "1", "ubuntuvnf_vnfd-VM" 

104 ) 

105 expected_vdur = { 

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", 

111 } 

112 

113 self.assertDictEqual(vdur, expected_vdur) 

114 

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" 

120 vim_id = "1" 

121 get_one.return_value = { 

122 "_id": vim_id, 

123 "vim_password": "vim_password", 

124 "schema_version": schema_version, 

125 "config": { 

126 "admin_password": "admin_password", 

127 "vrops_password": "vrops_password", 

128 "nsx_password": "nsx_password", 

129 "vcenter_password": "vcenter_password", 

130 }, 

131 } 

132 

133 common_db_client = CommonDbClient(self.config) 

134 common_db_client.get_vim_account("1") 

135 

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) 

141 

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" 

147 vim_id = "1" 

148 get_one.return_value = { 

149 "_id": vim_id, 

150 "vim_password": "vim_password", 

151 "schema_version": schema_version, 

152 "config": {"vrops_password": "vrops_password"}, 

153 } 

154 

155 common_db_client = CommonDbClient(self.config) 

156 common_db_client.get_vim_account("1") 

157 

158 decrypt_vim_password.assert_any_call("vim_password", schema_version, vim_id) 

159 self.assertRaises( 

160 AssertionError, 

161 decrypt_vim_password.assert_any_call, 

162 "vrops_password", 

163 schema_version, 

164 vim_id, 

165 ) 

166 

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 = [ 

171 { 

172 "uuid": "1", 

173 "name": "name", 

174 "severity": "severity", 

175 "threshold": 50, 

176 "operation": "operation", 

177 "statistic": "statistic", 

178 "tags": {}, 

179 } 

180 ] 

181 

182 common_db_client = CommonDbClient(self.config) 

183 alarms = common_db_client.get_alarms() 

184 self.assertEqual("1", alarms[0].uuid) 

185 

186 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock()) 

187 @mock.patch.object(dbmongo.DbMongo, "create") 

188 def test_create_alarm(self, create): 

189 alarm = Alarm( 

190 "name", 

191 "severity", 

192 50.0, 

193 "operation", 

194 "statistic", 

195 "metric", 

196 "scale_out", 

197 {}, 

198 "ok", 

199 ) 

200 alarm.uuid = "1" 

201 common_db_client = CommonDbClient(self.config) 

202 common_db_client.create_alarm(alarm) 

203 create.assert_called_with( 

204 "alarms", 

205 { 

206 "tags": {}, 

207 "threshold": 50.0, 

208 "metric": "metric", 

209 "severity": "severity", 

210 "statistic": "statistic", 

211 "name": "name", 

212 "operation": "operation", 

213 "uuid": "1", 

214 "alarm_status": "ok", 

215 "extra_labels": {}, 

216 }, 

217 ) 

218 

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"})