Remove RandomTmpDirPath

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-08-22 16:08:42 -04:00
parent e8bd671815
commit 9db68f4dea
7 changed files with 27 additions and 48 deletions

View File

@ -25,10 +25,9 @@ import (
"github.com/docker/docker/integration-cli/cli/build/fakestorage"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/stringutils"
"github.com/docker/docker/pkg/testutil"
icmd "github.com/docker/docker/pkg/testutil/cmd"
"github.com/go-check/check"
"github.com/opencontainers/go-digest"
digest "github.com/opencontainers/go-digest"
)
func (s *DockerSuite) TestBuildJSONEmptyRun(c *check.C) {

View File

@ -11,7 +11,6 @@ import (
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/pkg/jsonlog"
"github.com/docker/docker/pkg/testutil"
icmd "github.com/docker/docker/pkg/testutil/cmd"
"github.com/go-check/check"
)

View File

@ -28,7 +28,6 @@ import (
"github.com/docker/docker/pkg/mount"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/docker/pkg/stringutils"
"github.com/docker/docker/pkg/testutil"
icmd "github.com/docker/docker/pkg/testutil/cmd"
"github.com/docker/docker/runconfig"
"github.com/docker/go-connections/nat"
@ -495,7 +494,7 @@ func (s *DockerSuite) TestRunVolumesFromInReadWriteMode(c *check.C) {
func (s *DockerSuite) TestVolumesFromGetsProperMode(c *check.C) {
testRequires(c, SameHostDaemon)
prefix, slash := getPrefixAndSlashFromDaemonPlatform()
hostpath := testutil.RandomTmpDirPath("test", testEnv.DaemonPlatform())
hostpath := RandomTmpDirPath("test", testEnv.DaemonPlatform())
if err := os.MkdirAll(hostpath, 0755); err != nil {
c.Fatalf("Failed to create %s: %q", hostpath, err)
}
@ -518,8 +517,8 @@ func (s *DockerSuite) TestVolumesFromGetsProperMode(c *check.C) {
// Test for GH#10618
func (s *DockerSuite) TestRunNoDupVolumes(c *check.C) {
path1 := testutil.RandomTmpDirPath("test1", testEnv.DaemonPlatform())
path2 := testutil.RandomTmpDirPath("test2", testEnv.DaemonPlatform())
path1 := RandomTmpDirPath("test1", testEnv.DaemonPlatform())
path2 := RandomTmpDirPath("test2", testEnv.DaemonPlatform())
someplace := ":/someplace"
if testEnv.DaemonPlatform() == "windows" {
@ -2183,7 +2182,7 @@ func (s *DockerSuite) TestVolumesNoCopyData(c *check.C) {
c.Fatalf("Data was copied on volumes-from but shouldn't be:\n%q", out)
}
tmpDir := testutil.RandomTmpDirPath("docker_test_bind_mount_copy_data", testEnv.DaemonPlatform())
tmpDir := RandomTmpDirPath("docker_test_bind_mount_copy_data", testEnv.DaemonPlatform())
if out, _, err := dockerCmdWithError("run", "-v", tmpDir+":/foo", "dataimage", "ls", "-lh", "/foo/bar"); err == nil || !strings.Contains(out, "No such file or directory") {
c.Fatalf("Data was copied on bind mount but shouldn't be:\n%q", out)
}

View File

@ -9,7 +9,6 @@ import (
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/request"
"github.com/docker/docker/pkg/testutil"
"github.com/go-check/check"
)
@ -45,7 +44,7 @@ func (s *DockerSuite) TestDeprecatedContainerAPIStartVolumeBinds(c *check.C) {
c.Assert(err, checker.IsNil)
c.Assert(status, checker.Equals, http.StatusCreated)
bindPath := testutil.RandomTmpDirPath("test", testEnv.DaemonPlatform())
bindPath := RandomTmpDirPath("test", testEnv.DaemonPlatform())
config = map[string]interface{}{
"Binds": []string{bindPath + ":" + path},
}
@ -72,8 +71,8 @@ func (s *DockerSuite) TestDeprecatedContainerAPIStartDupVolumeBinds(c *check.C)
c.Assert(err, checker.IsNil)
c.Assert(status, checker.Equals, http.StatusCreated)
bindPath1 := testutil.RandomTmpDirPath("test1", testEnv.DaemonPlatform())
bindPath2 := testutil.RandomTmpDirPath("test2", testEnv.DaemonPlatform())
bindPath1 := RandomTmpDirPath("test1", testEnv.DaemonPlatform())
bindPath2 := RandomTmpDirPath("test2", testEnv.DaemonPlatform())
config = map[string]interface{}{
"Binds": []string{bindPath1 + ":/tmp", bindPath2 + ":/tmp"},

View File

@ -1,10 +1,13 @@
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/docker/docker/pkg/stringutils"
"github.com/docker/docker/pkg/testutil/cmd"
)
@ -46,3 +49,18 @@ func ParseCgroupPaths(procCgroupData string) map[string]string {
}
return cgroupPaths
}
// RandomTmpDirPath provides a temporary path with rand string appended.
// does not create or checks if it exists.
func RandomTmpDirPath(s string, platform string) string {
// TODO: why doesn't this use os.TempDir() ?
tmp := "/tmp"
if platform == "windows" {
tmp = os.Getenv("TEMP")
}
path := filepath.Join(tmp, fmt.Sprintf("%s.%s", s, stringutils.GenerateRandomAlphaOnlyString(10)))
if platform == "windows" {
return filepath.FromSlash(path) // Using \
}
return filepath.ToSlash(path) // Using /
}

View File

@ -3,12 +3,9 @@ package testutil
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/docker/docker/pkg/stringutils"
"github.com/docker/docker/pkg/system"
)
@ -63,17 +60,3 @@ func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, exitCode in
// wait on last cmd
return runCommandWithOutput(cmds[len(cmds)-1])
}
// RandomTmpDirPath provides a temporary path with rand string appended.
// does not create or checks if it exists.
func RandomTmpDirPath(s string, platform string) string {
tmp := "/tmp"
if platform == "windows" {
tmp = os.Getenv("TEMP")
}
path := filepath.Join(tmp, fmt.Sprintf("%s.%s", s, stringutils.GenerateRandomAlphaOnlyString(10)))
if platform == "windows" {
return filepath.FromSlash(path) // Using \
}
return filepath.ToSlash(path) // Using /
}

View File

@ -4,7 +4,6 @@ import (
"os"
"os/exec"
"runtime"
"strings"
"testing"
)
@ -54,20 +53,3 @@ func TestRunCommandPipelineWithOutput(t *testing.T) {
t.Fatalf("Expected %s for commands %v, got out:%s, exitCode:%d, err:%v", expectedOutput, cmds, out, exitCode, err)
}
}
func TestRandomTmpDirPath(t *testing.T) {
path := RandomTmpDirPath("something", runtime.GOOS)
prefix := "/tmp/something"
if runtime.GOOS == "windows" {
prefix = os.Getenv("TEMP") + `\something`
}
expectedSize := len(prefix) + 11
if !strings.HasPrefix(path, prefix) {
t.Fatalf("Expected generated path to have '%s' as prefix, got %s'", prefix, path)
}
if len(path) != expectedSize {
t.Fatalf("Expected generated path to be %d, got %d", expectedSize, len(path))
}
}