-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ramdisk_test.go
117 lines (102 loc) · 3.17 KB
/
ramdisk_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
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
// The only tests that really make sense are integration tests. Typically I
// would hide these behind a `+integration` build flag, so that you have to
// specify you want them for go test, but since they are the only ones might as
// well keep things simple.
package ramdisk_test
import (
"bytes"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/mroth/ramdisk"
)
/* basic test sequence I'm thinking about
- try to create, no errors
- use go FS to see if path exists
- if possible, stat the directory and try to read capacity?
- use go FS to write a file to ramdisk
- use go FS to read a file back from ramdisk, verify contents
- unmount ramdisk, no errors
- go FS to see if file exists (should not)
- go FS to verify mountpath no longer exists (if later implemented)
*/
func TestCreate(t *testing.T) {
// try to create, verify no errors
rd, err := ramdisk.Create(ramdisk.Options{
Logger: testLogger(t),
})
ifErrLogFatalDetailed(err, t)
// mark for cleanup after tests complete
defer ramdisk.Destroy(rd.DevicePath)
// use go FS to see if mountpath exists and is directory
t.Log("verifying mountpath", rd.MountPath)
stat, err := os.Stat(rd.MountPath)
if err != nil {
t.Fatal(err)
}
if !stat.IsDir() {
t.Fatalf("%v is not a directory", rd.MountPath)
}
// use go FS to write a file to ramdisk
target := filepath.Join(rd.MountPath, "msg.txt")
payload := []byte("hi mom")
t.Log("writing file to", target)
err = ioutil.WriteFile(target, payload, 0644)
if err != nil {
t.Fatal("failed to write file to", target)
}
// use go FS to read a file back from ramdisk, verify contents
t.Logf("reading back file %s to verify contents", target)
data, err := ioutil.ReadFile(target)
if err != nil {
t.Fatal(err)
}
if string(data) != string(payload) {
t.Errorf("read data did not match, want [%s] got [%s]", payload, data)
}
// unmount ramdisk, no errors
t.Log("unmounting", rd.DevicePath)
err = ramdisk.Destroy(rd.MountPath)
ifErrLogFatalDetailed(err, t)
// go FS to see if file exists (should not)
t.Logf("checking for existence of %v (should not exist)", target)
if _, err := os.Stat(target); os.IsExist(err) {
t.Fatalf("%v exists when it should not!", target)
}
}
func TestDestroy(t *testing.T) {
rd, err := ramdisk.Create(ramdisk.Options{})
if err != nil {
t.Skip("precondition creating ramdisk failed!")
}
err = ramdisk.Destroy(rd.DevicePath)
ifErrLogFatalDetailed(err, t)
}
// helper function to wrap some boilerplate for returns from Cmd.Exec,
// if there is an error log it fatally, but checking if its an ExitError
// and using the more detailed Stderr value if so.
func ifErrLogFatalDetailed(err error, t *testing.T) {
if err != nil {
exiterr, ok := err.(*exec.ExitError)
if ok {
t.Fatalf("ExitError: %s\n", exiterr.Stderr)
} else {
t.Fatal(err)
}
}
}
// boilerplate trick to be able to wrap t.Log() as a Logger
// via: https://github.com/golang/go/issues/22513
func testLogger(t *testing.T) *log.Logger {
return log.New(testWriter{t}, "testProcessLog: ", log.LstdFlags)
}
type testWriter struct {
t *testing.T
}
func (tw testWriter) Write(p []byte) (n int, err error) {
tw.t.Log(string(bytes.TrimSpace(p)))
return len(p), nil
}