|
| 1 | +// Copyright 2022 CoreOS, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//go:build !windows |
| 16 | +// +build !windows |
| 17 | + |
| 18 | +package journal_test |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "syscall" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/coreos/go-systemd/v22/journal" |
| 27 | +) |
| 28 | + |
| 29 | +func TestStderrIsJournalStream(t *testing.T) { |
| 30 | + if _, ok := os.LookupEnv("JOURNAL_STREAM"); ok { |
| 31 | + t.Fatal("unset JOURNAL_STREAM before running this test") |
| 32 | + } |
| 33 | + |
| 34 | + t.Run("Missing", func(t *testing.T) { |
| 35 | + ok, err := journal.StderrIsJournalStream() |
| 36 | + if err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + if ok { |
| 40 | + t.Error("stderr shouldn't be connected to journal stream") |
| 41 | + } |
| 42 | + }) |
| 43 | + t.Run("Present", func(t *testing.T) { |
| 44 | + f, stat := getUnixStreamSocket(t) |
| 45 | + defer f.Close() |
| 46 | + os.Setenv("JOURNAL_STREAM", fmt.Sprintf("%d:%d", stat.Dev, stat.Ino)) |
| 47 | + defer os.Unsetenv("JOURNAL_STREAM") |
| 48 | + replaceStderr(int(f.Fd()), func() { |
| 49 | + ok, err := journal.StderrIsJournalStream() |
| 50 | + if err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + if !ok { |
| 54 | + t.Error("stderr should've been connected to journal stream") |
| 55 | + } |
| 56 | + }) |
| 57 | + }) |
| 58 | + t.Run("NotMatching", func(t *testing.T) { |
| 59 | + f, stat := getUnixStreamSocket(t) |
| 60 | + defer f.Close() |
| 61 | + os.Setenv("JOURNAL_STREAM", fmt.Sprintf("%d:%d", stat.Dev+1, stat.Ino)) |
| 62 | + defer os.Unsetenv("JOURNAL_STREAM") |
| 63 | + replaceStderr(int(f.Fd()), func() { |
| 64 | + ok, err := journal.StderrIsJournalStream() |
| 65 | + if err != nil { |
| 66 | + t.Fatal(err) |
| 67 | + } |
| 68 | + if ok { |
| 69 | + t.Error("stderr shouldn't be connected to journal stream") |
| 70 | + } |
| 71 | + }) |
| 72 | + }) |
| 73 | + t.Run("Malformed", func(t *testing.T) { |
| 74 | + f, stat := getUnixStreamSocket(t) |
| 75 | + defer f.Close() |
| 76 | + os.Setenv("JOURNAL_STREAM", fmt.Sprintf("%d-%d", stat.Dev, stat.Ino)) |
| 77 | + defer os.Unsetenv("JOURNAL_STREAM") |
| 78 | + replaceStderr(int(f.Fd()), func() { |
| 79 | + _, err := journal.StderrIsJournalStream() |
| 80 | + if err == nil { |
| 81 | + t.Fatal("JOURNAL_STREAM is malformed, but no error returned") |
| 82 | + } |
| 83 | + }) |
| 84 | + }) |
| 85 | +} |
| 86 | + |
| 87 | +func ExampleStderrIsJournalStream() { |
| 88 | + // NOTE: this is just an example. Production code |
| 89 | + // will likely use this to setup a logging library |
| 90 | + // to write messages to either journal or stderr. |
| 91 | + ok, err := journal.StderrIsJournalStream() |
| 92 | + if err != nil { |
| 93 | + panic(err) |
| 94 | + } |
| 95 | + |
| 96 | + if ok { |
| 97 | + // use journal native protocol |
| 98 | + journal.Send("this is a message logged through the native protocol", journal.PriInfo, nil) |
| 99 | + } else { |
| 100 | + // use stderr |
| 101 | + fmt.Fprintln(os.Stderr, "this is a message logged through stderr") |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func replaceStderr(fd int, cb func()) { |
| 106 | + savedStderr, err := syscall.Dup(syscall.Stderr) |
| 107 | + if err != nil { |
| 108 | + panic(err) |
| 109 | + } |
| 110 | + defer syscall.Close(savedStderr) |
| 111 | + err = syscall.Dup2(fd, syscall.Stderr) |
| 112 | + if err != nil { |
| 113 | + panic(err) |
| 114 | + } |
| 115 | + defer func() { |
| 116 | + err := syscall.Dup2(savedStderr, syscall.Stderr) |
| 117 | + if err != nil { |
| 118 | + panic(err) |
| 119 | + } |
| 120 | + }() |
| 121 | + cb() |
| 122 | +} |
| 123 | + |
| 124 | +// getUnixStreamSocket returns a unix stream socket obtained with |
| 125 | +// socketpair(2), and its fstat result. Only one end of the socket pair |
| 126 | +// is returned, and the other end is closed immediately: we don't need |
| 127 | +// it for our purposes. |
| 128 | +func getUnixStreamSocket(t *testing.T) (*os.File, *syscall.Stat_t) { |
| 129 | + fds, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0) |
| 130 | + if err != nil { |
| 131 | + t.Fatal(os.NewSyscallError("socketpair", err)) |
| 132 | + } |
| 133 | + // we don't need the remote end for our tests |
| 134 | + syscall.Close(fds[1]) |
| 135 | + |
| 136 | + file := os.NewFile(uintptr(fds[0]), "unix-stream") |
| 137 | + stat, err := file.Stat() |
| 138 | + if err != nil { |
| 139 | + t.Fatal(err) |
| 140 | + } |
| 141 | + return file, stat.Sys().(*syscall.Stat_t) |
| 142 | +} |
0 commit comments