-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_test.go
149 lines (128 loc) · 3.27 KB
/
command_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
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"syscall"
"time"
"testing"
)
func TestNewCommand(t *testing.T) {
dummyCh := make(chan error)
defer close(dummyCh)
cases := []struct {
cmdString string
success bool
}{
{
cmdString: "",
success: false,
},
{
cmdString: "tail -f production.log",
success: true,
},
{
cmdString: "make",
success: true,
},
}
for _, c := range cases {
_, err := NewCommand(c.cmdString, dummyCh, dummyCh)
if (err == nil) != c.success {
t.Fatalf("unexpected result with error: %s", err.Error())
}
}
}
func TestHandleSignal(t *testing.T) {
timeout := 2 * time.Second
signalCh := make(chan os.Signal)
defer close(signalCh)
readyCh := make(chan error)
exitCh := make(chan error)
defer close(readyCh)
defer close(exitCh)
command, _ := NewCommand(fmt.Sprintf("sleep %d", int(timeout.Seconds())+1), readyCh, exitCh)
go command.Execute()
if err := <-readyCh; err != nil {
t.Fatalf("failed to execute command: %s", err.Error())
}
go command.HandleSignal(signalCh)
signalCh <- syscall.SIGTERM
select {
case <-time.After(timeout): // Fail
case <-exitCh: // Success
return
}
t.Fatalf("cannot proxy signal to command within %s", timeout.String())
}
func TestExecute(t *testing.T) {
cases := []struct {
cmdStr string
ready bool
exit bool
}{
{
cmdStr: "sleep 1",
ready: true,
exit: true,
},
{
cmdStr: "dummy_dummy_command",
ready: false,
exit: false, // Both are available true or false
},
{
cmdStr: "dummy_dummy_command",
ready: false,
exit: true, // Both are available true or false
},
{
cmdStr: "ln", // It will be exit with statu 1.
ready: true,
exit: false,
},
}
for _, c := range cases {
var err error
timeout := 2 * time.Second
signalCh := make(chan os.Signal)
readyCh := make(chan error)
exitCh := make(chan error)
command, _ := NewCommand(c.cmdStr, readyCh, exitCh)
// Discard output of command for testing
command.ExecCmd.Stdout = ioutil.Discard
command.ExecCmd.Stderr = ioutil.Discard
go command.Execute()
if err = <-readyCh; (err == nil) != c.ready {
if c.ready {
t.Fatalf("unexpected ready state\nactual: %s\nexpected: %s\ncommand: %s\nerror: %s", strconv.FormatBool(!c.ready), strconv.FormatBool(c.ready), c.cmdStr, err.Error())
} else {
t.Fatalf("unexpected ready state\nactual: %s\nexpected: %s\ncommand: %s", strconv.FormatBool(!c.ready), strconv.FormatBool(c.ready), c.cmdStr)
}
}
// If failed to start command, we cannot process continuation.
if err != nil {
continue
}
go command.HandleSignal(signalCh)
time.Sleep(100 * time.Millisecond) // Warm up command to get exitcode=1
signalCh <- syscall.SIGINT
select {
case <-time.After(timeout):
t.Fatalf("unexpected timeout to exit command: %s", c.cmdStr)
case err = <-exitCh:
if (err == nil) != c.exit {
if c.exit {
t.Fatalf("unexpected exit state\nactual: %s\nexpected: %s\ncommand: %s\nerror: %s", strconv.FormatBool(!c.exit), strconv.FormatBool(c.exit), c.cmdStr, err.Error())
} else {
t.Fatalf("unexpected exit state\nactual: %s\nexpected: %s\ncommand: %s", strconv.FormatBool(!c.exit), strconv.FormatBool(c.exit), c.cmdStr)
}
}
}
close(signalCh)
close(readyCh)
close(exitCh)
}
}