Coverage for osm_mon/tests/unit/collector/vnf_collectors/test_openstack.py: 100%

70 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 

24# pylint: disable=E1101 

25 

26import datetime 

27from unittest import TestCase, mock 

28 

29import gnocchiclient 

30 

31from osm_mon.collector.vnf_collectors.openstack import GnocchiBackend 

32from osm_mon.core.config import Config 

33 

34 

35class CollectorTest(TestCase): 

36 def setUp(self): 

37 super().setUp() 

38 self.config = Config() 

39 

40 def tearDown(self): 

41 super().tearDown() 

42 

43 @mock.patch.object(GnocchiBackend, "_build_neutron_client") 

44 @mock.patch.object(GnocchiBackend, "_build_gnocchi_client") 

45 def test_collect_gnocchi_rate_instance(self, build_gnocchi_client, _): 

46 mock_gnocchi_client = mock.Mock() 

47 mock_gnocchi_client.metric = mock.Mock() 

48 mock_vim_session = mock.Mock() 

49 mock_gnocchi_client.metric.get_measures.return_value = [ 

50 ( 

51 datetime.datetime( 

52 2019, 

53 4, 

54 12, 

55 15, 

56 43, 

57 tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"), 

58 ), 

59 60.0, 

60 0.0345442539, 

61 ), 

62 ( 

63 datetime.datetime( 

64 2019, 

65 4, 

66 12, 

67 15, 

68 44, 

69 tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"), 

70 ), 

71 60.0, 

72 600000000, 

73 ), 

74 ] 

75 build_gnocchi_client.return_value = mock_gnocchi_client 

76 

77 backend = GnocchiBackend({"_id": "test_uuid"}, mock_vim_session) 

78 value = backend._collect_instance_metric("cpu", "test_resource_id") 

79 self.assertEqual(value, 1.0) 

80 mock_gnocchi_client.metric.get_measures.assert_called_once_with( 

81 "cpu", 

82 aggregation="rate:mean", 

83 start=mock.ANY, 

84 resource_id="test_resource_id", 

85 ) 

86 

87 @mock.patch.object(GnocchiBackend, "_build_neutron_client") 

88 @mock.patch.object(GnocchiBackend, "_build_gnocchi_client") 

89 def test_collect_gnocchi_non_rate_instance(self, build_gnocchi_client, _): 

90 mock_gnocchi_client = mock.Mock() 

91 mock_vim_session = mock.Mock() 

92 mock_gnocchi_client.metric.get_measures.return_value = [ 

93 ( 

94 datetime.datetime( 

95 2019, 

96 4, 

97 12, 

98 15, 

99 43, 

100 tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"), 

101 ), 

102 60.0, 

103 0.0345442539, 

104 ), 

105 ( 

106 datetime.datetime( 

107 2019, 

108 4, 

109 12, 

110 15, 

111 44, 

112 tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"), 

113 ), 

114 60.0, 

115 128, 

116 ), 

117 ] 

118 build_gnocchi_client.return_value = mock_gnocchi_client 

119 

120 backend = GnocchiBackend({"_id": "test_uuid"}, mock_vim_session) 

121 value = backend._collect_instance_metric("memory.usage", "test_resource_id") 

122 self.assertEqual(value, 128) 

123 mock_gnocchi_client.metric.get_measures.assert_called_once_with( 

124 "memory.usage", 

125 aggregation=None, 

126 start=mock.ANY, 

127 resource_id="test_resource_id", 

128 ) 

129 

130 @mock.patch.object(GnocchiBackend, "_build_neutron_client") 

131 @mock.patch.object(GnocchiBackend, "_build_gnocchi_client") 

132 def test_collect_gnocchi_no_metric(self, build_gnocchi_client, _): 

133 mock_gnocchi_client = mock.Mock() 

134 mock_vim_session = mock.Mock() 

135 mock_gnocchi_client.metric.get_measures.side_effect = ( 

136 gnocchiclient.exceptions.NotFound() 

137 ) 

138 build_gnocchi_client.return_value = mock_gnocchi_client 

139 

140 backend = GnocchiBackend({"_id": "test_uuid"}, mock_vim_session) 

141 value = backend._collect_instance_metric("memory.usage", "test_resource_id") 

142 self.assertIsNone(value) 

143 mock_gnocchi_client.metric.get_measures.assert_called_once_with( 

144 "memory.usage", 

145 aggregation=None, 

146 start=mock.ANY, 

147 resource_id="test_resource_id", 

148 ) 

149 

150 @mock.patch.object(GnocchiBackend, "_build_neutron_client") 

151 @mock.patch.object(GnocchiBackend, "_build_gnocchi_client") 

152 def test_collect_interface_all_metric( 

153 self, build_gnocchi_client, build_neutron_client 

154 ): 

155 mock_gnocchi_client = mock.Mock() 

156 mock_vim_session = mock.Mock() 

157 mock_gnocchi_client.resource.search.return_value = [ 

158 {"id": "test_id_1"}, 

159 {"id": "test_id_2"}, 

160 ] 

161 mock_gnocchi_client.metric.get_measures.return_value = [ 

162 ( 

163 datetime.datetime( 

164 2019, 

165 4, 

166 12, 

167 15, 

168 43, 

169 tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"), 

170 ), 

171 60.0, 

172 0.0345442539, 

173 ), 

174 ( 

175 datetime.datetime( 

176 2019, 

177 4, 

178 12, 

179 15, 

180 44, 

181 tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"), 

182 ), 

183 60.0, 

184 0.0333070363, 

185 ), 

186 ] 

187 

188 build_gnocchi_client.return_value = mock_gnocchi_client 

189 

190 backend = GnocchiBackend({"_id": "test_uuid"}, mock_vim_session) 

191 value = backend._collect_interface_all_metric( 

192 "packets_received", "test_resource_id" 

193 ) 

194 self.assertEqual(value, 0.0666140726) 

195 mock_gnocchi_client.metric.get_measures.assert_any_call( 

196 "packets_received", resource_id="test_id_1", limit=1 

197 ) 

198 mock_gnocchi_client.metric.get_measures.assert_any_call( 

199 "packets_received", resource_id="test_id_2", limit=1 

200 ) 

201 

202 @mock.patch.object(GnocchiBackend, "_build_neutron_client") 

203 @mock.patch.object(GnocchiBackend, "_build_gnocchi_client") 

204 def test_collect_instance_disk_metric( 

205 self, build_gnocchi_client, build_neutron_client 

206 ): 

207 mock_gnocchi_client = mock.Mock() 

208 mock_vim_session = mock.Mock() 

209 mock_gnocchi_client.resource.search.return_value = [ 

210 {"id": "test_id"}, 

211 ] 

212 mock_gnocchi_client.metric.get_measures.return_value = [ 

213 ( 

214 datetime.datetime( 

215 2019, 

216 4, 

217 12, 

218 15, 

219 43, 

220 tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"), 

221 ), 

222 60.0, 

223 0.0225808, 

224 ), 

225 ( 

226 datetime.datetime( 

227 2019, 

228 4, 

229 12, 

230 15, 

231 44, 

232 tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"), 

233 ), 

234 60.0, 

235 230848, 

236 ), 

237 ] 

238 

239 build_gnocchi_client.return_value = mock_gnocchi_client 

240 

241 backend = GnocchiBackend({"_id": "test_uuid"}, mock_vim_session) 

242 value = backend._collect_instance_disk_metric( 

243 "disk_read_bytes", "test_resource_id" 

244 ) 

245 self.assertEqual(value, 230848) 

246 mock_gnocchi_client.metric.get_measures.assert_any_call( 

247 "disk_read_bytes", resource_id="test_id", limit=1 

248 )