-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
scandir_test.go
106 lines (98 loc) · 2.48 KB
/
scandir_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
// +build freebsd netbsd openbsd dragonfly darwin
package immortal
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)
func TestNewScanDir(t *testing.T) {
_, err := NewScanDir("/tmp")
if err != nil {
t.Error(err)
}
}
func TestNewScanDirNonexistent(t *testing.T) {
_, err := NewScanDir("/dev/null/non-existent")
if err == nil {
t.Error("Expecting error")
}
}
// TestWathFile create a dummy file, do changes on it and WathFile should return
// the same file
func TestWathFile(t *testing.T) {
dir, err := ioutil.TempDir("", "TestWathFile")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(dir)
s, err := NewScanDir(dir)
if err != nil {
t.Fatal(err)
}
file := filepath.Join(dir, "run.yml")
if err = ioutil.WriteFile(file, []byte(""), 0644); err != nil {
t.Fatal(err)
}
go s.WatchFile(file)
go func() {
time.Sleep(time.Second)
if err = ioutil.WriteFile(file, []byte("--"), 0644); err != nil {
t.Fatal(err)
}
}()
select {
case watch := <-s.watch:
expect(t, file, watch)
case <-time.After(time.Second * 2):
t.Fatal("time out waiting for file to change")
}
}
type mockController struct{}
func (mc *mockController) GetStatus(socket string) (*Status, error) { return &Status{}, nil }
func (mc *mockController) SendSignal(socket, signal string) (*SignalResponse, error) { return nil, nil }
func (mc *mockController) FindServices(dir string) ([]*ServiceStatus, error) { return nil, nil }
func (mc *mockController) PurgeServices(dir string) error { return nil }
func (mc *mockController) Run(command string) ([]byte, error) {
// TODO
return nil, nil
}
func TestScandir(t *testing.T) {
dir, err := ioutil.TempDir("", "TestWathFile")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(dir)
s, err := NewScanDir(dir)
if err != nil {
t.Fatal(err)
}
dir, err = filepath.EvalSymlinks(dir)
if err != nil {
t.Fatal(err)
}
file := filepath.Join(dir, "run.yml")
if err = ioutil.WriteFile(file, []byte(""), 0644); err != nil {
t.Fatal(err)
}
go func() {
time.Sleep(time.Second)
if err = ioutil.WriteFile(file, []byte("--"), 0644); err != nil {
t.Fatal(err)
}
}()
ctl := &mockController{}
s.Scandir(ctl)
select {
case watch := <-s.watch:
expect(t, file, watch)
case <-time.After(time.Second * 2):
t.Fatal("time out waiting for file to change")
}
val, ok := s.services.Load("run")
if !ok {
t.Fatalf("Not expecting value for %s", "run.yml")
}
expect(t, val, "d41d8cd98f00b204e9800998ecf8427e")
}