github.com/coreos/mantle@v0.13.0/network/journal/format_test.go (about) 1 // Copyright 2017 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 package journal 16 17 import ( 18 "bytes" 19 "io" 20 "strings" 21 "testing" 22 "time" 23 24 "github.com/kylelemons/godebug/diff" 25 ) 26 27 func TestFormatShort(t *testing.T) { 28 for _, testcase := range []struct { 29 name string 30 entry Entry 31 expect string 32 }{{ 33 name: "all", 34 entry: Entry{ 35 FIELD_REALTIME_TIMESTAMP: []byte("1342540861421465"), 36 FIELD_SOURCE_REALTIME_TIMESTAMP: []byte("1342540861416351"), 37 FIELD_SYSLOG_IDENTIFIER: []byte("/USR/SBIN/CROND"), 38 FIELD_SYSLOG_PID: []byte("8278"), 39 FIELD_PID: []byte("8278"), 40 FIELD_MESSAGE: []byte("(root) CMD (run-parts /etc/cron.hourly)"), 41 }, 42 expect: "Jul 17 16:01:01.416351 /USR/SBIN/CROND[8278]: (root) CMD (run-parts /etc/cron.hourly)\n", 43 }, { 44 name: "multiline", 45 entry: Entry{ 46 FIELD_REALTIME_TIMESTAMP: []byte("1342540861421465"), 47 FIELD_SOURCE_REALTIME_TIMESTAMP: []byte("1342540861416351"), 48 FIELD_SYSLOG_IDENTIFIER: []byte("/USR/SBIN/CROND"), 49 FIELD_SYSLOG_PID: []byte("8278"), 50 FIELD_PID: []byte("8278"), 51 FIELD_MESSAGE: []byte("first\nsecond"), 52 }, 53 expect: "Jul 17 16:01:01.416351 /USR/SBIN/CROND[8278]: first\n second\n", 54 }, { 55 name: "syslog_pid", 56 entry: Entry{ 57 FIELD_REALTIME_TIMESTAMP: []byte("1342540861421465"), 58 FIELD_SOURCE_REALTIME_TIMESTAMP: []byte("1342540861416351"), 59 FIELD_SYSLOG_IDENTIFIER: []byte("/USR/SBIN/CROND"), 60 FIELD_SYSLOG_PID: []byte("8278"), 61 FIELD_MESSAGE: []byte("(root) CMD (run-parts /etc/cron.hourly)"), 62 }, 63 expect: "Jul 17 16:01:01.416351 /USR/SBIN/CROND[8278]: (root) CMD (run-parts /etc/cron.hourly)\n", 64 }, { 65 name: "no_pid", 66 entry: Entry{ 67 FIELD_REALTIME_TIMESTAMP: []byte("1342540861421465"), 68 FIELD_SOURCE_REALTIME_TIMESTAMP: []byte("1342540861416351"), 69 FIELD_SYSLOG_IDENTIFIER: []byte("/USR/SBIN/CROND"), 70 FIELD_MESSAGE: []byte("(root) CMD (run-parts /etc/cron.hourly)"), 71 }, 72 expect: "Jul 17 16:01:01.416351 /USR/SBIN/CROND: (root) CMD (run-parts /etc/cron.hourly)\n", 73 }, { 74 name: "no_ident", 75 entry: Entry{ 76 FIELD_REALTIME_TIMESTAMP: []byte("1342540861421465"), 77 FIELD_SOURCE_REALTIME_TIMESTAMP: []byte("1342540861416351"), 78 FIELD_SYSLOG_PID: []byte("8278"), 79 FIELD_PID: []byte("8278"), 80 FIELD_MESSAGE: []byte("(root) CMD (run-parts /etc/cron.hourly)"), 81 }, 82 expect: "Jul 17 16:01:01.416351 unknown[8278]: (root) CMD (run-parts /etc/cron.hourly)\n", 83 }, { 84 name: "no_ident_pid", 85 entry: Entry{ 86 FIELD_REALTIME_TIMESTAMP: []byte("1342540861421465"), 87 FIELD_SOURCE_REALTIME_TIMESTAMP: []byte("1342540861416351"), 88 FIELD_MESSAGE: []byte("(root) CMD (run-parts /etc/cron.hourly)"), 89 }, 90 expect: "Jul 17 16:01:01.416351 unknown: (root) CMD (run-parts /etc/cron.hourly)\n", 91 }, { 92 name: "no_source", 93 entry: Entry{ 94 FIELD_REALTIME_TIMESTAMP: []byte("1342540861421465"), 95 FIELD_SYSLOG_IDENTIFIER: []byte("/USR/SBIN/CROND"), 96 FIELD_SYSLOG_PID: []byte("8278"), 97 FIELD_PID: []byte("8278"), 98 FIELD_MESSAGE: []byte("(root) CMD (run-parts /etc/cron.hourly)"), 99 }, 100 expect: "Jul 17 16:01:01.421465 /USR/SBIN/CROND[8278]: (root) CMD (run-parts /etc/cron.hourly)\n", 101 }, { 102 name: "no_time", 103 entry: Entry{ 104 FIELD_SYSLOG_IDENTIFIER: []byte("/USR/SBIN/CROND"), 105 FIELD_SYSLOG_PID: []byte("8278"), 106 FIELD_PID: []byte("8278"), 107 FIELD_MESSAGE: []byte("(root) CMD (run-parts /etc/cron.hourly)"), 108 }, 109 expect: "", 110 }, { 111 name: "zero_time", 112 entry: Entry{ 113 FIELD_REALTIME_TIMESTAMP: []byte("0"), 114 FIELD_SYSLOG_IDENTIFIER: []byte("/USR/SBIN/CROND"), 115 FIELD_SYSLOG_PID: []byte("8278"), 116 FIELD_PID: []byte("8278"), 117 FIELD_MESSAGE: []byte("(root) CMD (run-parts /etc/cron.hourly)"), 118 }, 119 expect: "", 120 }, { 121 name: "nearly_zero_time", 122 entry: Entry{ 123 FIELD_REALTIME_TIMESTAMP: []byte("2"), 124 FIELD_SYSLOG_IDENTIFIER: []byte("/USR/SBIN/CROND"), 125 FIELD_SYSLOG_PID: []byte("8278"), 126 FIELD_PID: []byte("8278"), 127 FIELD_MESSAGE: []byte("(root) CMD (run-parts /etc/cron.hourly)"), 128 }, 129 expect: "", 130 }, { 131 name: "no_message", 132 entry: Entry{ 133 FIELD_REALTIME_TIMESTAMP: []byte("1342540861421465"), 134 FIELD_SOURCE_REALTIME_TIMESTAMP: []byte("1342540861416351"), 135 FIELD_SYSLOG_IDENTIFIER: []byte("/USR/SBIN/CROND"), 136 FIELD_SYSLOG_PID: []byte("8278"), 137 FIELD_PID: []byte("8278"), 138 }, 139 expect: "", 140 }} { 141 t.Run(testcase.name, func(t *testing.T) { 142 var buf bytes.Buffer 143 w := ShortWriter(&buf) 144 w.SetTimezone(time.UTC) // Needed for consistent test results. 145 if err := w.WriteEntry(testcase.entry); err != nil { 146 t.Error(err) 147 } 148 if buf.String() != testcase.expect { 149 t.Errorf("%q != %q", buf.String(), testcase.expect) 150 } 151 }) 152 } 153 } 154 155 func TestFormatShortFromExport(t *testing.T) { 156 var buf bytes.Buffer 157 er := NewExportReader(strings.NewReader(exportText + exportBinary)) 158 sw := ShortWriter(&buf) 159 sw.SetTimezone(time.UTC) 160 for { 161 entry, err := er.ReadEntry() 162 if err == io.EOF { 163 break 164 } else if err != nil { 165 t.Fatal(err) 166 } 167 if err := sw.WriteEntry(entry); err != nil { 168 t.Error(err) 169 } 170 } 171 const expect = `Jul 17 16:01:01.413961 gdm-password][587]: AccountsService-DEBUG(+): ActUserManager: ignoring unspecified session '8' since it's not graphical: Success 172 Jul 17 16:01:01.416351 /USR/SBIN/CROND[8278]: (root) CMD (run-parts /etc/cron.hourly) 173 -- Reboot -- 174 Feb 14 20:15:16.372858 python3[16853]: foo 175 bar 176 ` 177 if d := diff.Diff(buf.String(), expect); d != "" { 178 t.Errorf("unexpected output:\n%s", d) 179 } 180 }