Coverage for manila/tests/fake_share.py: 95%

99 statements  

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

1# Copyright 2013 OpenStack Foundation 

2# Copyright 2015 Intel, Inc. 

3# All Rights Reserved 

4# 

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

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

7# a copy of the License at 

8# 

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

10# 

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

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

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

14# License for the specific language governing permissions and limitations 

15# under the License. 

16import datetime 

17 

18from manila.api.openstack import api_version_request as api_version 

19from manila.common import constants 

20from manila.db.sqlalchemy import models 

21from manila.tests.db import fakes as db_fakes 

22from oslo_utils import uuidutils 

23 

24 

25def fake_share(**kwargs): 

26 

27 share = { 

28 'id': 'fakeid', 

29 'name': 'fakename', 

30 'size': 1, 

31 'share_proto': 'fake_proto', 

32 'share_network_id': 'fake share network id', 

33 'share_server_id': 'fake share server id', 

34 'export_location': 'fake_location:/fake_share', 

35 'project_id': 'fake_project_uuid', 

36 'availability_zone': 'fake_az', 

37 'snapshot_support': 'True', 

38 'replication_type': None, 

39 'is_busy': False, 

40 'share_group_id': None, 

41 'instance': { 

42 'id': 'fake_share_instance_id', 

43 'host': 'fakehost', 

44 'share_type_id': '1', 

45 'share_network_id': 'fake share network id', 

46 }, 

47 'mount_snapshot_support': False, 

48 } 

49 share.update(kwargs) 

50 return db_fakes.FakeModel(share) 

51 

52 

53def fake_share_instance(base_share=None, **kwargs): 

54 if base_share is None: 

55 share = fake_share() 

56 else: 

57 share = base_share 

58 

59 share_instance = { 

60 'share_id': share['id'], 

61 'id': "fakeinstanceid", 

62 'status': "active", 

63 'host': 'fakehost', 

64 'share_network_id': 'fakesharenetworkid', 

65 'share_server_id': 'fakeshareserverid', 

66 'share_type_id': '1', 

67 'mount_point_name': None, 

68 } 

69 

70 for attr in models.ShareInstance._proxified_properties: 

71 share_instance[attr] = getattr(share, attr, None) 

72 

73 share_instance.update(kwargs) 

74 

75 return db_fakes.FakeModel(share_instance) 

76 

77 

78def fake_share_type(**kwargs): 

79 

80 share_type = { 

81 'id': "fakesharetype", 

82 'name': "fakesharetypename", 

83 'is_public': False, 

84 'extra_specs': { 

85 'driver_handles_share_servers': 'False', 

86 }, 

87 'mount_point_name_support': False 

88 } 

89 

90 extra_specs = kwargs.pop('extra_specs', {}) 

91 

92 for key, value in extra_specs.items(): 

93 share_type['extra_specs'][key] = value 

94 

95 share_type.update(kwargs) 

96 

97 return db_fakes.FakeModel(share_type) 

98 

99 

100def fake_snapshot(create_instance=False, **kwargs): 

101 

102 instance_keys = ('instance_id', 'snapshot_id', 'share_instance_id', 

103 'status', 'progress', 'provider_location') 

104 snapshot_keys = ('id', 'share_name', 'share_id', 'name', 'share_size', 

105 'share_proto', 'instance', 'aggregate_status', 'share', 

106 'project_id', 'size') 

107 

108 instance_kwargs = {k: kwargs.get(k) for k in instance_keys if k in kwargs} 

109 snapshot_kwargs = {k: kwargs.get(k) for k in snapshot_keys if k in kwargs} 

110 

111 aggregate_status = snapshot_kwargs.get( 

112 'aggregate_status', instance_kwargs.get( 

113 'status', constants.STATUS_CREATING)) 

114 

115 snapshot = { 

116 'id': 'fakesnapshotid', 

117 'share_name': 'fakename', 

118 'share_id': 'fakeid', 

119 'name': 'fakesnapshotname', 

120 'share_size': 1, 

121 'share_proto': 'fake_proto', 

122 'instance': {}, 

123 'share': 'fake_share', 

124 'aggregate_status': aggregate_status, 

125 'project_id': 'fakeprojectid', 

126 'size': 1, 

127 'user_id': 'xyzzy', 

128 } 

129 snapshot.update(snapshot_kwargs) 

130 if create_instance: 

131 if 'instance_id' in instance_kwargs: 131 ↛ 132line 131 didn't jump to line 132 because the condition on line 131 was never true

132 instance_kwargs['id'] = instance_kwargs.pop('instance_id') 

133 snapshot['instance'] = fake_snapshot_instance( 

134 base_snapshot=snapshot, **instance_kwargs) 

135 snapshot['status'] = snapshot['instance']['status'] 

136 snapshot['provider_location'] = ( 

137 snapshot['instance']['provider_location'] 

138 ) 

139 snapshot['progress'] = snapshot['instance']['progress'] 

140 snapshot['instances'] = snapshot['instance'], 

141 else: 

142 snapshot['status'] = constants.STATUS_AVAILABLE 

143 snapshot['progress'] = '0%' 

144 snapshot['provider_location'] = 'fake' 

145 snapshot.update(instance_kwargs) 

146 

147 return db_fakes.FakeModel(snapshot) 

148 

149 

150def fake_snapshot_instance(base_snapshot=None, as_primitive=False, **kwargs): 

151 if base_snapshot is None: 

152 base_snapshot = fake_snapshot() 

153 snapshot_instance = { 

154 'id': 'fakesnapshotinstanceid', 

155 'snapshot_id': base_snapshot['id'], 

156 'status': constants.STATUS_CREATING, 

157 'progress': '0%', 

158 'provider_location': 'i_live_here_actually', 

159 'share_name': 'fakename', 

160 'share_id': 'fakeshareinstanceid', 

161 'share_instance': { 

162 'share_id': 'fakeshareid', 

163 'share_type_id': '1', 

164 }, 

165 'share_instance_id': 'fakeshareinstanceid', 

166 'deleted': False, 

167 'updated_at': datetime.datetime(2016, 3, 21, 0, 5, 58), 

168 'created_at': datetime.datetime(2016, 3, 21, 0, 5, 58), 

169 'deleted_at': None, 

170 'share': fake_share(), 

171 } 

172 snapshot_instance.update(kwargs) 

173 if as_primitive: 

174 return snapshot_instance 

175 else: 

176 return db_fakes.FakeModel(snapshot_instance) 

177 

178 

179def expected_snapshot(version=None, id='fake_snapshot_id', **kwargs): 

180 api_major_version = 'v2' if version and version.startswith('2.') else 'v1' 

181 self_link = 'http://localhost/share/%s/fake/snapshots/%s' % ( 

182 api_major_version, id) 

183 bookmark_link = 'http://localhost/share/fake/snapshots/%s' % id 

184 snapshot = { 

185 'id': id, 

186 'share_id': 'fakeshareid', 

187 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1), 

188 'status': 'fakesnapstatus', 

189 'name': 'displaysnapname', 

190 'description': 'displaysnapdesc', 

191 'share_size': 1, 

192 'size': 1, 

193 'share_proto': 'fakesnapproto', 

194 'links': [ 

195 { 

196 'href': self_link, 

197 'rel': 'self', 

198 }, 

199 { 

200 'href': bookmark_link, 

201 'rel': 'bookmark', 

202 }, 

203 ], 

204 } 

205 

206 if version and (api_version.APIVersionRequest(version) 

207 >= api_version.APIVersionRequest('2.17')): 

208 snapshot.update({ 

209 'user_id': 'fakesnapuser', 

210 'project_id': 'fakesnapproject', 

211 }) 

212 if version and (api_version.APIVersionRequest(version) 

213 >= api_version.APIVersionRequest('2.73')): 

214 snapshot.update({ 

215 'metadata': {} 

216 }) 

217 snapshot.update(kwargs) 

218 return {'snapshot': snapshot} 

219 

220 

221def search_opts(**kwargs): 

222 search_opts = { 

223 'name': 'fake_name', 

224 'status': 'fake_status', 

225 'share_id': 'fake_share_id', 

226 'sort_key': 'fake_sort_key', 

227 'sort_dir': 'fake_sort_dir', 

228 'offset': '1', 

229 'limit': '1', 

230 } 

231 search_opts.update(kwargs) 

232 return search_opts 

233 

234 

235def fake_access(**kwargs): 

236 access = { 

237 'id': 'fakeaccid', 

238 'access_type': 'ip', 

239 'access_to': '10.0.0.1', 

240 'access_level': 'rw', 

241 'state': 'active', 

242 } 

243 access.update(kwargs) 

244 return db_fakes.FakeModel(access) 

245 

246 

247def fake_replica(id=None, as_primitive=True, for_manager=False, **kwargs): 

248 replica = { 

249 'id': id or uuidutils.generate_uuid(), 

250 'share_id': 'f0e4bb5e-65f0-11e5-9d70-feff819cdc9f', 

251 'deleted': False, 

252 'host': 'openstack@BackendZ#PoolA', 

253 'status': 'available', 

254 'scheduled_at': datetime.datetime(2015, 8, 10, 0, 5, 58), 

255 'launched_at': datetime.datetime(2015, 8, 10, 0, 5, 58), 

256 'terminated_at': None, 

257 'replica_state': None, 

258 'availability_zone_id': 'f6e146d0-65f0-11e5-9d70-feff819cdc9f', 

259 'export_locations': [{'path': 'path1'}, {'path': 'path2'}], 

260 'share_network_id': '4ccd5318-65f1-11e5-9d70-feff819cdc9f', 

261 'share_server_id': '53099868-65f1-11e5-9d70-feff819cdc9f', 

262 'access_rules_status': constants.SHARE_INSTANCE_RULES_SYNCING, 

263 } 

264 if for_manager: 

265 replica.update({ 

266 'user_id': None, 

267 'project_id': None, 

268 'share_type_id': None, 

269 'size': None, 

270 'display_name': None, 

271 'display_description': None, 

272 'replication_type': None, 

273 'snapshot_id': None, 

274 'share_proto': None, 

275 'is_public': None, 

276 'share_group_id': None, 

277 'source_share_group_snapshot_member_id': None, 

278 'availability_zone': 'fake_az', 

279 }) 

280 replica.update(kwargs) 

281 if as_primitive: 

282 return replica 

283 else: 

284 return db_fakes.FakeModel(replica) 

285 

286 

287def fake_replica_request_spec(as_primitive=True, **kwargs): 

288 replica = fake_replica(id='9c0db763-a109-4862-b010-10f2bd395295') 

289 all_replica_hosts = ','.join(['fake_active_replica_host', replica['host']]) 

290 request_spec = { 

291 'share_properties': fake_share( 

292 id='f0e4bb5e-65f0-11e5-9d70-feff819cdc9f'), 

293 'share_instance_properties': replica, 

294 'share_proto': 'nfs', 

295 'share_id': 'f0e4bb5e-65f0-11e5-9d70-feff819cdc9f', 

296 'snapshot_id': None, 

297 'share_type': 'fake_share_type', 

298 'share_group': None, 

299 'active_replica_host': 'fake_active_replica_host', 

300 'all_replica_hosts': all_replica_hosts, 

301 } 

302 request_spec.update(kwargs) 

303 if as_primitive: 303 ↛ 306line 303 didn't jump to line 306 because the condition on line 303 was always true

304 return request_spec 

305 else: 

306 return db_fakes.FakeModel(request_spec) 

307 

308 

309def fake_share_server_get(): 

310 fake_share_server = { 

311 'status': constants.STATUS_ACTIVE, 

312 'updated_at': None, 

313 'host': 'fake_host', 

314 'share_network_name': 'fake_sn_name', 

315 'project_id': 'fake_project_id', 

316 'id': 'fake_share_server_id', 

317 'backend_details': { 

318 'security_service_active_directory': '{"name": "fake_AD"}', 

319 'security_service_ldap': '{"name": "fake_LDAP"}', 

320 'security_service_kerberos': '{"name": "fake_kerberos"}', 

321 } 

322 } 

323 return fake_share_server 

324 

325 

326def fake_backup(as_primitive=True, **kwargs): 

327 backup = { 

328 'id': uuidutils.generate_uuid(), 

329 'host': "fake_host", 

330 'user_id': 'fake', 

331 'project_id': 'fake', 

332 'availability_zone': 'fake_availability_zone', 

333 'status': constants.STATUS_CREATING, 

334 'progress': '0', 

335 'restore_progress': '0', 

336 'topic': 'fake_topic', 

337 'share_id': uuidutils.generate_uuid(), 

338 'display_name': 'fake_name', 

339 'display_description': 'fake_description', 

340 'size': '1', 

341 } 

342 backup.update(kwargs) 

343 if as_primitive: 343 ↛ 346line 343 didn't jump to line 346 because the condition on line 343 was always true

344 return backup 

345 else: 

346 return db_fakes.FakeModel(backup)