test_cp.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: jmoney <justin@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import salt.modules.cp as cp
  8. import salt.transport.client
  9. # Import Salt Libs
  10. import salt.utils.files
  11. import salt.utils.platform
  12. import salt.utils.templates as templates
  13. from salt.exceptions import CommandExecutionError
  14. # Import Salt Testing Libs
  15. from tests.support.mixins import LoaderModuleMockMixin
  16. from tests.support.mock import MagicMock, Mock, mock_open, patch
  17. from tests.support.unit import TestCase
  18. class CpTestCase(TestCase, LoaderModuleMockMixin):
  19. """
  20. TestCase for salt.modules.cp module
  21. """
  22. def setup_loader_modules(self):
  23. return {cp: {}}
  24. def test__render_filenames_undefined_template(self):
  25. """
  26. Test if _render_filenames fails upon getting a template not in
  27. TEMPLATE_REGISTRY.
  28. """
  29. path = "/srv/salt/saltines"
  30. dest = "/srv/salt/cheese"
  31. saltenv = "base"
  32. template = "biscuits"
  33. ret = (path, dest)
  34. self.assertRaises(
  35. CommandExecutionError, cp._render_filenames, path, dest, saltenv, template
  36. )
  37. def test__render_filenames_render_failed(self):
  38. """
  39. Test if _render_filenames fails when template rendering fails.
  40. """
  41. path = "salt://saltines"
  42. dest = "/srv/salt/cheese"
  43. saltenv = "base"
  44. template = "jinja"
  45. file_data = "Remember to keep your files well salted."
  46. mock_jinja = lambda *args, **kwargs: {"result": False, "data": file_data}
  47. with patch.dict(templates.TEMPLATE_REGISTRY, {"jinja": mock_jinja}):
  48. with patch("salt.utils.files.fopen", mock_open(read_data=file_data)):
  49. self.assertRaises(
  50. CommandExecutionError,
  51. cp._render_filenames,
  52. path,
  53. dest,
  54. saltenv,
  55. template,
  56. )
  57. def test__render_filenames_success(self):
  58. """
  59. Test if _render_filenames succeeds.
  60. """
  61. path = "salt://saltines"
  62. dest = "/srv/salt/cheese"
  63. saltenv = "base"
  64. template = "jinja"
  65. file_data = "/srv/salt/biscuits"
  66. mock_jinja = lambda *args, **kwargs: {"result": True, "data": file_data}
  67. ret = (file_data, file_data) # salt.utils.files.fopen can only be mocked once
  68. with patch.dict(templates.TEMPLATE_REGISTRY, {"jinja": mock_jinja}):
  69. with patch("salt.utils.files.fopen", mock_open(read_data=file_data)):
  70. self.assertEqual(
  71. cp._render_filenames(path, dest, saltenv, template), ret
  72. )
  73. def test_get_file_not_found(self):
  74. """
  75. Test if get_file can't find the file.
  76. """
  77. with patch("salt.modules.cp.hash_file", MagicMock(return_value=False)):
  78. path = "salt://saltines"
  79. dest = "/srv/salt/cheese"
  80. ret = ""
  81. self.assertEqual(cp.get_file(path, dest), ret)
  82. def test_get_file_str_success(self):
  83. """
  84. Test if get_file_str succeeds.
  85. """
  86. path = "salt://saltines"
  87. dest = "/srv/salt/cheese/saltines"
  88. file_data = "Remember to keep your files well salted."
  89. saltenv = "base"
  90. ret = file_data
  91. with patch("salt.utils.files.fopen", mock_open(read_data=file_data)):
  92. with patch("salt.modules.cp.cache_file", MagicMock(return_value=dest)):
  93. self.assertEqual(cp.get_file_str(path, dest), ret)
  94. def test_push_non_absolute_path(self):
  95. """
  96. Test if push fails on a non absolute path.
  97. """
  98. path = "../saltines"
  99. ret = False
  100. self.assertEqual(cp.push(path), ret)
  101. def test_push_dir_non_absolute_path(self):
  102. """
  103. Test if push_dir fails on a non absolute path.
  104. """
  105. path = "../saltines"
  106. ret = False
  107. self.assertEqual(cp.push_dir(path), ret)
  108. def test_push(self):
  109. """
  110. Test if push works with good posix path.
  111. """
  112. filename = "/saltines/test.file"
  113. if salt.utils.platform.is_windows():
  114. filename = "c:\\saltines\\test.file"
  115. with patch(
  116. "salt.modules.cp.os.path",
  117. MagicMock(isfile=Mock(return_value=True), wraps=cp.os.path),
  118. ), patch(
  119. "salt.modules.cp.os.path",
  120. MagicMock(getsize=MagicMock(return_value=10), wraps=cp.os.path),
  121. ), patch.multiple(
  122. "salt.modules.cp",
  123. _auth=MagicMock(**{"return_value.gen_token.return_value": "token"}),
  124. __opts__={"id": "abc", "file_buffer_size": 10},
  125. ), patch(
  126. "salt.utils.files.fopen", mock_open(read_data=b"content")
  127. ) as m_open, patch(
  128. "salt.transport.client.ReqChannel.factory", MagicMock()
  129. ) as req_channel_factory_mock:
  130. response = cp.push(filename)
  131. assert response, response
  132. num_opens = len(m_open.filehandles[filename])
  133. assert num_opens == 1, num_opens
  134. fh_ = m_open.filehandles[filename][0]
  135. assert fh_.read.call_count == 2, fh_.read.call_count
  136. req_channel_factory_mock().__enter__().send.assert_called_once_with(
  137. dict(
  138. loc=fh_.tell(), # pylint: disable=resource-leakage
  139. cmd="_file_recv",
  140. tok="token",
  141. path=["saltines", "test.file"],
  142. size=10,
  143. data=b"", # data is empty here because load['data'] is overwritten
  144. id="abc",
  145. )
  146. )