1
0

test_cp.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. tests.integration.shell.cp
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. """
  7. from __future__ import absolute_import
  8. import logging
  9. import os
  10. import pipes
  11. import pytest
  12. import salt.utils.files
  13. import salt.utils.platform
  14. import salt.utils.yaml
  15. from salt.ext import six
  16. from tests.support.case import ShellCase
  17. from tests.support.helpers import slowTest
  18. from tests.support.mixins import ShellCaseCommonTestsMixin
  19. from tests.support.runtests import RUNTIME_VARS
  20. log = logging.getLogger(__name__)
  21. @pytest.mark.windows_whitelisted
  22. class CopyTest(ShellCase, ShellCaseCommonTestsMixin):
  23. _call_binary_ = "salt-cp"
  24. @slowTest
  25. def test_cp_testfile(self):
  26. """
  27. test salt-cp
  28. """
  29. minions = []
  30. for line in self.run_salt('--out yaml "*" test.ping'):
  31. if not line:
  32. continue
  33. data = salt.utils.yaml.safe_load(line)
  34. minions.extend(data.keys())
  35. self.assertNotEqual(minions, [])
  36. testfile = os.path.abspath(
  37. os.path.join(
  38. os.path.dirname(os.path.dirname(__file__)),
  39. "files",
  40. "file",
  41. "base",
  42. "testfile",
  43. )
  44. )
  45. with salt.utils.files.fopen(testfile, "r") as fh_:
  46. testfile_contents = fh_.read()
  47. def quote(arg):
  48. if salt.utils.platform.is_windows():
  49. return arg
  50. return pipes.quote(arg)
  51. for idx, minion in enumerate(minions):
  52. if "localhost" in minion:
  53. continue
  54. ret = self.run_salt(
  55. "--out yaml {0} file.directory_exists {1}".format(
  56. quote(minion), RUNTIME_VARS.TMP
  57. )
  58. )
  59. data = salt.utils.yaml.safe_load("\n".join(ret))
  60. if data[minion] is False:
  61. ret = self.run_salt(
  62. "--out yaml {0} file.makedirs {1}".format(
  63. quote(minion), RUNTIME_VARS.TMP
  64. )
  65. )
  66. data = salt.utils.yaml.safe_load("\n".join(ret))
  67. self.assertTrue(data[minion])
  68. minion_testfile = os.path.join(
  69. RUNTIME_VARS.TMP, "cp_{0}_testfile".format(idx)
  70. )
  71. ret = self.run_cp(
  72. "--out pprint {0} {1} {2}".format(
  73. quote(minion), quote(testfile), quote(minion_testfile),
  74. )
  75. )
  76. data = eval("\n".join(ret), {}, {}) # pylint: disable=eval-used
  77. for part in six.itervalues(data):
  78. key = minion_testfile
  79. self.assertTrue(part[key])
  80. ret = self.run_salt(
  81. "--out yaml {0} file.file_exists {1}".format(
  82. quote(minion), quote(minion_testfile)
  83. )
  84. )
  85. data = salt.utils.yaml.safe_load("\n".join(ret))
  86. self.assertTrue(data[minion])
  87. ret = self.run_salt(
  88. "--out yaml {0} file.contains {1} {2}".format(
  89. quote(minion), quote(minion_testfile), quote(testfile_contents)
  90. )
  91. )
  92. data = salt.utils.yaml.safe_load("\n".join(ret))
  93. self.assertTrue(data[minion])
  94. ret = self.run_salt(
  95. "--out yaml {0} file.remove {1}".format(
  96. quote(minion), quote(minion_testfile)
  97. )
  98. )
  99. data = salt.utils.yaml.safe_load("\n".join(ret))
  100. self.assertTrue(data[minion])