From 6ff3dfd88a07a961fc0af995faa5552747285c09 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 7 Feb 2025 22:04:45 +0100 Subject: [PATCH] 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 --- .../docker_cli_attach_unix_test.go | 6 +-- integration-cli/docker_cli_exec_test.go | 10 ++--- integration-cli/docker_cli_exec_unix_test.go | 2 +- integration-cli/docker_cli_run_test.go | 4 +- integration-cli/docker_cli_run_unix_test.go | 40 ++++++++++--------- .../docker_cli_update_unix_test.go | 2 +- 6 files changed, 33 insertions(+), 31 deletions(-) diff --git a/integration-cli/docker_cli_attach_unix_test.go b/integration-cli/docker_cli_attach_unix_test.go index f8bcc5e2de..d565f3f6ff 100644 --- a/integration-cli/docker_cli_attach_unix_test.go +++ b/integration-cli/docker_cli_attach_unix_test.go @@ -104,11 +104,11 @@ func (s *DockerCLIAttachSuite) TestAttachAfterDetach(c *testing.T) { go func() { time.Sleep(500 * time.Millisecond) - cpty.Write([]byte("\n")) + _, _ = cpty.WriteString("\n") time.Sleep(500 * time.Millisecond) nBytes, err = cpty.Read(bytes) - cpty.Close() + _ = cpty.Close() readErr <- err }() @@ -141,7 +141,7 @@ func (s *DockerCLIAttachSuite) TestAttachDetach(c *testing.T) { assert.NilError(c, err) cli.WaitRun(c, id) - _, err = cpty.Write([]byte("hello\n")) + _, err = cpty.WriteString("hello\n") assert.NilError(c, err) out, err = bufio.NewReader(stdout).ReadString('\n') assert.NilError(c, err) diff --git a/integration-cli/docker_cli_exec_test.go b/integration-cli/docker_cli_exec_test.go index 8162ddfc65..e5de66e7d1 100644 --- a/integration-cli/docker_cli_exec_test.go +++ b/integration-cli/docker_cli_exec_test.go @@ -410,20 +410,20 @@ func (s *DockerCLIExecSuite) TestRunMutableNetworkFiles(c *testing.T) { assert.NilError(c, err) if _, err := f.Seek(0, 0); err != nil { - f.Close() + _ = f.Close() c.Fatal(err) } if err := f.Truncate(0); err != nil { - f.Close() + _ = f.Close() c.Fatal(err) } - if _, err := f.Write([]byte("success2\n")); err != nil { - f.Close() + if _, err := f.WriteString("success2\n"); err != nil { + _ = f.Close() c.Fatal(err) } - f.Close() + _ = f.Close() res := cli.DockerCmd(c, "exec", contID, "cat", "/etc/"+fn).Stdout() assert.Equal(c, res, "success2\n") diff --git a/integration-cli/docker_cli_exec_unix_test.go b/integration-cli/docker_cli_exec_unix_test.go index c91ef36544..c3f8638d3a 100644 --- a/integration-cli/docker_cli_exec_unix_test.go +++ b/integration-cli/docker_cli_exec_unix_test.go @@ -54,7 +54,7 @@ func (s *DockerCLIExecSuite) TestExecTTY(c *testing.T) { assert.NilError(c, err) defer p.Close() - _, err = p.Write([]byte("cat /foo && exit\n")) + _, err = p.WriteString("cat /foo && exit\n") assert.NilError(c, err) chErr := make(chan error, 1) diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index d11cd5b3f7..d687128a97 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -4088,8 +4088,8 @@ func (s *DockerCLIRunSuite) TestRunDuplicateMount(c *testing.T) { assert.NilError(c, err) defer tmpFile.Close() - data := "touch-me-foo-bar\n" - if _, err := tmpFile.Write([]byte(data)); err != nil { + const data = "touch-me-foo-bar\n" + if _, err := tmpFile.WriteString(data); err != nil { c.Fatal(err) } diff --git a/integration-cli/docker_cli_run_unix_test.go b/integration-cli/docker_cli_run_unix_test.go index 39009ad96b..ebd3c2aa50 100644 --- a/integration-cli/docker_cli_run_unix_test.go +++ b/integration-cli/docker_cli_run_unix_test.go @@ -107,7 +107,7 @@ func (s *DockerCLIRunSuite) TestRunAttachDetach(c *testing.T) { assert.NilError(c, cmd.Start()) cli.WaitRun(c, name) - _, err = cpty.Write([]byte("hello\n")) + _, err = cpty.WriteString("hello\n") assert.NilError(c, err) out, err := bufio.NewReader(stdout).ReadString('\n') @@ -166,7 +166,7 @@ func (s *DockerCLIRunSuite) TestRunAttachDetachFromFlag(c *testing.T) { } cli.WaitRun(c, name) - if _, err := cpty.Write([]byte("hello\n")); err != nil { + if _, err := cpty.WriteString("hello\n"); err != nil { c.Fatal(err) } @@ -280,7 +280,7 @@ func (s *DockerCLIRunSuite) TestRunAttachDetachFromConfig(c *testing.T) { } cli.WaitRun(c, name) - if _, err := cpty.Write([]byte("hello\n")); err != nil { + if _, err := cpty.WriteString("hello\n"); err != nil { c.Fatal(err) } @@ -361,7 +361,7 @@ func (s *DockerCLIRunSuite) TestRunAttachDetachKeysOverrideConfig(c *testing.T) } cli.WaitRun(c, name) - if _, err := cpty.Write([]byte("hello\n")); err != nil { + if _, err := cpty.WriteString("hello\n"); err != nil { c.Fatal(err) } @@ -400,8 +400,8 @@ func (s *DockerCLIRunSuite) TestRunAttachDetachKeysOverrideConfig(c *testing.T) func (s *DockerCLIRunSuite) TestRunAttachInvalidDetachKeySequencePreserved(c *testing.T) { const name = "attach-detach" - keyA := []byte{97} - keyB := []byte{98} + const keyA = "a" + const keyB = "b" cli.DockerCmd(c, "run", "--name", name, "-itd", "busybox", "cat") @@ -423,19 +423,19 @@ func (s *DockerCLIRunSuite) TestRunAttachInvalidDetachKeySequencePreserved(c *te cli.WaitRun(c, name) // Invalid escape sequence aba, should print aba in output - if _, err := cpty.Write(keyA); err != nil { + if _, err := cpty.WriteString(keyA); err != nil { c.Fatal(err) } time.Sleep(100 * time.Millisecond) - if _, err := cpty.Write(keyB); err != nil { + if _, err := cpty.WriteString(keyB); err != nil { c.Fatal(err) } time.Sleep(100 * time.Millisecond) - if _, err := cpty.Write(keyA); err != nil { + if _, err := cpty.WriteString(keyA); err != nil { c.Fatal(err) } time.Sleep(100 * time.Millisecond) - if _, err := cpty.Write([]byte("\n")); err != nil { + if _, err := cpty.WriteString("\n"); err != nil { c.Fatal(err) } @@ -443,8 +443,10 @@ func (s *DockerCLIRunSuite) TestRunAttachInvalidDetachKeySequencePreserved(c *te if err != nil { c.Fatal(err) } - if strings.TrimSpace(out) != "aba" { - c.Fatalf("expected 'aba', got %q", out) + + expected := keyA + keyB + keyA + if strings.TrimSpace(out) != expected { + c.Fatalf("expected '%s', got %q", expected, out) } } @@ -877,7 +879,7 @@ func (s *DockerCLIRunSuite) TestRunSeccompProfileDenyUnshare(c *testing.T) { } defer tmpFile.Close() - if _, err := tmpFile.Write([]byte(jsonData)); err != nil { + if _, err := tmpFile.WriteString(jsonData); err != nil { c.Fatal(err) } icmd.RunCommand(dockerBinary, "run", "--security-opt", "apparmor=unconfined", @@ -912,7 +914,7 @@ func (s *DockerCLIRunSuite) TestRunSeccompProfileDenyChmod(c *testing.T) { assert.NilError(c, err) defer tmpFile.Close() - if _, err := tmpFile.Write([]byte(jsonData)); err != nil { + if _, err := tmpFile.WriteString(jsonData); err != nil { c.Fatal(err) } icmd.RunCommand(dockerBinary, "run", "--security-opt", "seccomp="+tmpFile.Name(), @@ -949,7 +951,7 @@ func (s *DockerCLIRunSuite) TestRunSeccompProfileDenyUnshareUserns(c *testing.T) } defer tmpFile.Close() - if _, err := tmpFile.Write([]byte(jsonData)); err != nil { + if _, err := tmpFile.WriteString(jsonData); err != nil { c.Fatal(err) } icmd.RunCommand(dockerBinary, "run", @@ -1415,7 +1417,7 @@ func (s *DockerDaemonSuite) TestRunSeccompJSONNewFormat(c *testing.T) { tmpFile, err := os.CreateTemp("", "profile.json") assert.NilError(c, err) defer tmpFile.Close() - _, err = tmpFile.Write([]byte(jsonData)) + _, err = tmpFile.WriteString(jsonData) assert.NilError(c, err) 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) { tmpFile, err := os.CreateTemp("", "profile.json") assert.NilError(c, err) defer tmpFile.Close() - _, err = tmpFile.Write([]byte(jsonData)) + _, err = tmpFile.WriteString(jsonData) assert.NilError(c, err) 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) { tmpFile, err := os.CreateTemp("", "profile.json") assert.NilError(c, err) defer tmpFile.Close() - _, err = tmpFile.Write([]byte(jsonData)) + _, err = tmpFile.WriteString(jsonData) assert.NilError(c, err) 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) tmpFile, err := os.CreateTemp("", "profile.json") assert.NilError(c, err) defer tmpFile.Close() - _, err = tmpFile.Write([]byte(jsonData)) + _, err = tmpFile.WriteString(jsonData) assert.NilError(c, err) // 2) restart the daemon and add a custom seccomp profile in which we deny chmod diff --git a/integration-cli/docker_cli_update_unix_test.go b/integration-cli/docker_cli_update_unix_test.go index 6be396301c..64c84e9cbc 100644 --- a/integration-cli/docker_cli_update_unix_test.go +++ b/integration-cli/docker_cli_update_unix_test.go @@ -232,7 +232,7 @@ func (s *DockerCLIUpdateSuite) TestUpdateNotAffectMonitorRestartPolicy(c *testin assert.NilError(c, cmd.Start()) defer cmd.Process.Kill() - _, err = cpty.Write([]byte("exit\n")) + _, err = cpty.WriteString("exit\n") assert.NilError(c, err) assert.NilError(c, cmd.Wait())