-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathrunner_test.go
136 lines (123 loc) · 4.08 KB
/
runner_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
package main
import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
)
// TestRealRunnerSignalForwarding will artificially put an interrupt signal (SIGINT) in the rr.signals chan.
// The chan will not be reinitialized in the runner considering we have already initialized it here.
// Once the sleep process starts, if the signal is successfully received by the parent process, it
// will interrupt and stop the sleep command.
func TestRealRunnerSignalForwarding(t *testing.T) {
rr := realRunner{}
rr.signals = make(chan os.Signal, 1)
rr.signal(syscall.SIGINT)
if err := rr.Run(context.Background(), "sleep", "3600"); err.Error() == "signal: interrupt" {
t.Logf("SIGINT forwarded to Entrypoint")
} else {
t.Fatalf("Unexpected error received: %v", err)
}
}
func TestRealRunnerStdoutAndStderrPaths(t *testing.T) {
tmp, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer os.RemoveAll(tmp)
expectedString := "hello world"
rr := realRunner{
stdoutPath: filepath.Join(tmp, "stdout"),
stderrPath: filepath.Join(tmp, "subpath/stderr"),
}
if err := rr.Run(context.Background(), "sh", "-c", fmt.Sprintf("echo %s && echo %s >&2", expectedString, expectedString)); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
for _, path := range []string{"stdout", "subpath/stderr"} {
if got, err := ioutil.ReadFile(filepath.Join(tmp, path)); err != nil {
t.Fatalf("Unexpected error: %v", err)
} else if gotString := strings.TrimSpace(string(got)); gotString != expectedString {
t.Errorf("%v: got: %v, wanted: %v", path, gotString, expectedString)
}
}
}
func TestRealRunnerStdoutAndStderrSamePath(t *testing.T) {
tmp, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer os.RemoveAll(tmp)
path := filepath.Join(tmp, "logs")
expectedString := "hello world"
rr := realRunner{
stdoutPath: path,
stderrPath: path,
}
if err := rr.Run(context.Background(), "sh", "-c", fmt.Sprintf("echo %s && echo %s >&2", expectedString, expectedString)); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Since writes to stdout and stderr might be racy, we only check for lengths here.
expectedSize := (len(expectedString) + 1) * 2
if got, err := ioutil.ReadFile(path); err != nil {
t.Fatalf("Unexpected error: %v", err)
} else if gotSize := len(got); gotSize != expectedSize {
t.Errorf("got: %v, wanted: %v", gotSize, expectedSize)
}
}
func TestRealRunnerStdoutPathWithSignal(t *testing.T) {
tmp, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer os.RemoveAll(tmp)
path := filepath.Join(tmp, "stdout")
rr := realRunner{
signals: make(chan os.Signal, 1),
stdoutPath: path,
}
expectedString := "hello world"
expectedError := "signal: interrupt"
go func() {
timer := time.Tick(100 * time.Millisecond)
for {
if stat, err := os.Stat(path); err != nil {
if !errors.Is(err, os.ErrNotExist) {
t.Errorf("Unexpected error: %v", err)
return
}
} else if int(stat.Size()) > len(expectedString) {
break
}
<-timer
}
rr.signal(syscall.SIGINT)
}()
if err := rr.Run(context.Background(), "sh", "-c", fmt.Sprintf("echo %s && sleep 20", expectedString)); err == nil || err.Error() != expectedError {
t.Fatalf("Expected error %v but got %v", expectedError, err)
}
if got, err := ioutil.ReadFile(path); err != nil {
t.Fatalf("Unexpected error: %v", err)
} else if gotString := strings.TrimSpace(string(got)); gotString != expectedString {
t.Errorf("got: %v, wanted: %v", gotString, expectedString)
}
}
// TestRealRunnerTimeout tests whether cmd is killed after a millisecond even though it's supposed to sleep for 10 milliseconds.
func TestRealRunnerTimeout(t *testing.T) {
rr := realRunner{}
timeout := time.Millisecond
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if err := rr.Run(ctx, "sleep", "0.01"); err != nil {
if err != context.DeadlineExceeded {
t.Fatalf("unexpected error received: %v", err)
}
} else {
t.Fatalf("step didn't timeout")
}
}