test_ec2.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import os
  5. import tempfile
  6. # Import Salt Libs
  7. from salt.cloud.clouds import ec2
  8. from salt.exceptions import SaltCloudSystemExit
  9. import salt.utils.files
  10. # Import Salt Testing Libs
  11. from tests.support.unit import TestCase, skipIf
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, PropertyMock
  14. from tests.support.paths import TMP
  15. from tests.unit.test_crypt import PRIVKEY_DATA
  16. PASS_DATA = (
  17. b'qOjCKDlBdcNEbJ/J8eRl7sH+bYIIm4cvHHY86gh2NEUnufFlFo0gGVTZR05Fj0cw3n/w7gR'
  18. b'urNXz5JoeSIHVuNI3YTwzL9yEAaC0kuy8EbOlO2yx8yPGdfml9BRwOV7A6b8UFo9co4H7fz'
  19. b'DdScMKU2yzvRYvp6N6Q2cJGBmPsemnXWWusb+1vZVWxcRAQmG3ogF6Z5rZSYAYH0N4rqJgH'
  20. b'mQfzuyb+jrBvV/IOoV1EdO9jGSH9338aS47NjrmNEN/SpnS6eCWZUwwyHbPASuOvWiY4QH/'
  21. b'0YZC6EGccwiUmt0ZOxIynk+tEyVPTkiS0V8RcZK6YKqMWHpKmPtLBzfuoA=='
  22. )
  23. @skipIf(NO_MOCK, NO_MOCK_REASON)
  24. class EC2TestCase(TestCase, LoaderModuleMockMixin):
  25. '''
  26. Unit TestCase for salt.cloud.clouds.ec2 module.
  27. '''
  28. def setUp(self):
  29. super(EC2TestCase, self).setUp()
  30. with tempfile.NamedTemporaryFile(dir=TMP, suffix='.pem', delete=True) as fp:
  31. self.key_file = fp.name
  32. def tearDown(self):
  33. super(EC2TestCase, self).tearDown()
  34. if os.path.exists(self.key_file):
  35. os.remove(self.key_file)
  36. def setup_loader_modules(self):
  37. return {ec2: {'__opts__': {}}}
  38. def test__validate_key_path_and_mode(self):
  39. # Key file exists
  40. with patch('os.path.exists', return_value=True):
  41. with patch('os.stat') as patched_stat:
  42. type(patched_stat.return_value).st_mode = PropertyMock(return_value=0o644)
  43. self.assertRaises(
  44. SaltCloudSystemExit, ec2._validate_key_path_and_mode, 'key_file')
  45. type(patched_stat.return_value).st_mode = PropertyMock(return_value=0o600)
  46. self.assertTrue(ec2._validate_key_path_and_mode('key_file'))
  47. type(patched_stat.return_value).st_mode = PropertyMock(return_value=0o400)
  48. self.assertTrue(ec2._validate_key_path_and_mode('key_file'))
  49. # Key file does not exist
  50. with patch('os.path.exists', return_value=False):
  51. self.assertRaises(
  52. SaltCloudSystemExit, ec2._validate_key_path_and_mode, 'key_file')
  53. @skipIf(not ec2.HAS_M2 and not ec2.HAS_PYCRYPTO, 'Needs crypto library')
  54. @patch('salt.cloud.clouds.ec2._get_node')
  55. @patch('salt.cloud.clouds.ec2.get_location')
  56. @patch('salt.cloud.clouds.ec2.get_provider')
  57. @patch('salt.utils.aws.query')
  58. def test_get_password_data(self, query, get_provider, get_location, _get_node):
  59. query.return_value = [
  60. {
  61. 'passwordData': PASS_DATA
  62. }
  63. ]
  64. _get_node.return_value = {'instanceId': 'i-abcdef'}
  65. get_location.return_value = 'us-west2'
  66. get_provider.return_value = 'ec2'
  67. with salt.utils.files.fopen(self.key_file, 'w') as fp:
  68. fp.write(PRIVKEY_DATA)
  69. ret = ec2.get_password_data(
  70. name='i-abcddef', kwargs={'key_file': self.key_file}, call='action'
  71. )
  72. assert ret['passwordData'] == PASS_DATA
  73. assert ret['password'] == 'testp4ss!'
  74. @patch('salt.cloud.clouds.ec2.get_location')
  75. @patch('salt.cloud.clouds.ec2.get_provider')
  76. @patch('salt.utils.aws.query')
  77. def test__get_imageid_by_name(self, query, get_provider, get_location):
  78. # Trimmed list and stripped dictionary keys for brevity
  79. query.return_value = [
  80. {u'creationDate': '2019-01-30T23:40:58.000Z', u'imageId': 'ami-02eac2c0129f6376b'},
  81. {u'creationDate': '2019-03-15T00:08:05.000Z', u'imageId': 'ami-089ccd342f0be98ab'},
  82. {u'creationDate': '2018-05-14T17:19:51.000Z', u'imageId': 'ami-4b6bff34'},
  83. {u'creationDate': '2018-01-12T20:33:32.000Z', u'imageId': 'ami-4bf3d731'}]
  84. get_location.return_value = 'us-west2'
  85. get_provider.return_value = 'ec2'
  86. # Mock makes argument irrelevant; illustrates value used to obtain mock
  87. imageid = ec2._get_imageid_from_image_name('CentOS Linux 7*')
  88. assert imageid == 'ami-089ccd342f0be98ab'