1
0

test_ec2.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.config.get_cloud_config_value')
  75. @patch('salt.cloud.clouds.ec2.get_location')
  76. @patch('salt.cloud.clouds.ec2.get_provider')
  77. @patch('salt.cloud.clouds.ec2.aws.query')
  78. def test_get_imageid(self, aws_query, get_provider, get_location, config):
  79. '''
  80. test querying imageid function
  81. '''
  82. vm = {}
  83. ami = 'ami-1234'
  84. config.return_value = 'test/*'
  85. get_location.return_value = 'us-west2'
  86. get_provider.return_value = 'ec2'
  87. aws_query.return_value = [{'imageId': ami}]
  88. # test image filter
  89. self.assertEqual(ec2.get_imageid(vm), ami)
  90. # test ami-image
  91. config.return_value = ami
  92. self.assertEqual(ec2.get_imageid(vm), ami)
  93. # we should have only ran aws.query once when testing the aws filter
  94. aws_query.assert_called_once()