From 36692b52d3e61df17dc9ef050106311f6f30b911 Mon Sep 17 00:00:00 2001 From: Alberto Bertogli Date: Mon, 4 Jun 2018 01:18:24 +0100 Subject: [PATCH] dovecot: Include cli-based tests in the coverage tests Many areas of the dovecot library are tested via chamuyero scripts, but these were not being included in the coverage report. This patch extends the dovecot-auth-cli tests so that they are now coverage-aware. --- cmd/dovecot-auth-cli/.gitignore | 1 + cmd/dovecot-auth-cli/coverage_test.go | 33 +++++++++++++++++++ cmd/dovecot-auth-cli/coverage_wrapper | 7 ++++ cmd/dovecot-auth-cli/dovecot-auth-cli.go | 13 ++++---- cmd/dovecot-auth-cli/test.sh | 9 ++++- cmd/dovecot-auth-cli/test_auth_error.cmy | 2 +- cmd/dovecot-auth-cli/test_auth_no.cmy | 2 +- cmd/dovecot-auth-cli/test_exists_notfound.cmy | 3 +- cmd/dovecot-auth-cli/test_missing_socket.cmy | 4 +-- test/cover.sh | 4 +++ 10 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 cmd/dovecot-auth-cli/coverage_test.go create mode 100755 cmd/dovecot-auth-cli/coverage_wrapper diff --git a/cmd/dovecot-auth-cli/.gitignore b/cmd/dovecot-auth-cli/.gitignore index ec06b5f..d605cc4 100644 --- a/cmd/dovecot-auth-cli/.gitignore +++ b/cmd/dovecot-auth-cli/.gitignore @@ -1,2 +1,3 @@ *.log dovecot-auth-cli +dovecot-auth-cli.test diff --git a/cmd/dovecot-auth-cli/coverage_test.go b/cmd/dovecot-auth-cli/coverage_test.go new file mode 100644 index 0000000..36b3b1a --- /dev/null +++ b/cmd/dovecot-auth-cli/coverage_test.go @@ -0,0 +1,33 @@ +// This package is tested externally (see test.sh). +// However, we need this to do coverage tests. +// +// See coverage_test.go for the details, this is the same horrible hack. +// +// +build coveragebin + +package main + +import ( + "os" + "os/signal" + "syscall" + "testing" +) + +func TestRunMain(t *testing.T) { + done := make(chan bool) + + signals := make(chan os.Signal, 1) + go func() { + <-signals + done <- true + }() + signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGTERM) + + go func() { + main() + done <- true + }() + + <-done +} diff --git a/cmd/dovecot-auth-cli/coverage_wrapper b/cmd/dovecot-auth-cli/coverage_wrapper new file mode 100755 index 0000000..d2188e4 --- /dev/null +++ b/cmd/dovecot-auth-cli/coverage_wrapper @@ -0,0 +1,7 @@ +#!/bin/bash +# +# Wrapper for dovecot-auth-cli to run when we build it in coverage mode. + +exec ./dovecot-auth-cli.test -test.run ^TestRunMain$ \ + -test.coverprofile="$COVER_DIR/test-`date +%s.%N`.out" \ + "$@" diff --git a/cmd/dovecot-auth-cli/dovecot-auth-cli.go b/cmd/dovecot-auth-cli/dovecot-auth-cli.go index fefb43d..67ee5a4 100644 --- a/cmd/dovecot-auth-cli/dovecot-auth-cli.go +++ b/cmd/dovecot-auth-cli/dovecot-auth-cli.go @@ -6,26 +6,26 @@ package main import ( + "flag" "fmt" - "os" "blitiri.com.ar/go/chasquid/internal/dovecot" ) func main() { - a := dovecot.NewAuth(os.Args[1]+"-userdb", os.Args[1]+"-client") + flag.Parse() + a := dovecot.NewAuth(flag.Arg(0)+"-userdb", flag.Arg(0)+"-client") var ok bool var err error - switch os.Args[2] { + switch flag.Arg(1) { case "exists": - ok, err = a.Exists(os.Args[3]) + ok, err = a.Exists(flag.Arg(2)) case "auth": - ok, err = a.Authenticate(os.Args[3], os.Args[4]) + ok, err = a.Authenticate(flag.Arg(2), flag.Arg(3)) default: fmt.Printf("unknown subcommand\n") - os.Exit(1) } if ok { @@ -34,5 +34,4 @@ func main() { } fmt.Printf("no: %v\n", err) - os.Exit(1) } diff --git a/cmd/dovecot-auth-cli/test.sh b/cmd/dovecot-auth-cli/test.sh index 7a9a917..4bb8c4f 100755 --- a/cmd/dovecot-auth-cli/test.sh +++ b/cmd/dovecot-auth-cli/test.sh @@ -8,7 +8,13 @@ init # Build the binary once, so we can use it and launch it in chamuyero scripts. # Otherwise, we not only spend time rebuilding it over and over, but also "go # run" masks the exit code, which is something we care about. -go build dovecot-auth-cli.go +if [ "${COVER_DIR}" != "" ]; then + go test -covermode=count -coverpkg=../../... -c \ + $GOFLAGS -tags="coveragebin $GOTAGS" + cp coverage_wrapper dovecot-auth-cli +else + go build $GOFLAGS -tags="$GOTAGS" dovecot-auth-cli.go +fi for i in *.cmy; do if ! chamuyero $i > $i.log 2>&1 ; then @@ -19,3 +25,4 @@ for i in *.cmy; do done success +exit 0 diff --git a/cmd/dovecot-auth-cli/test_auth_error.cmy b/cmd/dovecot-auth-cli/test_auth_error.cmy index 75bc37c..d931167 100644 --- a/cmd/dovecot-auth-cli/test_auth_error.cmy +++ b/cmd/dovecot-auth-cli/test_auth_error.cmy @@ -18,4 +18,4 @@ client <- AUTH 1 PLAIN service=smtp secured no-penalty nologin resp=dXNlcm5hbWUA client -> OTHER c <~ no: invalid response -c wait 1 +c wait 0 diff --git a/cmd/dovecot-auth-cli/test_auth_no.cmy b/cmd/dovecot-auth-cli/test_auth_no.cmy index 365e459..59efe74 100644 --- a/cmd/dovecot-auth-cli/test_auth_no.cmy +++ b/cmd/dovecot-auth-cli/test_auth_no.cmy @@ -18,4 +18,4 @@ client <- AUTH 1 PLAIN service=smtp secured no-penalty nologin resp=dXNlcm5hbWUA client -> FAIL 1 c <- no: -c wait 1 +c wait 0 diff --git a/cmd/dovecot-auth-cli/test_exists_notfound.cmy b/cmd/dovecot-auth-cli/test_exists_notfound.cmy index 20309e6..80d4ed7 100644 --- a/cmd/dovecot-auth-cli/test_exists_notfound.cmy +++ b/cmd/dovecot-auth-cli/test_exists_notfound.cmy @@ -11,6 +11,5 @@ userdb <- USER 1 username service=smtp userdb -> NOTFOUND 1 -c wait 1 - c <- no: +c wait 0 diff --git a/cmd/dovecot-auth-cli/test_missing_socket.cmy b/cmd/dovecot-auth-cli/test_missing_socket.cmy index a677964..08869fd 100644 --- a/cmd/dovecot-auth-cli/test_missing_socket.cmy +++ b/cmd/dovecot-auth-cli/test_missing_socket.cmy @@ -1,8 +1,8 @@ c = ./dovecot-auth-cli .missingsocket exists username c <~ no: dial unix .missingsocket-userdb -c wait 1 +c wait 0 c = ./dovecot-auth-cli .missingsocket auth username password c <~ no: dial unix .missingsocket-client -c wait 1 +c wait 0 diff --git a/test/cover.sh b/test/cover.sh index 657f21e..3368507 100755 --- a/test/cover.sh +++ b/test/cover.sh @@ -32,6 +32,10 @@ go test -tags coverage \ # Will run in coverage mode due to $COVER_DIR being set. setsid -w ./test/run.sh +# dovecot tests are also coverage-aware. +echo "dovecot cli ..." +setsid -w ./cmd/dovecot-auth-cli/test.sh + # Merge all coverage output into a single file. # Ignore protocol buffer-generated files, as they are not relevant. go run "${UTILDIR}/gocovcat.go" .coverage/*.out \