Coverage for manila/tests/api/v2/test_share_unmanage.py: 100%

125 statements  

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

1# Copyright 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 

16from unittest import mock 

17 

18import ddt 

19import webob 

20 

21from manila.api.v2 import share_unmanage 

22from manila.common import constants 

23from manila import exception 

24from manila import policy 

25from manila.share import api as share_api 

26from manila import test 

27from manila.tests.api.contrib import stubs 

28from manila.tests.api import fakes 

29 

30 

31@ddt.ddt 

32class ShareUnmanageTest(test.TestCase): 

33 """Share Unmanage Test.""" 

34 def setUp(self): 

35 super(ShareUnmanageTest, self).setUp() 

36 self.controller = share_unmanage.ShareUnmanageController() 

37 self.resource_name = self.controller.resource_name 

38 self.mock_object(share_api.API, 'get_all', 

39 stubs.stub_get_all_shares) 

40 self.mock_object(share_api.API, 'get', 

41 stubs.stub_share_get) 

42 self.mock_object(share_api.API, 'update', stubs.stub_share_update) 

43 self.mock_object(share_api.API, 'delete', stubs.stub_share_delete) 

44 self.mock_object(share_api.API, 'get_snapshot', 

45 stubs.stub_snapshot_get) 

46 self.share_id = 'fake' 

47 self.request = fakes.HTTPRequest.blank( 

48 '/share/%s/unmanage' % self.share_id, 

49 use_admin_context=True 

50 ) 

51 self.context = self.request.environ['manila.context'] 

52 self.mock_policy_check = self.mock_object( 

53 policy, 'check_policy', mock.Mock(return_value=True)) 

54 

55 @ddt.data(constants.STATUS_AVAILABLE, constants.STATUS_MANAGE_ERROR) 

56 def test_unmanage_share(self, status): 

57 share = dict(status=status, id='foo_id', instance={}) 

58 self.mock_object(share_api.API, 'get', mock.Mock(return_value=share)) 

59 self.mock_object(share_api.API, 'unmanage', mock.Mock()) 

60 self.mock_object( 

61 self.controller.share_api.db, 'share_snapshot_get_all_for_share', 

62 mock.Mock(return_value=[])) 

63 self.mock_object( 

64 self.controller.share_api.db, 'share_backups_get_all', 

65 mock.Mock(return_value=[])) 

66 

67 actual_result = self.controller.unmanage(self.request, share['id']) 

68 

69 self.assertEqual(202, actual_result.status_int) 

70 (self.controller.share_api.db.share_snapshot_get_all_for_share. 

71 assert_called_once_with( 

72 self.request.environ['manila.context'], share['id'])) 

73 filters = {'share_id': 'foo_id'} 

74 (self.controller.share_api.db.share_backups_get_all. 

75 assert_called_once_with( 

76 self.request.environ['manila.context'], filters)) 

77 self.controller.share_api.get.assert_called_once_with( 

78 self.request.environ['manila.context'], share['id']) 

79 share_api.API.unmanage.assert_called_once_with( 

80 self.request.environ['manila.context'], share) 

81 self.mock_policy_check.assert_called_once_with( 

82 self.context, self.resource_name, 'unmanage') 

83 

84 def test_unmanage_share_that_has_snapshots(self): 

85 share = dict(status=constants.STATUS_AVAILABLE, id='foo_id', 

86 instance={}) 

87 snapshots = ['foo', 'bar'] 

88 self.mock_object(self.controller.share_api, 'unmanage') 

89 self.mock_object( 

90 self.controller.share_api.db, 'share_snapshot_get_all_for_share', 

91 mock.Mock(return_value=snapshots)) 

92 self.mock_object( 

93 self.controller.share_api, 'get', 

94 mock.Mock(return_value=share)) 

95 

96 self.assertRaises( 

97 webob.exc.HTTPForbidden, 

98 self.controller.unmanage, self.request, share['id']) 

99 

100 self.assertFalse(self.controller.share_api.unmanage.called) 

101 (self.controller.share_api.db.share_snapshot_get_all_for_share. 

102 assert_called_once_with( 

103 self.request.environ['manila.context'], share['id'])) 

104 self.controller.share_api.get.assert_called_once_with( 

105 self.request.environ['manila.context'], share['id']) 

106 self.mock_policy_check.assert_called_once_with( 

107 self.context, self.resource_name, 'unmanage') 

108 

109 def test_unmanage_share_that_has_backups(self): 

110 share = dict(status=constants.STATUS_AVAILABLE, id='foo_id', 

111 instance={}) 

112 backups = ['foo', 'bar'] 

113 self.mock_object(self.controller.share_api, 'unmanage') 

114 self.mock_object( 

115 self.controller.share_api.db, 'share_backups_get_all', 

116 mock.Mock(return_value=backups)) 

117 self.mock_object( 

118 self.controller.share_api, 'get', 

119 mock.Mock(return_value=share)) 

120 

121 self.assertRaises( 

122 webob.exc.HTTPForbidden, 

123 self.controller.unmanage, self.request, share['id']) 

124 

125 self.assertFalse(self.controller.share_api.unmanage.called) 

126 filters = {'share_id': 'foo_id'} 

127 (self.controller.share_api.db.share_backups_get_all. 

128 assert_called_once_with( 

129 self.request.environ['manila.context'], filters)) 

130 self.controller.share_api.get.assert_called_once_with( 

131 self.request.environ['manila.context'], share['id']) 

132 self.mock_policy_check.assert_called_once_with( 

133 self.context, self.resource_name, 'unmanage') 

134 

135 def test_unmanage_share_that_has_replicas(self): 

136 share = dict(status=constants.STATUS_AVAILABLE, id='foo_id', 

137 instance={}, has_replicas=True) 

138 mock_api_unmanage = self.mock_object(self.controller.share_api, 

139 'unmanage') 

140 mock_db_snapshots_get = self.mock_object( 

141 self.controller.share_api.db, 'share_snapshot_get_all_for_share') 

142 mock_db_backups_get = self.mock_object( 

143 self.controller.share_api.db, 'share_backups_get_all') 

144 self.mock_object( 

145 self.controller.share_api, 'get', 

146 mock.Mock(return_value=share)) 

147 

148 self.assertRaises( 

149 webob.exc.HTTPConflict, 

150 self.controller.unmanage, self.request, share['id']) 

151 

152 self.assertFalse(mock_api_unmanage.called) 

153 self.assertFalse(mock_db_snapshots_get.called) 

154 self.assertFalse(mock_db_backups_get.called) 

155 self.controller.share_api.get.assert_called_once_with( 

156 self.request.environ['manila.context'], share['id']) 

157 self.mock_policy_check.assert_called_once_with( 

158 self.context, self.resource_name, 'unmanage') 

159 

160 def test_unmanage_share_that_has_been_soft_deleted(self): 

161 share = dict(status=constants.STATUS_AVAILABLE, id='foo_id', 

162 instance={}, is_soft_deleted=True) 

163 mock_api_unmanage = self.mock_object(self.controller.share_api, 

164 'unmanage') 

165 mock_db_snapshots_get = self.mock_object( 

166 self.controller.share_api.db, 'share_snapshot_get_all_for_share') 

167 self.mock_object( 

168 self.controller.share_api, 'get', 

169 mock.Mock(return_value=share)) 

170 

171 self.assertRaises( 

172 webob.exc.HTTPForbidden, 

173 self.controller.unmanage, self.request, share['id']) 

174 

175 self.assertFalse(mock_api_unmanage.called) 

176 self.assertFalse(mock_db_snapshots_get.called) 

177 self.controller.share_api.get.assert_called_once_with( 

178 self.request.environ['manila.context'], share['id']) 

179 self.mock_policy_check.assert_called_once_with( 

180 self.context, self.resource_name, 'unmanage') 

181 

182 def test_unmanage_share_based_on_share_server(self): 

183 share = dict(instance=dict(share_server_id='foo_id'), id='bar_id') 

184 self.mock_object( 

185 self.controller.share_api, 'get', 

186 mock.Mock(return_value=share)) 

187 

188 self.assertRaises( 

189 webob.exc.HTTPForbidden, 

190 self.controller.unmanage, self.request, share['id']) 

191 

192 self.controller.share_api.get.assert_called_once_with( 

193 self.request.environ['manila.context'], share['id']) 

194 self.mock_policy_check.assert_called_once_with( 

195 self.context, self.resource_name, 'unmanage') 

196 

197 @ddt.data(*constants.TRANSITIONAL_STATUSES) 

198 def test_unmanage_share_with_transitional_state(self, share_status): 

199 share = dict(status=share_status, id='foo_id', instance={}) 

200 self.mock_object( 

201 self.controller.share_api, 'get', 

202 mock.Mock(return_value=share)) 

203 

204 self.assertRaises( 

205 webob.exc.HTTPForbidden, 

206 self.controller.unmanage, self.request, share['id']) 

207 

208 self.controller.share_api.get.assert_called_once_with( 

209 self.request.environ['manila.context'], share['id']) 

210 self.mock_policy_check.assert_called_once_with( 

211 self.context, self.resource_name, 'unmanage') 

212 

213 def test_unmanage_share_not_found(self): 

214 self.mock_object(share_api.API, 'get', mock.Mock( 

215 side_effect=exception.NotFound)) 

216 self.mock_object(share_api.API, 'unmanage', mock.Mock()) 

217 

218 self.assertRaises(webob.exc.HTTPNotFound, 

219 self.controller.unmanage, 

220 self.request, self.share_id) 

221 self.mock_policy_check.assert_called_once_with( 

222 self.context, self.resource_name, 'unmanage') 

223 

224 @ddt.data(exception.InvalidShare(reason="fake"), 

225 exception.PolicyNotAuthorized(action="fake"),) 

226 def test_unmanage_share_invalid(self, side_effect): 

227 share = dict(status=constants.STATUS_AVAILABLE, id='foo_id', 

228 instance={}) 

229 self.mock_object(share_api.API, 'get', mock.Mock(return_value=share)) 

230 self.mock_object(share_api.API, 'unmanage', mock.Mock( 

231 side_effect=side_effect)) 

232 

233 self.assertRaises(webob.exc.HTTPForbidden, 

234 self.controller.unmanage, 

235 self.request, self.share_id) 

236 self.mock_policy_check.assert_called_once_with( 

237 self.context, self.resource_name, 'unmanage') 

238 

239 def test_unmanage_allow_dhss_true_with_share_server(self): 

240 share = { 

241 'status': constants.STATUS_AVAILABLE, 

242 'id': 'foo_id', 

243 'instance': '', 

244 'share_server_id': 'fake' 

245 } 

246 self.mock_object(share_api.API, 'get', mock.Mock(return_value=share)) 

247 self.mock_object(share_api.API, 'unmanage', mock.Mock()) 

248 self.mock_object( 

249 self.controller.share_api.db, 'share_snapshot_get_all_for_share', 

250 mock.Mock(return_value=[])) 

251 

252 actual_result = self.controller._unmanage(self.request, share['id'], 

253 allow_dhss_true=True) 

254 

255 self.assertEqual(202, actual_result.status_int) 

256 self.mock_policy_check.assert_called_once_with( 

257 self.context, self.resource_name, 'unmanage') 

258 

259 def test_wrong_permissions(self): 

260 share_id = 'fake' 

261 req = fakes.HTTPRequest.blank('/share/%s/unmanage' % share_id, 

262 use_admin_context=False) 

263 req_context = req.environ['manila.context'] 

264 

265 self.assertRaises(webob.exc.HTTPForbidden, 

266 self.controller.unmanage, 

267 req, 

268 share_id) 

269 self.mock_policy_check.assert_called_once_with( 

270 req_context, self.resource_name, 'unmanage')