forked from subgraph/paxrat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paxrat_test.go
113 lines (108 loc) · 2.71 KB
/
paxrat_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
// AUthor: David McKinney <mckinney@subgraph>
// Copyright (C) 2014-2015 Subgraph
package main
import (
"fmt"
"io/ioutil"
"os"
"testing"
"time"
)
func createTestConfig(path string, contents string) (err error) {
err = ioutil.WriteFile(path, []byte(contents), 0600)
if err != nil {
return
}
return
}
func TestRunWatcher1(t *testing.T) {
dir, err := ioutil.TempDir("", "inotify")
if err != nil {
t.Fatalf("TempDir failed: %s\n", err)
}
defer os.RemoveAll(dir)
files := []string{dir + "/test1", dir + "/test2"}
for _, file := range files {
_, err = os.OpenFile(file, os.O_WRONLY|os.O_CREATE, 000)
if err != nil {
t.Fatalf("creating test file: %s\n", err)
}
}
testJson := fmt.Sprintf(
`{"%s/test1": {`+
`"flags": "mr",`+
`"nonroot": false},`+
`"%s/test2": {`+
`"flags": "E",`+
`"nonroot": false}}`, dir, dir)
configPath := dir + "paxrat_conf.json"
Conf = new(Config)
err = createTestConfig(configPath, testJson)
if err != nil {
t.Fatalf("Could not create test config: %s\n", err)
}
err = Conf.readConfig(configPath)
if err != nil {
t.Fatalf("Could not load config: %s\n", err)
}
watcher, err := initWatcher()
if err != nil {
t.Fatalf("Failed to init watcher: %s\n", err)
}
done := make(chan bool)
go func(done chan bool) {
runWatcher(watcher)
}(done)
err = os.Remove(files[0])
if err != nil {
t.Fatalf("Could not remove testFile1: %s\n", err)
}
err = os.Rename(files[1], dir+"moved")
if err != nil {
t.Fatalf("Could not move/rename TestFile2: %s\n", err)
}
_, err = os.OpenFile(files[0], os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
t.Fatalf("creating test file: %s\n", err)
}
time.Sleep(1 * time.Second)
}
func TestRunWatcher2(t *testing.T) {
dir, err := ioutil.TempDir("", "inotify")
if err != nil {
t.Fatalf("TempDir failed: %s\n", err)
}
defer os.RemoveAll(dir)
testJson := fmt.Sprintf(
`{"%s/1/2/3/4/5/6/7/8/9/10/test1": {`+
`"flags": "mr",`+
`"nonroot": false}}`, dir)
configPath := dir + "paxrat_conf.json"
Conf = new(Config)
err = createTestConfig(configPath, testJson)
if err != nil {
t.Fatalf("Could not create test config: %s\n", err)
}
err = Conf.readConfig(configPath)
if err != nil {
t.Fatalf("Could not load config: %s\n", err)
}
watcher, err := initWatcher()
if err != nil {
fmt.Println(dir)
t.Fatalf("Failed to init watcher: %s\n", err)
}
done := make(chan bool)
go func(done chan bool) {
runWatcher(watcher)
}(done)
time.Sleep(1 * time.Second)
os.MkdirAll(dir+"/1/2/3/4/5/6/7/8/9/10", 0600)
time.Sleep(1 * time.Second)
file := dir + "/1/2/3/4/5/6/7/8/9/10/test1"
fmt.Printf("Creating test file: %s\n", file)
os.OpenFile(file, os.O_WRONLY|os.O_CREATE, 000)
if err != nil {
t.Fatalf("creating test file: %s\n", err)
}
}