1
0

test_ec2.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.runtime import RUNTIME_VARS
  12. from tests.support.unit import TestCase, skipIf
  13. from tests.support.mixins import LoaderModuleMockMixin
  14. from tests.support.mock import patch, PropertyMock
  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. class EC2TestCase(TestCase, LoaderModuleMockMixin):
  24. '''
  25. Unit TestCase for salt.cloud.clouds.ec2 module.
  26. '''
  27. def setUp(self):
  28. super(EC2TestCase, self).setUp()
  29. with tempfile.NamedTemporaryFile(dir=RUNTIME_VARS.TMP, suffix='.pem', delete=True) as fp:
  30. self.key_file = fp.name
  31. def tearDown(self):
  32. super(EC2TestCase, self).tearDown()
  33. if os.path.exists(self.key_file):
  34. os.remove(self.key_file)
  35. def setup_loader_modules(self):
  36. return {ec2: {'__opts__': {}}}
  37. def test__validate_key_path_and_mode(self):
  38. # Key file exists
  39. with patch('os.path.exists', return_value=True):
  40. with patch('os.stat') as patched_stat:
  41. type(patched_stat.return_value).st_mode = PropertyMock(return_value=0o644)
  42. self.assertRaises(
  43. SaltCloudSystemExit, ec2._validate_key_path_and_mode, 'key_file')
  44. type(patched_stat.return_value).st_mode = PropertyMock(return_value=0o600)
  45. self.assertTrue(ec2._validate_key_path_and_mode('key_file'))
  46. type(patched_stat.return_value).st_mode = PropertyMock(return_value=0o400)
  47. self.assertTrue(ec2._validate_key_path_and_mode('key_file'))
  48. # Key file does not exist
  49. with patch('os.path.exists', return_value=False):
  50. self.assertRaises(
  51. SaltCloudSystemExit, ec2._validate_key_path_and_mode, 'key_file')
  52. @skipIf(not ec2.HAS_M2 and not ec2.HAS_PYCRYPTO, 'Needs crypto library')
  53. @patch('salt.cloud.clouds.ec2._get_node')
  54. @patch('salt.cloud.clouds.ec2.get_location')
  55. @patch('salt.cloud.clouds.ec2.get_provider')
  56. @patch('salt.utils.aws.query')
  57. def test_get_password_data(self, query, get_provider, get_location, _get_node):
  58. query.return_value = [
  59. {
  60. 'passwordData': PASS_DATA
  61. }
  62. ]
  63. _get_node.return_value = {'instanceId': 'i-abcdef'}
  64. get_location.return_value = 'us-west2'
  65. get_provider.return_value = 'ec2'
  66. with salt.utils.files.fopen(self.key_file, 'w') as fp:
  67. fp.write(PRIVKEY_DATA)
  68. ret = ec2.get_password_data(
  69. name='i-abcddef', kwargs={'key_file': self.key_file}, call='action'
  70. )
  71. assert ret['passwordData'] == PASS_DATA
  72. assert ret['password'] == 'testp4ss!'
  73. @patch('salt.cloud.clouds.ec2.config.get_cloud_config_value')
  74. @patch('salt.cloud.clouds.ec2.get_location')
  75. @patch('salt.cloud.clouds.ec2.get_provider')
  76. @patch('salt.cloud.clouds.ec2.aws.query')
  77. def test_get_imageid(self, aws_query, get_provider, get_location, config):
  78. '''
  79. test querying imageid function
  80. '''
  81. vm = {}
  82. ami = 'ami-1234'
  83. config.return_value = 'test/*'
  84. get_location.return_value = 'us-west2'
  85. get_provider.return_value = 'ec2'
  86. aws_query.return_value = [{'imageId': ami}]
  87. # test image filter
  88. self.assertEqual(ec2.get_imageid(vm), ami)
  89. # test ami-image
  90. config.return_value = ami
  91. self.assertEqual(ec2.get_imageid(vm), ami)
  92. # we should have only ran aws.query once when testing the aws filter
  93. aws_query.assert_called_once()