TestFileResource.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. '''
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. '''
  16. from unittest import TestCase
  17. from mock.mock import patch, MagicMock
  18. import os
  19. import sys
  20. from resource_management.core import Environment, Fail
  21. from resource_management.core.resources import File
  22. from resource_management.core.system import System
  23. from resource_management.core import sudo
  24. import resource_management.core.providers.system
  25. import resource_management
  26. @patch.object(System, "os_family", new = 'redhat')
  27. class TestFileResource(TestCase):
  28. @patch.object(os.path, "dirname")
  29. @patch.object(os.path, "isdir")
  30. def test_action_create_dir_exist(self, isdir_mock, dirname_mock):
  31. """
  32. Tests if 'create' action fails when path is existent directory
  33. """
  34. isdir_mock.side_effect = [True, False]
  35. try:
  36. with Environment('/') as env:
  37. File('/existent_directory',
  38. action='create',
  39. mode=0777,
  40. content='file-content'
  41. )
  42. self.fail("Must fail when directory with name 'path' exist")
  43. except Fail as e:
  44. self.assertEqual("Applying File['/existent_directory'] failed, directory with name /existent_directory exists",
  45. str(e))
  46. self.assertFalse(dirname_mock.called)
  47. @patch.object(os.path, "dirname")
  48. @patch.object(os.path, "isdir")
  49. def test_action_create_parent_dir_non_exist(self, isdir_mock, dirname_mock):
  50. """
  51. Tests if 'create' action fails when parent directory of path
  52. doesn't exist
  53. """
  54. isdir_mock.side_effect = [False, False]
  55. dirname_mock.return_value = "/non_existent_directory"
  56. try:
  57. with Environment('/') as env:
  58. File('/non_existent_directory/file',
  59. action='create',
  60. mode=0777,
  61. content='file-content'
  62. )
  63. self.fail('Must fail on non existent parent directory')
  64. except Fail as e:
  65. self.assertEqual(
  66. "Applying File['/non_existent_directory/file'] failed, parent directory /non_existent_directory doesn't exist",
  67. str(e))
  68. self.assertTrue(dirname_mock.called)
  69. @patch("resource_management.core.providers.system._ensure_metadata")
  70. @patch.object(sudo, "read_file")
  71. @patch.object(sudo, "create_file")
  72. @patch.object(os.path, "exists")
  73. @patch.object(os.path, "isdir")
  74. def test_action_create_non_existent_file(self, isdir_mock, exists_mock, create_file_mock, read_file_mock, ensure_mock):
  75. """
  76. Tests if 'create' action create new non existent file and write proper data
  77. """
  78. isdir_mock.side_effect = [False, True]
  79. exists_mock.return_value = False
  80. with Environment('/') as env:
  81. File('/directory/file',
  82. action='create',
  83. mode=0777,
  84. content='file-content'
  85. )
  86. create_file_mock.assert_called_with('/directory/file', 'file-content')
  87. self.assertEqual(create_file_mock.call_count, 1)
  88. ensure_mock.assert_called()
  89. @patch("resource_management.core.providers.system._ensure_metadata")
  90. @patch.object(sudo, "read_file")
  91. @patch.object(sudo, "create_file")
  92. @patch.object(os.path, "exists")
  93. @patch.object(os.path, "isdir")
  94. def test_action_create_replace(self, isdir_mock, exists_mock, create_file_mock, read_file_mock, ensure_mock):
  95. """
  96. Tests if 'create' action rewrite existent file with new data
  97. """
  98. isdir_mock.side_effect = [False, True]
  99. exists_mock.return_value = True
  100. with Environment('/') as env:
  101. File('/directory/file',
  102. action='create',
  103. mode=0777,
  104. backup=False,
  105. content='new-content'
  106. )
  107. read_file_mock.assert_called_with('/directory/file')
  108. create_file_mock.assert_called_with('/directory/file', 'new-content')
  109. @patch.object(sudo, "unlink")
  110. @patch.object(os.path, "exists")
  111. @patch.object(os.path, "isdir")
  112. def test_action_delete_is_directory(self, isdir_mock, exist_mock, unlink_mock):
  113. """
  114. Tests if 'delete' action fails when path is directory
  115. """
  116. isdir_mock.return_value = True
  117. try:
  118. with Environment('/') as env:
  119. File('/directory/file',
  120. action='delete',
  121. mode=0777,
  122. backup=False,
  123. content='new-content'
  124. )
  125. self.fail("Should fail when deleting directory")
  126. except Fail:
  127. pass
  128. self.assertEqual(isdir_mock.call_count, 1)
  129. self.assertEqual(exist_mock.call_count, 0)
  130. self.assertEqual(unlink_mock.call_count, 0)
  131. @patch.object(sudo, "unlink")
  132. @patch.object(os.path, "exists")
  133. @patch.object(os.path, "isdir")
  134. def test_action_delete(self, isdir_mock, exist_mock, unlink_mock):
  135. """
  136. Tests if 'delete' action removes file
  137. """
  138. isdir_mock.return_value = False
  139. with Environment('/') as env:
  140. File('/directory/file',
  141. action='delete',
  142. mode=0777,
  143. backup=False,
  144. content='new-content'
  145. )
  146. self.assertEqual(isdir_mock.call_count, 1)
  147. self.assertEqual(exist_mock.call_count, 1)
  148. self.assertEqual(unlink_mock.call_count, 1)
  149. @patch.object(os.path, "isdir")
  150. def test_attribute_path(self, isdir_mock):
  151. """
  152. Tests 'path' attribute
  153. """
  154. isdir_mock.side_effect = [True, False]
  155. try:
  156. with Environment('/') as env:
  157. File('/existent_directory',
  158. action='create',
  159. mode=0777,
  160. content='file-content'
  161. )
  162. self.fail("Must fail when directory with name 'path' exist")
  163. except Fail as e:
  164. pass
  165. isdir_mock.assert_called_with('/existent_directory')
  166. @patch.object(resource_management.core.Environment, "backup_file")
  167. @patch("resource_management.core.providers.system._ensure_metadata")
  168. @patch.object(sudo, "read_file")
  169. @patch.object(sudo, "create_file")
  170. @patch.object(os.path, "exists")
  171. @patch.object(os.path, "isdir")
  172. def test_attribute_backup(self, isdir_mock, exists_mock, create_file_mock, read_file_mock, ensure_mock, backup_file_mock):
  173. """
  174. Tests 'backup' attribute
  175. """
  176. isdir_mock.side_effect = [False, True, False, True]
  177. exists_mock.return_value = True
  178. with Environment('/') as env:
  179. File('/directory/file',
  180. action='create',
  181. mode=0777,
  182. backup=False,
  183. content='new-content'
  184. )
  185. self.assertEqual(backup_file_mock.call_count, 0)
  186. with Environment('/') as env:
  187. File('/directory/file',
  188. action='create',
  189. mode=0777,
  190. backup=True,
  191. content='new-content'
  192. )
  193. self.assertEqual(backup_file_mock.call_count, 1)
  194. backup_file_mock.assert_called_with('/directory/file')
  195. @patch("resource_management.core.providers.system._ensure_metadata")
  196. @patch("__builtin__.open")
  197. @patch.object(os.path, "exists")
  198. @patch.object(os.path, "isdir")
  199. def test_attribute_replace(self, isdir_mock, exists_mock, open_mock, ensure_mock):
  200. """
  201. Tests 'replace' attribute
  202. """
  203. isdir_mock.side_effect = [False, True]
  204. old_file, new_file = MagicMock(), MagicMock()
  205. open_mock.side_effect = [old_file, new_file]
  206. old_file.read.return_value = 'old-content'
  207. exists_mock.return_value = True
  208. with Environment('/') as env:
  209. File('/directory/file',
  210. action='create',
  211. mode=0777,
  212. backup=False,
  213. content='new-content',
  214. replace=False
  215. )
  216. old_file.read.assert_called()
  217. self.assertEqual(new_file.__enter__().write.call_count, 0)
  218. ensure_mock.assert_called()
  219. self.assertEqual(open_mock.call_count, 0)
  220. @patch("resource_management.core.providers.system._coerce_uid")
  221. @patch("resource_management.core.providers.system._coerce_gid")
  222. @patch.object(sudo, "chown")
  223. @patch.object(sudo, "chmod")
  224. @patch.object(os, "stat")
  225. @patch.object(sudo, "create_file")
  226. @patch.object(os.path, "exists")
  227. @patch.object(os.path, "isdir")
  228. def test_ensure_metadata(self, isdir_mock, exists_mock, create_file_mock, stat_mock, chmod_mock, chown_mock, gid_mock,
  229. uid_mock):
  230. """
  231. Tests if _ensure_metadata changes owner, usergroup and permissions of file to proper values
  232. """
  233. isdir_mock.side_effect = [False, True, False, True]
  234. exists_mock.return_value = False
  235. class stat():
  236. def __init__(self):
  237. self.st_mode = 0666
  238. self.st_uid = 1
  239. self.st_gid = 1
  240. stat_mock.return_value = stat()
  241. gid_mock.return_value = 0
  242. uid_mock.return_value = 0
  243. with Environment('/') as env:
  244. File('/directory/file',
  245. action='create',
  246. mode=0777,
  247. content='file-content',
  248. owner='root',
  249. group='hdfs'
  250. )
  251. create_file_mock.assert_called_with('/directory/file', 'file-content')
  252. self.assertEqual(create_file_mock.call_count, 1)
  253. stat_mock.assert_called_with('/directory/file')
  254. self.assertEqual(chmod_mock.call_count, 1)
  255. self.assertEqual(chown_mock.call_count, 2)
  256. gid_mock.assert_called_once_with('hdfs')
  257. uid_mock.assert_called_once_with('root')
  258. chmod_mock.reset_mock()
  259. chown_mock.reset_mock()
  260. gid_mock.return_value = 1
  261. uid_mock.return_value = 1
  262. with Environment('/') as env:
  263. File('/directory/file',
  264. action='create',
  265. mode=0777,
  266. content='file-content',
  267. owner='root',
  268. group='hdfs'
  269. )
  270. self.assertEqual(chmod_mock.call_count, 1)
  271. self.assertEqual(chown_mock.call_count, 0)
  272. @patch("resource_management.core.providers.system._ensure_metadata")
  273. @patch("resource_management.core.providers.system.FileProvider._get_content")
  274. @patch.object(sudo, "read_file")
  275. @patch.object(sudo, "create_file")
  276. @patch.object(os.path, "exists")
  277. @patch.object(os.path, "isdir")
  278. def test_action_create_encoding(self, isdir_mock, exists_mock, create_file_mock, read_file_mock, get_content_mock ,ensure_mock):
  279. isdir_mock.side_effect = [False, True]
  280. content_mock = MagicMock()
  281. old_content_mock = MagicMock()
  282. get_content_mock.return_value = content_mock
  283. read_file_mock.return_value = old_content_mock
  284. exists_mock.return_value = True
  285. with Environment('/') as env:
  286. File('/directory/file',
  287. action='create',
  288. mode=0777,
  289. content='file-content',
  290. encoding = "UTF-8"
  291. )
  292. read_file_mock.assert_called_with('/directory/file')
  293. content_mock.encode.assert_called_with('UTF-8')
  294. old_content_mock.decode.assert_called_with('UTF-8')