Skip to content

Commit 8a80469

Browse files
committed
Harden journald test
Signed-off-by: apostasie <[email protected]>
1 parent 300a705 commit 8a80469

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

cmd/nerdctl/container/container_logs_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ package container
1818

1919
import (
2020
"fmt"
21+
"os/exec"
2122
"runtime"
2223
"strings"
2324
"testing"
2425
"time"
2526

2627
"gotest.tools/v3/assert"
28+
"gotest.tools/v3/icmd"
2729

2830
"github.com/containerd/nerdctl/v2/pkg/testutil"
2931
)
@@ -109,8 +111,14 @@ func TestLogsWithInheritedFlags(t *testing.T) {
109111
}
110112

111113
func TestLogsOfJournaldDriver(t *testing.T) {
112-
t.Parallel()
113114
testutil.RequireExecutable(t, "journalctl")
115+
journalctl, _ := exec.LookPath("journalctl")
116+
res := icmd.RunCmd(icmd.Command(journalctl, "-xe"))
117+
if res.ExitCode != 0 {
118+
t.Skipf("current user is not allowed to access journal logs: %s", res.Combined())
119+
}
120+
121+
t.Parallel()
114122
base := testutil.NewBase(t)
115123
containerName := testutil.Identifier(t)
116124

cmd/nerdctl/container/container_run_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,13 @@ func TestRunWithJsonFileLogDriverAndLogPathOpt(t *testing.T) {
316316
}
317317

318318
func TestRunWithJournaldLogDriver(t *testing.T) {
319+
testutil.RequireExecutable(t, "journalctl")
320+
journalctl, _ := exec.LookPath("journalctl")
321+
res := icmd.RunCmd(icmd.Command(journalctl, "-xe"))
322+
if res.ExitCode != 0 {
323+
t.Skipf("current user is not allowed to access journal logs: %s", res.Combined())
324+
}
325+
319326
if runtime.GOOS == "windows" {
320327
t.Skip("journald log driver is not yet implemented on Windows")
321328
}
@@ -327,8 +334,6 @@ func TestRunWithJournaldLogDriver(t *testing.T) {
327334
"sh", "-euxc", "echo foo; echo bar").AssertOK()
328335

329336
time.Sleep(3 * time.Second)
330-
journalctl, err := exec.LookPath("journalctl")
331-
assert.NilError(t, err)
332337

333338
inspectedContainer := base.InspectContainer(containerName)
334339

@@ -370,6 +375,13 @@ func TestRunWithJournaldLogDriver(t *testing.T) {
370375
}
371376

372377
func TestRunWithJournaldLogDriverAndLogOpt(t *testing.T) {
378+
testutil.RequireExecutable(t, "journalctl")
379+
journalctl, _ := exec.LookPath("journalctl")
380+
res := icmd.RunCmd(icmd.Command(journalctl, "-xe"))
381+
if res.ExitCode != 0 {
382+
t.Skipf("current user is not allowed to access journal logs: %s", res.Combined())
383+
}
384+
373385
if runtime.GOOS == "windows" {
374386
t.Skip("journald log driver is not yet implemented on Windows")
375387
}
@@ -381,8 +393,6 @@ func TestRunWithJournaldLogDriverAndLogOpt(t *testing.T) {
381393
"sh", "-euxc", "echo foo; echo bar").AssertOK()
382394

383395
time.Sleep(3 * time.Second)
384-
journalctl, err := exec.LookPath("journalctl")
385-
assert.NilError(t, err)
386396
inspectedContainer := base.InspectContainer(containerName)
387397
found := 0
388398
check := func(log poll.LogT) poll.Result {

0 commit comments

Comments
 (0)