test_cloud.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 Testing libs
  14. from tests.support.runtime import RUNTIME_VARS
  15. from tests.support.unit import TestCase, skipIf, SkipTest
  16. # Import salt libs
  17. import salt.utils.cloud as cloud
  18. import salt.utils.platform
  19. from salt.ext import six
  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(self, servicename, username, password):
  43. self.__storage.setdefault(servicename, {}).update({username: password})
  44. return 0
  45. def get_password(self, servicename, username):
  46. return self.__storage.setdefault(servicename, {}).get(username, None)
  47. def delete_password(self, servicename, username):
  48. self.__storage.setdefault(servicename, {}).pop(username, None)
  49. return 0
  50. # set the keyring for keyring lib
  51. keyring.set_keyring(CustomKeyring())
  52. except ImportError:
  53. raise SkipTest('The "keyring" python module is not installed')
  54. finally:
  55. os.chdir(old_cwd)
  56. @classmethod
  57. def tearDownClass(cls):
  58. if os.path.exists(cls.gpg_keydir):
  59. shutil.rmtree(cls.gpg_keydir)
  60. del cls.gpg_keydir
  61. def test_ssh_password_regex(self):
  62. '''Test matching ssh password patterns'''
  63. for pattern in ('Password for root@127.0.0.1:',
  64. 'root@127.0.0.1 Password:',
  65. ' Password:'):
  66. self.assertNotEqual(
  67. cloud.SSH_PASSWORD_PROMP_RE.match(pattern), None
  68. )
  69. self.assertNotEqual(
  70. cloud.SSH_PASSWORD_PROMP_RE.match(pattern.lower()), None
  71. )
  72. self.assertNotEqual(
  73. cloud.SSH_PASSWORD_PROMP_RE.match(pattern.strip()), None
  74. )
  75. self.assertNotEqual(
  76. cloud.SSH_PASSWORD_PROMP_RE.match(pattern.lower().strip()), None
  77. )
  78. def test__save_password_in_keyring(self):
  79. '''
  80. Test storing password in the keyring
  81. '''
  82. # Late import
  83. import keyring
  84. cloud._save_password_in_keyring(
  85. 'salt.cloud.provider.test_case_provider',
  86. 'fake_username',
  87. 'fake_password_c8231'
  88. )
  89. stored_pw = keyring.get_password(
  90. 'salt.cloud.provider.test_case_provider',
  91. 'fake_username',
  92. )
  93. keyring.delete_password(
  94. 'salt.cloud.provider.test_case_provider',
  95. 'fake_username',
  96. )
  97. self.assertEqual(stored_pw, 'fake_password_c8231')
  98. def test_retrieve_password_from_keyring(self):
  99. # Late import
  100. import keyring
  101. keyring.set_password(
  102. 'salt.cloud.provider.test_case_provider',
  103. 'fake_username',
  104. 'fake_password_c8231'
  105. )
  106. pw_in_keyring = cloud.retrieve_password_from_keyring(
  107. 'salt.cloud.provider.test_case_provider',
  108. 'fake_username')
  109. self.assertEqual(pw_in_keyring, 'fake_password_c8231')
  110. def test_sftp_file_with_content_under_python3(self):
  111. with self.assertRaises(Exception) as context:
  112. cloud.sftp_file("/tmp/test", "ТЕСТ test content")
  113. # we successful pass the place with os.write(tmpfd, ...
  114. self.assertNotEqual("a bytes-like object is required, not 'str'", six.text_type(context.exception))
  115. @skipIf(salt.utils.platform.is_windows(), 'Not applicable to Windows')
  116. def test_check_key_path_and_mode(self):
  117. with tempfile.NamedTemporaryFile() as f:
  118. key_file = f.name
  119. os.chmod(key_file, 0o644)
  120. self.assertFalse(cloud.check_key_path_and_mode('foo', key_file))
  121. os.chmod(key_file, 0o600)
  122. self.assertTrue(cloud.check_key_path_and_mode('foo', key_file))
  123. os.chmod(key_file, 0o400)
  124. self.assertTrue(cloud.check_key_path_and_mode('foo', key_file))
  125. # tmp file removed
  126. self.assertFalse(cloud.check_key_path_and_mode('foo', key_file))