Shared filesystem management project for OpenStack.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_cifs_cmode.py 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # Copyright (c) 2015 Clinton Knight. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. # not use this file except in compliance with the License. You may obtain
  5. # a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. """
  15. Mock unit tests for the NetApp driver protocols CIFS class module.
  16. """
  17. import copy
  18. import ddt
  19. import mock
  20. from manila.common import constants
  21. from manila import exception
  22. from manila.share.drivers.netapp.dataontap.protocols import cifs_cmode
  23. from manila import test
  24. from manila.tests.share.drivers.netapp.dataontap.protocols \
  25. import fakes as fake
  26. @ddt.ddt
  27. class NetAppClusteredCIFSHelperTestCase(test.TestCase):
  28. def setUp(self):
  29. super(NetAppClusteredCIFSHelperTestCase, self).setUp()
  30. self.mock_context = mock.Mock()
  31. self.mock_client = mock.Mock()
  32. self.helper = cifs_cmode.NetAppCmodeCIFSHelper()
  33. self.helper.set_client(self.mock_client)
  34. def test_create_share(self):
  35. result = self.helper.create_share(fake.CIFS_SHARE, fake.SHARE_NAME)
  36. export_addresses = [fake.SHARE_ADDRESS_1, fake.SHARE_ADDRESS_2]
  37. export_paths = [result(address) for address in export_addresses]
  38. expected_paths = [
  39. r'\\%s\%s' % (fake.SHARE_ADDRESS_1, fake.SHARE_NAME),
  40. r'\\%s\%s' % (fake.SHARE_ADDRESS_2, fake.SHARE_NAME),
  41. ]
  42. self.assertEqual(expected_paths, export_paths)
  43. self.mock_client.create_cifs_share.assert_called_once_with(
  44. fake.SHARE_NAME)
  45. self.mock_client.remove_cifs_share_access.assert_called_once_with(
  46. fake.SHARE_NAME, 'Everyone')
  47. self.mock_client.set_volume_security_style.assert_called_once_with(
  48. fake.SHARE_NAME, security_style='ntfs')
  49. def test_delete_share(self):
  50. self.helper.delete_share(fake.CIFS_SHARE, fake.SHARE_NAME)
  51. self.mock_client.remove_cifs_share.assert_called_once_with(
  52. fake.SHARE_NAME)
  53. def test_update_access(self):
  54. mock_validate_access_rule = self.mock_object(self.helper,
  55. '_validate_access_rule')
  56. mock_get_access_rules = self.mock_object(
  57. self.helper, '_get_access_rules',
  58. mock.Mock(return_value=fake.EXISTING_CIFS_RULES))
  59. mock_handle_added_rules = self.mock_object(self.helper,
  60. '_handle_added_rules')
  61. mock_handle_ro_to_rw_rules = self.mock_object(self.helper,
  62. '_handle_ro_to_rw_rules')
  63. mock_handle_rw_to_ro_rules = self.mock_object(self.helper,
  64. '_handle_rw_to_ro_rules')
  65. mock_handle_deleted_rules = self.mock_object(self.helper,
  66. '_handle_deleted_rules')
  67. self.helper.update_access(fake.CIFS_SHARE,
  68. fake.SHARE_NAME,
  69. [fake.USER_ACCESS])
  70. new_rules = {'fake_user': constants.ACCESS_LEVEL_RW}
  71. mock_validate_access_rule.assert_called_once_with(fake.USER_ACCESS)
  72. mock_get_access_rules.assert_called_once_with(fake.CIFS_SHARE,
  73. fake.SHARE_NAME)
  74. mock_handle_added_rules.assert_called_once_with(
  75. fake.SHARE_NAME, fake.EXISTING_CIFS_RULES, new_rules)
  76. mock_handle_ro_to_rw_rules.assert_called_once_with(
  77. fake.SHARE_NAME, fake.EXISTING_CIFS_RULES, new_rules)
  78. mock_handle_rw_to_ro_rules.assert_called_once_with(
  79. fake.SHARE_NAME, fake.EXISTING_CIFS_RULES, new_rules)
  80. mock_handle_deleted_rules.assert_called_once_with(
  81. fake.SHARE_NAME, fake.EXISTING_CIFS_RULES, new_rules)
  82. def test_validate_access_rule(self):
  83. result = self.helper._validate_access_rule(fake.USER_ACCESS)
  84. self.assertIsNone(result)
  85. def test_validate_access_rule_invalid_type(self):
  86. rule = copy.copy(fake.USER_ACCESS)
  87. rule['access_type'] = 'ip'
  88. self.assertRaises(exception.InvalidShareAccess,
  89. self.helper._validate_access_rule,
  90. rule)
  91. def test_validate_access_rule_invalid_level(self):
  92. rule = copy.copy(fake.USER_ACCESS)
  93. rule['access_level'] = 'none'
  94. self.assertRaises(exception.InvalidShareAccessLevel,
  95. self.helper._validate_access_rule,
  96. rule)
  97. def test_handle_added_rules(self):
  98. self.helper._handle_added_rules(fake.SHARE_NAME,
  99. fake.EXISTING_CIFS_RULES,
  100. fake.NEW_CIFS_RULES)
  101. self.mock_client.add_cifs_share_access.assert_has_calls([
  102. mock.call(fake.SHARE_NAME, 'user5', False),
  103. mock.call(fake.SHARE_NAME, 'user6', True),
  104. ], any_order=True)
  105. def test_handle_ro_to_rw_rules(self):
  106. self.helper._handle_ro_to_rw_rules(fake.SHARE_NAME,
  107. fake.EXISTING_CIFS_RULES,
  108. fake.NEW_CIFS_RULES)
  109. self.mock_client.modify_cifs_share_access.assert_has_calls([
  110. mock.call(fake.SHARE_NAME, 'user2', False)
  111. ])
  112. def test_handle_rw_to_ro_rules(self):
  113. self.helper._handle_rw_to_ro_rules(fake.SHARE_NAME,
  114. fake.EXISTING_CIFS_RULES,
  115. fake.NEW_CIFS_RULES)
  116. self.mock_client.modify_cifs_share_access.assert_has_calls([
  117. mock.call(fake.SHARE_NAME, 'user3', True)
  118. ])
  119. def test_handle_deleted_rules(self):
  120. self.helper._handle_deleted_rules(fake.SHARE_NAME,
  121. fake.EXISTING_CIFS_RULES,
  122. fake.NEW_CIFS_RULES)
  123. self.mock_client.remove_cifs_share_access.assert_has_calls([
  124. mock.call(fake.SHARE_NAME, 'user4')
  125. ])
  126. def test_get_access_rules(self):
  127. self.mock_client.get_cifs_share_access = (
  128. mock.Mock(return_value='fake_rules'))
  129. result = self.helper._get_access_rules(fake.CIFS_SHARE,
  130. fake.SHARE_NAME)
  131. self.assertEqual('fake_rules', result)
  132. self.mock_client.get_cifs_share_access.assert_called_once_with(
  133. fake.SHARE_NAME)
  134. def test_get_target(self):
  135. target = self.helper.get_target(fake.CIFS_SHARE)
  136. self.assertEqual(fake.SHARE_ADDRESS_1, target)
  137. def test_get_target_missing_location(self):
  138. target = self.helper.get_target({'export_location': ''})
  139. self.assertEqual('', target)
  140. def test_get_share_name_for_share(self):
  141. share_name = self.helper.get_share_name_for_share(fake.CIFS_SHARE)
  142. self.assertEqual(fake.SHARE_NAME, share_name)
  143. @ddt.data(
  144. {
  145. 'location': r'\\%s\%s' % (fake.SHARE_ADDRESS_1, fake.SHARE_NAME),
  146. 'ip': fake.SHARE_ADDRESS_1,
  147. 'share_name': fake.SHARE_NAME,
  148. }, {
  149. 'location': r'//%s/%s' % (fake.SHARE_ADDRESS_1, fake.SHARE_NAME),
  150. 'ip': fake.SHARE_ADDRESS_1,
  151. 'share_name': fake.SHARE_NAME,
  152. },
  153. {'location': '', 'ip': '', 'share_name': ''},
  154. {'location': 'invalid', 'ip': '', 'share_name': ''},
  155. )
  156. @ddt.unpack
  157. def test_get_export_location(self, location, ip, share_name):
  158. share = fake.CIFS_SHARE.copy()
  159. share['export_location'] = location
  160. result_ip, result_share_name = self.helper._get_export_location(share)
  161. self.assertEqual(ip, result_ip)
  162. self.assertEqual(share_name, result_share_name)