test_cp.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.runtime 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. log = logging.getLogger(__name__)
  23. class CopyTest(ShellCase, ShellCaseCommonTestsMixin):
  24. _call_binary_ = 'salt-cp'
  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', 'file', 'base', 'testfile'
  40. )
  41. )
  42. with salt.utils.files.fopen(testfile, 'r') as fh_:
  43. testfile_contents = fh_.read()
  44. def quote(arg):
  45. if salt.utils.platform.is_windows():
  46. return arg
  47. return pipes.quote(arg)
  48. for idx, minion in enumerate(minions):
  49. if 'localhost' in minion:
  50. continue
  51. ret = self.run_salt(
  52. '--out yaml {0} file.directory_exists {1}'.format(
  53. quote(minion), RUNTIME_VARS.TMP
  54. )
  55. )
  56. data = salt.utils.yaml.safe_load('\n'.join(ret))
  57. if data[minion] is False:
  58. ret = self.run_salt(
  59. '--out yaml {0} file.makedirs {1}'.format(
  60. quote(minion),
  61. RUNTIME_VARS.TMP
  62. )
  63. )
  64. data = salt.utils.yaml.safe_load('\n'.join(ret))
  65. self.assertTrue(data[minion])
  66. minion_testfile = os.path.join(
  67. RUNTIME_VARS.TMP, 'cp_{0}_testfile'.format(idx)
  68. )
  69. ret = self.run_cp('--out pprint {0} {1} {2}'.format(
  70. quote(minion),
  71. quote(testfile),
  72. quote(minion_testfile),
  73. ))
  74. data = eval('\n'.join(ret), {}, {}) # pylint: disable=eval-used
  75. for part in six.itervalues(data):
  76. key = minion_testfile
  77. self.assertTrue(part[key])
  78. ret = self.run_salt(
  79. '--out yaml {0} file.file_exists {1}'.format(
  80. quote(minion),
  81. quote(minion_testfile)
  82. )
  83. )
  84. data = salt.utils.yaml.safe_load('\n'.join(ret))
  85. self.assertTrue(data[minion])
  86. ret = self.run_salt(
  87. '--out yaml {0} file.contains {1} {2}'.format(
  88. quote(minion),
  89. quote(minion_testfile),
  90. quote(testfile_contents)
  91. )
  92. )
  93. data = salt.utils.yaml.safe_load('\n'.join(ret))
  94. self.assertTrue(data[minion])
  95. ret = self.run_salt(
  96. '--out yaml {0} file.remove {1}'.format(
  97. quote(minion),
  98. quote(minion_testfile)
  99. )
  100. )
  101. data = salt.utils.yaml.safe_load('\n'.join(ret))
  102. self.assertTrue(data[minion])