diff --git a/integration-cli/docker_cli_build_test.go b/integration-cli/docker_cli_build_test.go index 5a3d3efc65..c7630ae1d1 100644 --- a/integration-cli/docker_cli_build_test.go +++ b/integration-cli/docker_cli_build_test.go @@ -1526,7 +1526,7 @@ func (s *DockerSuite) TestBuildContextCleanup(c *check.C) { if err != nil { c.Fatalf("failed to list contents of tmp dir: %s", err) } - if err = testutil.CompareDirectoryEntries(entries, entriesFinal); err != nil { + if err = compareDirectoryEntries(entries, entriesFinal); err != nil { c.Fatalf("context should have been deleted, but wasn't") } @@ -1550,12 +1550,31 @@ func (s *DockerSuite) TestBuildContextCleanupFailedBuild(c *check.C) { if err != nil { c.Fatalf("failed to list contents of tmp dir: %s", err) } - if err = testutil.CompareDirectoryEntries(entries, entriesFinal); err != nil { + if err = compareDirectoryEntries(entries, entriesFinal); err != nil { c.Fatalf("context should have been deleted, but wasn't") } } +// compareDirectoryEntries compares two sets of FileInfo (usually taken from a directory) +// and returns an error if different. +func compareDirectoryEntries(e1 []os.FileInfo, e2 []os.FileInfo) error { + var ( + e1Entries = make(map[string]struct{}) + e2Entries = make(map[string]struct{}) + ) + for _, e := range e1 { + e1Entries[e.Name()] = struct{}{} + } + for _, e := range e2 { + e2Entries[e.Name()] = struct{}{} + } + if !reflect.DeepEqual(e1Entries, e2Entries) { + return fmt.Errorf("entries differ") + } + return nil +} + func (s *DockerSuite) TestBuildCmd(c *check.C) { name := "testbuildcmd" expected := "[/bin/echo Hello World]" diff --git a/integration-cli/docker_cli_build_unix_test.go b/integration-cli/docker_cli_build_unix_test.go index 11c6823255..6296489085 100644 --- a/integration-cli/docker_cli_build_unix_test.go +++ b/integration-cli/docker_cli_build_unix_test.go @@ -14,13 +14,13 @@ import ( "strings" "time" + "syscall" + "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" "github.com/docker/docker/integration-cli/cli/build/fakecontext" - "github.com/docker/docker/pkg/testutil" icmd "github.com/docker/docker/pkg/testutil/cmd" - "github.com/docker/go-units" "github.com/go-check/check" ) @@ -191,7 +191,7 @@ func (s *DockerSuite) TestBuildCancellationKillsSleep(c *check.C) { } // Get the exit status of `docker build`, check it exited because killed. - if err := buildCmd.Wait(); err != nil && !testutil.IsKilled(err) { + if err := buildCmd.Wait(); err != nil && !isKilled(err) { c.Fatalf("wait failed during build run: %T %s", err, err) } @@ -202,3 +202,17 @@ func (s *DockerSuite) TestBuildCancellationKillsSleep(c *check.C) { // ignore, done } } + +func isKilled(err error) bool { + if exitErr, ok := err.(*exec.ExitError); ok { + status, ok := exitErr.Sys().(syscall.WaitStatus) + if !ok { + return false + } + // status.ExitStatus() is required on Windows because it does not + // implement Signal() nor Signaled(). Just check it had a bad exit + // status could mean it was killed (and in tests we do kill) + return (status.Signaled() && status.Signal() == os.Kill) || status.ExitStatus() != 0 + } + return false +} diff --git a/pkg/testutil/utils.go b/pkg/testutil/utils.go index a4e5f1f37f..f7b41f48e7 100644 --- a/pkg/testutil/utils.go +++ b/pkg/testutil/utils.go @@ -9,30 +9,13 @@ import ( "os" "os/exec" "path/filepath" - "reflect" "strings" - "syscall" "time" "github.com/docker/docker/pkg/stringutils" "github.com/docker/docker/pkg/system" ) -// IsKilled process the specified error and returns whether the process was killed or not. -func IsKilled(err error) bool { - if exitErr, ok := err.(*exec.ExitError); ok { - status, ok := exitErr.Sys().(syscall.WaitStatus) - if !ok { - return false - } - // status.ExitStatus() is required on Windows because it does not - // implement Signal() nor Signaled(). Just check it had a bad exit - // status could mean it was killed (and in tests we do kill) - return (status.Signaled() && status.Signal() == os.Kill) || status.ExitStatus() != 0 - } - return false -} - func runCommandWithOutput(cmd *exec.Cmd) (output string, exitCode int, err error) { out, err := cmd.CombinedOutput() exitCode = system.ProcessExitCode(err) @@ -85,25 +68,6 @@ func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, exitCode in return runCommandWithOutput(cmds[len(cmds)-1]) } -// CompareDirectoryEntries compares two sets of FileInfo (usually taken from a directory) -// and returns an error if different. -func CompareDirectoryEntries(e1 []os.FileInfo, e2 []os.FileInfo) error { - var ( - e1Entries = make(map[string]struct{}) - e2Entries = make(map[string]struct{}) - ) - for _, e := range e1 { - e1Entries[e.Name()] = struct{}{} - } - for _, e := range e2 { - e2Entries[e.Name()] = struct{}{} - } - if !reflect.DeepEqual(e1Entries, e2Entries) { - return fmt.Errorf("entries differ") - } - return nil -} - // ListTar lists the entries of a tar. func ListTar(f io.Reader) ([]string, error) { tr := tar.NewReader(f) diff --git a/pkg/testutil/utils_test.go b/pkg/testutil/utils_test.go index 0ddf691c18..7b2ab997d9 100644 --- a/pkg/testutil/utils_test.go +++ b/pkg/testutil/utils_test.go @@ -12,47 +12,6 @@ import ( "time" ) -func TestIsKilledFalseWithNonKilledProcess(t *testing.T) { - var lsCmd *exec.Cmd - if runtime.GOOS != "windows" { - lsCmd = exec.Command("ls") - } else { - lsCmd = exec.Command("cmd", "/c", "dir") - } - - err := lsCmd.Run() - if IsKilled(err) { - t.Fatalf("Expected the ls command to not be killed, was.") - } -} - -func TestIsKilledTrueWithKilledProcess(t *testing.T) { - var longCmd *exec.Cmd - if runtime.GOOS != "windows" { - longCmd = exec.Command("top") - } else { - longCmd = exec.Command("powershell", "while ($true) { sleep 1 }") - } - - // Start a command - err := longCmd.Start() - if err != nil { - t.Fatal(err) - } - // Capture the error when *dying* - done := make(chan error, 1) - go func() { - done <- longCmd.Wait() - }() - // Then kill it - longCmd.Process.Kill() - // Get the error - err = <-done - if !IsKilled(err) { - t.Fatalf("Expected the command to be killed, was not.") - } -} - func TestRunCommandPipelineWithOutputWithNotEnoughCmds(t *testing.T) { _, _, err := RunCommandPipelineWithOutput(exec.Command("ls")) expectedError := "pipeline does not have multiple cmds" @@ -100,72 +59,6 @@ func TestRunCommandPipelineWithOutput(t *testing.T) { } } -func TestCompareDirectoryEntries(t *testing.T) { - tmpFolder, err := ioutil.TempDir("", "integration-cli-utils-compare-directories") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpFolder) - - file1 := filepath.Join(tmpFolder, "file1") - file2 := filepath.Join(tmpFolder, "file2") - os.Create(file1) - os.Create(file2) - - fi1, err := os.Stat(file1) - if err != nil { - t.Fatal(err) - } - fi1bis, err := os.Stat(file1) - if err != nil { - t.Fatal(err) - } - fi2, err := os.Stat(file2) - if err != nil { - t.Fatal(err) - } - - cases := []struct { - e1 []os.FileInfo - e2 []os.FileInfo - shouldError bool - }{ - // Empty directories - { - []os.FileInfo{}, - []os.FileInfo{}, - false, - }, - // Same FileInfos - { - []os.FileInfo{fi1}, - []os.FileInfo{fi1}, - false, - }, - // Different FileInfos but same names - { - []os.FileInfo{fi1}, - []os.FileInfo{fi1bis}, - false, - }, - // Different FileInfos, different names - { - []os.FileInfo{fi1}, - []os.FileInfo{fi2}, - true, - }, - } - for _, elt := range cases { - err := CompareDirectoryEntries(elt.e1, elt.e2) - if elt.shouldError && err == nil { - t.Fatalf("Should have return an error, did not with %v and %v", elt.e1, elt.e2) - } - if !elt.shouldError && err != nil { - t.Fatalf("Should have not returned an error, but did : %v with %v and %v", err, elt.e1, elt.e2) - } - } -} - // FIXME make an "unhappy path" test for ListTar without "panicking" :-) func TestListTar(t *testing.T) { // TODO Windows: Figure out why this fails. Should be portable.