Updates for coverage

This commit is contained in:
Arthur Barr
2017-12-18 15:38:05 +00:00
parent b6b72952e4
commit b938a5eb95
4 changed files with 56 additions and 20 deletions

View File

@@ -19,6 +19,8 @@ import (
"archive/tar"
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"log"
"os"
@@ -34,6 +36,7 @@ import (
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/moby/moby/pkg/jsonmessage"
)
func imageName() string {
@@ -44,6 +47,14 @@ func imageName() string {
return image
}
func coverage() bool {
cover := os.Getenv("TEST_COVER")
if cover == "true" || cover == "1" {
return true
}
return false
}
// coverageDir returns the host directory to use for code coverage data
func coverageDir(t *testing.T) string {
dir, err := os.Getwd()
@@ -63,6 +74,11 @@ func cleanContainer(t *testing.T, cli *client.Client, ID string) {
if err == nil {
// Log the results and continue
t.Logf("Inspected container %v: %#v", ID, i)
s, err := json.MarshalIndent(i, "", " ")
if err != nil {
t.Fatal(err)
}
t.Logf("Inspected container %v: %v", ID, string(s))
}
t.Logf("Stopping container: %v", ID)
timeout := 10 * time.Second
@@ -152,7 +168,7 @@ func getCoverageExitCode(t *testing.T, orig int64) int64 {
f := filepath.Join(coverageDir(t), "exitCode")
_, err := os.Stat(f)
if err != nil {
//t.Log(err)
t.Log(err)
return orig
}
// Remove the file, ready for the next test
@@ -177,10 +193,12 @@ func waitForContainer(t *testing.T, cli *client.Client, ID string, timeout int64
//defer cancel()
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)
if coverage() {
// 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
if err != nil {
@@ -378,9 +396,22 @@ func createImage(t *testing.T, cli *client.Client, files []struct{ Name, Body st
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
// Sleep for two seconds, to try and prevent "No such image" errors
time.Sleep(2 * time.Second)
// resp (ImageBuildResponse) contains a series of JSON messages
dec := json.NewDecoder(resp.Body)
for {
m := jsonmessage.JSONMessage{}
err := dec.Decode(&m)
if m.Error != nil {
t.Fatal(m.ErrorMessage)
}
t.Log(strings.TrimSpace(m.Stream))
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
}
return tag
}