From bcca79684827cb7603204165731e004be90a448e Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 5 May 2021 11:30:22 -0700 Subject: [PATCH 1/4] libct/int: simplify/fix showing errors Instead of manually figuring out the file and line number of the caller, use t.Helper() so t.Fatal prints the correct one. Before: > utils_test.go:85: exec_test.go:536: unexpected error: container_linux.go:380: starting container process caused: exec: "catt": executable file not found in $PATH After: > exec_test.go:536: unexpected error: container_linux.go:380: starting container process caused: exec: "catt": executable file not found in $PATH (the error is introduced by s/cat/catt/) Signed-off-by: Kir Kolyshkin --- libcontainer/integration/utils_test.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/libcontainer/integration/utils_test.go b/libcontainer/integration/utils_test.go index 74bd7e073..75203d6f5 100644 --- a/libcontainer/integration/utils_test.go +++ b/libcontainer/integration/utils_test.go @@ -80,22 +80,21 @@ func (b *stdBuffers) String() string { // ok fails the test if an err is not nil. func ok(t testing.TB, err error) { + t.Helper() if err != nil { - _, file, line, _ := runtime.Caller(1) - t.Fatalf("%s:%d: unexpected error: %s\n\n", filepath.Base(file), line, err.Error()) + t.Fatalf("unexpected error: %v", err) } } func waitProcess(p *libcontainer.Process, t *testing.T) { - _, file, line, _ := runtime.Caller(1) + t.Helper() status, err := p.Wait() - if err != nil { - t.Fatalf("%s:%d: unexpected error: %s\n\n", filepath.Base(file), line, err.Error()) + t.Fatalf("unexpected error: %v", err) } if !status.Success() { - t.Fatalf("%s:%d: unexpected status: %s\n\n", filepath.Base(file), line, status.String()) + t.Fatalf("unexpected status: %v", status) } } From 72d7a8249d697f91c54e1cfd5bec7042f770cb34 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 5 May 2021 11:37:13 -0700 Subject: [PATCH 2/4] libct/int/checkpoint_test: use t.Helper Otherwise showFile log statements show its own file/line info, rather than that of the caller's. Signed-off-by: Kir Kolyshkin --- libcontainer/integration/checkpoint_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/libcontainer/integration/checkpoint_test.go b/libcontainer/integration/checkpoint_test.go index 06e130608..2d13469da 100644 --- a/libcontainer/integration/checkpoint_test.go +++ b/libcontainer/integration/checkpoint_test.go @@ -14,6 +14,7 @@ import ( ) func showFile(t *testing.T, fname string) error { + t.Helper() t.Logf("=== %s ===\n", fname) f, err := os.Open(fname) From 7eb1405b60eda1f2c3bd38db2c36a5e293d37fa4 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 5 May 2021 11:45:02 -0700 Subject: [PATCH 3/4] libct/int/checkpoint_test: use waitProcess helper Signed-off-by: Kir Kolyshkin --- libcontainer/integration/checkpoint_test.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/libcontainer/integration/checkpoint_test.go b/libcontainer/integration/checkpoint_test.go index 2d13469da..311e33c99 100644 --- a/libcontainer/integration/checkpoint_test.go +++ b/libcontainer/integration/checkpoint_test.go @@ -196,12 +196,7 @@ func testCheckpoint(t *testing.T, userns bool) { ok(t, err) restoreStdinW.Close() - s, err := restoreProcessConfig.Wait() - ok(t, err) - - if !s.Success() { - t.Fatal(s.String(), pid) - } + waitProcess(restoreProcessConfig, t) output := restoreStdout.String() if !strings.Contains(output, "Hello!") { From 0fabed76c139737635b81ce7363cfe0ed0f95ed8 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 5 May 2021 11:45:29 -0700 Subject: [PATCH 4/4] libct/int/checkpoint_test: use kill(0) for pid check os.FindProcess never returns an error on Unix/Linux. Use kill(0) to actually check if the process exists. Signed-off-by: Kir Kolyshkin --- libcontainer/integration/checkpoint_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libcontainer/integration/checkpoint_test.go b/libcontainer/integration/checkpoint_test.go index 311e33c99..9ec03b92d 100644 --- a/libcontainer/integration/checkpoint_test.go +++ b/libcontainer/integration/checkpoint_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/opencontainers/runc/libcontainer" + "golang.org/x/sys/unix" ) func showFile(t *testing.T, fname string) error { @@ -189,7 +190,7 @@ func testCheckpoint(t *testing.T, userns bool) { pid, err = restoreProcessConfig.Pid() ok(t, err) - _, err = os.FindProcess(pid) + err = unix.Kill(pid, 0) ok(t, err) _, err = restoreStdinW.WriteString("Hello!")