test_cloud.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 tempfile
  12. # Import Salt Testing libs
  13. from tests.support.unit import TestCase, skipIf
  14. from tests.support.paths import TMP, CODE_DIR
  15. # Import salt libs
  16. import salt.utils.cloud as cloud
  17. import salt.utils.platform
  18. from salt.ext import six
  19. GPG_KEYDIR = os.path.join(TMP, 'gpg-keydir')
  20. # The keyring library uses `getcwd()`, let's make sure we in a good directory
  21. # before importing keyring
  22. if not os.path.isdir(GPG_KEYDIR):
  23. os.makedirs(GPG_KEYDIR)
  24. os.chdir(GPG_KEYDIR)
  25. # Import external deps
  26. try:
  27. import keyring
  28. import keyring.backend
  29. class TestKeyring(keyring.backend.KeyringBackend):
  30. '''
  31. A test keyring which always outputs same password
  32. '''
  33. def __init__(self):
  34. self.__storage = {}
  35. def supported(self):
  36. return 0
  37. def set_password(self, servicename, username, password):
  38. self.__storage.setdefault(servicename, {}).update({username: password})
  39. return 0
  40. def get_password(self, servicename, username):
  41. return self.__storage.setdefault(servicename, {}).get(username, None)
  42. def delete_password(self, servicename, username):
  43. self.__storage.setdefault(servicename, {}).pop(username, None)
  44. return 0
  45. # set the keyring for keyring lib
  46. keyring.set_keyring(TestKeyring())
  47. HAS_KEYRING = True
  48. except ImportError:
  49. HAS_KEYRING = False
  50. os.chdir(CODE_DIR)
  51. class CloudUtilsTestCase(TestCase):
  52. def test_ssh_password_regex(self):
  53. '''Test matching ssh password patterns'''
  54. for pattern in ('Password for root@127.0.0.1:',
  55. 'root@127.0.0.1 Password:',
  56. ' Password:'):
  57. self.assertNotEqual(
  58. cloud.SSH_PASSWORD_PROMP_RE.match(pattern), None
  59. )
  60. self.assertNotEqual(
  61. cloud.SSH_PASSWORD_PROMP_RE.match(pattern.lower()), None
  62. )
  63. self.assertNotEqual(
  64. cloud.SSH_PASSWORD_PROMP_RE.match(pattern.strip()), None
  65. )
  66. self.assertNotEqual(
  67. cloud.SSH_PASSWORD_PROMP_RE.match(pattern.lower().strip()), None
  68. )
  69. @skipIf(HAS_KEYRING is False, 'The "keyring" python module is not installed')
  70. def test__save_password_in_keyring(self):
  71. '''
  72. Test storing password in the keyring
  73. '''
  74. cloud._save_password_in_keyring(
  75. 'salt.cloud.provider.test_case_provider',
  76. 'fake_username',
  77. 'fake_password_c8231'
  78. )
  79. stored_pw = keyring.get_password(
  80. 'salt.cloud.provider.test_case_provider',
  81. 'fake_username',
  82. )
  83. keyring.delete_password(
  84. 'salt.cloud.provider.test_case_provider',
  85. 'fake_username',
  86. )
  87. self.assertEqual(stored_pw, 'fake_password_c8231')
  88. @skipIf(HAS_KEYRING is False, 'The "keyring" python module is not installed')
  89. def test_retrieve_password_from_keyring(self):
  90. keyring.set_password(
  91. 'salt.cloud.provider.test_case_provider',
  92. 'fake_username',
  93. 'fake_password_c8231'
  94. )
  95. pw_in_keyring = cloud.retrieve_password_from_keyring(
  96. 'salt.cloud.provider.test_case_provider',
  97. 'fake_username')
  98. self.assertEqual(pw_in_keyring, 'fake_password_c8231')
  99. def test_sftp_file_with_content_under_python3(self):
  100. with self.assertRaises(Exception) as context:
  101. cloud.sftp_file("/tmp/test", "ТЕСТ test content")
  102. # we successful pass the place with os.write(tmpfd, ...
  103. self.assertNotEqual("a bytes-like object is required, not 'str'", six.text_type(context.exception))
  104. @skipIf(salt.utils.platform.is_windows(), 'Not applicable to Windows')
  105. def test_check_key_path_and_mode(self):
  106. with tempfile.NamedTemporaryFile() as f:
  107. key_file = f.name
  108. os.chmod(key_file, 0o644)
  109. self.assertFalse(cloud.check_key_path_and_mode('foo', key_file))
  110. os.chmod(key_file, 0o600)
  111. self.assertTrue(cloud.check_key_path_and_mode('foo', key_file))
  112. os.chmod(key_file, 0o400)
  113. self.assertTrue(cloud.check_key_path_and_mode('foo', key_file))
  114. # tmp file removed
  115. self.assertFalse(cloud.check_key_path_and_mode('foo', key_file))