aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/cpp/naming/utils/run_dns_server_for_lb_interop_tests.py
blob: 97171e21da8b1d352299c10da3cfecf2de42a1c1 (plain)
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
#!/usr/bin/env python2.7
# Copyright 2015 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import argparse
import subprocess
import os
import tempfile
import sys
import time
import signal
import yaml

argp = argparse.ArgumentParser(description='Runs a DNS server for LB interop tests')
argp.add_argument('-l', '--grpclb_ips', default=None, type=str,
                  help='Comma-separated list of IP addresses of balancers')
argp.add_argument('-f', '--fallback_ips', default=None, type=str,
                  help='Comma-separated list of IP addresses of fallback servers')
argp.add_argument('-c', '--cause_no_error_no_data_for_balancer_a_record',
                  default=False, action='store_const', const=True,
                  help=('Used for testing the case in which the grpclb '
                        'balancer A record lookup results in a DNS NOERROR response '
                        'but with no ANSWER section i.e. no addresses'))
args = argp.parse_args()

balancer_records = []
grpclb_ips = args.grpclb_ips.split(',')
if grpclb_ips[0]:
    for ip in grpclb_ips:
        balancer_records.append({
            'TTL': '2100',
            'data': ip,
            'type': 'A',
        })
fallback_records = []
fallback_ips = args.fallback_ips.split(',')
if fallback_ips[0]:
    for ip in fallback_ips:
        fallback_records.append({
            'TTL': '2100',
            'data': ip,
            'type': 'A',
        })
records_config_yaml = {
    'resolver_tests_common_zone_name':
    'test.google.fr.',
    'resolver_component_tests': [{
        'records': {
            '_grpclb._tcp.server': [
                {
                    'TTL': '2100',
                    'data': '0 0 12000 balancer',
                    'type': 'SRV'
                },
            ],
            'balancer':
            balancer_records,
            'server':
            fallback_records,
        }
    }]
}
if args.cause_no_error_no_data_for_balancer_a_record:
    balancer_records = records_config_yaml[
        'resolver_component_tests'][0]['records']['balancer']
    assert not balancer_records
    # Insert a TXT record at the balancer.test.google.fr. domain.
    # This TXT record won't actually be resolved or used by gRPC clients;
    # inserting this record is just a way get the balancer.test.google.fr.
    # A record queries to return NOERROR DNS responses that also have no
    # ANSWER section, in order to simulate this failure case.
    balancer_records.append({
        'TTL': '2100',
        'data': 'arbitrary string that wont actually be resolved',
        'type': 'TXT',
    })
# Generate the actual DNS server records config file
records_config_path = tempfile.mktemp()
with open(records_config_path, 'w') as records_config_generated:
    records_config_generated.write(yaml.dump(records_config_yaml))

with open(records_config_path, 'r') as records_config_generated:
    sys.stderr.write('===== DNS server records config: =====\n')
    sys.stderr.write(records_config_generated.read())
    sys.stderr.write('======================================\n')

# Run the DNS server
# Note that we need to add the extra
# A record for metadata.google.internal in order for compute engine
# OAuth creds and ALTS creds to work.
# TODO(apolcyn): should metadata.google.internal always resolve
# to 169.254.169.254?
subprocess.check_output([
    '/var/local/git/grpc/test/cpp/naming/utils/dns_server.py', '--port=53',
    '--records_config_path', records_config_path,
    '--add_a_record=metadata.google.internal:169.254.169.254',
])