@@ -20,13 +20,14 @@ package journal_test
20
20
import (
21
21
"fmt"
22
22
"os"
23
+ "os/exec"
23
24
"syscall"
24
25
"testing"
25
26
26
27
"github.com/coreos/go-systemd/v22/journal"
27
28
)
28
29
29
- func TestStderrIsJournalStream (t * testing.T ) {
30
+ func TestJournalStreamParsing (t * testing.T ) {
30
31
if _ , ok := os .LookupEnv ("JOURNAL_STREAM" ); ok {
31
32
t .Fatal ("unset JOURNAL_STREAM before running this test" )
32
33
}
@@ -84,6 +85,51 @@ func TestStderrIsJournalStream(t *testing.T) {
84
85
})
85
86
}
86
87
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
+
87
133
func ExampleStderrIsJournalStream () {
88
134
// NOTE: this is just an example. Production code
89
135
// will likely use this to setup a logging library
0 commit comments