1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import unittest
import azure.mgmt.dns
import azure.mgmt.network
from devtools_testutils import (
AzureMgmtRecordedTestCase,
ResourceGroupPreparer,
recorded_by_proxy,
AzureMgmtPreparer,
FakeResource,
)
import pytest
@pytest.mark.live_test_only
class VirtualNetworkPreparer(AzureMgmtPreparer):
def __init__(self, name_prefix="pvtzonevnet"):
super(VirtualNetworkPreparer, self).__init__(name_prefix, 24)
def create_resource(self, name, **kwargs):
registration_virtual_network_name = name + "reg"
resolution_virtual_network_name = name + "res"
if self.is_live:
resource_group_name = kwargs["resource_group"].name
location_name = kwargs["location"]
registration_network = self.test_class_instance.network_client.virtual_networks.begin_create_or_update(
resource_group_name,
registration_virtual_network_name,
{
"location": location_name,
"address_space": {"address_prefixes": ["10.0.0.0/8"]},
"subnets": [{"name": "default", "address_prefix": "10.0.0.0/24"}],
},
).result()
resolution_network = self.test_class_instance.network_client.virtual_networks.begin_create_or_update(
resource_group_name,
resolution_virtual_network_name,
{
"location": location_name,
"address_space": {"address_prefixes": ["10.0.0.0/8"]},
"subnets": [{"name": "default", "address_prefix": "10.0.0.0/24"}],
},
).result()
else:
registration_network = FakeResource(name=registration_virtual_network_name, id="")
resolution_network = FakeResource(name=resolution_virtual_network_name, id="")
return {"registration_virtual_network": registration_network, "resolution_virtual_network": resolution_network}
@pytest.mark.live_test_only
class TestMgmtDns(AzureMgmtRecordedTestCase):
def setup_method(self, method):
self.dns_client = self.create_mgmt_client(
azure.mgmt.dns.DnsManagementClient,
)
self.network_client = self.create_mgmt_client(
azure.mgmt.network.NetworkManagementClient,
)
@ResourceGroupPreparer()
@recorded_by_proxy
def test_public_zone(self, resource_group, location):
zone_name = self.get_resource_name("pydns.com")
# Zones are a 'global' resource.
zone = self.dns_client.zones.create_or_update(
resource_group.name, zone_name, {"zone_type": "Public", "location": "global"}
)
assert zone.name == zone_name
zone = self.dns_client.zones.get(resource_group.name, zone.name)
assert zone.name == zone_name
zones = list(self.dns_client.zones.list_by_resource_group(resource_group.name))
assert len(zones) >= 1
zones = list(self.dns_client.zones.list())
assert len(zones) >= 1
# Record set
record_set_name = self.get_resource_name("record_set")
self.dns_client.record_sets.create_or_update(
resource_group.name,
zone.name,
record_set_name,
"A",
{"ttl": 300, "arecords": [{"ipv4_address": "1.2.3.4"}]},
)
self.dns_client.record_sets.update(
resource_group.name,
zone.name,
record_set_name,
"A",
{"ttl": 300, "arecords": [{"ipv4_address": "1.2.3.4"}, {"ipv4_address": "5.6.7.8"}]},
)
record_set = self.dns_client.record_sets.get(resource_group.name, zone.name, record_set_name, "A")
assert record_set is not None
record_sets = list(self.dns_client.record_sets.list_by_type(resource_group.name, zone.name, "A"))
assert len(record_sets) == 1
# NS and SOA records are created by default in public zones.
record_sets = list(self.dns_client.record_sets.list_by_dns_zone(resource_group.name, zone.name))
assert len(record_sets) == 3
self.dns_client.record_sets.delete(resource_group.name, zone.name, record_set_name, "A")
async_delete = self.dns_client.zones.begin_delete(resource_group.name, zone.name)
async_delete.wait()
@unittest.skip("using this API is no longer allowed")
@ResourceGroupPreparer()
@VirtualNetworkPreparer()
@recorded_by_proxy
def test_private_zone(self, resource_group, location, registration_virtual_network, resolution_virtual_network):
zone_name = self.get_resource_name("pydns.com")
# Zones are a 'global' resource.
zone = self.dns_client.zones.create_or_update(
resource_group.name,
zone_name,
{
"zone_type": "Private",
"location": "global",
"registration_virtual_networks": [{"id": registration_virtual_network.id}],
"resolution_virtual_networks": [{"id": resolution_virtual_network.id}],
},
)
assert zone.name == zone_name
zone = self.dns_client.zones.get(resource_group.name, zone.name)
assert zone.name == zone_name
zones = list(self.dns_client.zones.list_by_resource_group(resource_group.name))
assert len(zones) >= 1
zones = list(self.dns_client.zones.list())
assert len(zones) >= 1
# Record set
record_set_name = self.get_resource_name("record_set")
self.dns_client.record_sets.create_or_update(
resource_group.name,
zone.name,
record_set_name,
"A",
{"ttl": 300, "arecords": [{"ipv4_address": "1.2.3.4"}]},
)
self.dns_client.record_sets.update(
resource_group.name,
zone.name,
record_set_name,
"A",
{"ttl": 300, "arecords": [{"ipv4_address": "1.2.3.4"}, {"ipv4_address": "5.6.7.8"}]},
)
record_set = self.dns_client.record_sets.get(resource_group.name, zone.name, record_set_name, "A")
assert record_set is not None
record_sets = list(self.dns_client.record_sets.list_by_type(resource_group.name, zone.name, "A"))
assert len(record_sets) == 1
# SOA record is created by default in private zones.
record_sets = list(self.dns_client.record_sets.list_by_dns_zone(resource_group.name, zone.name))
assert len(record_sets) == 2
self.dns_client.record_sets.delete(resource_group.name, zone.name, record_set_name, "A")
async_delete = self.dns_client.zones.begin_delete(resource_group.name, zone.name)
async_delete.wait()
# ------------------------------------------------------------------------------
if __name__ == "__main__":
unittest.main()
|