-
Notifications
You must be signed in to change notification settings - Fork 0
/
nfsmon_test.go
79 lines (63 loc) · 1.59 KB
/
nfsmon_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 nfsmon_test
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/glinton/nfsmon"
)
var localPath = "/tmp/nfsmon-test"
func TestMain(m *testing.M) {
nfsmon.SetRemountFunc(remountFunc)
os.Create(localPath)
m.Run()
os.RemoveAll(localPath)
}
func TestWatch(t *testing.T) {
mount := nfsmon.Mount{
Server: "192.168.0.1",
ServerPath: "/export/thing",
DestPath: localPath,
}
// test WatchMount
nfsmon.WatchMount(mount)
nfsmon.WatchMount(mount)
// limit test
d := time.Now().Add(time.Second * 6)
ctx, cancel := context.WithDeadline(context.Background(), d)
defer cancel()
// test remount no error
nfsmon.SetRemountFunc(remountFunc)
// start watcher
go nfsmon.Watch(ctx, func(c *nfsmon.WatchCfg) { c.WatchFreq = time.Second })
// test remount with error
time.Sleep(time.Millisecond * 1100)
nfsmon.SetErrConditionFunc(errTrue)
time.Sleep(time.Millisecond * 1100)
nfsmon.SetRemountFunc(remountFuncErr)
// test UnwatchMount
<-ctx.Done()
nfsmon.UnwatchMount(mount)
}
func remountFunc(m nfsmon.Mount) error {
cmd := fmt.Sprintf("%s:%s", m.Server, m.ServerPath)
if m.MountOpts != "" {
cmd += fmt.Sprintf(",%s", m.MountOpts)
}
cmd += fmt.Sprintf(" %s", m.DestPath)
fmt.Println(cmd)
return nil
}
func remountFuncErr(m nfsmon.Mount) error {
return fmt.Errorf("triggerred failure")
}
func errTrue(err error) bool {
return true
}
// This example demonstrates the use of a functional option to set how
// frequently to check for a stale mount.
func ExampleWatch() {
ctx := context.Background()
nfsmon.Watch(ctx, func(c *nfsmon.WatchCfg) { c.WatchFreq = time.Second })
}