1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
|
// Copyright 2015 RedHat, Inc.
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sdjournal
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"strings"
"testing"
"time"
"github.com/coreos/go-systemd/v22/journal"
)
func TestJournalFollow(t *testing.T) {
r, err := NewJournalReader(JournalReaderConfig{
Since: time.Duration(-15) * time.Second,
Matches: []Match{
{
Field: SD_JOURNAL_FIELD_SYSTEMD_UNIT,
Value: "NetworkManager.service",
},
},
})
if err != nil {
t.Fatalf("Error opening journal: %s", err)
}
if r == nil {
t.Fatal("Got a nil reader")
}
defer r.Close()
// start writing some test entries
done := make(chan struct{}, 1)
errCh := make(chan error, 1)
defer close(done)
go func() {
for {
select {
case <-done:
return
default:
if perr := journal.Print(journal.PriInfo, "test message %s", time.Now()); err != nil {
errCh <- perr
return
}
time.Sleep(time.Second)
}
}
}()
// and follow the reader synchronously
timeout := time.Duration(5) * time.Second
if err = r.Follow(time.After(timeout), os.Stdout); err != ErrExpired {
t.Fatalf("Error during follow: %s", err)
}
select {
case err := <-errCh:
t.Fatalf("Error writing to journal: %s", err)
default:
}
}
func TestJournalWait(t *testing.T) {
id := time.Now().String()
j, err := NewJournal()
if err != nil {
t.Fatalf("Error opening journal: %s", err)
}
if err := j.AddMatch("TEST=TestJournalWait " + id); err != nil {
t.Fatalf("Error adding match: %s", err)
}
if err := j.SeekTail(); err != nil {
t.Fatalf("Error seeking to tail: %s", err)
}
if _, err := j.Previous(); err != nil {
t.Fatalf("Error retrieving previous entry: %s", err)
}
var t1, t2 time.Time
for ret := -1; ret != SD_JOURNAL_NOP; {
// Wait() might return for reasons other than timeout.
// For example the first call initializes stuff and returns immediately.
t1 = time.Now()
ret = j.Wait(time.Millisecond * 300)
t2 = time.Now()
}
duration := t2.Sub(t1)
if duration > time.Millisecond*325 || duration < time.Millisecond*300 {
t.Errorf("Wait did not wait 300ms. Actually waited %s", duration.String())
}
journal.Send("test message", journal.PriInfo, map[string]string{"TEST": "TestJournalWait " + id})
for ret := -1; ret != SD_JOURNAL_APPEND; {
t1 = time.Now()
ret = j.Wait(time.Millisecond * 300)
t2 = time.Now()
}
duration = t2.Sub(t1)
if duration >= time.Millisecond*300 {
t.Errorf("Wait took longer than 300ms. Actual duration %s", duration.String())
}
}
func TestJournalGetUsage(t *testing.T) {
j, err := NewJournal()
if err != nil {
t.Fatalf("Error opening journal: %s", err)
}
if j == nil {
t.Fatal("Got a nil journal")
}
defer j.Close()
_, err = j.GetUsage()
if err != nil {
t.Fatalf("Error getting journal size: %s", err)
}
}
func TestJournalCursorGetSeekAndTest(t *testing.T) {
j, err := NewJournal()
if err != nil {
t.Fatalf("Error opening journal: %s", err)
}
if j == nil {
t.Fatal("Got a nil journal")
}
defer j.Close()
err = journal.Print(journal.PriInfo, "test message for cursor %s", time.Now())
if err != nil {
t.Fatalf("Error writing to journal: %s", err)
}
if err = waitAndNext(j); err != nil {
t.Fatalf(err.Error())
}
c, err := j.GetCursor()
if err != nil {
t.Fatalf("Error getting cursor from journal: %s", err)
}
err = j.SeekCursor(c)
if err != nil {
t.Fatalf("Error seeking cursor to journal: %s", err)
}
if err = waitAndNext(j); err != nil {
t.Fatalf(err.Error())
}
err = j.TestCursor(c)
if err != nil {
t.Fatalf("Error testing cursor to journal: %s", err)
}
err = journal.Print(journal.PriInfo, "second message %s", time.Now())
if err != nil {
t.Fatalf("Error writing to journal: %s", err)
}
if err = waitAndNext(j); err != nil {
t.Fatalf(err.Error())
}
err = j.TestCursor(c)
if err != ErrNoTestCursor {
t.Fatalf("Error, TestCursor should fail because current cursor has moved from the previous obtained cursor")
}
}
func TestNewJournalFromDir(t *testing.T) {
// test for error handling
dir := "/ClearlyNonExistingPath/"
j, err := NewJournalFromDir(dir)
if err == nil {
defer j.Close()
t.Fatalf("Error expected when opening dummy path (%s)", dir)
}
// test for main code path
dir, err = ioutil.TempDir("", "go-systemd-test")
if err != nil {
t.Fatalf("Error creating tempdir: %s", err)
}
defer os.RemoveAll(dir)
j, err = NewJournalFromDir(dir)
if err != nil {
t.Fatalf("Error opening journal: %s", err)
}
if j == nil {
t.Fatal("Got a nil journal")
}
j.Close()
}
func setupJournalRoundtrip() (*Journal, map[string]string, error) {
j, err := NewJournal()
if err != nil {
return nil, nil, fmt.Errorf("Error opening journal: %s", err)
}
if j == nil {
return nil, nil, fmt.Errorf("Got a nil journal")
}
j.FlushMatches()
matchField := "TESTJOURNALENTRY"
matchValue := fmt.Sprintf("%d", time.Now().UnixNano())
m := Match{Field: matchField, Value: matchValue}
err = j.AddMatch(m.String())
if err != nil {
return nil, nil, fmt.Errorf("Error adding matches to journal: %s", err)
}
msg := fmt.Sprintf("test journal get entry message %s", time.Now())
data := map[string]string{matchField: matchValue}
err = journal.Send(msg, journal.PriInfo, data)
if err != nil {
return nil, nil, fmt.Errorf("Error writing to journal: %s", err)
}
time.Sleep(time.Duration(1) * time.Second)
n, err := j.Next()
if err != nil {
return nil, nil, fmt.Errorf("Error reading from journal: %s", err)
}
if n == 0 {
return nil, nil, fmt.Errorf("Error reading from journal: %s", io.EOF)
}
data["MESSAGE"] = msg
return j, data, nil
}
func TestJournalGetData(t *testing.T) {
j, wantEntry, err := setupJournalRoundtrip()
if err != nil {
t.Fatal(err.Error())
}
defer j.Close()
for k, v := range wantEntry {
data := fmt.Sprintf("%s=%s", k, v)
dataStr, err := j.GetData(k)
if err != nil {
t.Fatalf("GetData() error: %v", err)
}
if dataStr != data {
t.Fatalf("Invalid data for \"%s\": got %s, want %s", k, dataStr, data)
}
dataBytes, err := j.GetDataBytes(k)
if err != nil {
t.Fatalf("GetDataBytes() error: %v", err)
}
if string(dataBytes) != data {
t.Fatalf("Invalid data bytes for \"%s\": got %s, want %s", k, string(dataBytes), data)
}
valStr, err := j.GetDataValue(k)
if err != nil {
t.Fatalf("GetDataValue() error: %v", err)
}
if valStr != v {
t.Fatalf("Invalid data value for \"%s\": got %s, want %s", k, valStr, v)
}
valBytes, err := j.GetDataValueBytes(k)
if err != nil {
t.Fatalf("GetDataValueBytes() error: %v", err)
}
if string(valBytes) != v {
t.Fatalf("Invalid data value bytes for \"%s\": got %s, want %s", k, string(valBytes), v)
}
}
}
func TestJournalGetEntry(t *testing.T) {
j, wantEntry, err := setupJournalRoundtrip()
if err != nil {
t.Fatal(err.Error())
}
defer j.Close()
entry, err := j.GetEntry()
if err != nil {
t.Fatalf("Error getting the entry to journal: %s", err)
}
for k, wantV := range wantEntry {
gotV := entry.Fields[k]
if gotV != wantV {
t.Fatalf("Bad result for entry.Fields[\"%s\"]: got %s, want %s", k, gotV, wantV)
}
}
}
// Check for incorrect read into small buffers,
// see https://github.com/coreos/go-systemd/issues/172
func TestJournalReaderSmallReadBuffer(t *testing.T) {
// Write a long entry ...
delim := "%%%%%%"
longEntry := strings.Repeat("a", 256)
matchField := "TESTJOURNALREADERSMALLBUF"
matchValue := fmt.Sprintf("%d", time.Now().UnixNano())
r, err := NewJournalReader(JournalReaderConfig{
Since: time.Duration(-15) * time.Second,
Matches: []Match{
{
Field: matchField,
Value: matchValue,
},
},
})
if err != nil {
t.Fatalf("Error opening journal: %s", err)
}
if r == nil {
t.Fatal("Got a nil reader")
}
defer r.Close()
want := fmt.Sprintf("%slongentry %s%s", delim, longEntry, delim)
err = journal.Send(want, journal.PriInfo, map[string]string{matchField: matchValue})
if err != nil {
t.Fatal("Error writing to journal", err)
}
time.Sleep(time.Second)
// ... and try to read it back piece by piece via a small buffer
finalBuff := new(bytes.Buffer)
var e error
for c := -1; c != 0 && e == nil; {
smallBuf := make([]byte, 5)
c, e = r.Read(smallBuf)
if c > len(smallBuf) {
t.Fatalf("Got unexpected read length: %d vs %d", c, len(smallBuf))
}
_, _ = finalBuff.Write(smallBuf)
}
b := finalBuff.String()
got := strings.Split(b, delim)
if len(got) != 3 {
t.Fatalf("Got unexpected entry %s", b)
}
if got[1] != strings.Trim(want, delim) {
t.Fatalf("Got unexpected message %s", got[1])
}
}
func TestJournalGetUniqueValues(t *testing.T) {
j, err := NewJournal()
if err != nil {
t.Fatal(err)
}
defer j.Close()
uniqueString := generateRandomField(20)
testEntries := []string{"A", "B", "C", "D"}
for _, v := range testEntries {
err = journal.Send("TEST: "+uniqueString, journal.PriInfo, map[string]string{uniqueString: v})
if err != nil {
t.Fatal(err)
}
}
// TODO: add proper `waitOnMatch` function which should wait for journal entry with filter to commit.
time.Sleep(time.Millisecond * 500)
values, err := j.GetUniqueValues(uniqueString)
if err != nil {
t.Fatal(err)
}
if len(values) != len(testEntries) {
t.Fatalf("Expect %d entries. Got %d", len(testEntries), len(values))
}
if !contains(values, "A") || !contains(values, "B") || !contains(values, "C") || !contains(values, "D") {
t.Fatalf("Expect 4 values for %s field: A,B,C,D. Got %s", uniqueString, values)
}
}
func TestJournalGetCatalog(t *testing.T) {
want := []string{
"Subject: ",
"Defined-By: systemd",
"Support: ",
}
j, err := NewJournal()
if err != nil {
t.Fatalf("Error opening journal: %s", err)
}
if j == nil {
t.Fatal("Got a nil journal")
}
defer j.Close()
if err = j.SeekHead(); err != nil {
t.Fatalf("Seek to head failed: %s", err)
}
matchField := SD_JOURNAL_FIELD_SYSTEMD_UNIT
m := Match{Field: matchField, Value: "systemd-journald.service"}
if err = j.AddMatch(m.String()); err != nil {
t.Fatalf("Error adding matches to journal: %s", err)
}
if err = waitAndNext(j); err != nil {
t.Fatalf(err.Error())
}
catalog, err := j.GetCatalog()
if err != nil {
t.Fatalf("Failed to retrieve catalog entry: %s", err)
}
for _, w := range want {
if !strings.Contains(catalog, w) {
t.Fatalf("Failed to find \"%s\" in \n%s", w, catalog)
}
}
}
func TestJournalGetBootID(t *testing.T) {
j, err := NewJournal()
if err != nil {
t.Fatal(err)
}
defer j.Close()
bootID, err := j.GetBootID()
if err != nil {
t.Fatalf("Failed to get bootID : %s", err)
}
if len(bootID) <= 0 {
t.Fatalf("Get bootID: %s is Null", bootID)
}
fmt.Printf("Test GetBootID: %s", bootID)
}
func contains(s []string, v string) bool {
for _, entry := range s {
if entry == v {
return true
}
}
return false
}
func generateRandomField(n int) string {
letters := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
s := make([]rune, n)
rand.Seed(time.Now().UnixNano())
for i := range s {
s[i] = letters[rand.Intn(len(letters))]
}
return string(s)
}
func waitAndNext(j *Journal) error {
r := j.Wait(time.Duration(1) * time.Second)
if r < 0 {
return errors.New("Error waiting to journal")
}
n, err := j.Next()
if err != nil {
return fmt.Errorf("Error reading to journal: %s", err)
}
if n == 0 {
return fmt.Errorf("Error reading to journal: %s", io.EOF)
}
return nil
}
|