-
Notifications
You must be signed in to change notification settings - Fork 6
/
rediseen_test.go
108 lines (81 loc) · 2.63 KB
/
rediseen_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
package main
import (
"log"
"os"
"os/exec"
"strings"
"testing"
)
func Test_savePID(t *testing.T) {
testPidFileLocation := "/xxx/yyy.pid"
err := savePID(100, testPidFileLocation)
compareAndShout(t, "unable to create PID file", strings.Split(err.Error(), ":")[0])
}
func Test_stopDaemon_no_pid_file(t *testing.T) {
err := stopDaemon("/tmp/non-existing")
compareAndShout(t, "no running service found", err.Error())
}
func Test_stopDaemon_invalid_pid(t *testing.T) {
testPidFileLocation := "/tmp/temptesting.pid"
defer os.Remove(testPidFileLocation)
file, err := os.Create(testPidFileLocation)
if err != nil {
log.Printf("unable to create PID file : %v\n", err)
os.Exit(1)
}
defer file.Close()
_, err = file.WriteString("invalid PID")
if err != nil {
log.Printf("unable to create PID file : %v\n", err)
os.Exit(1)
}
file.Sync()
err = stopDaemon(testPidFileLocation)
compareAndShout(t, "invalid PID found in "+testPidFileLocation, err.Error())
}
func Test_stopDaemon_normal(t *testing.T) {
testPidFileLocation := "/tmp/temptesting.pid"
defer os.Remove(testPidFileLocation)
cmd := exec.Command("sleep", "30")
cmd.Start()
savePID(cmd.Process.Pid, testPidFileLocation)
err := stopDaemon(testPidFileLocation)
if err != nil {
t.Error("Expecting nil \ngot\n", err)
}
process, _ := os.FindProcess(cmd.Process.Pid)
status, _ := process.Wait()
compareAndShout(t, "signal: killed", status.String())
}
func Test_Main(t *testing.T) {
// First element "" is a placeholder for executable
//ref: https://stackoverflow.com/a/48674736
originalValue := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", "redis://:@localhost:6400")
defer os.Setenv("REDISEEN_REDIS_URI", originalValue)
// command "rediseen"
os.Args = []string{""}
main()
// commands "rediseen version", "rediseen help", "rediseen stop", "rediseen wrong_command"
for _, command := range []string{"version", "help", "configdoc", "stop", "wrong_command"} {
os.Args = []string{"", command}
main()
}
}
func Test_Main_invalid_config(t *testing.T) {
// First element "" is a placeholder for executable
//ref: https://stackoverflow.com/a/48674736
// command "rediseen start" with invalid configuration
originalValue := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", "invalid_url")
defer os.Setenv("REDISEEN_REDIS_URI", originalValue)
os.Args = []string{"", "start"}
main()
}
func Test_Main_start_command(t *testing.T) {
originalValue := os.Getenv("REDISEEN_REDIS_URI")
os.Setenv("REDISEEN_REDIS_URI", "redis://:@localhost:6400")
defer os.Setenv("REDISEEN_REDIS_URI", originalValue)
os.Args = []string{"", "-d", "start"}
main()
}