test_scaleway.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import copy
  5. # Import Salt Libs
  6. import salt.utils.json
  7. from salt.cloud.clouds import scaleway
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.mock import MagicMock, patch
  11. from tests.support.unit import TestCase
  12. class DummyRequest(object):
  13. def __init__(self, status_code, **response):
  14. self.status_code = status_code
  15. self.response = response
  16. def __getitem__(self, item):
  17. if item == "status":
  18. return self.status_code
  19. elif item in self.response:
  20. return self.response[item]
  21. raise KeyError(item)
  22. class ScalewayTestCase(TestCase, LoaderModuleMockMixin):
  23. """
  24. Test cases for salt.cloud.clouds.scaleway
  25. """
  26. _profile = {
  27. "profile": "my_scaleway",
  28. "name": "foo",
  29. "driver": "scaleway",
  30. "token": "foobarbaz",
  31. }
  32. def setup_loader_modules(self):
  33. return {
  34. scaleway: {
  35. "__utils__": {},
  36. "__opts__": {
  37. "providers": {"my_scaleway": {}},
  38. "profiles": {"my_scaleway": {}},
  39. },
  40. }
  41. }
  42. def test_query(self):
  43. """
  44. Confirm that using a different root affects the HTTP query made
  45. """
  46. body = '{"result": "success"}'
  47. server_id = "foo"
  48. expected = salt.utils.json.loads(body)
  49. http_query = MagicMock(return_value=DummyRequest(200, body=body))
  50. utils_dunder = {"http.query": http_query}
  51. with patch.dict(scaleway.__utils__, utils_dunder):
  52. # Case 1: use default api_root
  53. profile = copy.copy(self._profile)
  54. with patch.object(scaleway, "get_configured_provider", lambda: profile):
  55. result = scaleway.query(server_id=server_id)
  56. assert result == expected, result
  57. http_query.assert_called_once_with(
  58. "https://cp-par1.scaleway.com/servers/foo/",
  59. data="{}",
  60. headers={
  61. "X-Auth-Token": "foobarbaz",
  62. "User-Agent": "salt-cloud",
  63. "Content-Type": "application/json",
  64. },
  65. method="GET",
  66. )
  67. # Case 2: api_root overridden in profile
  68. http_query.reset_mock()
  69. profile = copy.copy(self._profile)
  70. profile["api_root"] = "https://my.api.root"
  71. with patch.object(scaleway, "get_configured_provider", lambda: profile):
  72. result = scaleway.query(server_id=server_id)
  73. assert result == expected, result
  74. http_query.assert_called_once_with(
  75. "https://my.api.root/servers/foo/",
  76. data="{}",
  77. headers={
  78. "X-Auth-Token": "foobarbaz",
  79. "User-Agent": "salt-cloud",
  80. "Content-Type": "application/json",
  81. },
  82. method="GET",
  83. )
  84. # Case 3: use default alternative root
  85. http_query.reset_mock()
  86. profile = copy.copy(self._profile)
  87. with patch.object(scaleway, "get_configured_provider", lambda: profile):
  88. result = scaleway.query(server_id=server_id, root="alt_root")
  89. assert result == expected, result
  90. http_query.assert_called_once_with(
  91. "https://api-marketplace.scaleway.com/servers/foo/",
  92. data="{}",
  93. headers={
  94. "X-Auth-Token": "foobarbaz",
  95. "User-Agent": "salt-cloud",
  96. "Content-Type": "application/json",
  97. },
  98. method="GET",
  99. )
  100. # Case 4: use alternative root specified in profile
  101. http_query.reset_mock()
  102. profile = copy.copy(self._profile)
  103. profile["alt_root"] = "https://my.alt.api.root"
  104. with patch.object(scaleway, "get_configured_provider", lambda: profile):
  105. result = scaleway.query(server_id=server_id, root="alt_root")
  106. assert result == expected, result
  107. http_query.assert_called_once_with(
  108. "https://my.alt.api.root/servers/foo/",
  109. data="{}",
  110. headers={
  111. "X-Auth-Token": "foobarbaz",
  112. "User-Agent": "salt-cloud",
  113. "Content-Type": "application/json",
  114. },
  115. method="GET",
  116. )