Browse code

Move utils.TestDirectory to pkg/testutil/tempfile

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2016/12/12 17:28:32
Showing 3 changed files
... ...
@@ -1,19 +1,23 @@
1 1
 package distribution
2 2
 
3 3
 import (
4
+	"fmt"
5
+	"io/ioutil"
4 6
 	"net/http"
5 7
 	"net/http/httptest"
6 8
 	"net/url"
7 9
 	"os"
10
+	"runtime"
8 11
 	"strings"
9 12
 	"testing"
10 13
 
11 14
 	"github.com/Sirupsen/logrus"
12 15
 	"github.com/docker/docker/api/types"
13 16
 	registrytypes "github.com/docker/docker/api/types/registry"
17
+	"github.com/docker/docker/pkg/archive"
18
+	"github.com/docker/docker/pkg/stringid"
14 19
 	"github.com/docker/docker/reference"
15 20
 	"github.com/docker/docker/registry"
16
-	"github.com/docker/docker/utils"
17 21
 	"golang.org/x/net/context"
18 22
 )
19 23
 
... ...
@@ -38,7 +42,7 @@ func (h *tokenPassThruHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
38 38
 }
39 39
 
40 40
 func testTokenPassThru(t *testing.T, ts *httptest.Server) {
41
-	tmp, err := utils.TestDirectory("")
41
+	tmp, err := testDirectory("")
42 42
 	if err != nil {
43 43
 		t.Fatal(err)
44 44
 	}
... ...
@@ -131,3 +135,36 @@ func TestTokenPassThruDifferentHost(t *testing.T) {
131 131
 		t.Fatal("Redirect should not forward Authorization header to another host")
132 132
 	}
133 133
 }
134
+
135
+// TestDirectory creates a new temporary directory and returns its path.
136
+// The contents of directory at path `templateDir` is copied into the
137
+// new directory.
138
+func testDirectory(templateDir string) (dir string, err error) {
139
+	testID := stringid.GenerateNonCryptoID()[:4]
140
+	prefix := fmt.Sprintf("docker-test%s-%s-", testID, getCallerName(2))
141
+	if prefix == "" {
142
+		prefix = "docker-test-"
143
+	}
144
+	dir, err = ioutil.TempDir("", prefix)
145
+	if err = os.Remove(dir); err != nil {
146
+		return
147
+	}
148
+	if templateDir != "" {
149
+		if err = archive.CopyWithTar(templateDir, dir); err != nil {
150
+			return
151
+		}
152
+	}
153
+	return
154
+}
155
+
156
+// getCallerName introspects the call stack and returns the name of the
157
+// function `depth` levels down in the stack.
158
+func getCallerName(depth int) string {
159
+	// Use the caller function name as a prefix.
160
+	// This helps trace temp directories back to their test.
161
+	pc, _, _, _ := runtime.Caller(depth + 1)
162
+	callerLongName := runtime.FuncForPC(pc).Name()
163
+	parts := strings.Split(callerLongName, ".")
164
+	callerShortName := parts[len(parts)-1]
165
+	return callerShortName
166
+}
... ...
@@ -1,10 +1,9 @@
1 1
 package tempfile
2 2
 
3 3
 import (
4
+	"github.com/docker/docker/pkg/testutil/assert"
4 5
 	"io/ioutil"
5 6
 	"os"
6
-
7
-	"github.com/docker/docker/pkg/testutil/assert"
8 7
 )
9 8
 
10 9
 // TempFile is a temporary file that can be used with unit tests. TempFile
... ...
@@ -1,53 +1,9 @@
1 1
 package utils
2 2
 
3 3
 import (
4
-	"fmt"
5
-	"io/ioutil"
6
-	"os"
7
-	"runtime"
8 4
 	"strings"
9
-
10
-	"github.com/docker/docker/pkg/archive"
11
-	"github.com/docker/docker/pkg/stringid"
12 5
 )
13 6
 
14
-var globalTestID string
15
-
16
-// TestDirectory creates a new temporary directory and returns its path.
17
-// The contents of directory at path `templateDir` is copied into the
18
-// new directory.
19
-func TestDirectory(templateDir string) (dir string, err error) {
20
-	if globalTestID == "" {
21
-		globalTestID = stringid.GenerateNonCryptoID()[:4]
22
-	}
23
-	prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, GetCallerName(2))
24
-	if prefix == "" {
25
-		prefix = "docker-test-"
26
-	}
27
-	dir, err = ioutil.TempDir("", prefix)
28
-	if err = os.Remove(dir); err != nil {
29
-		return
30
-	}
31
-	if templateDir != "" {
32
-		if err = archive.CopyWithTar(templateDir, dir); err != nil {
33
-			return
34
-		}
35
-	}
36
-	return
37
-}
38
-
39
-// GetCallerName introspects the call stack and returns the name of the
40
-// function `depth` levels down in the stack.
41
-func GetCallerName(depth int) string {
42
-	// Use the caller function name as a prefix.
43
-	// This helps trace temp directories back to their test.
44
-	pc, _, _, _ := runtime.Caller(depth + 1)
45
-	callerLongName := runtime.FuncForPC(pc).Name()
46
-	parts := strings.Split(callerLongName, ".")
47
-	callerShortName := parts[len(parts)-1]
48
-	return callerShortName
49
-}
50
-
51 7
 // ReplaceOrAppendEnvValues returns the defaults with the overrides either
52 8
 // replaced by env key or appended to the list
53 9
 func ReplaceOrAppendEnvValues(defaults, overrides []string) []string {