Skip to content

Commit 093717c

Browse files
committed
sdjournal: add test for NewJournalFromDir()
Simple test to exercise main code path and error handling of `NewJournalFromDir()`.
1 parent 4484981 commit 093717c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

sdjournal/journal_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"errors"
2020
"fmt"
2121
"io"
22+
"io/ioutil"
2223
"os"
2324
"testing"
2425
"time"
@@ -150,3 +151,27 @@ func TestJournalCursorGetSeekAndTest(t *testing.T) {
150151
t.Fatalf("Error testing cursor to journal: %s", err)
151152
}
152153
}
154+
155+
func TestNewJournalFromDir(t *testing.T) {
156+
// test for error handling
157+
dir := "/ClearlyNonExistingPath/"
158+
j, err := NewJournalFromDir(dir)
159+
if err == nil {
160+
defer j.Close()
161+
t.Fatalf("Error expected when opening dummy path (%s)", dir)
162+
}
163+
// test for main code path
164+
dir, err = ioutil.TempDir("", "go-systemd-test")
165+
if err != nil {
166+
t.Fatalf("Error creating tempdir: %s", err)
167+
}
168+
defer os.RemoveAll(dir)
169+
j, err = NewJournalFromDir(dir)
170+
if err != nil {
171+
t.Fatalf("Error opening journal: %s", err)
172+
}
173+
if j == nil {
174+
t.Fatal("Got a nil journal")
175+
}
176+
j.Close()
177+
}

0 commit comments

Comments
 (0)