/
config-supervisor_test.go
134 lines (112 loc) · 3.85 KB
/
config-supervisor_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
package main
import (
"flag"
"github.com/adobe-apiplatform/api-gateway-config-supervisor/sync"
"io/ioutil"
"os"
"strings"
"testing"
"time"
)
func TestMain(m *testing.M) {
flag.Parse()
os.Exit(m.Run())
}
func setup(t *testing.T) (tempdir string) {
tmpDir, err := ioutil.TempDir("", "api-gateway-config-supervisor-")
if err != nil {
t.Fatal(err)
}
// setup a syncFolder to watch by the main program
syncFolder = &tmpDir
// setup a sync interval for the test
var sync_interval = time.Duration(time.Second * 1)
syncInterval = &sync_interval
// setup extra debug information
debugOn := true
debug = &debugOn
return tmpDir
}
func createFile(t *testing.T, tmpDir string, file_content string) (f *os.File, err error) {
content := []byte(file_content)
tmpfile, err := ioutil.TempFile(tmpDir, "new-file-")
if err != nil {
t.Fatal(err)
}
if _, err := tmpfile.Write(content); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
return tmpfile, err
}
func TestThatReloadCommandExecutesOnFsChanges(t *testing.T) {
tmpDir := setup(t)
defer os.RemoveAll(tmpDir)
// start the utility in background
go main()
status = sync.GetStatusInstance()
if time.Since(status.LastSync).Seconds() < 2 {
t.Fatal("sync should not happen immediately" + time.Since(status.LastSync).String())
}
if time.Since(status.LastReload).Seconds() > 2 {
t.Fatal("LastReload should be current but was " + time.Since(status.LastReload).String())
}
if time.Since(status.LastFSChangeDetected).Seconds() > 2 {
t.Fatal("LastFSChangeDetected should be current but was " + time.Since(status.LastFSChangeDetected).String())
}
// wait for some time to init
time.Sleep(500 * time.Millisecond)
//modifyFS: create a new directory and file
dir, err := ioutil.TempDir(tmpDir, "new-dir-")
if err != nil {
t.Fatal(err)
}
f1, err := createFile(t, dir, "new-file-content")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f1.Name()) // clean up
// use a reload command to change the content of the new file added
// so that we later test its content, expecting to find this change
reload_cmd := "sed -i.old s/new/reload_cmd/ " + f1.Name()
reloadCmd = &reload_cmd
// wait for some time to track the changes
time.Sleep(1000 * time.Millisecond)
// check that reload cmd has been executed
if time.Since(status.LastSync).Seconds() > 1 {
t.Fatal("sync should have executed earlier than 1.5 but was executed " + time.Since(status.LastSync).String())
}
if time.Since(status.LastReload).Seconds() > 1 {
t.Fatal("reload should have executed earlier than 1.5s but was executed " + time.Since(status.LastReload).String())
}
if time.Since(status.LastFSChangeDetected).Seconds() > 1 {
t.Fatal("FS changes should have been detected earlier than 1.5s but was detected " + time.Since(status.LastFSChangeDetected).String())
}
c, err := ioutil.ReadFile(f1.Name())
if !strings.HasPrefix(string(c), "reload_cmd-file-content") {
t.Fatal("reload cmd did not run correctly. File content was:" + string(c))
}
//reset the reload command
reload_cmd = "echo reload-cmd not defined"
reloadCmd = &reload_cmd
}
func TestThatSyncCommandExecutes(t *testing.T) {
tmpDir := setup(t)
defer os.RemoveAll(tmpDir)
// in order to test that the sync command executed we create a file to later verify that it exists on the disk
sync_cmd_test := "touch " + tmpDir + "/sync_cmd.txt"
syncCmd = &sync_cmd_test
// wait for some time to init
time.Sleep(500 * time.Millisecond)
// main is already started by the previous test
// and we can't start it here again due to: https://github.com/golang/go/issues/4674
//go main()
// wait for some time to init
time.Sleep(500 * time.Millisecond)
// check that the sync_cmd.txt file exists
if _, err := os.Stat(tmpDir + "/sync_cmd.txt"); err != nil {
t.Fatal("Expected to find the file created by the sync command " + tmpDir + "/sync_cmd.txt")
}
}