Browse code

tests: migrate away from assert.Assert(v == nil)

If a values is non-nil when we don't expect it, it would be quite
helpful to get an error message explaining what happened.

find . -type f -name "*_test.go" | \
xargs gofmt -w -r "assert.Assert(t, a == nil) -> assert.Assert(t, is.Nil(a))"
find . -type f -name "*_test.go" | \
xargs gofmt -w -r "assert.Check(t, a == nil) -> assert.Check(t, is.Nil(a))"

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

Aleksa Sarai authored on 2024/11/21 18:18:21
Showing 11 changed files
... ...
@@ -141,7 +141,7 @@ func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) {
141 141
 	loadedConfig, err := loadDaemonCliConfig(opts)
142 142
 	assert.NilError(t, err)
143 143
 	assert.Assert(t, loadedConfig != nil)
144
-	assert.Check(t, loadedConfig.TLS == nil)
144
+	assert.Check(t, is.Nil(loadedConfig.TLS))
145 145
 }
146 146
 
147 147
 func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) {
... ...
@@ -7,6 +7,7 @@ import (
7 7
 	swarmapi "github.com/moby/swarmkit/v2/api"
8 8
 
9 9
 	"gotest.tools/v3/assert"
10
+	is "gotest.tools/v3/assert/cmp"
10 11
 )
11 12
 
12 13
 func TestTopologyFromGRPC(t *testing.T) {
... ...
@@ -23,7 +24,7 @@ func TestTopologyFromGRPC(t *testing.T) {
23 23
 
24 24
 func TestCapacityRangeFromGRPC(t *testing.T) {
25 25
 	nilCapacity := capacityRangeFromGRPC(nil)
26
-	assert.Assert(t, nilCapacity == nil)
26
+	assert.Assert(t, is.Nil(nilCapacity))
27 27
 
28 28
 	swarmZeroCapacity := &swarmapi.CapacityRange{}
29 29
 	zeroCapacity := capacityRangeFromGRPC(swarmZeroCapacity)
... ...
@@ -35,15 +35,15 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
35 35
 
36 36
 		err := daemon.setWindowsCredentialSpec(&container.Container{}, spec)
37 37
 		assert.NilError(t, err)
38
-		assert.Check(t, spec.Windows == nil)
38
+		assert.Check(t, is.Nil(spec.Windows))
39 39
 
40 40
 		err = daemon.setWindowsCredentialSpec(&container.Container{HostConfig: &containertypes.HostConfig{}}, spec)
41 41
 		assert.NilError(t, err)
42
-		assert.Check(t, spec.Windows == nil)
42
+		assert.Check(t, is.Nil(spec.Windows))
43 43
 
44 44
 		err = daemon.setWindowsCredentialSpec(&container.Container{HostConfig: &containertypes.HostConfig{SecurityOpt: []string{}}}, spec)
45 45
 		assert.NilError(t, err)
46
-		assert.Check(t, spec.Windows == nil)
46
+		assert.Check(t, is.Nil(spec.Windows))
47 47
 	})
48 48
 
49 49
 	dummyContainerID := "dummy-container-ID"
... ...
@@ -90,7 +90,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
90 90
 		err := daemon.setWindowsCredentialSpec(containerFactory(`file://C:\path\to\my\credspec.json`), spec)
91 91
 		assert.ErrorContains(t, err, "invalid credential spec: file:// path cannot be absolute")
92 92
 
93
-		assert.Check(t, spec.Windows == nil)
93
+		assert.Check(t, is.Nil(spec.Windows))
94 94
 	})
95 95
 
96 96
 	t.Run("it's not allowed to use a 'file://' option breaking out of the cred specs' directory", func(t *testing.T) {
... ...
@@ -99,7 +99,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
99 99
 		err := daemon.setWindowsCredentialSpec(containerFactory(`file://..\credspec.json`), spec)
100 100
 		assert.ErrorContains(t, err, fmt.Sprintf("invalid credential spec: file:// path must be under %s", credSpecsDir))
101 101
 
102
-		assert.Check(t, spec.Windows == nil)
102
+		assert.Check(t, is.Nil(spec.Windows))
103 103
 	})
104 104
 
105 105
 	t.Run("when using a 'file://' option pointing to a file that doesn't exist, it fails gracefully", func(t *testing.T) {
... ...
@@ -108,7 +108,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
108 108
 		err := daemon.setWindowsCredentialSpec(containerFactory("file://i-dont-exist.json"), spec)
109 109
 		assert.Check(t, is.ErrorContains(err, fmt.Sprintf("failed to load credential spec for container %s", dummyContainerID)))
110 110
 		assert.Check(t, is.ErrorIs(err, os.ErrNotExist))
111
-		assert.Check(t, spec.Windows == nil)
111
+		assert.Check(t, is.Nil(spec.Windows))
112 112
 	})
113 113
 
114 114
 	t.Run("happy path with a 'registry://' option", func(t *testing.T) {
... ...
@@ -138,7 +138,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
138 138
 		err := daemon.setWindowsCredentialSpec(containerFactory("registry://my-cred-spec"), spec)
139 139
 		assert.ErrorContains(t, err, fmt.Sprintf("registry key %s could not be opened: %v", credentialSpecRegistryLocation, dummyError))
140 140
 
141
-		assert.Check(t, spec.Windows == nil)
141
+		assert.Check(t, is.Nil(spec.Windows))
142 142
 	})
143 143
 
144 144
 	t.Run("when using a 'registry://' option pointing to a value that doesn't exist, it fails gracefully", func(t *testing.T) {
... ...
@@ -219,7 +219,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
219 219
 		err := daemon.setWindowsCredentialSpec(containerFactory("config://whatever"), spec)
220 220
 		assert.Equal(t, errInvalidCredentialSpecSecOpt, err)
221 221
 
222
-		assert.Check(t, spec.Windows == nil)
222
+		assert.Check(t, is.Nil(spec.Windows))
223 223
 	})
224 224
 
225 225
 	t.Run("happy path with a 'raw://' option", func(t *testing.T) {
... ...
@@ -250,7 +250,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
250 250
 		err := daemon.setWindowsCredentialSpec(containerFactory("credentialspe=config://whatever"), spec)
251 251
 		assert.ErrorContains(t, err, "security option not supported: credentialspe")
252 252
 
253
-		assert.Check(t, spec.Windows == nil)
253
+		assert.Check(t, is.Nil(spec.Windows))
254 254
 	})
255 255
 
256 256
 	t.Run("it rejects unsupported credentialspec options", func(t *testing.T) {
... ...
@@ -259,7 +259,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
259 259
 		err := daemon.setWindowsCredentialSpec(containerFactory("idontexist://whatever"), spec)
260 260
 		assert.Equal(t, errInvalidCredentialSpecSecOpt, err)
261 261
 
262
-		assert.Check(t, spec.Windows == nil)
262
+		assert.Check(t, is.Nil(spec.Windows))
263 263
 	})
264 264
 
265 265
 	for _, option := range []string{"file", "registry", "config", "raw"} {
... ...
@@ -269,7 +269,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
269 269
 			err := daemon.setWindowsCredentialSpec(containerFactory(option+"://"), spec)
270 270
 			assert.Equal(t, errInvalidCredentialSpecSecOpt, err)
271 271
 
272
-			assert.Check(t, spec.Windows == nil)
272
+			assert.Check(t, is.Nil(spec.Windows))
273 273
 		})
274 274
 	}
275 275
 }
... ...
@@ -215,7 +215,7 @@ func (s *DockerAPISuite) TestGetContainerStatsRmRunning(c *testing.T) {
215 215
 	assert.NilError(c, err)
216 216
 
217 217
 	cli.DockerCmd(c, "rm", "-f", id)
218
-	assert.Assert(c, <-chErr == nil)
218
+	assert.Assert(c, is.Nil(<-chErr))
219 219
 }
220 220
 
221 221
 // ChannelBuffer holds a chan of byte array that can be populate in a goroutine.
... ...
@@ -1347,7 +1347,7 @@ func (s *DockerAPISuite) TestPostContainersCreateMemorySwappinessHostConfigOmitt
1347 1347
 	containerJSON, err := apiClient.ContainerInspect(testutil.GetContext(c), ctr.ID)
1348 1348
 	assert.NilError(c, err)
1349 1349
 
1350
-	assert.Assert(c, containerJSON.HostConfig.MemorySwappiness == nil)
1350
+	assert.Assert(c, is.Nil(containerJSON.HostConfig.MemorySwappiness))
1351 1351
 }
1352 1352
 
1353 1353
 // check validation is done daemon side and not only in cli
... ...
@@ -163,7 +163,7 @@ func (s *DockerAPISuite) TestExecAPIStartValidCommand(c *testing.T) {
163 163
 	var inspectJSON struct{ ExecIDs []string }
164 164
 	inspectContainer(ctx, c, name, &inspectJSON)
165 165
 
166
-	assert.Assert(c, inspectJSON.ExecIDs == nil)
166
+	assert.Assert(c, is.Nil(inspectJSON.ExecIDs))
167 167
 }
168 168
 
169 169
 // #30311
... ...
@@ -179,7 +179,7 @@ func (s *DockerAPISuite) TestExecAPIStartInvalidCommand(c *testing.T) {
179 179
 	var inspectJSON struct{ ExecIDs []string }
180 180
 	inspectContainer(ctx, c, name, &inspectJSON)
181 181
 
182
-	assert.Assert(c, inspectJSON.ExecIDs == nil)
182
+	assert.Assert(c, is.Nil(inspectJSON.ExecIDs))
183 183
 }
184 184
 
185 185
 func (s *DockerAPISuite) TestExecStateCleanup(c *testing.T) {
... ...
@@ -575,7 +575,7 @@ func (s *DockerSwarmSuite) TestAPISwarmServicesStateReporting(c *testing.T) {
575 575
 	assert.Assert(c, len(containers2) == instances)
576 576
 	for i := range containers {
577 577
 		if i == toRemove {
578
-			assert.Assert(c, containers2[i] == nil)
578
+			assert.Assert(c, is.Nil(containers2[i]))
579 579
 		} else {
580 580
 			assert.Assert(c, containers2[i] != nil)
581 581
 		}
... ...
@@ -601,7 +601,7 @@ func (s *DockerSwarmSuite) TestAPISwarmServicesStateReporting(c *testing.T) {
601 601
 	assert.Assert(c, len(containers2) == instances)
602 602
 	for i := range containers {
603 603
 		if i == toRemove {
604
-			assert.Assert(c, containers2[i] == nil)
604
+			assert.Assert(c, is.Nil(containers2[i]))
605 605
 		} else {
606 606
 			assert.Assert(c, containers2[i] != nil)
607 607
 		}
... ...
@@ -96,5 +96,5 @@ func TestNetworkStateCleanupOnDaemonStart(t *testing.T) {
96 96
 	assert.NilError(t, err)
97 97
 	assert.Assert(t, inspect.NetworkSettings.SandboxID == "")
98 98
 	assert.Assert(t, inspect.NetworkSettings.SandboxKey == "")
99
-	assert.Assert(t, inspect.NetworkSettings.Ports["80/tcp"] == nil)
99
+	assert.Assert(t, is.Nil(inspect.NetworkSettings.Ports["80/tcp"]))
100 100
 }
... ...
@@ -69,7 +69,7 @@ func TestImageInspectDescriptor(t *testing.T) {
69 69
 	assert.NilError(t, err)
70 70
 
71 71
 	if !testEnv.UsingSnapshotter() {
72
-		assert.Check(t, inspect.Descriptor == nil)
72
+		assert.Check(t, is.Nil(inspect.Descriptor))
73 73
 		return
74 74
 	}
75 75
 
... ...
@@ -50,7 +50,7 @@ func testServiceCreateInit(ctx context.Context, daemonEnabled bool) func(t *test
50 50
 		poll.WaitOn(t, swarm.RunningTasksCount(ctx, client, serviceID, 1), swarm.ServicePoll)
51 51
 		i := inspectServiceContainer(ctx, t, client, serviceID)
52 52
 		// HostConfig.Init == nil means that it delegates to daemon configuration
53
-		assert.Check(t, i.HostConfig.Init == nil)
53
+		assert.Check(t, is.Nil(i.HostConfig.Init))
54 54
 
55 55
 		serviceID = swarm.CreateService(ctx, t, d, swarm.ServiceWithInit(&booleanTrue))
56 56
 		poll.WaitOn(t, swarm.RunningTasksCount(ctx, client, serviceID, 1), swarm.ServicePoll)
... ...
@@ -261,7 +261,7 @@ func TestBindHostPortsError(t *testing.T) {
261 261
 	}
262 262
 	pbs, err := bindHostPorts(context.Background(), cfg, "")
263 263
 	assert.Check(t, is.Error(err, "port binding mismatch 80/tcp:8080-8080, 80/tcp:8080-8081"))
264
-	assert.Check(t, pbs == nil)
264
+	assert.Check(t, is.Nil(pbs))
265 265
 }
266 266
 
267 267
 func newIPNet(t *testing.T, cidr string) *net.IPNet {
... ...
@@ -132,7 +132,7 @@ func TestServiceGet(t *testing.T) {
132 132
 
133 133
 	v, err := service.Get(ctx, "notexist")
134 134
 	assert.Assert(t, IsNotExist(err))
135
-	assert.Check(t, v == nil)
135
+	assert.Check(t, is.Nil(v))
136 136
 
137 137
 	created, err := service.Create(ctx, "test", "d1")
138 138
 	assert.NilError(t, err)