-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
98 lines (79 loc) · 2.87 KB
/
main_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
package main
import (
"fmt"
"os/exec"
"strings"
"testing"
"time"
)
func TestMatchesPatternInStdOut(t *testing.T) {
test_command := "echo pattern matched"
pattern := "pattern matched"
cmd := exec.Command("./exec_until", "-p", pattern, test_command)
output, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("exec_until command failed: %v, %s", err, output)
}
expected_output := "pattern matched"
if !strings.Contains(string(output), expected_output) {
t.Errorf("Unexpected output: got %s, want %s", string(output), expected_output)
}
}
func TestProcessIsNotTerminatedAfterPatternIsMatchedWhenFlagKFalse(t *testing.T) {
test_command := "sleep 1"
cmd := exec.Command("./exec_until", "-k=false", "-t", "1ms", "-p", "never", test_command)
output, err := cmd.CombinedOutput()
if err == nil {
t.Errorf("expected error, but no error returned. output: %s", output)
}
// verify
verify_cmd := exec.Command("bash", "-c", "ps aux | grep \"[s]leep 1\" | wc -l")
verify_output, _ := verify_cmd.CombinedOutput()
if strings.Trim(string(verify_output), "\n ") == "0" {
t.Errorf("expected `test_command=%s` not to be terminated", test_command)
}
}
func TestProcessIsTerminatedAfterPatternIsMatched(t *testing.T) {
test_command := "sleep 10"
cmd := exec.Command("./exec_until", "-t", "1ms", "-p", "never", test_command)
output, err := cmd.CombinedOutput()
if err == nil {
t.Errorf("expected error, but no error returned. output: %s", output)
}
// verify
verify_cmd := exec.Command("bash", "-c", "ps aux | grep \"[s]leep 10\" | wc -l")
verify_output, _ := verify_cmd.CombinedOutput()
if strings.Trim(string(verify_output), "\n ") != "0" {
t.Errorf("expected `test_command` to be terminated")
}
}
func TestMatchesPatternInStdErr(t *testing.T) {
expected_message := "message to stderr"
cmd := exec.Command("./exec_until", "-p", expected_message, fmt.Sprintf("echo \"%s\" >&2", expected_message))
out, err := cmd.CombinedOutput()
if err != nil {
t.Error("should not throw error", err, string(out))
}
}
func TestErrorTimeout(t *testing.T) {
test_command := "sleep 0.1" // Command will not produce expected pattern within timeout
timeout := 1 * time.Millisecond // Timeout shorter than time needed for pattern matching
cmd := exec.Command("./exec_until", "-p", "whatever", "-t", timeout.String(), test_command)
message, err := cmd.CombinedOutput()
if err == nil {
t.Error("should have exited with an exit code")
}
if !strings.Contains(string(message), ERROR_TIMEOUT) {
t.Error("should have errored due to timeout")
}
}
func TestErrorPatternNotFound(t *testing.T) {
cmd := exec.Command("./exec_until", "-p", "whatever", "echo asd")
message, err := cmd.CombinedOutput()
if err == nil {
t.Error("should have exited with an exit code")
}
if !strings.Contains(string(message), ERROR_PATTERN_NOT_FOUND) {
t.Error("should have exited due to completing without a match")
}
}