@@ -20,13 +20,14 @@ package journal_test
2020import (
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+
87133func ExampleStderrIsJournalStream () {
88134 // NOTE: this is just an example. Production code
89135 // will likely use this to setup a logging library
0 commit comments