author | Alberto Bertogli
<albertito@blitiri.com.ar> 2016-08-20 11:33:23 UTC |
committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2016-08-20 11:50:50 UTC |
parent | beee266a5b11dbf92e4228bce6b1011e70644168 |
testing/grpc/grpc_test.go | +9 | -6 |
testing/https/https_test.go | +4 | -4 |
testing/util/util.go | +9 | -0 |
diff --git a/testing/grpc/grpc_test.go b/testing/grpc/grpc_test.go index 95c3a36..0172b9a 100644 --- a/testing/grpc/grpc_test.go +++ b/testing/grpc/grpc_test.go @@ -24,12 +24,15 @@ import ( "github.com/miekg/dns" ) -const ( - // TODO: Don't hard-code these. - dnsToGrpcAddr = "127.0.0.1:13451" - grpcToDnsAddr = "127.0.0.1:13452" - dnsSrvAddr = "127.0.0.1:13453" -) +// Addresses to use for testing. These will be picked at initialization time, +// see init(). +var dnsToGrpcAddr, grpcToDnsAddr, dnsSrvAddr string + +func init() { + dnsToGrpcAddr = util.GetFreePort() + grpcToDnsAddr = util.GetFreePort() + dnsSrvAddr = util.GetFreePort() +} // // === Tests === diff --git a/testing/https/https_test.go b/testing/https/https_test.go index 1794d56..b984ea2 100644 --- a/testing/https/https_test.go +++ b/testing/https/https_test.go @@ -128,10 +128,8 @@ const jsonNXDOMAIN = ` { "data": "root. nstld. 2016052201 1800 900 604800 86400" } ] } ` -const ( - // TODO: don't hardcode these. - DNSAddr string = "127.0.0.1:19251" -) +// Address where we will set up the DNS server. +var DNSAddr string // realMain is the real main function, which returns the value to pass to // os.Exit(). We have to do this so we can use defer. @@ -139,6 +137,8 @@ func realMain(m *testing.M) int { flag.Parse() defer glog.Flush() + DNSAddr = util.GetFreePort() + // Test http server. httpsrv := httptest.NewServer(http.HandlerFunc(DNSHandler)) diff --git a/testing/util/util.go b/testing/util/util.go index 975cef6..c59d72b 100644 --- a/testing/util/util.go +++ b/testing/util/util.go @@ -3,6 +3,7 @@ package util import ( "fmt" + "net" "testing" "time" @@ -40,6 +41,14 @@ func WaitForDNSServer(addr string) error { return fmt.Errorf("not reachable") } +// Get a free (TCP) port. This is hacky and not race-free, but it works well +// enough for testing purposes. +func GetFreePort() string { + l, _ := net.Listen("tcp", "localhost:0") + defer l.Close() + return l.Addr().String() +} + // TestTrace implements the tracer.Trace interface, but prints using the test // logging infrastructure. type TestTrace struct {