Browse code

integration-cli: avoid allocations with (*os.File).WriteString (mirror)

integration-cli/docker_cli_attach_unix_test.go:107:3: avoid allocations with (*os.File).WriteString (mirror)
cpty.Write([]byte("\n"))
^
integration-cli/docker_cli_attach_unix_test.go:144:11: avoid allocations with (*os.File).WriteString (mirror)
_, err = cpty.Write([]byte("hello\n"))
^
integration-cli/docker_cli_exec_test.go:422:16: avoid allocations with (*os.File).WriteString (mirror)
if _, err := f.Write([]byte("success2\n")); err != nil {
^
integration-cli/docker_cli_exec_unix_test.go:57:11: avoid allocations with (*os.File).WriteString (mirror)
_, err = p.Write([]byte("cat /foo && exit\n"))
^
integration-cli/docker_cli_run_test.go:4092:15: avoid allocations with (*os.File).WriteString (mirror)
if _, err := tmpFile.Write([]byte(data)); err != nil {
^
integration-cli/docker_cli_run_unix_test.go:110:11: avoid allocations with (*os.File).WriteString (mirror)
_, err = cpty.Write([]byte("hello\n"))
^
integration-cli/docker_cli_run_unix_test.go:169:15: avoid allocations with (*os.File).WriteString (mirror)
if _, err := cpty.Write([]byte("hello\n")); err != nil {
^
integration-cli/docker_cli_run_unix_test.go:283:15: avoid allocations with (*os.File).WriteString (mirror)
if _, err := cpty.Write([]byte("hello\n")); err != nil {
^
integration-cli/docker_cli_run_unix_test.go:364:15: avoid allocations with (*os.File).WriteString (mirror)
if _, err := cpty.Write([]byte("hello\n")); err != nil {
^
integration-cli/docker_cli_run_unix_test.go:438:15: avoid allocations with (*os.File).WriteString (mirror)
if _, err := cpty.Write([]byte("\n")); err != nil {
^
integration-cli/docker_cli_run_unix_test.go:880:15: avoid allocations with (*os.File).WriteString (mirror)
if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
^
integration-cli/docker_cli_run_unix_test.go:915:15: avoid allocations with (*os.File).WriteString (mirror)
if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
^
integration-cli/docker_cli_run_unix_test.go:952:15: avoid allocations with (*os.File).WriteString (mirror)
if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
^
integration-cli/docker_cli_run_unix_test.go:1418:11: avoid allocations with (*os.File).WriteString (mirror)
_, err = tmpFile.Write([]byte(jsonData))
^
integration-cli/docker_cli_run_unix_test.go:1445:11: avoid allocations with (*os.File).WriteString (mirror)
_, err = tmpFile.Write([]byte(jsonData))
^
integration-cli/docker_cli_run_unix_test.go:1483:11: avoid allocations with (*os.File).WriteString (mirror)
_, err = tmpFile.Write([]byte(jsonData))
^
integration-cli/docker_cli_run_unix_test.go:1517:11: avoid allocations with (*os.File).WriteString (mirror)
_, err = tmpFile.Write([]byte(jsonData))
^
integration-cli/docker_cli_update_unix_test.go:235:11: avoid allocations with (*os.File).WriteString (mirror)
_, err = cpty.Write([]byte("exit\n"))
^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/02/08 06:04:45
Showing 6 changed files
... ...
@@ -104,11 +104,11 @@ func (s *DockerCLIAttachSuite) TestAttachAfterDetach(c *testing.T) {
104 104
 
105 105
 	go func() {
106 106
 		time.Sleep(500 * time.Millisecond)
107
-		cpty.Write([]byte("\n"))
107
+		_, _ = cpty.WriteString("\n")
108 108
 		time.Sleep(500 * time.Millisecond)
109 109
 
110 110
 		nBytes, err = cpty.Read(bytes)
111
-		cpty.Close()
111
+		_ = cpty.Close()
112 112
 		readErr <- err
113 113
 	}()
114 114
 
... ...
@@ -141,7 +141,7 @@ func (s *DockerCLIAttachSuite) TestAttachDetach(c *testing.T) {
141 141
 	assert.NilError(c, err)
142 142
 	cli.WaitRun(c, id)
143 143
 
144
-	_, err = cpty.Write([]byte("hello\n"))
144
+	_, err = cpty.WriteString("hello\n")
145 145
 	assert.NilError(c, err)
146 146
 	out, err = bufio.NewReader(stdout).ReadString('\n')
147 147
 	assert.NilError(c, err)
... ...
@@ -410,20 +410,20 @@ func (s *DockerCLIExecSuite) TestRunMutableNetworkFiles(c *testing.T) {
410 410
 		assert.NilError(c, err)
411 411
 
412 412
 		if _, err := f.Seek(0, 0); err != nil {
413
-			f.Close()
413
+			_ = f.Close()
414 414
 			c.Fatal(err)
415 415
 		}
416 416
 
417 417
 		if err := f.Truncate(0); err != nil {
418
-			f.Close()
418
+			_ = f.Close()
419 419
 			c.Fatal(err)
420 420
 		}
421 421
 
422
-		if _, err := f.Write([]byte("success2\n")); err != nil {
423
-			f.Close()
422
+		if _, err := f.WriteString("success2\n"); err != nil {
423
+			_ = f.Close()
424 424
 			c.Fatal(err)
425 425
 		}
426
-		f.Close()
426
+		_ = f.Close()
427 427
 
428 428
 		res := cli.DockerCmd(c, "exec", contID, "cat", "/etc/"+fn).Stdout()
429 429
 		assert.Equal(c, res, "success2\n")
... ...
@@ -54,7 +54,7 @@ func (s *DockerCLIExecSuite) TestExecTTY(c *testing.T) {
54 54
 	assert.NilError(c, err)
55 55
 	defer p.Close()
56 56
 
57
-	_, err = p.Write([]byte("cat /foo && exit\n"))
57
+	_, err = p.WriteString("cat /foo && exit\n")
58 58
 	assert.NilError(c, err)
59 59
 
60 60
 	chErr := make(chan error, 1)
... ...
@@ -4088,8 +4088,8 @@ func (s *DockerCLIRunSuite) TestRunDuplicateMount(c *testing.T) {
4088 4088
 	assert.NilError(c, err)
4089 4089
 	defer tmpFile.Close()
4090 4090
 
4091
-	data := "touch-me-foo-bar\n"
4092
-	if _, err := tmpFile.Write([]byte(data)); err != nil {
4091
+	const data = "touch-me-foo-bar\n"
4092
+	if _, err := tmpFile.WriteString(data); err != nil {
4093 4093
 		c.Fatal(err)
4094 4094
 	}
4095 4095
 
... ...
@@ -107,7 +107,7 @@ func (s *DockerCLIRunSuite) TestRunAttachDetach(c *testing.T) {
107 107
 	assert.NilError(c, cmd.Start())
108 108
 	cli.WaitRun(c, name)
109 109
 
110
-	_, err = cpty.Write([]byte("hello\n"))
110
+	_, err = cpty.WriteString("hello\n")
111 111
 	assert.NilError(c, err)
112 112
 
113 113
 	out, err := bufio.NewReader(stdout).ReadString('\n')
... ...
@@ -166,7 +166,7 @@ func (s *DockerCLIRunSuite) TestRunAttachDetachFromFlag(c *testing.T) {
166 166
 	}
167 167
 	cli.WaitRun(c, name)
168 168
 
169
-	if _, err := cpty.Write([]byte("hello\n")); err != nil {
169
+	if _, err := cpty.WriteString("hello\n"); err != nil {
170 170
 		c.Fatal(err)
171 171
 	}
172 172
 
... ...
@@ -280,7 +280,7 @@ func (s *DockerCLIRunSuite) TestRunAttachDetachFromConfig(c *testing.T) {
280 280
 	}
281 281
 	cli.WaitRun(c, name)
282 282
 
283
-	if _, err := cpty.Write([]byte("hello\n")); err != nil {
283
+	if _, err := cpty.WriteString("hello\n"); err != nil {
284 284
 		c.Fatal(err)
285 285
 	}
286 286
 
... ...
@@ -361,7 +361,7 @@ func (s *DockerCLIRunSuite) TestRunAttachDetachKeysOverrideConfig(c *testing.T)
361 361
 	}
362 362
 	cli.WaitRun(c, name)
363 363
 
364
-	if _, err := cpty.Write([]byte("hello\n")); err != nil {
364
+	if _, err := cpty.WriteString("hello\n"); err != nil {
365 365
 		c.Fatal(err)
366 366
 	}
367 367
 
... ...
@@ -400,8 +400,8 @@ func (s *DockerCLIRunSuite) TestRunAttachDetachKeysOverrideConfig(c *testing.T)
400 400
 
401 401
 func (s *DockerCLIRunSuite) TestRunAttachInvalidDetachKeySequencePreserved(c *testing.T) {
402 402
 	const name = "attach-detach"
403
-	keyA := []byte{97}
404
-	keyB := []byte{98}
403
+	const keyA = "a"
404
+	const keyB = "b"
405 405
 
406 406
 	cli.DockerCmd(c, "run", "--name", name, "-itd", "busybox", "cat")
407 407
 
... ...
@@ -423,19 +423,19 @@ func (s *DockerCLIRunSuite) TestRunAttachInvalidDetachKeySequencePreserved(c *te
423 423
 	cli.WaitRun(c, name)
424 424
 
425 425
 	// Invalid escape sequence aba, should print aba in output
426
-	if _, err := cpty.Write(keyA); err != nil {
426
+	if _, err := cpty.WriteString(keyA); err != nil {
427 427
 		c.Fatal(err)
428 428
 	}
429 429
 	time.Sleep(100 * time.Millisecond)
430
-	if _, err := cpty.Write(keyB); err != nil {
430
+	if _, err := cpty.WriteString(keyB); err != nil {
431 431
 		c.Fatal(err)
432 432
 	}
433 433
 	time.Sleep(100 * time.Millisecond)
434
-	if _, err := cpty.Write(keyA); err != nil {
434
+	if _, err := cpty.WriteString(keyA); err != nil {
435 435
 		c.Fatal(err)
436 436
 	}
437 437
 	time.Sleep(100 * time.Millisecond)
438
-	if _, err := cpty.Write([]byte("\n")); err != nil {
438
+	if _, err := cpty.WriteString("\n"); err != nil {
439 439
 		c.Fatal(err)
440 440
 	}
441 441
 
... ...
@@ -443,8 +443,10 @@ func (s *DockerCLIRunSuite) TestRunAttachInvalidDetachKeySequencePreserved(c *te
443 443
 	if err != nil {
444 444
 		c.Fatal(err)
445 445
 	}
446
-	if strings.TrimSpace(out) != "aba" {
447
-		c.Fatalf("expected 'aba', got %q", out)
446
+
447
+	expected := keyA + keyB + keyA
448
+	if strings.TrimSpace(out) != expected {
449
+		c.Fatalf("expected '%s', got %q", expected, out)
448 450
 	}
449 451
 }
450 452
 
... ...
@@ -877,7 +879,7 @@ func (s *DockerCLIRunSuite) TestRunSeccompProfileDenyUnshare(c *testing.T) {
877 877
 	}
878 878
 	defer tmpFile.Close()
879 879
 
880
-	if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
880
+	if _, err := tmpFile.WriteString(jsonData); err != nil {
881 881
 		c.Fatal(err)
882 882
 	}
883 883
 	icmd.RunCommand(dockerBinary, "run", "--security-opt", "apparmor=unconfined",
... ...
@@ -912,7 +914,7 @@ func (s *DockerCLIRunSuite) TestRunSeccompProfileDenyChmod(c *testing.T) {
912 912
 	assert.NilError(c, err)
913 913
 	defer tmpFile.Close()
914 914
 
915
-	if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
915
+	if _, err := tmpFile.WriteString(jsonData); err != nil {
916 916
 		c.Fatal(err)
917 917
 	}
918 918
 	icmd.RunCommand(dockerBinary, "run", "--security-opt", "seccomp="+tmpFile.Name(),
... ...
@@ -949,7 +951,7 @@ func (s *DockerCLIRunSuite) TestRunSeccompProfileDenyUnshareUserns(c *testing.T)
949 949
 	}
950 950
 	defer tmpFile.Close()
951 951
 
952
-	if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
952
+	if _, err := tmpFile.WriteString(jsonData); err != nil {
953 953
 		c.Fatal(err)
954 954
 	}
955 955
 	icmd.RunCommand(dockerBinary, "run",
... ...
@@ -1415,7 +1417,7 @@ func (s *DockerDaemonSuite) TestRunSeccompJSONNewFormat(c *testing.T) {
1415 1415
 	tmpFile, err := os.CreateTemp("", "profile.json")
1416 1416
 	assert.NilError(c, err)
1417 1417
 	defer tmpFile.Close()
1418
-	_, err = tmpFile.Write([]byte(jsonData))
1418
+	_, err = tmpFile.WriteString(jsonData)
1419 1419
 	assert.NilError(c, err)
1420 1420
 
1421 1421
 	out, err := s.d.Cmd("run", "--security-opt", "seccomp="+tmpFile.Name(), "busybox", "chmod", "777", ".")
... ...
@@ -1442,7 +1444,7 @@ func (s *DockerDaemonSuite) TestRunSeccompJSONNoNameAndNames(c *testing.T) {
1442 1442
 	tmpFile, err := os.CreateTemp("", "profile.json")
1443 1443
 	assert.NilError(c, err)
1444 1444
 	defer tmpFile.Close()
1445
-	_, err = tmpFile.Write([]byte(jsonData))
1445
+	_, err = tmpFile.WriteString(jsonData)
1446 1446
 	assert.NilError(c, err)
1447 1447
 
1448 1448
 	out, err := s.d.Cmd("run", "--security-opt", "seccomp="+tmpFile.Name(), "busybox", "chmod", "777", ".")
... ...
@@ -1480,7 +1482,7 @@ func (s *DockerDaemonSuite) TestRunSeccompJSONNoArchAndArchMap(c *testing.T) {
1480 1480
 	tmpFile, err := os.CreateTemp("", "profile.json")
1481 1481
 	assert.NilError(c, err)
1482 1482
 	defer tmpFile.Close()
1483
-	_, err = tmpFile.Write([]byte(jsonData))
1483
+	_, err = tmpFile.WriteString(jsonData)
1484 1484
 	assert.NilError(c, err)
1485 1485
 
1486 1486
 	out, err := s.d.Cmd("run", "--security-opt", "seccomp="+tmpFile.Name(), "busybox", "chmod", "777", ".")
... ...
@@ -1514,7 +1516,7 @@ func (s *DockerDaemonSuite) TestRunWithDaemonDefaultSeccompProfile(c *testing.T)
1514 1514
 	tmpFile, err := os.CreateTemp("", "profile.json")
1515 1515
 	assert.NilError(c, err)
1516 1516
 	defer tmpFile.Close()
1517
-	_, err = tmpFile.Write([]byte(jsonData))
1517
+	_, err = tmpFile.WriteString(jsonData)
1518 1518
 	assert.NilError(c, err)
1519 1519
 
1520 1520
 	// 2) restart the daemon and add a custom seccomp profile in which we deny chmod
... ...
@@ -232,7 +232,7 @@ func (s *DockerCLIUpdateSuite) TestUpdateNotAffectMonitorRestartPolicy(c *testin
232 232
 	assert.NilError(c, cmd.Start())
233 233
 	defer cmd.Process.Kill()
234 234
 
235
-	_, err = cpty.Write([]byte("exit\n"))
235
+	_, err = cpty.WriteString("exit\n")
236 236
 	assert.NilError(c, err)
237 237
 
238 238
 	assert.NilError(c, cmd.Wait())