-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_test.go
More file actions
106 lines (88 loc) · 2.3 KB
/
exec_test.go
File metadata and controls
106 lines (88 loc) · 2.3 KB
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
package vc
import (
"fmt"
"io"
"net/http"
"os"
"reflect"
"strings"
"testing"
)
var (
sh = "/bin/sh"
)
type dummyHttpClient struct {
err bool
status int
}
func (d dummyHttpClient) Get(url string) (resp *http.Response, err error) {
if d.err {
err = fmt.Errorf("an error")
return
}
resp = &http.Response{
StatusCode: d.status,
Status: "",
Body: io.NopCloser(strings.NewReader("echo 123")),
}
return
}
func TestScript_Run(t *testing.T) {
for _, test := range []struct {
name string
script string
expectError bool
expectLog []string
expectStatus string
}{
{"successful script", "testdata/scripts/success.sh", false, []string{"+ echo hello world", "hello world", ""}, "completed successfully"},
{"failing script", "testdata/scripts/fail.sh", true, []string{"uh-oh", ""}, "task failed"},
} {
t.Run(test.name, func(t *testing.T) {
body, _ := os.ReadFile(test.script)
bodyStr := string(body)
s := Script{
Interpreter: &sh,
Body: &bodyStr,
}
s.Run()
if s.Error == "" && test.expectError {
t.Error("expected error, received none")
} else if s.Error != "" && !test.expectError {
t.Errorf("unexpected error: %+v", s.Error)
}
if !reflect.DeepEqual(test.expectLog, s.Logs) {
t.Errorf("expected %#v, received %#v", test.expectLog, s.Logs)
}
if test.expectStatus != s.RunningState.String() {
t.Errorf("expected %q, received %q", test.expectStatus, s.RunningState.String())
}
})
}
}
func TestScript_Run_Download(t *testing.T) {
url := "https://gist.github.com/jspc/af5adde4977610bbb9eee7efcaf14dee/raw/986f6a3f6be4f07ff0fd00f2c80bc25c3e658b9a/vc-test-script.sh"
for _, test := range []struct {
name string
client httpClient
expectError bool
}{
{"happy path", dummyHttpClient{status: 200}, false},
{"http client errors are returned upwards", dummyHttpClient{err: true}, true},
{"http 404s error out", dummyHttpClient{status: 404}, true},
} {
t.Run(test.name, func(t *testing.T) {
client = test.client
s := Script{
Interpreter: &sh,
URL: &url,
}
err := s.Run()
if err == nil && test.expectError {
t.Error("expected error, received none")
} else if err != nil && !test.expectError {
t.Errorf("unexpected error: %+v", s.Error)
}
})
}
}