1#!/usr/bin/env python2.7 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 subprocess 18import os 19import tempfile 20import sys 21import time 22import signal 23import yaml 24 25argp = argparse.ArgumentParser( 26 description='Runs a DNS server for LB interop tests') 27argp.add_argument('-l', 28 '--grpclb_ips', 29 default=None, 30 type=str, 31 help='Comma-separated list of IP addresses of balancers') 32argp.add_argument( 33 '-f', 34 '--fallback_ips', 35 default=None, 36 type=str, 37 help='Comma-separated list of IP addresses of fallback servers') 38argp.add_argument( 39 '-c', 40 '--cause_no_error_no_data_for_balancer_a_record', 41 default=False, 42 action='store_const', 43 const=True, 44 help=('Used for testing the case in which the grpclb ' 45 'balancer A record lookup results in a DNS NOERROR response ' 46 'but with no ANSWER section i.e. no addresses')) 47args = argp.parse_args() 48 49balancer_records = [] 50grpclb_ips = args.grpclb_ips.split(',') 51if grpclb_ips[0]: 52 for ip in grpclb_ips: 53 balancer_records.append({ 54 'TTL': '2100', 55 'data': ip, 56 'type': 'A', 57 }) 58fallback_records = [] 59fallback_ips = args.fallback_ips.split(',') 60if fallback_ips[0]: 61 for ip in fallback_ips: 62 fallback_records.append({ 63 'TTL': '2100', 64 'data': ip, 65 'type': 'A', 66 }) 67records_config_yaml = { 68 'resolver_tests_common_zone_name': 69 'test.google.fr.', 70 'resolver_component_tests': [{ 71 'records': { 72 '_grpclb._tcp.server': [{ 73 'TTL': '2100', 74 'data': '0 0 12000 balancer', 75 'type': 'SRV' 76 },], 77 'balancer': balancer_records, 78 'server': fallback_records, 79 } 80 }] 81} 82if args.cause_no_error_no_data_for_balancer_a_record: 83 balancer_records = records_config_yaml['resolver_component_tests'][0][ 84 'records']['balancer'] 85 assert not balancer_records 86 # Insert a TXT record at the balancer.test.google.fr. domain. 87 # This TXT record won't actually be resolved or used by gRPC clients; 88 # inserting this record is just a way get the balancer.test.google.fr. 89 # A record queries to return NOERROR DNS responses that also have no 90 # ANSWER section, in order to simulate this failure case. 91 balancer_records.append({ 92 'TTL': '2100', 93 'data': 'arbitrary string that wont actually be resolved', 94 'type': 'TXT', 95 }) 96# Generate the actual DNS server records config file 97records_config_path = tempfile.mktemp() 98with open(records_config_path, 'w') as records_config_generated: 99 records_config_generated.write(yaml.dump(records_config_yaml)) 100 101with open(records_config_path, 'r') as records_config_generated: 102 sys.stderr.write('===== DNS server records config: =====\n') 103 sys.stderr.write(records_config_generated.read()) 104 sys.stderr.write('======================================\n') 105 106# Run the DNS server 107# Note that we need to add the extra 108# A record for metadata.google.internal in order for compute engine 109# OAuth creds and ALTS creds to work. 110# TODO(apolcyn): should metadata.google.internal always resolve 111# to 169.254.169.254? 112subprocess.check_output([ 113 '/var/local/git/grpc/test/cpp/naming/utils/dns_server.py', 114 '--port=53', 115 '--records_config_path', 116 records_config_path, 117 '--add_a_record=metadata.google.internal:169.254.169.254', 118]) 119