1
0

test_gce.py 3.7 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. # Import Salt Libs
  10. from salt.cloud.clouds import gce
  11. from salt.exceptions import SaltCloudSystemExit
  12. from salt.utils.versions import LooseVersion
  13. # Import Salt Testing Libs
  14. from tests.support.mixins import LoaderModuleMockMixin
  15. from tests.support.mock import __version__ as mock_version
  16. from tests.support.mock import patch
  17. from tests.support.unit import TestCase
  18. try:
  19. import libcloud.security
  20. HAS_LIBCLOUD = True
  21. except ImportError:
  22. HAS_LIBCLOUD = False
  23. VM_NAME = "kings_landing"
  24. DUMMY_TOKEN = {
  25. "refresh_token": None,
  26. "client_id": "dany123",
  27. "client_secret": "lalalalalalala",
  28. "grant_type": "refresh_token",
  29. }
  30. # Use certifi if installed
  31. try:
  32. if HAS_LIBCLOUD:
  33. # This work-around for Issue #32743 is no longer needed for libcloud >=
  34. # 1.4.0. However, older versions of libcloud must still be supported
  35. # with this work-around. This work-around can be removed when the
  36. # required minimum version of libcloud is 2.0.0 (See PR #40837 - which
  37. # is implemented in Salt 2018.3.0).
  38. if LooseVersion(libcloud.__version__) < LooseVersion("1.4.0"):
  39. import certifi
  40. libcloud.security.CA_CERTS_PATH.append(certifi.where())
  41. except ImportError:
  42. pass
  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, gce.destroy, vm_name=VM_NAME, call="function"
  73. )
  74. def test_fail_virtual_missing_deps(self):
  75. # Missing deps
  76. with patch("salt.config.check_driver_dependencies", return_value=False):
  77. v = gce.__virtual__()
  78. self.assertEqual(v, False)
  79. def test_fail_virtual_deps_missing_config(self):
  80. with patch("salt.config.check_driver_dependencies", return_value=True):
  81. with patch("salt.config.is_provider_configured", return_value=False):
  82. v = gce.__virtual__()
  83. self.assertEqual(v, False)
  84. def test_import(self):
  85. """
  86. Test that the module picks up installed deps
  87. """
  88. with patch("salt.config.check_driver_dependencies", return_value=True) as p:
  89. get_deps = gce.get_dependencies()
  90. self.assertEqual(get_deps, True)
  91. if LooseVersion(mock_version) >= LooseVersion("2.0.0"):
  92. self.assert_called_once(p)
  93. def test_provider_matches(self):
  94. """
  95. Test that the first configured instance of a gce driver is matched
  96. """
  97. p = gce.get_configured_provider()
  98. self.assertNotEqual(p, None)