From 3f3eb68a27a7fe5523a75cfa3c0f38649b7762a6 Mon Sep 17 00:00:00 2001 From: Danielle Tomlinson Date: Mon, 14 Jan 2019 13:34:24 +0100 Subject: [PATCH] chore: Fix docker test linting Due to https://github.com/tsenart/deadcode/issues/3 we can't specify these consts on their own. This moves them into the _platform_test.go files to avoid creating a package that only exposes a couple of values. --- drivers/docker/driver_unix_test.go | 52 ++++++++++++++++ ...ting_windows.go => driver_windows_test.go} | 4 +- drivers/docker/testing_unix.go | 61 ------------------- 3 files changed, 54 insertions(+), 63 deletions(-) rename drivers/docker/{testing_windows.go => driver_windows_test.go} (80%) delete mode 100644 drivers/docker/testing_unix.go diff --git a/drivers/docker/driver_unix_test.go b/drivers/docker/driver_unix_test.go index 2ff3024a9..70ceebb47 100644 --- a/drivers/docker/driver_unix_test.go +++ b/drivers/docker/driver_unix_test.go @@ -4,6 +4,9 @@ package docker import ( "fmt" + "io" + "os" + "path/filepath" "sort" "strconv" "strings" @@ -11,6 +14,7 @@ import ( "time" docker "github.com/fsouza/go-dockerclient" + "github.com/hashicorp/nomad/client/allocdir" "github.com/hashicorp/nomad/client/testutil" "github.com/hashicorp/nomad/helper/uuid" "github.com/hashicorp/nomad/plugins/drivers" @@ -641,3 +645,51 @@ func TestDockerDriver_CreateContainerConfig_MountsCombined(t *testing.T) { }) require.EqualValues(t, expectedDevices, foundDevices) } + +func newTaskConfig(variant string, command []string) TaskConfig { + // busyboxImageID is the ID stored in busybox.tar + busyboxImageID := "busybox:1.29.3" + + image := busyboxImageID + loadImage := "busybox.tar" + if variant != "" { + image = fmt.Sprintf("%s-%s", busyboxImageID, variant) + loadImage = fmt.Sprintf("busybox_%s.tar", variant) + } + + return TaskConfig{ + Image: image, + LoadImage: loadImage, + Command: command[0], + Args: command[1:], + } +} + +func copyImage(t *testing.T, taskDir *allocdir.TaskDir, image string) { + dst := filepath.Join(taskDir.LocalDir, image) + copyFile(filepath.Join("./test-resources/docker", image), dst, t) +} + +// copyFile moves an existing file to the destination +func copyFile(src, dst string, t *testing.T) { + in, err := os.Open(src) + if err != nil { + t.Fatalf("copying %v -> %v failed: %v", src, dst, err) + } + defer in.Close() + out, err := os.Create(dst) + if err != nil { + t.Fatalf("copying %v -> %v failed: %v", src, dst, err) + } + defer func() { + if err := out.Close(); err != nil { + t.Fatalf("copying %v -> %v failed: %v", src, dst, err) + } + }() + if _, err = io.Copy(out, in); err != nil { + t.Fatalf("copying %v -> %v failed: %v", src, dst, err) + } + if err := out.Sync(); err != nil { + t.Fatalf("copying %v -> %v failed: %v", src, dst, err) + } +} diff --git a/drivers/docker/testing_windows.go b/drivers/docker/driver_windows_test.go similarity index 80% rename from drivers/docker/testing_windows.go rename to drivers/docker/driver_windows_test.go index 2c5c89efc..b163353d9 100644 --- a/drivers/docker/testing_windows.go +++ b/drivers/docker/driver_windows_test.go @@ -3,7 +3,7 @@ package docker import ( - testing "github.com/mitchellh/go-testing-interface" + "testing" "github.com/hashicorp/nomad/client/allocdir" ) @@ -21,5 +21,5 @@ func newTaskConfig(variant string, command []string) TaskConfig { } } -func copyImage(t testing.T, taskDir *allocdir.TaskDir, image string) { +func copyImage(t *testing.T, taskDir *allocdir.TaskDir, image string) { } diff --git a/drivers/docker/testing_unix.go b/drivers/docker/testing_unix.go deleted file mode 100644 index c9189f134..000000000 --- a/drivers/docker/testing_unix.go +++ /dev/null @@ -1,61 +0,0 @@ -// +build darwin dragonfly freebsd linux netbsd openbsd solaris - -package docker - -import ( - "fmt" - "io" - "os" - "path/filepath" - - "github.com/hashicorp/nomad/client/allocdir" - testing "github.com/mitchellh/go-testing-interface" -) - -func newTaskConfig(variant string, command []string) TaskConfig { - // busyboxImageID is the ID stored in busybox.tar - busyboxImageID := "busybox:1.29.3" - - image := busyboxImageID - loadImage := "busybox.tar" - if variant != "" { - image = fmt.Sprintf("%s-%s", busyboxImageID, variant) - loadImage = fmt.Sprintf("busybox_%s.tar", variant) - } - - return TaskConfig{ - Image: image, - LoadImage: loadImage, - Command: command[0], - Args: command[1:], - } -} - -func copyImage(t testing.T, taskDir *allocdir.TaskDir, image string) { - dst := filepath.Join(taskDir.LocalDir, image) - copyFile(filepath.Join("./test-resources/docker", image), dst, t) -} - -// copyFile moves an existing file to the destination -func copyFile(src, dst string, t testing.T) { - in, err := os.Open(src) - if err != nil { - t.Fatalf("copying %v -> %v failed: %v", src, dst, err) - } - defer in.Close() - out, err := os.Create(dst) - if err != nil { - t.Fatalf("copying %v -> %v failed: %v", src, dst, err) - } - defer func() { - if err := out.Close(); err != nil { - t.Fatalf("copying %v -> %v failed: %v", src, dst, err) - } - }() - if _, err = io.Copy(out, in); err != nil { - t.Fatalf("copying %v -> %v failed: %v", src, dst, err) - } - if err := out.Sync(); err != nil { - t.Fatalf("copying %v -> %v failed: %v", src, dst, err) - } -}