-
Notifications
You must be signed in to change notification settings - Fork 0
/
fate_test.go
137 lines (113 loc) · 2.65 KB
/
fate_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"errors"
"github.com/fsnotify/fsnotify"
"os"
"strings"
"testing"
"time"
)
type MockVaultClient struct {
count int
}
func (m *MockVaultClient) Write(path string, data interface{}) error {
m.count++
return nil
}
type MockFileInfo struct {
isDir bool
name string
}
func (m *MockFileInfo) Name() string {
return m.name
}
func (m *MockFileInfo) Size() int64 {
panic("not implemented")
}
func (m *MockFileInfo) Mode() os.FileMode {
panic("not implemented")
}
func (m *MockFileInfo) ModTime() time.Time {
panic("not implemented")
}
func (m *MockFileInfo) IsDir() bool {
return m.isDir
}
func (m *MockFileInfo) Sys() interface{} {
panic("not implemented")
}
type FakeWatcher struct {
event chan fsnotify.Event
error chan error
}
func (w *FakeWatcher) Close() error {
close(w.event)
return nil
}
func (w *FakeWatcher) Add(name string) error { return nil }
func (w *FakeWatcher) Events() chan fsnotify.Event { return w.event }
func (w *FakeWatcher) Errors() chan error { return w.error }
var sampleYml = `
good:
boy:
- test
- JavaScript:
front-end:
react: 1000
redux: 69.22
back-end:
node: .12
next: true
Java: false
Java.fake:
`
func TestScanAndWrite(t *testing.T) {
client, _ := mockStuff()
scanDir("fake-dir")
if client.count != 1 {
t.Fatalf("Expected one hit to vault, but got %d", client.count)
}
}
func mockStuff() (*MockVaultClient, <-chan os.Signal) {
readDir = func(dirname string) ([]os.FileInfo, error) {
return []os.FileInfo{
&MockFileInfo{true, "dir1"},
&MockFileInfo{false, "empty-file"},
&MockFileInfo{false, "good.yml"},
}, nil
}
readFile = func(filename string) ([]byte, error) {
if strings.HasSuffix(filename, "good.yml") {
return []byte(sampleYml), nil
}
return nil, errors.New("failed")
}
var exitChn = make(chan os.Signal)
signalNotify = func(c chan<- os.Signal, sig ...os.Signal) {
go func() {
time.Sleep(time.Second * 1)
c <- sig[0]
exitChn <- sig[0]
}()
}
client := new(MockVaultClient)
fate.client = client
return client, exitChn
}
func TestWatchDir(t *testing.T) {
client, exited := mockStuff()
delayReadDuration = 0
watcher = &FakeWatcher{
event: make(chan fsnotify.Event),
}
go watchDir("", watcher)
events := watcher.Events()
events <- fsnotify.Event{Name: "good.yml", Op: fsnotify.Create}
events <- fsnotify.Event{Name: "good.yml", Op: fsnotify.Write}
events <- fsnotify.Event{Name: "good.yml", Op: fsnotify.Chmod}
events <- fsnotify.Event{Name: "good.yml", Op: fsnotify.Remove}
<-exited
if client.count != 2 {
t.Fatalf("Expected 2 hits to vault, but got %d", client.count)
}
}