|
| 1 | +# Copyright 2025 Genesis Corporation. |
| 2 | +# |
| 3 | +# All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. You may obtain |
| 7 | +# a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | + |
| 17 | +import uuid as sys_uuid |
| 18 | +import typing as tp |
| 19 | + |
| 20 | +import dns.resolver |
| 21 | +import pytest |
| 22 | +from gcl_iam.tests.functional import clients as iam_clients |
| 23 | + |
| 24 | +from genesis_core.common import constants as c |
| 25 | + |
| 26 | + |
| 27 | +DEF_DOMAIN = "core.internal" |
| 28 | + |
| 29 | + |
| 30 | +class TestDnsApi: |
| 31 | + |
| 32 | + # Utils |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def _cmp_shallow( |
| 36 | + left: tp.Dict[str, tp.Any], |
| 37 | + right: tp.Dict[str, tp.Any], |
| 38 | + ): |
| 39 | + return all((left[key] == right[key]) for key in left.keys()) |
| 40 | + |
| 41 | + @pytest.fixture() |
| 42 | + def domain1( |
| 43 | + self, |
| 44 | + user_api_client: iam_clients.GenesisCoreTestRESTClient, |
| 45 | + auth_user_admin: iam_clients.GenesisCoreAuth, |
| 46 | + ): |
| 47 | + domain = { |
| 48 | + "uuid": str(sys_uuid.uuid4()), |
| 49 | + "name": DEF_DOMAIN, |
| 50 | + "project_id": str(c.SERVICE_PROJECT_ID), |
| 51 | + } |
| 52 | + client = user_api_client(auth_user_admin) |
| 53 | + url = client.build_collection_uri(["dns", "domains"]) |
| 54 | + |
| 55 | + response = client.post(url, json=domain) |
| 56 | + output = response.json() |
| 57 | + |
| 58 | + assert response.status_code == 201 |
| 59 | + assert self._cmp_shallow(domain, output) |
| 60 | + yield output |
| 61 | + |
| 62 | + # DNS |
| 63 | + |
| 64 | + def test_domains_list( |
| 65 | + self, |
| 66 | + user_api_client: iam_clients.GenesisCoreTestRESTClient, |
| 67 | + auth_user_admin: iam_clients.GenesisCoreAuth, |
| 68 | + ): |
| 69 | + client = user_api_client(auth_user_admin) |
| 70 | + url = client.build_collection_uri(["dns", "domains"]) |
| 71 | + |
| 72 | + response = client.get(url) |
| 73 | + |
| 74 | + assert response.status_code == 200 |
| 75 | + assert len(response.json()) == 0 |
| 76 | + |
| 77 | + def test_domains_add( |
| 78 | + self, |
| 79 | + user_api_client: iam_clients.GenesisCoreTestRESTClient, |
| 80 | + auth_user_admin: iam_clients.GenesisCoreAuth, |
| 81 | + domain1: tp.Dict, |
| 82 | + pdns_server: int | None, |
| 83 | + ): |
| 84 | + client = user_api_client(auth_user_admin) |
| 85 | + |
| 86 | + # Check SOA Record |
| 87 | + |
| 88 | + url = client.build_collection_uri( |
| 89 | + ["dns", "domains", domain1["uuid"], "records"] |
| 90 | + ) |
| 91 | + |
| 92 | + response = client.get(url) |
| 93 | + records = response.json() |
| 94 | + assert response.status_code == 200 |
| 95 | + assert len(records) == 1 |
| 96 | + assert records[0]["type"] == "SOA" |
| 97 | + assert records[0]["record"]["name"] == "@" |
| 98 | + assert ( |
| 99 | + records[0]["record"]["primary_dns"] |
| 100 | + == "a.misconfigured.dns.server.invalid" |
| 101 | + ) |
| 102 | + |
| 103 | + if pdns_server: |
| 104 | + res = dns.resolver.make_resolver_at("127.0.0.1", port=pdns_server) |
| 105 | + answer = res.resolve(DEF_DOMAIN, "SOA") |
| 106 | + |
| 107 | + assert len(answer) == 1 |
| 108 | + assert ( |
| 109 | + answer[0].to_text() |
| 110 | + == "a.misconfigured.dns.server.invalid. core.internal. 0 10800 3600 604800 3600" |
| 111 | + ) |
| 112 | + |
| 113 | + # Delete |
| 114 | + |
| 115 | + url = client.build_resource_uri(["dns", "domains", domain1["uuid"]]) |
| 116 | + |
| 117 | + response = client.delete(url) |
| 118 | + |
| 119 | + assert response.status_code == 204 |
| 120 | + |
| 121 | + url = client.build_collection_uri(["dns", "domains"]) |
| 122 | + |
| 123 | + response = client.get(url) |
| 124 | + |
| 125 | + assert response.status_code == 200 |
| 126 | + assert len(response.json()) == 0 |
| 127 | + |
| 128 | + def test_a_record( |
| 129 | + self, |
| 130 | + user_api_client: iam_clients.GenesisCoreTestRESTClient, |
| 131 | + auth_user_admin: iam_clients.GenesisCoreAuth, |
| 132 | + domain1: tp.Dict, |
| 133 | + pdns_server: int | None, |
| 134 | + ): |
| 135 | + client = user_api_client(auth_user_admin) |
| 136 | + |
| 137 | + data = { |
| 138 | + "uuid": str(sys_uuid.uuid4()), |
| 139 | + "type": "A", |
| 140 | + "ttl": 0, |
| 141 | + "record": {"kind": "A", "name": "test", "address": "1.2.3.4"}, |
| 142 | + } |
| 143 | + |
| 144 | + url = client.build_collection_uri( |
| 145 | + ["dns", "domains", domain1["uuid"], "records"] |
| 146 | + ) |
| 147 | + |
| 148 | + response = client.post(url, json=data) |
| 149 | + output = response.json() |
| 150 | + |
| 151 | + assert response.status_code == 201 |
| 152 | + assert self._cmp_shallow(data, output) |
| 153 | + |
| 154 | + url = client.build_resource_uri( |
| 155 | + ["dns", "domains", domain1["uuid"], "records", data["uuid"]] |
| 156 | + ) |
| 157 | + |
| 158 | + response = client.get(url) |
| 159 | + record = response.json() |
| 160 | + assert response.status_code == 200 |
| 161 | + assert self._cmp_shallow(data, record) |
| 162 | + |
| 163 | + if pdns_server: |
| 164 | + res = dns.resolver.make_resolver_at("127.0.0.1", port=pdns_server) |
| 165 | + answer = res.resolve(f"test.{DEF_DOMAIN}", "A") |
| 166 | + |
| 167 | + assert len(answer) == 1 |
| 168 | + assert answer[0].address == "1.2.3.4" |
| 169 | + |
| 170 | + # Delete |
| 171 | + response = client.delete(url) |
| 172 | + |
| 173 | + assert response.status_code == 204 |
0 commit comments