Skip to content

Commit 4ca6222

Browse files
committed
journal: add proper StderrIsJournalStream test
To ensure that StderrIsJournalStream properly works in real conditions, this test re-executes itself with systemd-run, and observes exit code and logged entries.
1 parent fe8fac5 commit 4ca6222

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

journal/journal_unix_test.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ package journal_test
2020
import (
2121
"fmt"
2222
"os"
23+
"os/exec"
2324
"syscall"
2425
"testing"
2526

2627
"github.com/coreos/go-systemd/v22/journal"
2728
)
2829

29-
func TestStderrIsJournalStream(t *testing.T) {
30+
func TestJournalStreamParsing(t *testing.T) {
3031
if _, ok := os.LookupEnv("JOURNAL_STREAM"); ok {
3132
t.Fatal("unset JOURNAL_STREAM before running this test")
3233
}
@@ -84,6 +85,51 @@ func TestStderrIsJournalStream(t *testing.T) {
8485
})
8586
}
8687

88+
func TestStderrIsJournalStream(t *testing.T) {
89+
const (
90+
message = "TEST_MESSAGE"
91+
)
92+
93+
userOrSystem := "--user"
94+
if os.Getuid() == 0 {
95+
userOrSystem = "--system"
96+
}
97+
98+
if _, ok := os.LookupEnv("JOURNAL_STREAM"); !ok {
99+
// Re-execute this test under systemd (see the else branch),
100+
// and observe its exit code.
101+
args := []string{
102+
"systemd-run",
103+
userOrSystem,
104+
"--wait",
105+
"--quiet",
106+
"--",
107+
os.Args[0],
108+
"-test.run=TestStderrIsJournalStream",
109+
"-test.count=1", // inhibit caching
110+
}
111+
112+
cmd := exec.Command(args[0], args[1:]...)
113+
cmd.Stderr = os.Stderr
114+
if err := cmd.Run(); err != nil {
115+
t.Fatal(err)
116+
}
117+
} else {
118+
ok, err := journal.StderrIsJournalStream()
119+
if err != nil {
120+
t.Fatal(err)
121+
}
122+
if !ok {
123+
t.Fatal("StderrIsJournalStream should've returned true")
124+
}
125+
126+
err = journal.Send(message, journal.PriInfo, nil)
127+
if err != nil {
128+
t.Fatal(err)
129+
}
130+
}
131+
}
132+
87133
func ExampleStderrIsJournalStream() {
88134
// NOTE: this is just an example. Production code
89135
// will likely use this to setup a logging library

0 commit comments

Comments
 (0)