git » dnss » commit 733e4c5

Use a more standard testing layout

author Alberto Bertogli
2017-07-30 12:11:35 UTC
committer Alberto Bertogli
2017-07-30 16:21:46 UTC
parent 9677b915c6892a9b5ebf4dd547be7129c428168f

Use a more standard testing layout

The "testing" directory is awkward, not very common, and it makes some
things more difficult.

This patch moves the tests next to the main package they are testing and
the utilities to the internal directory, leaving the "testing" directory
empty.

The end result is that the tests are now organized more like any other
Go program.

dnss_test.go +11 -11
internal/dnstohttps/caching_test.go +4 -4
{testing/https => internal/dnstohttps}/https_test.go +11 -12
testing/util/util.go => internal/testutil/testutil.go +2 -2
testing/https/https.go +0 -4

diff --git a/dnss_test.go b/dnss_test.go
index 4ad62a0..2b5383b 100644
--- a/dnss_test.go
+++ b/dnss_test.go
@@ -11,7 +11,7 @@ import (
 
 	"blitiri.com.ar/go/dnss/internal/dnstohttps"
 	"blitiri.com.ar/go/dnss/internal/httpstodns"
-	"blitiri.com.ar/go/dnss/testing/util"
+	"blitiri.com.ar/go/dnss/internal/testutil"
 	"github.com/golang/glog"
 	"github.com/miekg/dns"
 )
@@ -32,9 +32,9 @@ func realMain(m *testing.M) int {
 	flag.Parse()
 	defer glog.Flush()
 
-	DNSToHTTPSAddr := util.GetFreePort()
-	HTTPSToDNSAddr := util.GetFreePort()
-	DNSServerAddr := util.GetFreePort()
+	DNSToHTTPSAddr := testutil.GetFreePort()
+	HTTPSToDNSAddr := testutil.GetFreePort()
+	DNSServerAddr := testutil.GetFreePort()
 
 	// We want tests talking to the DNS-to-HTTPS server, the first in the
 	// chain.
@@ -57,9 +57,9 @@ func realMain(m *testing.M) int {
 	go ServeFakeDNSServer(DNSServerAddr)
 
 	// Wait for the servers to start up.
-	err1 := util.WaitForDNSServer(DNSToHTTPSAddr)
-	err2 := util.WaitForHTTPServer(HTTPSToDNSAddr)
-	err3 := util.WaitForDNSServer(DNSServerAddr)
+	err1 := testutil.WaitForDNSServer(DNSToHTTPSAddr)
+	err2 := testutil.WaitForHTTPServer(HTTPSToDNSAddr)
+	err3 := testutil.WaitForDNSServer(DNSServerAddr)
 	if err1 != nil || err2 != nil || err3 != nil {
 		fmt.Printf("Error waiting for the test servers to start:\n")
 		fmt.Printf("  DNS to HTTPS: %v\n", err1)
@@ -149,7 +149,7 @@ func handleFakeDNS(w dns.ResponseWriter, r *dns.Msg) {
 func TestSimple(t *testing.T) {
 	resetAnswers()
 	addAnswers(t, "test.blah. A 1.2.3.4")
-	_, ans, err := util.DNSQuery(ServerAddr, "test.blah.", dns.TypeA)
+	_, ans, err := testutil.DNSQuery(ServerAddr, "test.blah.", dns.TypeA)
 	if err != nil {
 		t.Errorf("dns query returned error: %v", err)
 	}
@@ -158,7 +158,7 @@ func TestSimple(t *testing.T) {
 	}
 
 	addAnswers(t, "test.blah. MX 10 mail.test.blah.")
-	_, ans, err = util.DNSQuery(ServerAddr, "test.blah.", dns.TypeMX)
+	_, ans, err = testutil.DNSQuery(ServerAddr, "test.blah.", dns.TypeMX)
 	if err != nil {
 		t.Errorf("dns query returned error: %v", err)
 	}
@@ -166,7 +166,7 @@ func TestSimple(t *testing.T) {
 		t.Errorf("unexpected result: %q", ans.(*dns.MX).Mx)
 	}
 
-	in, _, err := util.DNSQuery(ServerAddr, "unknown.", dns.TypeA)
+	in, _, err := testutil.DNSQuery(ServerAddr, "unknown.", dns.TypeA)
 	if err != nil {
 		t.Errorf("dns query returned error: %v", err)
 	}
@@ -186,7 +186,7 @@ func BenchmarkSimple(b *testing.B) {
 
 	var err error
 	for i := 0; i < b.N; i++ {
-		_, _, err = util.DNSQuery(ServerAddr, "test.blah.", dns.TypeA)
+		_, _, err = testutil.DNSQuery(ServerAddr, "test.blah.", dns.TypeA)
 		if err != nil {
 			b.Errorf("dns query returned error: %v", err)
 		}
diff --git a/internal/dnstohttps/caching_test.go b/internal/dnstohttps/caching_test.go
index 9c35eee..4a59555 100644
--- a/internal/dnstohttps/caching_test.go
+++ b/internal/dnstohttps/caching_test.go
@@ -11,7 +11,7 @@ import (
 	"testing"
 	"time"
 
-	"blitiri.com.ar/go/dnss/testing/util"
+	"blitiri.com.ar/go/dnss/internal/testutil"
 
 	"github.com/miekg/dns"
 	"golang.org/x/net/trace"
@@ -255,7 +255,7 @@ func BenchmarkCacheSimple(b *testing.B) {
 	c := NewCachingResolver(r)
 	c.Init()
 
-	tr := &util.NullTrace{}
+	tr := &testutil.NullTrace{}
 	req := newQuery("test.", dns.TypeA)
 
 	b.ResetTimer()
@@ -297,7 +297,7 @@ func queryA(t *testing.T, c *cachingResolver, rr, domain, expected string) *dns.
 		back.response = newReply(mustNewRR(t, rr))
 	}
 
-	tr := util.NewTestTrace(t)
+	tr := testutil.NewTestTrace(t)
 	defer tr.Finish()
 
 	req := newQuery(domain, dns.TypeA)
@@ -325,7 +325,7 @@ func queryFail(t *testing.T, c *cachingResolver) *dns.Msg {
 	back.response.Response = true
 	back.response.Rcode = dns.RcodeNameError
 
-	tr := util.NewTestTrace(t)
+	tr := testutil.NewTestTrace(t)
 	defer tr.Finish()
 
 	req := newQuery("doesnotexist.", dns.TypeA)
diff --git a/testing/https/https_test.go b/internal/dnstohttps/https_test.go
similarity index 84%
rename from testing/https/https_test.go
rename to internal/dnstohttps/https_test.go
index a29ced9..973ee84 100644
--- a/testing/https/https_test.go
+++ b/internal/dnstohttps/https_test.go
@@ -1,5 +1,5 @@
-// Tests for dnss in HTTPS mode.
-package https
+// Tests for dnss-to-https mode.
+package dnstohttps
 
 import (
 	"flag"
@@ -9,8 +9,7 @@ import (
 	"os"
 	"testing"
 
-	"blitiri.com.ar/go/dnss/internal/dnstohttps"
-	"blitiri.com.ar/go/dnss/testing/util"
+	"blitiri.com.ar/go/dnss/internal/testutil"
 
 	"github.com/golang/glog"
 	"github.com/miekg/dns"
@@ -21,7 +20,7 @@ import (
 //
 
 func TestSimple(t *testing.T) {
-	_, ans, err := util.DNSQuery(DNSAddr, "test.blah.", dns.TypeA)
+	_, ans, err := testutil.DNSQuery(DNSAddr, "test.blah.", dns.TypeA)
 	if err != nil {
 		t.Errorf("dns query returned error: %v", err)
 	}
@@ -29,7 +28,7 @@ func TestSimple(t *testing.T) {
 		t.Errorf("unexpected result: %q", ans)
 	}
 
-	_, ans, err = util.DNSQuery(DNSAddr, "test.blah.", dns.TypeMX)
+	_, ans, err = testutil.DNSQuery(DNSAddr, "test.blah.", dns.TypeMX)
 	if err != nil {
 		t.Errorf("dns query returned error: %v", err)
 	}
@@ -37,7 +36,7 @@ func TestSimple(t *testing.T) {
 		t.Errorf("unexpected result: %q", ans.(*dns.MX).Mx)
 	}
 
-	in, _, err := util.DNSQuery(DNSAddr, "unknown.", dns.TypeA)
+	in, _, err := testutil.DNSQuery(DNSAddr, "unknown.", dns.TypeA)
 	if err != nil {
 		t.Errorf("dns query returned error: %v", err)
 	}
@@ -53,7 +52,7 @@ func TestSimple(t *testing.T) {
 func BenchmarkHTTPSimple(b *testing.B) {
 	var err error
 	for i := 0; i < b.N; i++ {
-		_, _, err = util.DNSQuery(DNSAddr, "test.blah.", dns.TypeA)
+		_, _, err = testutil.DNSQuery(DNSAddr, "test.blah.", dns.TypeA)
 		if err != nil {
 			b.Errorf("dns query returned error: %v", err)
 		}
@@ -124,18 +123,18 @@ func realMain(m *testing.M) int {
 	flag.Parse()
 	defer glog.Flush()
 
-	DNSAddr = util.GetFreePort()
+	DNSAddr = testutil.GetFreePort()
 
 	// Test http server.
 	httpsrv := httptest.NewServer(http.HandlerFunc(DNSHandler))
 
 	// DNS to HTTPS server.
-	r := dnstohttps.NewHTTPSResolver(httpsrv.URL, "")
-	dth := dnstohttps.New(DNSAddr, r, "")
+	r := NewHTTPSResolver(httpsrv.URL, "")
+	dth := New(DNSAddr, r, "")
 	go dth.ListenAndServe()
 
 	// Wait for the servers to start up.
-	err := util.WaitForDNSServer(DNSAddr)
+	err := testutil.WaitForDNSServer(DNSAddr)
 	if err != nil {
 		fmt.Printf("Error waiting for the test servers to start: %v\n", err)
 		fmt.Printf("Check the INFO logs for more details\n")
diff --git a/testing/util/util.go b/internal/testutil/testutil.go
similarity index 97%
rename from testing/util/util.go
rename to internal/testutil/testutil.go
index 748fe5b..92033e4 100644
--- a/testing/util/util.go
+++ b/internal/testutil/testutil.go
@@ -1,5 +1,5 @@
-// Package util implements common testing utilities.
-package util
+// Package testutil implements common testing utilities.
+package testutil
 
 import (
 	"fmt"
diff --git a/testing/https/https.go b/testing/https/https.go
deleted file mode 100644
index cc8acf1..0000000
--- a/testing/https/https.go
+++ /dev/null
@@ -1,4 +0,0 @@
-package https
-
-// Dummy file so "go build ./..." does not complain about the directory not
-// having buildable files.