github.com/coreos/mantle@v0.13.0/network/journal/export_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 "io" 19 "strings" 20 "testing" 21 ) 22 23 const ( 24 exportText = `__CURSOR=s=739ad463348b4ceca5a9e69c95a3c93f;i=4ece7;b=6c7c6013a26343b29e964691ff25d04c;m=4fc72436e;t=4c508a72423d9;x=d3e5610681098c10;p=system.journal 25 __REALTIME_TIMESTAMP=1342540861416409 26 __MONOTONIC_TIMESTAMP=21415215982 27 _BOOT_ID=6c7c6013a26343b29e964691ff25d04c 28 _TRANSPORT=syslog 29 PRIORITY=4 30 SYSLOG_FACILITY=3 31 SYSLOG_IDENTIFIER=gdm-password] 32 SYSLOG_PID=587 33 MESSAGE=AccountsService-DEBUG(+): ActUserManager: ignoring unspecified session '8' since it's not graphical: Success 34 _PID=587 35 _UID=0 36 _GID=500 37 _COMM=gdm-session-wor 38 _EXE=/usr/libexec/gdm-session-worker 39 _CMDLINE=gdm-session-worker [pam/gdm-password] 40 _AUDIT_SESSION=2 41 _AUDIT_LOGINUID=500 42 _SYSTEMD_CGROUP=/user/lennart/2 43 _SYSTEMD_SESSION=2 44 _SELINUX_CONTEXT=system_u:system_r:xdm_t:s0-s0:c0.c1023 45 _SOURCE_REALTIME_TIMESTAMP=1342540861413961 46 _MACHINE_ID=a91663387a90b89f185d4e860000001a 47 _HOSTNAME=epsilon 48 49 __CURSOR=s=739ad463348b4ceca5a9e69c95a3c93f;i=4ece8;b=6c7c6013a26343b29e964691ff25d04c;m=4fc72572f;t=4c508a7243799;x=68597058a89b7246;p=system.journal 50 __REALTIME_TIMESTAMP=1342540861421465 51 __MONOTONIC_TIMESTAMP=21415221039 52 _BOOT_ID=6c7c6013a26343b29e964691ff25d04c 53 _TRANSPORT=syslog 54 PRIORITY=6 55 SYSLOG_FACILITY=9 56 SYSLOG_IDENTIFIER=/USR/SBIN/CROND 57 SYSLOG_PID=8278 58 MESSAGE=(root) CMD (run-parts /etc/cron.hourly) 59 _PID=8278 60 _UID=0 61 _GID=0 62 _COMM=run-parts 63 _EXE=/usr/bin/bash 64 _CMDLINE=/bin/bash /bin/run-parts /etc/cron.hourly 65 _AUDIT_SESSION=8 66 _AUDIT_LOGINUID=0 67 _SYSTEMD_CGROUP=/user/root/8 68 _SYSTEMD_SESSION=8 69 _SELINUX_CONTEXT=system_u:system_r:crond_t:s0-s0:c0.c1023 70 _SOURCE_REALTIME_TIMESTAMP=1342540861416351 71 _MACHINE_ID=a91663387a90b89f185d4e860000001a 72 _HOSTNAME=epsilon 73 74 ` 75 76 exportBinary = ` 77 __CURSOR=s=bcce4fb8ffcb40e9a6e05eee8b7831bf;i=5ef603;b=ec25d6795f0645619ddac9afdef453ee;m=545242e7049;t=50f1202 78 __REALTIME_TIMESTAMP=1423944916375353 79 __MONOTONIC_TIMESTAMP=5794517905481 80 _BOOT_ID=ec25d6795f0645619ddac9afdef453ee 81 _TRANSPORT=journal 82 _UID=1001 83 _GID=1001 84 _CAP_EFFECTIVE=0 85 _SYSTEMD_OWNER_UID=1001 86 _SYSTEMD_SLICE=user-1001.slice 87 _MACHINE_ID=5833158886a8445e801d437313d25eff 88 _HOSTNAME=bupkis 89 _AUDIT_LOGINUID=1001 90 _SELINUX_CONTEXT=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 91 CODE_LINE=1 92 CODE_FUNC=<module> 93 SYSLOG_IDENTIFIER=python3 94 _COMM=python3 95 _EXE=/usr/bin/python3.4 96 _AUDIT_SESSION=35898 97 _SYSTEMD_CGROUP=/user.slice/user-1001.slice/session-35898.scope 98 _SYSTEMD_SESSION=35898 99 _SYSTEMD_UNIT=session-35898.scope 100 MESSAGE 101 ` + "\x07\x00\x00\x00\x00\x00\x00\x00foo\nbar" + ` 102 CODE_FILE=<string> 103 _PID=16853 104 _CMDLINE=python3 -c from systemd import journal; journal.send("foo\nbar") 105 _SOURCE_REALTIME_TIMESTAMP=1423944916372858 106 107 ` 108 ) 109 110 func TestExportReaderText(t *testing.T) { 111 er := NewExportReader(strings.NewReader(exportText)) 112 if entry, err := er.ReadEntry(); err != nil { 113 t.Errorf("first read failed: %v", err) 114 } else { 115 const expect = "AccountsService-DEBUG(+): ActUserManager: ignoring unspecified session '8' since it's not graphical: Success" 116 msg := string(entry[FIELD_MESSAGE]) 117 if msg != expect { 118 t.Errorf("%q != %q", msg, expect) 119 } 120 } 121 if entry, err := er.ReadEntry(); err != nil { 122 t.Errorf("second read failed: %v", err) 123 } else { 124 const expect = "(root) CMD (run-parts /etc/cron.hourly)" 125 msg := string(entry[FIELD_MESSAGE]) 126 if msg != expect { 127 t.Errorf("%q != %q", msg, expect) 128 } 129 } 130 if _, err := er.ReadEntry(); err != io.EOF { 131 t.Errorf("final read didn't return EOF: %v", err) 132 } 133 } 134 135 func TestExportReaderLeadingNewline(t *testing.T) { 136 er := NewExportReader(strings.NewReader("\n" + exportText)) 137 if entry, err := er.ReadEntry(); err != nil { 138 t.Errorf("first read failed: %v", err) 139 } else { 140 const expect = "AccountsService-DEBUG(+): ActUserManager: ignoring unspecified session '8' since it's not graphical: Success" 141 msg := string(entry[FIELD_MESSAGE]) 142 if msg != expect { 143 t.Errorf("%q != %q", msg, expect) 144 } 145 } 146 if entry, err := er.ReadEntry(); err != nil { 147 t.Errorf("second read failed: %v", err) 148 } else { 149 const expect = "(root) CMD (run-parts /etc/cron.hourly)" 150 msg := string(entry[FIELD_MESSAGE]) 151 if msg != expect { 152 t.Errorf("%q != %q", msg, expect) 153 } 154 } 155 if _, err := er.ReadEntry(); err != io.EOF { 156 t.Errorf("final read didn't return EOF: %v", err) 157 } 158 } 159 160 func TestExportReaderBinary(t *testing.T) { 161 er := NewExportReader(strings.NewReader(exportBinary)) 162 if entry, err := er.ReadEntry(); err != nil { 163 t.Errorf("first read failed: %v", err) 164 } else { 165 const expect = "foo\nbar" 166 msg := string(entry[FIELD_MESSAGE]) 167 if msg != expect { 168 t.Errorf("%q != %q", msg, expect) 169 } 170 } 171 if _, err := er.ReadEntry(); err != io.EOF { 172 t.Errorf("final read didn't return EOF: %v", err) 173 } 174 } 175 176 func BenchmarkExportReader(b *testing.B) { 177 testData := strings.Repeat(exportText+exportBinary, 10) 178 b.ResetTimer() 179 for i := 0; i < b.N; i++ { 180 er := NewExportReader(strings.NewReader(testData)) 181 for { 182 if _, err := er.ReadEntry(); err == io.EOF { 183 break 184 } else if err != nil { 185 b.Error(err) 186 } 187 } 188 } 189 }