xref: /aosp_15_r20/external/grpc-grpc/test/cpp/naming/utils/run_dns_server_for_lb_interop_tests.py (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1#!/usr/bin/env python3
2# Copyright 2015 gRPC authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17import os
18import signal
19import subprocess
20import sys
21import tempfile
22import time
23
24import yaml
25
26argp = argparse.ArgumentParser(
27    description="Runs a DNS server for LB interop tests"
28)
29argp.add_argument(
30    "-l",
31    "--grpclb_ips",
32    default=None,
33    type=str,
34    help="Comma-separated list of IP addresses of balancers",
35)
36argp.add_argument(
37    "-f",
38    "--fallback_ips",
39    default=None,
40    type=str,
41    help="Comma-separated list of IP addresses of fallback servers",
42)
43argp.add_argument(
44    "-c",
45    "--cause_no_error_no_data_for_balancer_a_record",
46    default=False,
47    action="store_const",
48    const=True,
49    help=(
50        "Used for testing the case in which the grpclb "
51        "balancer A record lookup results in a DNS NOERROR response "
52        "but with no ANSWER section i.e. no addresses"
53    ),
54)
55args = argp.parse_args()
56
57balancer_records = []
58grpclb_ips = args.grpclb_ips.split(",")
59if grpclb_ips[0]:
60    for ip in grpclb_ips:
61        balancer_records.append(
62            {
63                "TTL": "2100",
64                "data": ip,
65                "type": "A",
66            }
67        )
68fallback_records = []
69fallback_ips = args.fallback_ips.split(",")
70if fallback_ips[0]:
71    for ip in fallback_ips:
72        fallback_records.append(
73            {
74                "TTL": "2100",
75                "data": ip,
76                "type": "A",
77            }
78        )
79records_config_yaml = {
80    "resolver_tests_common_zone_name": "test.google.fr.",
81    "resolver_component_tests": [
82        {
83            "records": {
84                "_grpclb._tcp.server": [
85                    {
86                        "TTL": "2100",
87                        "data": "0 0 12000 balancer",
88                        "type": "SRV",
89                    },
90                ],
91                "balancer": balancer_records,
92                "server": fallback_records,
93            }
94        }
95    ],
96}
97if args.cause_no_error_no_data_for_balancer_a_record:
98    balancer_records = records_config_yaml["resolver_component_tests"][0][
99        "records"
100    ]["balancer"]
101    assert not balancer_records
102    # Insert a TXT record at the balancer.test.google.fr. domain.
103    # This TXT record won't actually be resolved or used by gRPC clients;
104    # inserting this record is just a way get the balancer.test.google.fr.
105    # A record queries to return NOERROR DNS responses that also have no
106    # ANSWER section, in order to simulate this failure case.
107    balancer_records.append(
108        {
109            "TTL": "2100",
110            "data": "arbitrary string that wont actually be resolved",
111            "type": "TXT",
112        }
113    )
114# Generate the actual DNS server records config file
115records_config_path = tempfile.mktemp()
116with open(records_config_path, "w") as records_config_generated:
117    records_config_generated.write(yaml.dump(records_config_yaml))
118
119with open(records_config_path, "r") as records_config_generated:
120    sys.stderr.write("===== DNS server records config: =====\n")
121    sys.stderr.write(records_config_generated.read())
122    sys.stderr.write("======================================\n")
123
124# Run the DNS server
125# Note that we need to add the extra
126# A record for metadata.google.internal in order for compute engine
127# OAuth creds and ALTS creds to work.
128# TODO(apolcyn): should metadata.google.internal always resolve
129# to 169.254.169.254?
130subprocess.check_output(
131    [
132        "/var/local/git/grpc/test/cpp/naming/utils/dns_server.py",
133        "--port=53",
134        "--records_config_path",
135        records_config_path,
136        "--add_a_record=metadata.google.internal:169.254.169.254",
137    ]
138)
139