Skip to content

Commit

Permalink
Remove testutil.CompareDirectoryEntries and IsKilled
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Nephin <dnephin@docker.com>
  • Loading branch information
dnephin committed Aug 22, 2017
1 parent d30e514 commit 6151d55
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 148 deletions.
23 changes: 21 additions & 2 deletions integration-cli/docker_cli_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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]"
Expand Down
20 changes: 17 additions & 3 deletions integration-cli/docker_cli_build_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}

Expand All @@ -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
}
36 changes: 0 additions & 36 deletions pkg/testutil/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
107 changes: 0 additions & 107 deletions pkg/testutil/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6151d55

Please sign in to comment.