Enable coverage for failure cases
This commit is contained in:
@@ -179,9 +179,11 @@ func doMain() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var osExit = os.Exit
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := doMain()
|
err := doMain()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Exit(1)
|
osExit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,6 +32,15 @@ func init() {
|
|||||||
// Test started when the test binary is started. Only calls main.
|
// Test started when the test binary is started. Only calls main.
|
||||||
func TestSystem(t *testing.T) {
|
func TestSystem(t *testing.T) {
|
||||||
if *test {
|
if *test {
|
||||||
|
var oldExit = osExit
|
||||||
|
defer func() {
|
||||||
|
osExit = oldExit
|
||||||
|
}()
|
||||||
|
osExit = func(rc int) {
|
||||||
|
// Write the exit code to a file instead
|
||||||
|
log.Printf("Writing exit code %v to file", strconv.Itoa(rc))
|
||||||
|
ioutil.WriteFile("/var/coverage/exitCode", []byte(strconv.Itoa(rc)), 0644)
|
||||||
|
}
|
||||||
main()
|
main()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -136,11 +137,40 @@ func stopContainer(t *testing.T, cli *client.Client, ID string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCoverageExitCode(t *testing.T, orig int64) int64 {
|
||||||
|
f := filepath.Join(coverageDir(t), "exitCode")
|
||||||
|
_, err := os.Stat(f)
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
return orig
|
||||||
|
}
|
||||||
|
// Remove the file, ready for the next test
|
||||||
|
//defer os.Remove(f)
|
||||||
|
buf, err := ioutil.ReadFile(f)
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
return orig
|
||||||
|
}
|
||||||
|
rc, err := strconv.Atoi(string(buf))
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
return orig
|
||||||
|
}
|
||||||
|
t.Logf("Retrieved exit code %v from file", rc)
|
||||||
|
return int64(rc)
|
||||||
|
}
|
||||||
|
|
||||||
// waitForContainer waits until a container has exited
|
// waitForContainer waits until a container has exited
|
||||||
func waitForContainer(t *testing.T, cli *client.Client, ID string, timeout int64) int64 {
|
func waitForContainer(t *testing.T, cli *client.Client, ID string, timeout int64) int64 {
|
||||||
//ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
|
//ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
|
||||||
//defer cancel()
|
//defer cancel()
|
||||||
rc, err := cli.ContainerWait(context.Background(), ID)
|
rc, err := cli.ContainerWait(context.Background(), ID)
|
||||||
|
|
||||||
|
// COVERAGE: When running coverage, the exit code is written to a file,
|
||||||
|
// to allow the coverage to be generated (which doesn't happen for non-zero
|
||||||
|
// exit codes)
|
||||||
|
rc = getCoverageExitCode(t, rc)
|
||||||
|
|
||||||
// err := <-errC
|
// err := <-errC
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user