TestLinkResource.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. from only_for_platform import get_platform, not_for_platform, os_distro_value, PLATFORM_WINDOWS
  19. from ambari_commons.os_check import OSCheck
  20. from resource_management.core import Environment, Fail
  21. from resource_management.core.system import System
  22. from resource_management.core.resources.system import Link
  23. import os
  24. @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))
  25. class TestLinkResource(TestCase):
  26. @patch.object(os.path, "realpath")
  27. @patch("resource_management.core.sudo.path_lexists")
  28. @patch("resource_management.core.sudo.path_lexists")
  29. @patch("resource_management.core.sudo.unlink")
  30. @patch("resource_management.core.sudo.symlink")
  31. def test_action_create_relink(self, symlink_mock, unlink_mock,
  32. islink_mock, lexists_mock,
  33. realmock):
  34. lexists_mock.return_value = True
  35. realmock.return_value = "/old_to_link_path"
  36. islink_mock.return_value = True
  37. with Environment('/') as env:
  38. Link("/some_path",
  39. to = "/a/b/link_to_path"
  40. )
  41. unlink_mock.assert_called_with("/some_path")
  42. symlink_mock.assert_called_with("/a/b/link_to_path", "/some_path")
  43. @patch.object(os.path, "realpath")
  44. @patch("resource_management.core.sudo.path_lexists")
  45. def test_action_create_failed_due_to_file_exists(self,
  46. lexists_mock, realmock):
  47. lexists_mock.side_effect = [True, False]
  48. realmock.return_value = "/old_to_link_path"
  49. with Environment('/') as env:
  50. try:
  51. Link("/some_path",
  52. to = "/a/b/link_to_path"
  53. )
  54. self.fail("Must fail when directory or file with name /some_path exist")
  55. except Fail as e:
  56. self.assertEqual("Link['/some_path'] trying to create a symlink with the same name as an existing file or directory",
  57. str(e))
  58. @patch("resource_management.core.sudo.path_lexists")
  59. @patch("resource_management.core.sudo.symlink")
  60. def test_action_create_symlink_clean_create(self, symlink_mock, lexists_mock):
  61. lexists_mock.return_value = False
  62. with Environment('/') as env:
  63. Link("/some_path",
  64. to = "/a/b/link_to_path"
  65. )
  66. symlink_mock.assert_called_with("/a/b/link_to_path", "/some_path")
  67. @patch.object(os.path, "isdir")
  68. @patch("resource_management.core.sudo.path_exists")
  69. @patch("resource_management.core.sudo.path_lexists")
  70. @patch("resource_management.core.sudo.link")
  71. def test_action_create_hardlink_clean_create(self, link_mock, lexists_mock,
  72. exists_mock, isdir_mock):
  73. lexists_mock.return_value = False
  74. exists_mock.return_value = True
  75. isdir_mock.return_value = False
  76. with Environment('/') as env:
  77. Link("/some_path",
  78. hard = True,
  79. to = "/a/b/link_to_path"
  80. )
  81. link_mock.assert_called_with("/a/b/link_to_path", "/some_path")
  82. @patch("resource_management.core.sudo.path_exists")
  83. @patch("resource_management.core.sudo.path_lexists")
  84. def test_action_create_hardlink_target_doesnt_exist(self, lexists_mock,
  85. exists_mock):
  86. lexists_mock.return_value = False
  87. exists_mock.return_value = False
  88. with Environment('/') as env:
  89. try:
  90. Link("/some_path",
  91. hard = True,
  92. to = "/a/b/link_to_path"
  93. )
  94. self.fail("Must fail when target directory do doenst exist")
  95. except Fail as e:
  96. self.assertEqual('Failed to apply Link[\'/some_path\'], linking to nonexistent location /a/b/link_to_path',
  97. str(e))
  98. @patch("resource_management.core.sudo.path_isdir")
  99. @patch("resource_management.core.sudo.path_exists")
  100. @patch("resource_management.core.sudo.path_lexists")
  101. def test_action_create_hardlink_target_is_dir(self, lexists_mock,
  102. exists_mock, isdir_mock):
  103. lexists_mock.return_value = False
  104. exists_mock.return_value = True
  105. isdir_mock = True
  106. with Environment('/') as env:
  107. try:
  108. Link("/some_path",
  109. hard = True,
  110. to = "/a/b/link_to_path"
  111. )
  112. self.fail("Must fail when hardlinking to directory")
  113. except Fail as e:
  114. self.assertEqual('Failed to apply Link[\'/some_path\'], cannot create hard link to a directory (/a/b/link_to_path)',
  115. str(e))
  116. @patch("resource_management.core.sudo.unlink")
  117. @patch("resource_management.core.sudo.path_exists")
  118. def test_action_delete(self, exists_mock, unlink_mock):
  119. exists_mock.return_value = True
  120. with Environment('/') as env:
  121. Link("/some_path",
  122. action = "delete"
  123. )
  124. unlink_mock.assert_called_with("/some_path")