Coverage for manila/tests/api/views/test_share_networks.py: 98%

129 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2026-02-18 22:19 +0000

1# Copyright (c) 2015 Mirantis, Inc. 

2# All Rights Reserved. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); you may 

5# not use this file except in compliance with the License. You may obtain 

6# a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

13# License for the specific language governing permissions and limitations 

14# under the License. 

15 

16import copy 

17import ddt 

18import itertools 

19 

20from manila.api.openstack import api_version_request as api_version 

21from manila.api.views import share_networks 

22from manila import test 

23from manila.tests.api import fakes 

24 

25 

26@ddt.ddt 

27class ViewBuilderTestCase(test.TestCase): 

28 

29 def setUp(self): 

30 super(ViewBuilderTestCase, self).setUp() 

31 self.builder = share_networks.ViewBuilder() 

32 

33 def test__collection_name(self): 

34 self.assertEqual('share_networks', self.builder._collection_name) 

35 

36 @ddt.data(*itertools.product( 

37 [ 

38 {'id': 'fake_sn_id', 'name': 'fake_sn_name', 

39 'share_network_subnets': []}, 

40 {'id': 'fake_sn_id', 'name': 'fake_sn_name', 

41 'share_network_subnets': [], 'fake_extra_key': 'foo'}, 

42 {'id': 'fake_sn_id', 'name': 'fake_sn_name', 

43 'share_network_subnets': [ 

44 {'availability_zone_id': None, 

45 'id': 'fake', 

46 'availability_zone': None, 

47 'is_default': False 

48 }], 

49 'fake_extra_key': 'foo'}, 

50 ], 

51 ["1.0", "2.0", "2.18", "2.20", "2.25", "2.26", 

52 "2.49", api_version._MAX_API_VERSION]) 

53 ) 

54 @ddt.unpack 

55 def test_build_share_network(self, share_network_data, microversion): 

56 gateway_support = (api_version.APIVersionRequest(microversion) >= 

57 api_version.APIVersionRequest('2.18')) 

58 mtu_support = (api_version.APIVersionRequest(microversion) >= 

59 api_version.APIVersionRequest('2.20')) 

60 nova_net_support = (api_version.APIVersionRequest(microversion) < 

61 api_version.APIVersionRequest('2.26')) 

62 default_net_info_support = (api_version.APIVersionRequest(microversion) 

63 <= api_version.APIVersionRequest('2.49')) 

64 subnets_support = (api_version.APIVersionRequest(microversion) > 

65 api_version.APIVersionRequest('2.49')) 

66 status_and_sec_serv_update = ( 

67 api_version.APIVersionRequest(microversion) >= 

68 api_version.APIVersionRequest('2.63')) 

69 network_allocation_update_support = ( 

70 api_version.APIVersionRequest(microversion) >= 

71 api_version.APIVersionRequest('2.69')) 

72 req = fakes.HTTPRequest.blank('/share-networks', version=microversion) 

73 expected_keys = { 

74 'id', 'name', 'project_id', 'created_at', 'updated_at', 

75 'description'} 

76 if subnets_support: 

77 expected_keys.add('share_network_subnets') 

78 else: 

79 if default_net_info_support: 79 ↛ 84line 79 didn't jump to line 84 because the condition on line 79 was always true

80 network_info = { 

81 'neutron_net_id', 'neutron_subnet_id', 'network_type', 

82 'segmentation_id', 'cidr', 'ip_version'} 

83 expected_keys.update(network_info) 

84 if gateway_support: 

85 expected_keys.add('gateway') 

86 if mtu_support: 

87 expected_keys.add('mtu') 

88 if nova_net_support: 

89 expected_keys.add('nova_net_id') 

90 if status_and_sec_serv_update: 

91 expected_keys.update({'status', 'security_service_update_support'}) 

92 if network_allocation_update_support: 

93 expected_keys.add('network_allocation_update_support') 

94 

95 result = self.builder.build_share_network(req, share_network_data) 

96 self.assertEqual(1, len(result)) 

97 self.assertIn('share_network', result) 

98 self.assertEqual(share_network_data['id'], 

99 result['share_network']['id']) 

100 self.assertEqual(share_network_data['name'], 

101 result['share_network']['name']) 

102 self.assertEqual(len(expected_keys), 

103 len(result['share_network'])) 

104 for key in expected_keys: 

105 self.assertIn(key, result['share_network']) 

106 for key in result['share_network']: 

107 self.assertIn(key, expected_keys) 

108 

109 @ddt.data(*itertools.product( 

110 [ 

111 [], 

112 [{'id': 'fake_id', 

113 'name': 'fake_name', 

114 'project_id': 'fake_project_id', 

115 'created_at': 'fake_created_at', 

116 'updated_at': 'fake_updated_at', 

117 'neutron_net_id': 'fake_neutron_net_id', 

118 'neutron_subnet_id': 'fake_neutron_subnet_id', 

119 'network_type': 'fake_network_type', 

120 'segmentation_id': 'fake_segmentation_id', 

121 'cidr': 'fake_cidr', 

122 'ip_version': 'fake_ip_version', 

123 'description': 'fake_description'}, 

124 {'id': 'fake_id2', 

125 'name': 'fake_name2'}], 

126 ], 

127 set(["1.0", "2.0", "2.18", "2.20", "2.25", "2.26", "2.49", 

128 api_version._MAX_API_VERSION])) 

129 ) 

130 @ddt.unpack 

131 def test_build_share_networks_with_details(self, share_networks, 

132 microversion): 

133 gateway_support = (api_version.APIVersionRequest(microversion) >= 

134 api_version.APIVersionRequest('2.18')) 

135 mtu_support = (api_version.APIVersionRequest(microversion) >= 

136 api_version.APIVersionRequest('2.20')) 

137 nova_net_support = (api_version.APIVersionRequest(microversion) < 

138 api_version.APIVersionRequest('2.26')) 

139 default_net_info_support = (api_version.APIVersionRequest(microversion) 

140 <= api_version.APIVersionRequest('2.49')) 

141 subnets_support = (api_version.APIVersionRequest(microversion) > 

142 api_version.APIVersionRequest('2.49')) 

143 status_and_sec_serv_update = ( 

144 api_version.APIVersionRequest(microversion) >= 

145 api_version.APIVersionRequest('2.63')) 

146 network_allocation_update_support = ( 

147 api_version.APIVersionRequest(microversion) >= 

148 api_version.APIVersionRequest('2.69')) 

149 subnet_metadata_support = ( 

150 api_version.APIVersionRequest(microversion) >= 

151 api_version.APIVersionRequest('2.78')) 

152 

153 req = fakes.HTTPRequest.blank('/share-networks', version=microversion) 

154 expected_networks_list = [] 

155 for share_network in share_networks: 

156 expected_data = { 

157 'id': share_network.get('id'), 

158 'name': share_network.get('name'), 

159 'project_id': share_network.get('project_id'), 

160 'created_at': share_network.get('created_at'), 

161 'updated_at': share_network.get('updated_at'), 

162 'description': share_network.get('description'), 

163 } 

164 if subnets_support: 

165 expected_subnet = { 

166 'id': 'fake_subnet_id', 

167 'availability_zone': 'fake_az', 

168 'created_at': share_network.get('created_at'), 

169 'updated_at': share_network.get('updated_at'), 

170 'segmentation_id': share_network.get('segmentation_id'), 

171 'neutron_net_id': share_network.get('neutron_net_id'), 

172 'neutron_subnet_id': share_network.get( 

173 'neutron_subnet_id'), 

174 'ip_version': share_network.get('ip_version'), 

175 'cidr': share_network.get('cidr'), 

176 'network_type': share_network.get('network_type'), 

177 'mtu': share_network.get('mtu'), 

178 'gateway': share_network.get('gateway'), 

179 } 

180 subnet = expected_subnet 

181 if subnet_metadata_support: 181 ↛ 186line 181 didn't jump to line 186 because the condition on line 181 was always true

182 subnet = copy.deepcopy(expected_subnet) 

183 expected_subnet['metadata'] = {'fake_key': 'fake_value'} 

184 subnet['subnet_metadata'] = expected_subnet['metadata'] 

185 

186 expected_data.update( 

187 {'share_network_subnets': [expected_subnet]}) 

188 share_network.update({'share_network_subnets': [subnet]}) 

189 else: 

190 if default_net_info_support: 190 ↛ 202line 190 didn't jump to line 202 because the condition on line 190 was always true

191 network_data = { 

192 'neutron_net_id': share_network.get('neutron_net_id'), 

193 'neutron_subnet_id': share_network.get( 

194 'neutron_subnet_id'), 

195 'network_type': share_network.get('network_type'), 

196 'segmentation_id': share_network.get( 

197 'segmentation_id'), 

198 'cidr': share_network.get('cidr'), 

199 'ip_version': share_network.get('ip_version'), 

200 } 

201 expected_data.update(network_data) 

202 if gateway_support: 

203 share_network.update({'gateway': 'fake_gateway'}) 

204 expected_data.update({'gateway': 

205 share_network.get('gateway')}) 

206 if mtu_support: 

207 share_network.update({'mtu': 1509}) 

208 expected_data.update({'mtu': share_network.get('mtu')}) 

209 if nova_net_support: 

210 share_network.update({'nova_net_id': 'fake_nova_net_id'}) 

211 expected_data.update({'nova_net_id': None}) 

212 if status_and_sec_serv_update: 

213 share_network.update( 

214 {'status': 'active', 

215 'security_service_update_support': False}) 

216 expected_data.update( 

217 {'status': 'active', 

218 'security_service_update_support': False}) 

219 if network_allocation_update_support: 

220 share_network.update( 

221 {'network_allocation_update_support': None}) 

222 expected_data.update( 

223 {'network_allocation_update_support': None}) 

224 expected_networks_list.append(expected_data) 

225 

226 expected = {'share_networks': expected_networks_list} 

227 

228 result = self.builder.build_share_networks(req, share_networks, 

229 is_detail=True) 

230 

231 self.assertEqual(expected, result) 

232 

233 @ddt.data(*itertools.product( 

234 [ 

235 [], 

236 [{'id': 'foo', 'name': 'bar'}], 

237 [{'id': 'id1', 'name': 'name1'}, {'id': 'id2', 'name': 'name2'}], 

238 [{'id': 'id1', 'name': 'name1'}, 

239 {'id': 'id2', 'name': 'name2', 

240 'fake': 'I should not be returned'}] 

241 ], 

242 set(["1.0", "2.0", "2.18", "2.20", "2.25", "2.26", "2.49", 

243 api_version._MAX_API_VERSION])) 

244 ) 

245 @ddt.unpack 

246 def test_build_share_networks_without_details(self, share_networks, 

247 microversion): 

248 req = fakes.HTTPRequest.blank('/share-networks', version=microversion) 

249 expected = [] 

250 for share_network in share_networks: 

251 expected.append({ 

252 'id': share_network.get('id'), 

253 'name': share_network.get('name') 

254 }) 

255 expected = {'share_networks': expected} 

256 

257 result = self.builder.build_share_networks(req, share_networks, 

258 is_detail=False) 

259 

260 self.assertEqual(expected, result) 

261 

262 @ddt.data(('update_security_service', True), 

263 ('add_security_service', False)) 

264 @ddt.unpack 

265 def test_build_security_service_update_check(self, operation, is_admin): 

266 req = fakes.HTTPRequest.blank('/share-networks', 

267 use_admin_context=is_admin) 

268 params = {'new_service_id': 'new_id'} 

269 if operation == 'update_security_service': 

270 params['current_service_id'] = 'current_id' 

271 

272 hosts_result = { 

273 'compatible': True, 

274 'hosts_check_result': {'hostA': True} 

275 } 

276 expected = { 

277 'compatible': True, 

278 'requested_operation': { 

279 'operation': operation, 

280 'current_security_service': params.get('current_service_id'), 

281 'new_security_service': params.get('new_service_id'), 

282 }, 

283 } 

284 if is_admin: 

285 expected['hosts_check_result'] = hosts_result['hosts_check_result'] 

286 

287 result = self.builder.build_security_service_update_check(req, 

288 params, 

289 hosts_result) 

290 

291 self.assertEqual(expected, result) 

292 

293 @ddt.data(True, False) 

294 def test_build_share_network_subnet_create_check(self, is_admin): 

295 req = fakes.HTTPRequest.blank('/share-networks', 

296 use_admin_context=is_admin) 

297 hosts_result = { 

298 'compatible': True, 

299 'hosts_check_result': {'hostA': True} 

300 } 

301 expected = {'compatible': True} 

302 if is_admin: 

303 expected['hosts_check_result'] = hosts_result['hosts_check_result'] 

304 

305 result = self.builder.build_share_network_subnet_create_check( 

306 req, hosts_result) 

307 

308 self.assertEqual(expected, result)