Also enable GC in linting to reduce memory usage.
Signed-off-by: Daniel Nephin <dnephin@docker.com>
... | ... |
@@ -9,6 +9,7 @@ import ( |
9 | 9 |
"github.com/docker/docker/api/types/container" |
10 | 10 |
"github.com/docker/docker/builder/dockerfile/parser" |
11 | 11 |
"github.com/docker/docker/builder/remotecontext" |
12 |
+ "github.com/docker/docker/internal/testutil" |
|
12 | 13 |
"github.com/docker/docker/pkg/archive" |
13 | 14 |
"github.com/docker/docker/pkg/reexec" |
14 | 15 |
) |
... | ... |
@@ -197,14 +198,6 @@ func executeTestCase(t *testing.T, testCase dispatchTestCase) { |
197 | 197 |
shlex: shlex, |
198 | 198 |
source: context, |
199 | 199 |
} |
200 |
- state, err = b.dispatch(opts) |
|
201 |
- |
|
202 |
- if err == nil { |
|
203 |
- t.Fatalf("No error when executing test %s", testCase.name) |
|
204 |
- } |
|
205 |
- |
|
206 |
- if !strings.Contains(err.Error(), testCase.expectedError) { |
|
207 |
- t.Fatalf("Wrong error message. Should be \"%s\". Got \"%s\"", testCase.expectedError, err.Error()) |
|
208 |
- } |
|
209 |
- |
|
200 |
+ _, err = b.dispatch(opts) |
|
201 |
+ testutil.ErrorContains(t, err, testCase.expectedError) |
|
210 | 202 |
} |
... | ... |
@@ -149,15 +149,12 @@ func (cs *CachableSource) normalize(path string) (cleanpath, fullpath string, er |
149 | 149 |
// Hash returns a hash for a single file in the source |
150 | 150 |
func (cs *CachableSource) Hash(path string) (string, error) { |
151 | 151 |
n := cs.getRoot() |
152 |
- sum := "" |
|
153 | 152 |
// TODO: check this for symlinks |
154 | 153 |
v, ok := n.Get([]byte(path)) |
155 | 154 |
if !ok { |
156 |
- sum = path |
|
157 |
- } else { |
|
158 |
- sum = v.(*fileInfo).sum |
|
155 |
+ return path, nil |
|
159 | 156 |
} |
160 |
- return sum, nil |
|
157 |
+ return v.(*fileInfo).sum, nil |
|
161 | 158 |
} |
162 | 159 |
|
163 | 160 |
// Root returns a root directory for the source |
... | ... |
@@ -88,13 +88,12 @@ func (cli *Client) ServiceCreate(ctx context.Context, service swarm.ServiceSpec, |
88 | 88 |
|
89 | 89 |
func imageDigestAndPlatforms(ctx context.Context, cli DistributionAPIClient, image, encodedAuth string) (string, []swarm.Platform, error) { |
90 | 90 |
distributionInspect, err := cli.DistributionInspect(ctx, image, encodedAuth) |
91 |
- imageWithDigest := image |
|
92 | 91 |
var platforms []swarm.Platform |
93 | 92 |
if err != nil { |
94 | 93 |
return "", nil, err |
95 | 94 |
} |
96 | 95 |
|
97 |
- imageWithDigest = imageWithDigestString(image, distributionInspect.Descriptor.Digest) |
|
96 |
+ imageWithDigest := imageWithDigestString(image, distributionInspect.Descriptor.Digest) |
|
98 | 97 |
|
99 | 98 |
if len(distributionInspect.Platforms) > 0 { |
100 | 99 |
platforms = make([]swarm.Platform, 0, len(distributionInspect.Platforms)) |
... | ... |
@@ -105,12 +104,12 @@ func imageDigestAndPlatforms(ctx context.Context, cli DistributionAPIClient, ima |
105 | 105 |
// something like "armv7l" (includes the variant), which causes arm images |
106 | 106 |
// to stop working with swarm mode. This patch removes the architecture |
107 | 107 |
// constraint for arm images to ensure tasks get scheduled. |
108 |
- arch := strings.ToLower(p.Architecture) |
|
109 |
- if arch == "arm" { |
|
108 |
+ arch := p.Architecture |
|
109 |
+ if strings.ToLower(arch) == "arm" { |
|
110 | 110 |
arch = "" |
111 | 111 |
} |
112 | 112 |
platforms = append(platforms, swarm.Platform{ |
113 |
- Architecture: p.Architecture, |
|
113 |
+ Architecture: arch, |
|
114 | 114 |
OS: p.OS, |
115 | 115 |
}) |
116 | 116 |
} |
... | ... |
@@ -17,6 +17,7 @@ import ( |
17 | 17 |
"github.com/docker/docker/volume/drivers" |
18 | 18 |
"github.com/docker/docker/volume/local" |
19 | 19 |
"github.com/docker/docker/volume/store" |
20 |
+ "github.com/stretchr/testify/require" |
|
20 | 21 |
) |
21 | 22 |
|
22 | 23 |
type fakeContainerGetter struct { |
... | ... |
@@ -289,13 +290,12 @@ func TestMigratePre17Volumes(t *testing.T) { |
289 | 289 |
containerRoot := filepath.Join(rootDir, "containers") |
290 | 290 |
cid := "1234" |
291 | 291 |
err = os.MkdirAll(filepath.Join(containerRoot, cid), 0755) |
292 |
+ require.NoError(t, err) |
|
292 | 293 |
|
293 | 294 |
vid := "5678" |
294 | 295 |
vfsPath := filepath.Join(rootDir, "vfs", "dir", vid) |
295 | 296 |
err = os.MkdirAll(vfsPath, 0755) |
296 |
- if err != nil { |
|
297 |
- t.Fatal(err) |
|
298 |
- } |
|
297 |
+ require.NoError(t, err) |
|
299 | 298 |
|
300 | 299 |
config := []byte(` |
301 | 300 |
{ |
... | ... |
@@ -4,92 +4,77 @@ import ( |
4 | 4 |
"fmt" |
5 | 5 |
"testing" |
6 | 6 |
"time" |
7 |
-) |
|
8 |
- |
|
9 |
-func TestDiscoveryOpts(t *testing.T) { |
|
10 |
- clusterOpts := map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "5"} |
|
11 |
- heartbeat, ttl, err := discoveryOpts(clusterOpts) |
|
12 |
- if err == nil { |
|
13 |
- t.Fatal("discovery.ttl < discovery.heartbeat must fail") |
|
14 |
- } |
|
15 |
- |
|
16 |
- clusterOpts = map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "10"} |
|
17 |
- heartbeat, ttl, err = discoveryOpts(clusterOpts) |
|
18 |
- if err == nil { |
|
19 |
- t.Fatal("discovery.ttl == discovery.heartbeat must fail") |
|
20 |
- } |
|
21 |
- |
|
22 |
- clusterOpts = map[string]string{"discovery.heartbeat": "-10", "discovery.ttl": "10"} |
|
23 |
- heartbeat, ttl, err = discoveryOpts(clusterOpts) |
|
24 |
- if err == nil { |
|
25 |
- t.Fatal("negative discovery.heartbeat must fail") |
|
26 |
- } |
|
27 | 7 |
|
28 |
- clusterOpts = map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "-10"} |
|
29 |
- heartbeat, ttl, err = discoveryOpts(clusterOpts) |
|
30 |
- if err == nil { |
|
31 |
- t.Fatal("negative discovery.ttl must fail") |
|
32 |
- } |
|
33 |
- |
|
34 |
- clusterOpts = map[string]string{"discovery.heartbeat": "invalid"} |
|
35 |
- heartbeat, ttl, err = discoveryOpts(clusterOpts) |
|
36 |
- if err == nil { |
|
37 |
- t.Fatal("invalid discovery.heartbeat must fail") |
|
38 |
- } |
|
39 |
- |
|
40 |
- clusterOpts = map[string]string{"discovery.ttl": "invalid"} |
|
41 |
- heartbeat, ttl, err = discoveryOpts(clusterOpts) |
|
42 |
- if err == nil { |
|
43 |
- t.Fatal("invalid discovery.ttl must fail") |
|
44 |
- } |
|
45 |
- |
|
46 |
- clusterOpts = map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "20"} |
|
47 |
- heartbeat, ttl, err = discoveryOpts(clusterOpts) |
|
48 |
- if err != nil { |
|
49 |
- t.Fatal(err) |
|
50 |
- } |
|
8 |
+ "github.com/stretchr/testify/assert" |
|
9 |
+ "github.com/stretchr/testify/require" |
|
10 |
+) |
|
51 | 11 |
|
52 |
- if heartbeat != 10*time.Second { |
|
53 |
- t.Fatalf("Heartbeat - Expected : %v, Actual : %v", 10*time.Second, heartbeat) |
|
12 |
+func TestDiscoveryOptsErrors(t *testing.T) { |
|
13 |
+ var testcases = []struct { |
|
14 |
+ doc string |
|
15 |
+ opts map[string]string |
|
16 |
+ }{ |
|
17 |
+ { |
|
18 |
+ doc: "discovery.ttl < discovery.heartbeat", |
|
19 |
+ opts: map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "5"}, |
|
20 |
+ }, |
|
21 |
+ { |
|
22 |
+ doc: "discovery.ttl == discovery.heartbeat", |
|
23 |
+ opts: map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "10"}, |
|
24 |
+ }, |
|
25 |
+ { |
|
26 |
+ doc: "negative discovery.heartbeat", |
|
27 |
+ opts: map[string]string{"discovery.heartbeat": "-10", "discovery.ttl": "10"}, |
|
28 |
+ }, |
|
29 |
+ { |
|
30 |
+ doc: "negative discovery.ttl", |
|
31 |
+ opts: map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "-10"}, |
|
32 |
+ }, |
|
33 |
+ { |
|
34 |
+ doc: "invalid discovery.heartbeat", |
|
35 |
+ opts: map[string]string{"discovery.heartbeat": "invalid"}, |
|
36 |
+ }, |
|
37 |
+ { |
|
38 |
+ doc: "invalid discovery.ttl", |
|
39 |
+ opts: map[string]string{"discovery.ttl": "invalid"}, |
|
40 |
+ }, |
|
41 |
+ } |
|
42 |
+ |
|
43 |
+ for _, testcase := range testcases { |
|
44 |
+ _, _, err := discoveryOpts(testcase.opts) |
|
45 |
+ assert.Error(t, err, testcase.doc) |
|
54 | 46 |
} |
47 |
+} |
|
55 | 48 |
|
56 |
- if ttl != 20*time.Second { |
|
57 |
- t.Fatalf("TTL - Expected : %v, Actual : %v", 20*time.Second, ttl) |
|
58 |
- } |
|
49 |
+func TestDiscoveryOpts(t *testing.T) { |
|
50 |
+ clusterOpts := map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "20"} |
|
51 |
+ heartbeat, ttl, err := discoveryOpts(clusterOpts) |
|
52 |
+ require.NoError(t, err) |
|
53 |
+ assert.Equal(t, 10*time.Second, heartbeat) |
|
54 |
+ assert.Equal(t, 20*time.Second, ttl) |
|
59 | 55 |
|
60 | 56 |
clusterOpts = map[string]string{"discovery.heartbeat": "10"} |
61 | 57 |
heartbeat, ttl, err = discoveryOpts(clusterOpts) |
62 |
- if err != nil { |
|
63 |
- t.Fatal(err) |
|
64 |
- } |
|
65 |
- |
|
66 |
- if heartbeat != 10*time.Second { |
|
67 |
- t.Fatalf("Heartbeat - Expected : %v, Actual : %v", 10*time.Second, heartbeat) |
|
68 |
- } |
|
69 |
- |
|
70 |
- expected := 10 * defaultDiscoveryTTLFactor * time.Second |
|
71 |
- if ttl != expected { |
|
72 |
- t.Fatalf("TTL - Expected : %v, Actual : %v", expected, ttl) |
|
73 |
- } |
|
58 |
+ require.NoError(t, err) |
|
59 |
+ assert.Equal(t, 10*time.Second, heartbeat) |
|
60 |
+ assert.Equal(t, 10*defaultDiscoveryTTLFactor*time.Second, ttl) |
|
74 | 61 |
|
75 | 62 |
clusterOpts = map[string]string{"discovery.ttl": "30"} |
76 | 63 |
heartbeat, ttl, err = discoveryOpts(clusterOpts) |
77 |
- if err != nil { |
|
78 |
- t.Fatal(err) |
|
79 |
- } |
|
64 |
+ require.NoError(t, err) |
|
80 | 65 |
|
81 | 66 |
if ttl != 30*time.Second { |
82 | 67 |
t.Fatalf("TTL - Expected : %v, Actual : %v", 30*time.Second, ttl) |
83 | 68 |
} |
84 | 69 |
|
85 |
- expected = 30 * time.Second / defaultDiscoveryTTLFactor |
|
70 |
+ expected := 30 * time.Second / defaultDiscoveryTTLFactor |
|
86 | 71 |
if heartbeat != expected { |
87 | 72 |
t.Fatalf("Heartbeat - Expected : %v, Actual : %v", expected, heartbeat) |
88 | 73 |
} |
89 | 74 |
|
90 | 75 |
discoveryTTL := fmt.Sprintf("%d", defaultDiscoveryTTLFactor-1) |
91 | 76 |
clusterOpts = map[string]string{"discovery.ttl": discoveryTTL} |
92 |
- heartbeat, ttl, err = discoveryOpts(clusterOpts) |
|
77 |
+ heartbeat, _, err = discoveryOpts(clusterOpts) |
|
93 | 78 |
if err == nil && heartbeat == 0 { |
94 | 79 |
t.Fatal("discovery.heartbeat must be positive") |
95 | 80 |
} |
... | ... |
@@ -1,21 +1,15 @@ |
1 | 1 |
package distribution |
2 | 2 |
|
3 | 3 |
import ( |
4 |
- "fmt" |
|
5 |
- "io/ioutil" |
|
6 | 4 |
"net/http" |
7 | 5 |
"net/http/httptest" |
8 | 6 |
"net/url" |
9 |
- "os" |
|
10 |
- "runtime" |
|
11 | 7 |
"strings" |
12 | 8 |
"testing" |
13 | 9 |
|
14 | 10 |
"github.com/docker/distribution/reference" |
15 | 11 |
"github.com/docker/docker/api/types" |
16 | 12 |
registrytypes "github.com/docker/docker/api/types/registry" |
17 |
- "github.com/docker/docker/pkg/archive" |
|
18 |
- "github.com/docker/docker/pkg/stringid" |
|
19 | 13 |
"github.com/docker/docker/registry" |
20 | 14 |
"github.com/sirupsen/logrus" |
21 | 15 |
"golang.org/x/net/context" |
... | ... |
@@ -42,12 +36,6 @@ func (h *tokenPassThruHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) |
42 | 42 |
} |
43 | 43 |
|
44 | 44 |
func testTokenPassThru(t *testing.T, ts *httptest.Server) { |
45 |
- tmp, err := testDirectory("") |
|
46 |
- if err != nil { |
|
47 |
- t.Fatal(err) |
|
48 |
- } |
|
49 |
- defer os.RemoveAll(tmp) |
|
50 |
- |
|
51 | 45 |
uri, err := url.Parse(ts.URL) |
52 | 46 |
if err != nil { |
53 | 47 |
t.Fatalf("could not parse url from test server: %v", err) |
... | ... |
@@ -137,36 +125,3 @@ func TestTokenPassThruDifferentHost(t *testing.T) { |
137 | 137 |
t.Fatal("Redirect should not forward Authorization header to another host") |
138 | 138 |
} |
139 | 139 |
} |
140 |
- |
|
141 |
-// testDirectory creates a new temporary directory and returns its path. |
|
142 |
-// The contents of directory at path `templateDir` is copied into the |
|
143 |
-// new directory. |
|
144 |
-func testDirectory(templateDir string) (dir string, err error) { |
|
145 |
- testID := stringid.GenerateNonCryptoID()[:4] |
|
146 |
- prefix := fmt.Sprintf("docker-test%s-%s-", testID, getCallerName(2)) |
|
147 |
- if prefix == "" { |
|
148 |
- prefix = "docker-test-" |
|
149 |
- } |
|
150 |
- dir, err = ioutil.TempDir("", prefix) |
|
151 |
- if err = os.Remove(dir); err != nil { |
|
152 |
- return |
|
153 |
- } |
|
154 |
- if templateDir != "" { |
|
155 |
- if err = archive.NewDefaultArchiver().CopyWithTar(templateDir, dir); err != nil { |
|
156 |
- return |
|
157 |
- } |
|
158 |
- } |
|
159 |
- return |
|
160 |
-} |
|
161 |
- |
|
162 |
-// getCallerName introspects the call stack and returns the name of the |
|
163 |
-// function `depth` levels down in the stack. |
|
164 |
-func getCallerName(depth int) string { |
|
165 |
- // Use the caller function name as a prefix. |
|
166 |
- // This helps trace temp directories back to their test. |
|
167 |
- pc, _, _, _ := runtime.Caller(depth + 1) |
|
168 |
- callerLongName := runtime.FuncForPC(pc).Name() |
|
169 |
- parts := strings.Split(callerLongName, ".") |
|
170 |
- callerShortName := parts[len(parts)-1] |
|
171 |
- return callerShortName |
|
172 |
-} |
... | ... |
@@ -1,6 +1,7 @@ |
1 | 1 |
{ |
2 | 2 |
"Vendor": true, |
3 | 3 |
"Deadline": "2m", |
4 |
+ "EnableGC": true, |
|
4 | 5 |
"Sort": ["linter", "severity", "path"], |
5 | 6 |
"Exclude": [ |
6 | 7 |
".*\\.pb\\.go", |
... | ... |
@@ -17,6 +18,7 @@ |
17 | 17 |
"gofmt", |
18 | 18 |
"goimports", |
19 | 19 |
"golint", |
20 |
+ "ineffassign", |
|
20 | 21 |
"interfacer", |
21 | 22 |
"unconvert", |
22 | 23 |
"vet" |
... | ... |
@@ -41,10 +41,10 @@ func TestRestore(t *testing.T) { |
41 | 41 |
assert.Equal(t, "abc", img1.Comment) |
42 | 42 |
assert.Equal(t, "def", img2.Comment) |
43 | 43 |
|
44 |
- p, err := is.GetParent(ID(id1)) |
|
44 |
+ _, err = is.GetParent(ID(id1)) |
|
45 | 45 |
testutil.ErrorContains(t, err, "failed to read metadata") |
46 | 46 |
|
47 |
- p, err = is.GetParent(ID(id2)) |
|
47 |
+ p, err := is.GetParent(ID(id2)) |
|
48 | 48 |
assert.NoError(t, err) |
49 | 49 |
assert.Equal(t, ID(id1), p) |
50 | 50 |
|
... | ... |
@@ -1183,8 +1183,10 @@ func TestUntarInvalidSymlink(t *testing.T) { |
1183 | 1183 |
func TestTempArchiveCloseMultipleTimes(t *testing.T) { |
1184 | 1184 |
reader := ioutil.NopCloser(strings.NewReader("hello")) |
1185 | 1185 |
tempArchive, err := NewTempArchive(reader, "") |
1186 |
+ require.NoError(t, err) |
|
1186 | 1187 |
buf := make([]byte, 10) |
1187 | 1188 |
n, err := tempArchive.Read(buf) |
1189 |
+ require.NoError(t, err) |
|
1188 | 1190 |
if n != 5 { |
1189 | 1191 |
t.Fatalf("Expected to read 5 bytes. Read %d instead", n) |
1190 | 1192 |
} |
... | ... |
@@ -188,6 +188,7 @@ func TestChangesWithChangesGH13590(t *testing.T) { |
188 | 188 |
t.Skip("symlinks on Windows") |
189 | 189 |
} |
190 | 190 |
baseLayer, err := ioutil.TempDir("", "docker-changes-test.") |
191 |
+ require.NoError(t, err) |
|
191 | 192 |
defer os.RemoveAll(baseLayer) |
192 | 193 |
|
193 | 194 |
dir3 := path.Join(baseLayer, "dir1/dir2/dir3") |
... | ... |
@@ -197,6 +198,7 @@ func TestChangesWithChangesGH13590(t *testing.T) { |
197 | 197 |
ioutil.WriteFile(file, []byte("hello"), 0666) |
198 | 198 |
|
199 | 199 |
layer, err := ioutil.TempDir("", "docker-changes-test2.") |
200 |
+ require.NoError(t, err) |
|
200 | 201 |
defer os.RemoveAll(layer) |
201 | 202 |
|
202 | 203 |
// Test creating a new file |
... | ... |
@@ -219,6 +221,7 @@ func TestChangesWithChangesGH13590(t *testing.T) { |
219 | 219 |
|
220 | 220 |
// Now test changing a file |
221 | 221 |
layer, err = ioutil.TempDir("", "docker-changes-test3.") |
222 |
+ require.NoError(t, err) |
|
222 | 223 |
defer os.RemoveAll(layer) |
223 | 224 |
|
224 | 225 |
if err := copyDir(baseLayer+"/dir1", layer+"/"); err != nil { |
... | ... |
@@ -465,6 +468,7 @@ func TestChangesSizeWithOnlyDeleteChanges(t *testing.T) { |
465 | 465 |
|
466 | 466 |
func TestChangesSize(t *testing.T) { |
467 | 467 |
parentPath, err := ioutil.TempDir("", "docker-changes-test") |
468 |
+ require.NoError(t, err) |
|
468 | 469 |
defer os.RemoveAll(parentPath) |
469 | 470 |
addition := path.Join(parentPath, "addition") |
470 | 471 |
err = ioutil.WriteFile(addition, []byte{0x01, 0x01, 0x01}, 0744) |
... | ... |
@@ -10,6 +10,8 @@ import ( |
10 | 10 |
"path/filepath" |
11 | 11 |
"reflect" |
12 | 12 |
"testing" |
13 |
+ |
|
14 |
+ "github.com/stretchr/testify/require" |
|
13 | 15 |
) |
14 | 16 |
|
15 | 17 |
func TestLocalSocket(t *testing.T) { |
... | ... |
@@ -89,6 +91,7 @@ func TestScan(t *testing.T) { |
89 | 89 |
|
90 | 90 |
r := newLocalRegistry() |
91 | 91 |
p, err := r.Plugin(name) |
92 |
+ require.NoError(t, err) |
|
92 | 93 |
|
93 | 94 |
pluginNamesNotEmpty, err := Scan() |
94 | 95 |
if err != nil { |
... | ... |
@@ -7,12 +7,13 @@ import "testing" |
7 | 7 |
// TestCheckSystemDriveAndRemoveDriveLetter tests CheckSystemDriveAndRemoveDriveLetter |
8 | 8 |
func TestCheckSystemDriveAndRemoveDriveLetter(t *testing.T) { |
9 | 9 |
// Fails if not C drive. |
10 |
- path, err := CheckSystemDriveAndRemoveDriveLetter(`d:\`) |
|
10 |
+ _, err := CheckSystemDriveAndRemoveDriveLetter(`d:\`) |
|
11 | 11 |
if err == nil || (err != nil && err.Error() != "The specified path is not on the system drive (C:)") { |
12 | 12 |
t.Fatalf("Expected error for d:") |
13 | 13 |
} |
14 | 14 |
|
15 | 15 |
// Single character is unchanged |
16 |
+ var path string |
|
16 | 17 |
if path, err = CheckSystemDriveAndRemoveDriveLetter("z"); err != nil { |
17 | 18 |
t.Fatalf("Single character should pass") |
18 | 19 |
} |
... | ... |
@@ -6,6 +6,8 @@ import ( |
6 | 6 |
"os" |
7 | 7 |
"syscall" |
8 | 8 |
"testing" |
9 |
+ |
|
10 |
+ "github.com/stretchr/testify/require" |
|
9 | 11 |
) |
10 | 12 |
|
11 | 13 |
// TestFromStatT tests fromStatT for a tempfile |
... | ... |
@@ -15,11 +17,10 @@ func TestFromStatT(t *testing.T) { |
15 | 15 |
|
16 | 16 |
stat := &syscall.Stat_t{} |
17 | 17 |
err := syscall.Lstat(file, stat) |
18 |
+ require.NoError(t, err) |
|
18 | 19 |
|
19 | 20 |
s, err := fromStatT(stat) |
20 |
- if err != nil { |
|
21 |
- t.Fatal(err) |
|
22 |
- } |
|
21 |
+ require.NoError(t, err) |
|
23 | 22 |
|
24 | 23 |
if stat.Mode != s.Mode() { |
25 | 24 |
t.Fatal("got invalid mode") |
... | ... |
@@ -16,6 +16,9 @@ import ( |
16 | 16 |
"os" |
17 | 17 |
"strings" |
18 | 18 |
"testing" |
19 |
+ |
|
20 |
+ "github.com/stretchr/testify/assert" |
|
21 |
+ "github.com/stretchr/testify/require" |
|
19 | 22 |
) |
20 | 23 |
|
21 | 24 |
type testLayer struct { |
... | ... |
@@ -222,17 +225,13 @@ func TestNewTarSumForLabel(t *testing.T) { |
222 | 222 |
func TestEmptyTar(t *testing.T) { |
223 | 223 |
// Test without gzip. |
224 | 224 |
ts, err := emptyTarSum(false) |
225 |
- if err != nil { |
|
226 |
- t.Fatal(err) |
|
227 |
- } |
|
225 |
+ require.NoError(t, err) |
|
228 | 226 |
|
229 | 227 |
zeroBlock := make([]byte, 1024) |
230 | 228 |
buf := new(bytes.Buffer) |
231 | 229 |
|
232 | 230 |
n, err := io.Copy(buf, ts) |
233 |
- if err != nil { |
|
234 |
- t.Fatal(err) |
|
235 |
- } |
|
231 |
+ require.NoError(t, err) |
|
236 | 232 |
|
237 | 233 |
if n != int64(len(zeroBlock)) || !bytes.Equal(buf.Bytes(), zeroBlock) { |
238 | 234 |
t.Fatalf("tarSum did not write the correct number of zeroed bytes: %d", n) |
... | ... |
@@ -247,19 +246,16 @@ func TestEmptyTar(t *testing.T) { |
247 | 247 |
|
248 | 248 |
// Test with gzip. |
249 | 249 |
ts, err = emptyTarSum(true) |
250 |
- if err != nil { |
|
251 |
- t.Fatal(err) |
|
252 |
- } |
|
250 |
+ require.NoError(t, err) |
|
253 | 251 |
buf.Reset() |
254 | 252 |
|
255 |
- n, err = io.Copy(buf, ts) |
|
256 |
- if err != nil { |
|
257 |
- t.Fatal(err) |
|
258 |
- } |
|
253 |
+ _, err = io.Copy(buf, ts) |
|
254 |
+ require.NoError(t, err) |
|
259 | 255 |
|
260 | 256 |
bufgz := new(bytes.Buffer) |
261 | 257 |
gz := gzip.NewWriter(bufgz) |
262 | 258 |
n, err = io.Copy(gz, bytes.NewBuffer(zeroBlock)) |
259 |
+ require.NoError(t, err) |
|
263 | 260 |
gz.Close() |
264 | 261 |
gzBytes := bufgz.Bytes() |
265 | 262 |
|
... | ... |
@@ -279,10 +275,7 @@ func TestEmptyTar(t *testing.T) { |
279 | 279 |
} |
280 | 280 |
|
281 | 281 |
resultSum = ts.Sum(nil) |
282 |
- |
|
283 |
- if resultSum != expectedSum { |
|
284 |
- t.Fatalf("expected [%s] but got [%s]", expectedSum, resultSum) |
|
285 |
- } |
|
282 |
+ assert.Equal(t, expectedSum, resultSum) |
|
286 | 283 |
} |
287 | 284 |
|
288 | 285 |
var ( |
... | ... |
@@ -1,43 +1,25 @@ |
1 | 1 |
package term |
2 | 2 |
|
3 |
-import "testing" |
|
3 |
+import ( |
|
4 |
+ "testing" |
|
5 |
+ |
|
6 |
+ "github.com/stretchr/testify/assert" |
|
7 |
+ "github.com/stretchr/testify/require" |
|
8 |
+) |
|
4 | 9 |
|
5 | 10 |
func TestToBytes(t *testing.T) { |
6 | 11 |
codes, err := ToBytes("ctrl-a,a") |
7 |
- if err != nil { |
|
8 |
- t.Fatal(err) |
|
9 |
- } |
|
10 |
- if len(codes) != 2 { |
|
11 |
- t.Fatalf("Expected 2 codes, got %d", len(codes)) |
|
12 |
- } |
|
13 |
- if codes[0] != 1 || codes[1] != 97 { |
|
14 |
- t.Fatalf("Expected '1' '97', got '%d' '%d'", codes[0], codes[1]) |
|
15 |
- } |
|
12 |
+ require.NoError(t, err) |
|
13 |
+ assert.Equal(t, []byte{1, 97}, codes) |
|
16 | 14 |
|
17 |
- codes, err = ToBytes("shift-z") |
|
18 |
- if err == nil { |
|
19 |
- t.Fatalf("Expected error, got none") |
|
20 |
- } |
|
15 |
+ _, err = ToBytes("shift-z") |
|
16 |
+ assert.Error(t, err) |
|
21 | 17 |
|
22 | 18 |
codes, err = ToBytes("ctrl-@,ctrl-[,~,ctrl-o") |
23 |
- if err != nil { |
|
24 |
- t.Fatal(err) |
|
25 |
- } |
|
26 |
- if len(codes) != 4 { |
|
27 |
- t.Fatalf("Expected 4 codes, got %d", len(codes)) |
|
28 |
- } |
|
29 |
- if codes[0] != 0 || codes[1] != 27 || codes[2] != 126 || codes[3] != 15 { |
|
30 |
- t.Fatalf("Expected '0' '27' '126', '15', got '%d' '%d' '%d' '%d'", codes[0], codes[1], codes[2], codes[3]) |
|
31 |
- } |
|
19 |
+ require.NoError(t, err) |
|
20 |
+ assert.Equal(t, []byte{0, 27, 126, 15}, codes) |
|
32 | 21 |
|
33 | 22 |
codes, err = ToBytes("DEL,+") |
34 |
- if err != nil { |
|
35 |
- t.Fatal(err) |
|
36 |
- } |
|
37 |
- if len(codes) != 2 { |
|
38 |
- t.Fatalf("Expected 2 codes, got %d", len(codes)) |
|
39 |
- } |
|
40 |
- if codes[0] != 127 || codes[1] != 43 { |
|
41 |
- t.Fatalf("Expected '127 '43'', got '%d' '%d'", codes[0], codes[1]) |
|
42 |
- } |
|
23 |
+ require.NoError(t, err) |
|
24 |
+ assert.Equal(t, []byte{127, 43}, codes) |
|
43 | 25 |
} |
... | ... |
@@ -68,6 +68,7 @@ func TestGetFdInfo(t *testing.T) { |
68 | 68 |
require.Equal(t, inFd, tty.Fd()) |
69 | 69 |
require.Equal(t, isTerminal, true) |
70 | 70 |
tmpFile, err := newTempFile() |
71 |
+ require.NoError(t, err) |
|
71 | 72 |
defer tmpFile.Close() |
72 | 73 |
inFd, isTerminal = GetFdInfo(tmpFile) |
73 | 74 |
require.Equal(t, inFd, tmpFile.Fd()) |
... | ... |
@@ -81,6 +82,7 @@ func TestIsTerminal(t *testing.T) { |
81 | 81 |
isTerminal := IsTerminal(tty.Fd()) |
82 | 82 |
require.Equal(t, isTerminal, true) |
83 | 83 |
tmpFile, err := newTempFile() |
84 |
+ require.NoError(t, err) |
|
84 | 85 |
defer tmpFile.Close() |
85 | 86 |
isTerminal = IsTerminal(tmpFile.Fd()) |
86 | 87 |
require.Equal(t, isTerminal, false) |
... | ... |
@@ -94,6 +96,7 @@ func TestSaveState(t *testing.T) { |
94 | 94 |
require.NoError(t, err) |
95 | 95 |
require.NotNil(t, state) |
96 | 96 |
tty, err = newTtyForTest(t) |
97 |
+ require.NoError(t, err) |
|
97 | 98 |
defer tty.Close() |
98 | 99 |
err = RestoreTerminal(tty.Fd(), state) |
99 | 100 |
require.NoError(t, err) |
... | ... |
@@ -9,7 +9,9 @@ import ( |
9 | 9 |
"testing" |
10 | 10 |
|
11 | 11 |
"github.com/docker/distribution/reference" |
12 |
- "github.com/opencontainers/go-digest" |
|
12 |
+ digest "github.com/opencontainers/go-digest" |
|
13 |
+ "github.com/stretchr/testify/assert" |
|
14 |
+ "github.com/stretchr/testify/require" |
|
13 | 15 |
) |
14 | 16 |
|
15 | 17 |
var ( |
... | ... |
@@ -62,10 +64,10 @@ func TestLoad(t *testing.T) { |
62 | 62 |
|
63 | 63 |
func TestSave(t *testing.T) { |
64 | 64 |
jsonFile, err := ioutil.TempFile("", "tag-store-test") |
65 |
- if err != nil { |
|
66 |
- t.Fatalf("error creating temp file: %v", err) |
|
67 |
- } |
|
65 |
+ require.NoError(t, err) |
|
66 |
+ |
|
68 | 67 |
_, err = jsonFile.Write([]byte(`{}`)) |
68 |
+ require.NoError(t, err) |
|
69 | 69 |
jsonFile.Close() |
70 | 70 |
defer os.RemoveAll(jsonFile.Name()) |
71 | 71 |
|
... | ... |
@@ -326,32 +328,23 @@ func TestAddDeleteGet(t *testing.T) { |
326 | 326 |
|
327 | 327 |
func TestInvalidTags(t *testing.T) { |
328 | 328 |
tmpDir, err := ioutil.TempDir("", "tag-store-test") |
329 |
+ require.NoError(t, err) |
|
329 | 330 |
defer os.RemoveAll(tmpDir) |
330 | 331 |
|
331 | 332 |
store, err := NewReferenceStore(filepath.Join(tmpDir, "repositories.json")) |
332 |
- if err != nil { |
|
333 |
- t.Fatalf("error creating tag store: %v", err) |
|
334 |
- } |
|
333 |
+ require.NoError(t, err) |
|
335 | 334 |
id := digest.Digest("sha256:470022b8af682154f57a2163d030eb369549549cba00edc69e1b99b46bb924d6") |
336 | 335 |
|
337 | 336 |
// sha256 as repo name |
338 | 337 |
ref, err := reference.ParseNormalizedNamed("sha256:abc") |
339 |
- if err != nil { |
|
340 |
- t.Fatal(err) |
|
341 |
- } |
|
338 |
+ require.NoError(t, err) |
|
342 | 339 |
err = store.AddTag(ref, id, true) |
343 |
- if err == nil { |
|
344 |
- t.Fatalf("expected setting tag %q to fail", ref) |
|
345 |
- } |
|
340 |
+ assert.Error(t, err) |
|
346 | 341 |
|
347 | 342 |
// setting digest as a tag |
348 | 343 |
ref, err = reference.ParseNormalizedNamed("registry@sha256:367eb40fd0330a7e464777121e39d2f5b3e8e23a1e159342e53ab05c9e4d94e6") |
349 |
- if err != nil { |
|
350 |
- t.Fatal(err) |
|
351 |
- } |
|
352 |
- err = store.AddTag(ref, id, true) |
|
353 |
- if err == nil { |
|
354 |
- t.Fatalf("expected setting tag %q to fail", ref) |
|
355 |
- } |
|
344 |
+ require.NoError(t, err) |
|
356 | 345 |
|
346 |
+ err = store.AddTag(ref, id, true) |
|
347 |
+ assert.Error(t, err) |
|
357 | 348 |
} |