Skip to content

Commit

Permalink
journal: add proper StderrIsJournalStream test
Browse files Browse the repository at this point in the history
To ensure that StderrIsJournalStream properly works in real conditions,
this test re-executes itself with systemd-run, and observes exit code
and logged entries.
  • Loading branch information
WGH- committed Nov 4, 2022
1 parent 0b82238 commit bbbf9b7
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 51 deletions.
37 changes: 0 additions & 37 deletions examples/journal/main.go

This file was deleted.

13 changes: 0 additions & 13 deletions examples/journal/run.sh

This file was deleted.

98 changes: 97 additions & 1 deletion journal/journal_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@
package journal_test

import (
"crypto/rand"
"encoding/json"
"fmt"
"os"
"os/exec"
"syscall"
"testing"
"time"

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

func TestStderrIsJournalStream(t *testing.T) {
func TestJournalStreamParsing(t *testing.T) {
if _, ok := os.LookupEnv("JOURNAL_STREAM"); ok {
t.Fatal("unset JOURNAL_STREAM before running this test")
}
Expand Down Expand Up @@ -84,6 +88,98 @@ func TestStderrIsJournalStream(t *testing.T) {
})
}

func TestStderrIsJournalStream(t *testing.T) {
const (
message = "TEST_MESSAGE"
)

userOrSystem := "--user"
if os.Getuid() == 0 {
userOrSystem = "--system"
}

if _, ok := os.LookupEnv("JOURNAL_STREAM"); !ok {
// Re-execute this test under systemd (see the else branch),
// and observe its exit code and logged entries.

var randBytes [16]byte
_, err := rand.Read(randBytes[:])
if err != nil {
t.Fatal(err)
}

unitName := fmt.Sprintf("run-r%x.service", randBytes[:])

args := []string{
"systemd-run",
"--unit", unitName,
userOrSystem,
"--wait",
"--quiet",
"--",
os.Args[0],
"-test.run=TestStderrIsJournalStream",
"-test.count=1", // inhibit caching
}

cmd := exec.Command(args[0], args[1:]...)
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
t.Fatal(err)
}

cmd = exec.Command(
"journalctl",
userOrSystem,
"-u", unitName,
"-o", "json",
"MESSAGE="+message,
)
output, err := cmd.Output()
if err != nil {
t.Fatal(err)
}
t.Logf("Journalled output: %s", output)

var entry map[string]interface{}

err = json.Unmarshal(output, &entry)
if err != nil {
t.Fatal(err)
}

if expected, got := "journal", entry["_TRANSPORT"]; expected != got {
t.Errorf("wrong _TRANSPORT field value: expected %q got %q", expected, got)
}

} else {
ok, err := journal.StderrIsJournalStream()
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("StderrIsJournalStream should've returned true")
}

err = journal.Send(message, journal.PriInfo, nil)
if err != nil {
t.Fatal(err)
}

if userOrSystem == "--system" {
err = exec.Command("journalctl", "--sync").Run()
if err != nil {
t.Fatal(err)
}
} else {
// XXX there's no good way to ensure that journald has received
// and stored our message.
time.Sleep(time.Second)
}
}
}

func ExampleStderrIsJournalStream() {
// NOTE: this is just an example. Production code
// will likely use this to setup a logging library
Expand Down

0 comments on commit bbbf9b7

Please sign in to comment.