test_azurearm_dns.py 4.3 KB

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