1
0

test_azurearm_dns.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, print_function, unicode_literals
  3. import logging
  4. import pytest
  5. import salt.config
  6. import salt.loader
  7. import salt.modules.azurearm_dns as azurearm_dns
  8. from tests.support.mock import MagicMock
  9. from tests.support.sminion import create_sminion
  10. HAS_LIBS = False
  11. try:
  12. import azure.mgmt.dns.models # pylint: disable=import-error
  13. HAS_LIBS = True
  14. except ImportError:
  15. HAS_LIBS = False
  16. log = logging.getLogger(__name__)
  17. pytestmark = pytest.mark.skipif(
  18. HAS_LIBS is False, reason="The azure.mgmt.dns module must be installed."
  19. )
  20. class AzureObjMock(object):
  21. """
  22. mock azure object for as_dict calls
  23. """
  24. args = None
  25. kwargs = None
  26. def __init__(self, args, kwargs, return_value=None):
  27. self.args = args
  28. self.kwargs = kwargs
  29. self.__return_value = return_value
  30. def __getattr__(self, item):
  31. return self
  32. def __call__(self, *args, **kwargs):
  33. return MagicMock(return_value=self.__return_value)()
  34. def as_dict(self, *args, **kwargs):
  35. return self.args, self.kwargs
  36. class AzureFuncMock(object):
  37. """
  38. mock azure client function calls
  39. """
  40. def __init__(self, return_value=None):
  41. self.__return_value = return_value
  42. def __getattr__(self, item):
  43. return self
  44. def __call__(self, *args, **kwargs):
  45. return MagicMock(return_value=self.__return_value)()
  46. def create_or_update(self, *args, **kwargs):
  47. azure_obj = AzureObjMock(args, kwargs)
  48. return azure_obj
  49. class AzureSubMock(object):
  50. """
  51. mock azure client sub-modules
  52. """
  53. record_sets = AzureFuncMock()
  54. zones = AzureFuncMock()
  55. def __init__(self, return_value=None):
  56. self.__return_value = return_value
  57. def __getattr__(self, item):
  58. return self
  59. def __call__(self, *args, **kwargs):
  60. return MagicMock(return_value=self.__return_value)()
  61. class AzureClientMock(object):
  62. """
  63. mock azure client
  64. """
  65. def __init__(self, return_value=AzureSubMock):
  66. self.__return_value = return_value
  67. def __getattr__(self, item):
  68. return self
  69. def __call__(self, *args, **kwargs):
  70. return MagicMock(return_value=self.__return_value)()
  71. @pytest.fixture
  72. def credentials():
  73. azurearm_dns.__virtual__()
  74. return {
  75. "client_id": "CLIENT_ID",
  76. "secret": "SECRET",
  77. "subscription_id": "SUBSCRIPTION_ID",
  78. "tenant": "TENANT",
  79. }
  80. @pytest.fixture(autouse=True)
  81. def setup_loader(request):
  82. """
  83. setup loader modules and override the azurearm.get_client utility
  84. """
  85. minion_config = create_sminion().opts.copy()
  86. utils = salt.loader.utils(minion_config)
  87. funcs = salt.loader.minion_mods(
  88. minion_config, utils=utils, whitelist=["azurearm_dns", "config"]
  89. )
  90. utils["azurearm.get_client"] = AzureClientMock()
  91. setup_loader_modules = {
  92. azurearm_dns: {"__utils__": utils, "__salt__": funcs},
  93. }
  94. with pytest.helpers.loader_mock(request, setup_loader_modules) as loader_mock:
  95. yield loader_mock
  96. def test_record_set_create_or_update(credentials):
  97. """
  98. tests record set object creation
  99. """
  100. expected = {
  101. "if_match": None,
  102. "if_none_match": None,
  103. "parameters": {"arecords": [{"ipv4_address": "10.0.0.1"}], "ttl": 300},
  104. "record_type": "A",
  105. "relative_record_set_name": "myhost",
  106. "resource_group_name": "testgroup",
  107. "zone_name": "myzone",
  108. }
  109. record_set_args, record_set_kwargs = azurearm_dns.record_set_create_or_update(
  110. "myhost",
  111. "myzone",
  112. "testgroup",
  113. "A",
  114. arecords=[{"ipv4_address": "10.0.0.1"}],
  115. ttl=300,
  116. **credentials
  117. )
  118. for key, val in record_set_kwargs.items():
  119. if isinstance(val, azure.mgmt.dns.models.RecordSet):
  120. record_set_kwargs[key] = val.as_dict()
  121. assert record_set_kwargs == expected
  122. def test_zone_create_or_update(credentials):
  123. """
  124. tests zone object creation
  125. """
  126. expected = {
  127. "if_match": None,
  128. "if_none_match": None,
  129. "parameters": {"location": "global", "zone_type": "Public"},
  130. "resource_group_name": "testgroup",
  131. "zone_name": "myzone",
  132. }
  133. zone_args, zone_kwargs = azurearm_dns.zone_create_or_update(
  134. "myzone", "testgroup", **credentials
  135. )
  136. for key, val in zone_kwargs.items():
  137. if isinstance(val, azure.mgmt.dns.models.Zone):
  138. zone_kwargs[key] = val.as_dict()
  139. assert zone_kwargs == expected