-
Notifications
You must be signed in to change notification settings - Fork 251
/
interactive_test.go
186 lines (163 loc) · 4.29 KB
/
interactive_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//go:build darwin || linux || freebsd
package main
import (
"fmt"
"os"
"testing"
"time"
"github.com/gorilla/websocket"
"github.com/mcuadros/go-defaults"
)
func TestInteractiveArtifact(t *testing.T) {
setup(t)
config.EnableInteractive = true
payload := GenericWorkerPayload{
Command: returnExitCode(0),
MaxRunTime: 10,
Features: FeatureFlags{
Interactive: true,
},
}
defaults.SetDefaults(&payload)
td := testTask(t)
taskID := submitAndAssert(t, td, payload, "completed", "completed")
expectedArtifacts := ExpectedArtifacts{
"public/logs/live_backing.log": {
ContentType: "text/plain; charset=utf-8",
ContentEncoding: "gzip",
Expires: td.Expires,
},
"public/logs/live.log": {
Extracts: []string{
"exit 0",
"=== Task Finished ===",
},
ContentType: "text/plain; charset=utf-8",
ContentEncoding: "gzip",
Expires: td.Expires,
},
"private/generic-worker/shell.html": {
ContentType: "text/html; charset=utf-8",
SkipContentCheck: true,
},
}
expectedArtifacts.Validate(t, taskID, 0)
}
func TestInteractiveCommand(t *testing.T) {
setup(t)
config.EnableInteractive = true
payload := GenericWorkerPayload{
Command: sleep(5),
MaxRunTime: 10,
Features: FeatureFlags{
Interactive: true,
},
}
defaults.SetDefaults(&payload)
td := testTask(t)
done := make(chan string, 1)
go func() {
done <- submitAndAssert(t, td, payload, "completed", "completed")
}()
// Wait for server to start
timeout := time.After(10 * time.Second)
tick := time.Tick(500 * time.Millisecond)
var conn *websocket.Conn
var err error
for {
select {
case <-timeout:
// Timeout reached
t.Fatal("timeout waiting for server to start")
case <-tick:
// Try to connect to the server
url := fmt.Sprintf("ws://localhost:%v/shell/%v", config.InteractivePort, os.Getenv("INTERACTIVE_ACCESS_TOKEN"))
conn, _, err = websocket.DefaultDialer.Dial(url, nil)
if err == nil {
err = conn.WriteMessage(websocket.TextMessage, []byte("echo hello\n"))
if err != nil {
t.Fatalf("write error: %v", err)
}
var output []byte
_, output, err = conn.ReadMessage()
if err != nil {
t.Fatalf("read error: %v", err)
}
expected := "hello\n"
if string(output) != expected {
t.Fatalf("unexpected output: %v\nexpected: %v", string(output), expected)
}
err = conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "Closing connection"))
if err != nil {
t.Fatalf("Error sending WebSocket close message: %v", err)
}
_, _, err = conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseNormalClosure) {
t.Fatalf("Unexpected close error: %v", err)
}
}
err = conn.Close()
if err != nil {
t.Fatalf("Error closing WebSocket connection: %v", err)
}
<-done
return
} else {
t.Logf("error connecting to server: %v", err)
}
}
}
}
func TestInteractiveWrongSecret(t *testing.T) {
setup(t)
config.EnableInteractive = true
payload := GenericWorkerPayload{
Command: sleep(5),
MaxRunTime: 10,
Features: FeatureFlags{
Interactive: true,
},
}
defaults.SetDefaults(&payload)
td := testTask(t)
done := make(chan string, 1)
go func() {
done <- submitAndAssert(t, td, payload, "completed", "completed")
}()
// Wait for server to start
timeout := time.After(10 * time.Second)
tick := time.Tick(500 * time.Millisecond)
for {
select {
case <-done:
return
case <-timeout:
// Timeout reached, could not connect to server
// which should be the case since we are using the wrong secret
return
case <-tick:
// Try to connect to the server
url := fmt.Sprintf("ws://localhost:%v/shell/%v", config.InteractivePort, "bad-secret")
_, _, err := websocket.DefaultDialer.Dial(url, nil)
if err == nil {
t.Fatal("expected error connecting to server")
return
}
}
}
}
func TestInteractiveNoConfigSetMalformedPayload(t *testing.T) {
setup(t)
config.EnableInteractive = false
payload := GenericWorkerPayload{
Command: returnExitCode(0),
MaxRunTime: 10,
Features: FeatureFlags{
Interactive: true,
},
}
defaults.SetDefaults(&payload)
td := testTask(t)
_ = submitAndAssert(t, td, payload, "exception", "malformed-payload")
}