-
Notifications
You must be signed in to change notification settings - Fork 7
/
rfw_test.go
79 lines (65 loc) · 1.7 KB
/
rfw_test.go
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
package rfw
import (
"bytes"
"io/ioutil"
"os"
"path"
"runtime"
"testing"
)
func TestWriter(t *testing.T) {
tmpdir := makeTempDir(t)
defer rmTempDir(tmpdir)
l, err := Open(path.Join(tmpdir, "rfw"), 0644)
chkerr(t, err)
// Basic writer tests
_, err = l.Write([]byte("Hello there!\n"))
chkerr(t, err)
assertFileEquals(t, l, []byte("Hello there!\n"))
_, err = l.Write([]byte("Goodbye.\n"))
chkerr(t, err)
assertFileEquals(t, l, []byte("Hello there!\nGoodbye.\n"))
// Move the file
err = os.Rename(l.path, l.path+".1")
chkerr(t, err)
_, err = l.Write([]byte("New content\n"))
chkerr(t, err)
assertFileEquals(t, l, []byte("New content\n"))
// Delete the file
err = os.Remove(l.path)
chkerr(t, err)
_, err = l.Write([]byte("More new content\n"))
chkerr(t, err)
assertFileEquals(t, l, []byte("More new content\n"))
// Close & Re-open does not destroy the file
err = l.Close()
chkerr(t, err)
l, err = Open(l.path, l.mode)
chkerr(t, err)
_, err = l.Write([]byte("foo\n"))
chkerr(t, err)
assertFileEquals(t, l, []byte("More new content\nfoo\n"))
}
/* Helpers */
func chkerr(t *testing.T, err error) {
if err != nil {
_, _, line, _ := runtime.Caller(1)
t.Errorf("Error encountered at line %d: %s", line, err)
}
}
func makeTempDir(t *testing.T) string {
tmpdir, err := ioutil.TempDir("", "logstash-config-test")
chkerr(t, err)
return tmpdir
}
func rmTempDir(tmpdir string) {
_ = os.RemoveAll(tmpdir)
}
func assertFileEquals(t *testing.T, l *Writer, expected []byte) {
contents, err := ioutil.ReadFile(l.path)
chkerr(t, err)
if !bytes.Equal(contents, expected) {
_, _, line, _ := runtime.Caller(1)
t.Fatalf("line %d: Got %v, expected %v from rfw file.", line, contents, expected)
}
}