1
0

test_cloud.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. tests.unit.utils.cloud_test
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. Test the salt-cloud utilities module
  7. """
  8. # Import Python libs
  9. from __future__ import absolute_import, print_function, unicode_literals
  10. import os
  11. import shutil
  12. import tempfile
  13. # Import salt libs
  14. import salt.utils.cloud as cloud
  15. import salt.utils.platform
  16. from salt.ext import six
  17. # Import Salt Testing libs
  18. from tests.support.runtests import RUNTIME_VARS
  19. from tests.support.unit import SkipTest, TestCase, skipIf
  20. class CloudUtilsTestCase(TestCase):
  21. @classmethod
  22. def setUpClass(cls):
  23. old_cwd = os.getcwd()
  24. cls.gpg_keydir = gpg_keydir = os.path.join(RUNTIME_VARS.TMP, "gpg-keydir")
  25. try:
  26. # The keyring library uses `getcwd()`, let's make sure we in a good directory
  27. # before importing keyring
  28. if not os.path.isdir(gpg_keydir):
  29. os.makedirs(gpg_keydir)
  30. os.chdir(gpg_keydir)
  31. # Late import because of the above reason
  32. import keyring
  33. import keyring.backend
  34. class CustomKeyring(keyring.backend.KeyringBackend):
  35. """
  36. A test keyring which always outputs same password
  37. """
  38. def __init__(self):
  39. self.__storage = {}
  40. def supported(self):
  41. return 0
  42. def set_password(
  43. self, servicename, username, password
  44. ): # pylint: disable=arguments-differ
  45. self.__storage.setdefault(servicename, {}).update(
  46. {username: password}
  47. )
  48. return 0
  49. def get_password(
  50. self, servicename, username
  51. ): # pylint: disable=arguments-differ
  52. return self.__storage.setdefault(servicename, {}).get(
  53. username, None
  54. )
  55. def delete_password(
  56. self, servicename, username
  57. ): # pylint: disable=arguments-differ
  58. self.__storage.setdefault(servicename, {}).pop(username, None)
  59. return 0
  60. # set the keyring for keyring lib
  61. keyring.set_keyring(CustomKeyring())
  62. except ImportError:
  63. raise SkipTest('The "keyring" python module is not installed')
  64. finally:
  65. os.chdir(old_cwd)
  66. @classmethod
  67. def tearDownClass(cls):
  68. if os.path.exists(cls.gpg_keydir):
  69. shutil.rmtree(cls.gpg_keydir)
  70. del cls.gpg_keydir
  71. def test_ssh_password_regex(self):
  72. """Test matching ssh password patterns"""
  73. for pattern in (
  74. "Password for root@127.0.0.1:",
  75. "root@127.0.0.1 Password:",
  76. " Password:",
  77. ):
  78. self.assertNotEqual(cloud.SSH_PASSWORD_PROMP_RE.match(pattern), None)
  79. self.assertNotEqual(
  80. cloud.SSH_PASSWORD_PROMP_RE.match(pattern.lower()), None
  81. )
  82. self.assertNotEqual(
  83. cloud.SSH_PASSWORD_PROMP_RE.match(pattern.strip()), None
  84. )
  85. self.assertNotEqual(
  86. cloud.SSH_PASSWORD_PROMP_RE.match(pattern.lower().strip()), None
  87. )
  88. def test__save_password_in_keyring(self):
  89. """
  90. Test storing password in the keyring
  91. """
  92. # Late import
  93. import keyring
  94. cloud._save_password_in_keyring(
  95. "salt.cloud.provider.test_case_provider",
  96. "fake_username",
  97. "fake_password_c8231",
  98. )
  99. stored_pw = keyring.get_password(
  100. "salt.cloud.provider.test_case_provider", "fake_username",
  101. )
  102. keyring.delete_password(
  103. "salt.cloud.provider.test_case_provider", "fake_username",
  104. )
  105. self.assertEqual(stored_pw, "fake_password_c8231")
  106. def test_retrieve_password_from_keyring(self):
  107. # Late import
  108. import keyring
  109. keyring.set_password(
  110. "salt.cloud.provider.test_case_provider",
  111. "fake_username",
  112. "fake_password_c8231",
  113. )
  114. pw_in_keyring = cloud.retrieve_password_from_keyring(
  115. "salt.cloud.provider.test_case_provider", "fake_username"
  116. )
  117. self.assertEqual(pw_in_keyring, "fake_password_c8231")
  118. def test_sftp_file_with_content_under_python3(self):
  119. with self.assertRaises(Exception) as context:
  120. cloud.sftp_file("/tmp/test", "ТЕСТ test content")
  121. # we successful pass the place with os.write(tmpfd, ...
  122. self.assertNotEqual(
  123. "a bytes-like object is required, not 'str'",
  124. six.text_type(context.exception),
  125. )
  126. @skipIf(salt.utils.platform.is_windows(), "Not applicable to Windows")
  127. def test_check_key_path_and_mode(self):
  128. with tempfile.NamedTemporaryFile() as f:
  129. key_file = f.name
  130. os.chmod(key_file, 0o644)
  131. self.assertFalse(cloud.check_key_path_and_mode("foo", key_file))
  132. os.chmod(key_file, 0o600)
  133. self.assertTrue(cloud.check_key_path_and_mode("foo", key_file))
  134. os.chmod(key_file, 0o400)
  135. self.assertTrue(cloud.check_key_path_and_mode("foo", key_file))
  136. # tmp file removed
  137. self.assertFalse(cloud.check_key_path_and_mode("foo", key_file))