Browse code

tests: migrate to assert.ErrorContains when possible

If we have an error type that we're checking a substring against, we
should really be checking using ErrorContains to indicate the right
semantics to assert.

Mostly done using these transforms:

find . -type f -name "*_test.go" | \
xargs gofmt -w -r 'assert.Assert(t, is.ErrorContains(e, s)) -> assert.ErrorContains(t, e, s)'
find . -type f -name "*_test.go" | \
xargs gofmt -w -r 'assert.Assert(t, is.Contains(err.Error(), s)) -> assert.ErrorContains(t, err, s)'
find . -type f -name "*_test.go" | \
xargs gofmt -w -r 'assert.Check(t, is.Contains(err.Error(), s)) -> assert.Check(t, is.ErrorContains(err, s))'

As well as some small fixups to helpers that were doing
strings.Contains explicitly.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>

Aleksa Sarai authored on 2024/11/22 10:12:30
Showing 12 changed files
... ...
@@ -324,7 +324,7 @@ func TestCheckoutGit(t *testing.T) {
324 324
 				assert.Check(t, is.Equal("subcontents", string(b)))
325 325
 			} else {
326 326
 				_, err := os.Stat(filepath.Join(r, "sub/subfile"))
327
-				assert.Assert(t, is.ErrorContains(err, ""))
327
+				assert.ErrorContains(t, err, "")
328 328
 				assert.Assert(t, os.IsNotExist(err))
329 329
 			}
330 330
 
... ...
@@ -373,6 +373,8 @@ func TestGitInvalidRef(t *testing.T) {
373 373
 	for _, url := range gitUrls {
374 374
 		_, err := Clone(url)
375 375
 		assert.Assert(t, err != nil)
376
+		// On Windows, git has different case for the "invalid refspec" error,
377
+		// so we can't use ErrorContains.
376 378
 		assert.Check(t, is.Contains(strings.ToLower(err.Error()), "invalid refspec"))
377 379
 	}
378 380
 }
... ...
@@ -14,6 +14,7 @@ import (
14 14
 	"github.com/docker/docker/integration-cli/cli"
15 15
 	"github.com/docker/docker/pkg/archive"
16 16
 	"gotest.tools/v3/assert"
17
+	is "gotest.tools/v3/assert/cmp"
17 18
 )
18 19
 
19 20
 type fileType uint32
... ...
@@ -225,12 +226,12 @@ func getTestDir(c *testing.T, label string) (tmpDir string) {
225 225
 	return
226 226
 }
227 227
 
228
-func isCpDirNotExist(err error) bool {
229
-	return strings.Contains(err.Error(), archive.ErrDirNotExists.Error())
228
+func isCpDirNotExist(err error) is.Comparison {
229
+	return is.ErrorContains(err, archive.ErrDirNotExists.Error())
230 230
 }
231 231
 
232
-func isCpCannotCopyDir(err error) bool {
233
-	return strings.Contains(err.Error(), archive.ErrCannotCopyDir.Error())
232
+func isCpCannotCopyDir(err error) is.Comparison {
233
+	return is.ErrorContains(err, archive.ErrCannotCopyDir.Error())
234 234
 }
235 235
 
236 236
 func fileContentEquals(c *testing.T, filename, contents string) error {
... ...
@@ -627,7 +627,7 @@ func TestBuildWithEmptyDockerfile(t *testing.T) {
627 627
 					ForceRemove: true,
628 628
 				})
629 629
 
630
-			assert.Check(t, is.Contains(err.Error(), tc.expectedErr))
630
+			assert.Check(t, is.ErrorContains(err, tc.expectedErr))
631 631
 		})
632 632
 	}
633 633
 }
... ...
@@ -46,7 +46,7 @@ func TestCopyFromContainerPathIsNotDir(t *testing.T) {
46 46
 		expected = "The filename, directory name, or volume label syntax is incorrect."
47 47
 	}
48 48
 	_, _, err := apiClient.CopyFromContainer(ctx, cid, path)
49
-	assert.Assert(t, is.ErrorContains(err, expected))
49
+	assert.ErrorContains(t, err, expected)
50 50
 }
51 51
 
52 52
 func TestCopyToContainerPathDoesNotExist(t *testing.T) {
... ...
@@ -126,8 +126,8 @@ func TestKillStoppedContainer(t *testing.T) {
126 126
 	apiClient := testEnv.APIClient()
127 127
 	id := container.Create(ctx, t, apiClient)
128 128
 	err := apiClient.ContainerKill(ctx, id, "SIGKILL")
129
-	assert.Assert(t, is.ErrorContains(err, ""))
130
-	assert.Assert(t, is.Contains(err.Error(), "is not running"))
129
+	assert.ErrorContains(t, err, "")
130
+	assert.ErrorContains(t, err, "is not running")
131 131
 }
132 132
 
133 133
 func TestKillDifferentUserContainer(t *testing.T) {
... ...
@@ -452,7 +452,7 @@ func testIpvlanExperimentalV4Only(t *testing.T, ctx context.Context, client dcli
452 452
 		net.WithIPv4(false),
453 453
 	)
454 454
 	defer client.NetworkRemove(ctx, "testnet")
455
-	assert.Assert(t, is.ErrorContains(err, "IPv4 can only be disabled if experimental features are enabled"))
455
+	assert.ErrorContains(t, err, "IPv4 can only be disabled if experimental features are enabled")
456 456
 }
457 457
 
458 458
 // Check that an ipvlan interface with '--ipv6=false' doesn't get kernel-assigned
... ...
@@ -448,7 +448,7 @@ func testMacvlanExperimentalV4Only(t *testing.T, ctx context.Context, client cli
448 448
 		net.WithIPv4(false),
449 449
 	)
450 450
 	defer client.NetworkRemove(ctx, "testnet")
451
-	assert.Assert(t, is.ErrorContains(err, "IPv4 can only be disabled if experimental features are enabled"))
451
+	assert.ErrorContains(t, err, "IPv4 can only be disabled if experimental features are enabled")
452 452
 }
453 453
 
454 454
 // Check that a macvlan interface with '--ipv6=false' doesn't get kernel-assigned
... ...
@@ -15,7 +15,6 @@ import (
15 15
 	"github.com/docker/docker/integration/internal/container"
16 16
 	"github.com/docker/docker/integration/internal/requirement"
17 17
 	"gotest.tools/v3/assert"
18
-	is "gotest.tools/v3/assert/cmp"
19 18
 	"gotest.tools/v3/skip"
20 19
 )
21 20
 
... ...
@@ -72,7 +71,7 @@ func TestAuthZPluginV2Disable(t *testing.T) {
72 72
 
73 73
 	_, err = c.VolumeCreate(ctx, volume.CreateOptions{Driver: "local"})
74 74
 	assert.Assert(t, err != nil)
75
-	assert.Assert(t, is.Contains(err.Error(), fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag)))
75
+	assert.ErrorContains(t, err, fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))
76 76
 
77 77
 	// disable the plugin
78 78
 	err = c.PluginDisable(ctx, authzPluginNameWithTag, types.PluginDisableOptions{})
... ...
@@ -98,24 +97,24 @@ func TestAuthZPluginV2RejectVolumeRequests(t *testing.T) {
98 98
 
99 99
 	_, err = c.VolumeCreate(ctx, volume.CreateOptions{Driver: "local"})
100 100
 	assert.Assert(t, err != nil)
101
-	assert.Assert(t, is.Contains(err.Error(), fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag)))
101
+	assert.ErrorContains(t, err, fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))
102 102
 
103 103
 	_, err = c.VolumeList(ctx, volume.ListOptions{})
104 104
 	assert.Assert(t, err != nil)
105
-	assert.Assert(t, is.Contains(err.Error(), fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag)))
105
+	assert.ErrorContains(t, err, fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))
106 106
 
107 107
 	// The plugin will block the command before it can determine the volume does not exist
108 108
 	err = c.VolumeRemove(ctx, "test", false)
109 109
 	assert.Assert(t, err != nil)
110
-	assert.Assert(t, is.Contains(err.Error(), fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag)))
110
+	assert.ErrorContains(t, err, fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))
111 111
 
112 112
 	_, err = c.VolumeInspect(ctx, "test")
113 113
 	assert.Assert(t, err != nil)
114
-	assert.Assert(t, is.Contains(err.Error(), fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag)))
114
+	assert.ErrorContains(t, err, fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))
115 115
 
116 116
 	_, err = c.VolumesPrune(ctx, filters.Args{})
117 117
 	assert.Assert(t, err != nil)
118
-	assert.Assert(t, is.Contains(err.Error(), fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag)))
118
+	assert.ErrorContains(t, err, fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))
119 119
 }
120 120
 
121 121
 func TestAuthZPluginV2BadManifestFailsDaemonStart(t *testing.T) {
... ...
@@ -10,7 +10,6 @@ import (
10 10
 
11 11
 	"github.com/docker/docker/pkg/plugingetter"
12 12
 	"gotest.tools/v3/assert"
13
-	is "gotest.tools/v3/assert/cmp"
14 13
 )
15 14
 
16 15
 func TestMiddlewareWrapHandler(t *testing.T) {
... ...
@@ -47,7 +46,7 @@ func TestMiddlewareWrapHandler(t *testing.T) {
47 47
 			Msg:   "Server Auth Not Allowed",
48 48
 		}
49 49
 		if err := mdHandler(ctx, resp, req, map[string]string{}); err == nil {
50
-			assert.Assert(t, is.ErrorContains(err, ""))
50
+			assert.ErrorContains(t, err, "")
51 51
 		}
52 52
 	})
53 53
 
... ...
@@ -61,7 +61,7 @@ func testBiggerThanQuota(t *testing.T, ctrl *Control, homeDir, testDir, testSubD
61 61
 
62 62
 	biggerThanQuotaFile := filepath.Join(testSubDir, "bigger-than-quota")
63 63
 	err := os.WriteFile(biggerThanQuotaFile, make([]byte, testQuotaSize+1), 0o644)
64
-	assert.Assert(t, is.ErrorContains(err, ""))
64
+	assert.ErrorContains(t, err, "")
65 65
 	if err == io.ErrShortWrite {
66 66
 		assert.NilError(t, os.Remove(biggerThanQuotaFile))
67 67
 	}
... ...
@@ -8,7 +8,6 @@ import (
8 8
 
9 9
 	bolt "go.etcd.io/bbolt"
10 10
 	"gotest.tools/v3/assert"
11
-	is "gotest.tools/v3/assert/cmp"
12 11
 )
13 12
 
14 13
 func TestSetGetMeta(t *testing.T) {
... ...
@@ -25,7 +24,7 @@ func TestSetGetMeta(t *testing.T) {
25 25
 	defer store.Shutdown()
26 26
 
27 27
 	_, err = store.getMeta("test")
28
-	assert.Assert(t, is.ErrorContains(err, ""))
28
+	assert.ErrorContains(t, err, "")
29 29
 
30 30
 	err = db.Update(func(tx *bolt.Tx) error {
31 31
 		_, err := tx.CreateBucket(volumeBucketName)
... ...
@@ -15,7 +15,6 @@ import (
15 15
 	volumetestutils "github.com/docker/docker/volume/testutils"
16 16
 	"github.com/google/go-cmp/cmp"
17 17
 	"gotest.tools/v3/assert"
18
-	is "gotest.tools/v3/assert/cmp"
19 18
 )
20 19
 
21 20
 func TestCreate(t *testing.T) {
... ...
@@ -300,7 +299,7 @@ func TestRefDerefRemove(t *testing.T) {
300 300
 	assert.NilError(t, err)
301 301
 
302 302
 	err = s.Remove(ctx, v)
303
-	assert.Assert(t, is.ErrorContains(err, ""))
303
+	assert.ErrorContains(t, err, "")
304 304
 	assert.Equal(t, errVolumeInUse, err.(*OpErr).Err)
305 305
 
306 306
 	s.Release(ctx, v.Name(), "test-ref")
... ...
@@ -318,7 +317,7 @@ func TestGet(t *testing.T) {
318 318
 
319 319
 	ctx := context.Background()
320 320
 	_, err := s.Get(ctx, "not-exist")
321
-	assert.Assert(t, is.ErrorContains(err, ""))
321
+	assert.ErrorContains(t, err, "")
322 322
 	assert.Equal(t, errNoSuchVolume, err.(*OpErr).Err)
323 323
 
324 324
 	v1, err := s.Create(ctx, "test", driverName, opts.WithCreateLabels(map[string]string{"a": "1"}))
... ...
@@ -345,7 +344,7 @@ func TestGetWithReference(t *testing.T) {
345 345
 
346 346
 	ctx := context.Background()
347 347
 	_, err := s.Get(ctx, "not-exist", opts.WithGetDriver(driverName), opts.WithGetReference("test-ref"))
348
-	assert.Assert(t, is.ErrorContains(err, ""))
348
+	assert.ErrorContains(t, err, "")
349 349
 
350 350
 	v1, err := s.Create(ctx, "test", driverName, opts.WithCreateLabels(map[string]string{"a": "1"}))
351 351
 	assert.NilError(t, err)
... ...
@@ -355,7 +354,7 @@ func TestGetWithReference(t *testing.T) {
355 355
 	assert.DeepEqual(t, v1, v2, cmpVolume)
356 356
 
357 357
 	err = s.Remove(ctx, v2)
358
-	assert.Assert(t, is.ErrorContains(err, ""))
358
+	assert.ErrorContains(t, err, "")
359 359
 	assert.Equal(t, errVolumeInUse, err.(*OpErr).Err)
360 360
 
361 361
 	s.Release(ctx, v2.Name(), "test-ref")