git » gofer » commit 49127f1

test: Move utilities to their own directories

author Alberto Bertogli
2022-09-29 19:19:22 UTC
committer Alberto Bertogli
2022-10-07 10:59:43 UTC
parent 034e2065f7ad5e4e543410ec009cd37796a94059

test: Move utilities to their own directories

This patch moves testing utilities to their own directories, and removes
the "//go:build ignore" tag.

This makes sure they are included in all tooling invocations.

.gitignore +1 -1
test/util/cover-report.sh +3 -2
test/util/{ => exp}/exp.go +15 -6
test/util/{ => generate_cert}/generate_cert.go +0 -3
test/util/{ => gocovcat}/gocovcat.go +0 -2
test/util/lib.sh +4 -3

diff --git a/.gitignore b/.gitignore
index c3a2ba8..9be132b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,7 +9,7 @@
 
 # The binaries.
 /gofer
-/test/util/exp
+/test/util/exp/exp
 
 # Configuration and certificates, to prevent accidents.
 *.yaml
diff --git a/test/util/cover-report.sh b/test/util/cover-report.sh
index dc16535..ee8067b 100755
--- a/test/util/cover-report.sh
+++ b/test/util/cover-report.sh
@@ -12,8 +12,9 @@ echo "## Coverage"
 
 # Merge all coverage output into a single file.
 # Ignore protocol buffer-generated files, as they are not relevant.
-go run "${UTILDIR}/gocovcat.go" "${COVER_DIR}"/*.out \
-> "${COVER_DIR}/all.out"
+go run "${UTILDIR}/gocovcat/gocovcat.go" "${COVER_DIR}"/*.out \
+	| grep -v "blitiri.com.ar/go/gofer/test/util/" \
+	> "${COVER_DIR}/all.out"
 
 # Generate reports based on the merged output.
 go tool cover -func="$COVER_DIR/all.out" | sort -k 3 -n > "$COVER_DIR/func.txt"
diff --git a/test/util/exp.go b/test/util/exp/exp.go
similarity index 89%
rename from test/util/exp.go
rename to test/util/exp/exp.go
index 4ecdcdb..e605602 100644
--- a/test/util/exp.go
+++ b/test/util/exp/exp.go
@@ -1,6 +1,3 @@
-//go:build ignore
-// +build ignore
-
 // Fetch an URL, and check if the response matches what we expect.
 package main
 
@@ -10,6 +7,7 @@ import (
 	"flag"
 	"fmt"
 	"io/ioutil"
+	"net"
 	"net/http"
 	"os"
 	"regexp"
@@ -42,12 +40,14 @@ func main() {
 			"expect a header matching these contents (regexp match)")
 		caCert = flag.String("cacert", "",
 			"file to read CA cert from")
+		forceLocalhost = flag.Bool("forcelocalhost", false,
+			"force connection to go to localhost")
 	)
 	flag.Parse()
 
 	client := &http.Client{
 		CheckRedirect: noRedirect,
-		Transport:     mkTransport(*caCert),
+		Transport:     mkTransport(*caCert, *forceLocalhost),
 	}
 
 	resp, err := client.Get(url)
@@ -143,7 +143,7 @@ func noRedirect(req *http.Request, via []*http.Request) error {
 	return http.ErrUseLastResponse
 }
 
-func mkTransport(caCert string) *http.Transport {
+func mkTransport(caCert string, forceLocalhost bool) *http.Transport {
 	if caCert == "" {
 		return nil
 	}
@@ -158,11 +158,20 @@ func mkTransport(caCert string) *http.Transport {
 		fatalf("error adding certs to root")
 	}
 
-	return &http.Transport{
+	t := &http.Transport{
 		TLSClientConfig: &tls.Config{
 			RootCAs: rootCAs,
 		},
 	}
+
+	if forceLocalhost {
+		t.Dial = func(network, addr string) (net.Conn, error) {
+			_, port, _ := net.SplitHostPort(addr)
+			return net.Dial(network, "localhost:"+port)
+		}
+	}
+
+	return t
 }
 
 func fatalf(s string, a ...interface{}) {
diff --git a/test/util/generate_cert.go b/test/util/generate_cert/generate_cert.go
similarity index 98%
rename from test/util/generate_cert.go
rename to test/util/generate_cert/generate_cert.go
index 99c5e0d..f05d8b2 100644
--- a/test/util/generate_cert.go
+++ b/test/util/generate_cert/generate_cert.go
@@ -1,6 +1,3 @@
-//go:build ignore
-// +build ignore
-
 // Utility to generate self-signed certificates.
 // It generates a self-signed x509 certificate and key pair, and writes them
 // to "fullchain.pem" and "privkey.pem".
diff --git a/test/util/gocovcat.go b/test/util/gocovcat/gocovcat.go
similarity index 98%
rename from test/util/gocovcat.go
rename to test/util/gocovcat/gocovcat.go
index 4018912..ecb63db 100755
--- a/test/util/gocovcat.go
+++ b/test/util/gocovcat/gocovcat.go
@@ -2,8 +2,6 @@
 //
 // From: https://git.lukeshu.com/go/cmd/gocovcat/
 //
-//go:build ignore
-// +build ignore
 
 // Copyright 2017 Luke Shumaker <lukeshu@parabola.nu>
 //
diff --git a/test/util/lib.sh b/test/util/lib.sh
index 4a0adb5..067d1fa 100644
--- a/test/util/lib.sh
+++ b/test/util/lib.sh
@@ -25,7 +25,7 @@ function build() {
 	else
 		( cd ..; make )
 	fi
-	( cd util; go build exp.go )
+	( cd util/exp; go build )
 }
 
 function set_cover() {
@@ -64,7 +64,7 @@ function generate_certs() {
 	mkdir -p .certs/localhost
 	(
 		cd .certs/localhost
-		go run ${UTILDIR}/generate_cert.go \
+		go run ${UTILDIR}/generate_cert/generate_cert.go \
 			-ca -validfor=1h --host=localhost
 	)
 }
@@ -74,7 +74,8 @@ function exp() {
 		VF="-v"
 	fi
 	echo "  $@"
-	${UTILDIR}/exp "$@" \
+
+	${UTILDIR}/exp/exp "$@" \
 		$VF \
 		-cacert=".certs/localhost/fullchain.pem"
 }