1
0

test_gce.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: `Anthony Shaw <anthonyshaw@apache.org>`
  4. tests.unit.cloud.clouds.gce_test
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. '''
  7. # Import Python libs
  8. from __future__ import absolute_import, print_function, unicode_literals
  9. try:
  10. import libcloud.security
  11. HAS_LIBCLOUD = True
  12. except ImportError:
  13. HAS_LIBCLOUD = False
  14. # Import Salt Libs
  15. from salt.cloud.clouds import gce
  16. from salt.exceptions import SaltCloudSystemExit
  17. from salt.utils.versions import LooseVersion
  18. # Import Salt Testing Libs
  19. from tests.support.mixins import LoaderModuleMockMixin
  20. from tests.support.unit import TestCase, skipIf
  21. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, __version__ as mock_version
  22. VM_NAME = 'kings_landing'
  23. DUMMY_TOKEN = {
  24. 'refresh_token': None,
  25. 'client_id': 'dany123',
  26. 'client_secret': 'lalalalalalala',
  27. 'grant_type': 'refresh_token'
  28. }
  29. # Use certifi if installed
  30. try:
  31. if HAS_LIBCLOUD:
  32. # This work-around for Issue #32743 is no longer needed for libcloud >=
  33. # 1.4.0. However, older versions of libcloud must still be supported
  34. # with this work-around. This work-around can be removed when the
  35. # required minimum version of libcloud is 2.0.0 (See PR #40837 - which
  36. # is implemented in Salt 2018.3.0).
  37. if LooseVersion(libcloud.__version__) < LooseVersion('1.4.0'):
  38. import certifi
  39. libcloud.security.CA_CERTS_PATH.append(certifi.where())
  40. except ImportError:
  41. pass
  42. @skipIf(NO_MOCK, NO_MOCK_REASON)
  43. class GCETestCase(TestCase, LoaderModuleMockMixin):
  44. '''
  45. Unit TestCase for salt.cloud.clouds.gce module.
  46. '''
  47. def setup_loader_modules(self):
  48. return {
  49. gce: {
  50. '__active_provider_name__': '',
  51. '__opts__': {
  52. 'providers': {
  53. 'my-google-cloud': {
  54. 'gce': {
  55. 'project': 'daenerys-cloud',
  56. 'service_account_email_address': 'dany@targaryen.westeros.cloud',
  57. 'service_account_private_key': '/home/dany/PRIVKEY.pem',
  58. 'driver': 'gce',
  59. 'ssh_interface': 'public_ips'
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. def test_destroy_call(self):
  67. '''
  68. Tests that a SaltCloudSystemExit is raised when trying to call destroy
  69. with --function or -f.
  70. '''
  71. self.assertRaises(
  72. SaltCloudSystemExit,
  73. gce.destroy,
  74. vm_name=VM_NAME,
  75. call='function'
  76. )
  77. def test_fail_virtual_missing_deps(self):
  78. # Missing deps
  79. with patch('salt.config.check_driver_dependencies', return_value=False):
  80. v = gce.__virtual__()
  81. self.assertEqual(v, False)
  82. def test_fail_virtual_deps_missing_config(self):
  83. with patch('salt.config.check_driver_dependencies', return_value=True):
  84. with patch('salt.config.is_provider_configured', return_value=False):
  85. v = gce.__virtual__()
  86. self.assertEqual(v, False)
  87. def test_import(self):
  88. """
  89. Test that the module picks up installed deps
  90. """
  91. with patch('salt.config.check_driver_dependencies', return_value=True) as p:
  92. get_deps = gce.get_dependencies()
  93. self.assertEqual(get_deps, True)
  94. if LooseVersion(mock_version) >= LooseVersion('2.0.0'):
  95. self.assert_called_once(p)
  96. def test_provider_matches(self):
  97. """
  98. Test that the first configured instance of a gce driver is matched
  99. """
  100. p = gce.get_configured_provider()
  101. self.assertNotEqual(p, None)