1
0

test_cp.py 3.6 KB

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