test_cp.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.case import ShellCase
  14. from tests.support.paths import TMP
  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. def test_cp_testfile(self):
  25. '''
  26. test salt-cp
  27. '''
  28. minions = []
  29. for line in self.run_salt('--out yaml "*" test.ping'):
  30. if not line:
  31. continue
  32. data = salt.utils.yaml.safe_load(line)
  33. minions.extend(data.keys())
  34. self.assertNotEqual(minions, [])
  35. testfile = os.path.abspath(
  36. os.path.join(
  37. os.path.dirname(os.path.dirname(__file__)),
  38. 'files', 'file', 'base', 'testfile'
  39. )
  40. )
  41. with salt.utils.files.fopen(testfile, 'r') as fh_:
  42. testfile_contents = fh_.read()
  43. def quote(arg):
  44. if salt.utils.platform.is_windows():
  45. return arg
  46. return pipes.quote(arg)
  47. for idx, minion in enumerate(minions):
  48. if 'localhost' in minion:
  49. continue
  50. ret = self.run_salt(
  51. '--out yaml {0} file.directory_exists {1}'.format(
  52. quote(minion), TMP
  53. )
  54. )
  55. data = salt.utils.yaml.safe_load('\n'.join(ret))
  56. if data[minion] is False:
  57. ret = self.run_salt(
  58. '--out yaml {0} file.makedirs {1}'.format(
  59. quote(minion),
  60. TMP
  61. )
  62. )
  63. data = salt.utils.yaml.safe_load('\n'.join(ret))
  64. self.assertTrue(data[minion])
  65. minion_testfile = os.path.join(
  66. TMP, 'cp_{0}_testfile'.format(idx)
  67. )
  68. ret = self.run_cp('--out pprint {0} {1} {2}'.format(
  69. quote(minion),
  70. quote(testfile),
  71. quote(minion_testfile),
  72. ))
  73. data = eval('\n'.join(ret), {}, {}) # pylint: disable=eval-used
  74. for part in six.itervalues(data):
  75. key = minion_testfile
  76. self.assertTrue(part[key])
  77. ret = self.run_salt(
  78. '--out yaml {0} file.file_exists {1}'.format(
  79. quote(minion),
  80. quote(minion_testfile)
  81. )
  82. )
  83. data = salt.utils.yaml.safe_load('\n'.join(ret))
  84. self.assertTrue(data[minion])
  85. ret = self.run_salt(
  86. '--out yaml {0} file.contains {1} {2}'.format(
  87. quote(minion),
  88. quote(minion_testfile),
  89. 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),
  97. quote(minion_testfile)
  98. )
  99. )
  100. data = salt.utils.yaml.safe_load('\n'.join(ret))
  101. self.assertTrue(data[minion])