-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
57 lines (51 loc) · 1.38 KB
/
client_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
package bintest_test
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/buildkite/bintest/v3"
"github.com/buildkite/bintest/v3/testutil"
)
func TestClient(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case `/calls/new`:
w.WriteHeader(http.StatusOK)
case `/calls/1234567/stdout`:
fmt.Fprintln(w, `Success (stdout)!`)
case `/calls/1234567/stderr`:
fmt.Fprintln(w, `Success (stderr)!`)
case `/calls/1234567/exitcode`:
fmt.Fprintln(w, `0`)
case `/debug`:
out, _ := io.ReadAll(r.Body)
_ = r.Body.Close()
t.Logf("%s", out)
default:
t.Logf("No handler for %s", r.URL.Path)
w.WriteHeader(http.StatusNotFound)
}
}))
defer ts.Close()
stdout := &testutil.ClosingBuffer{}
stderr := &testutil.ClosingBuffer{}
c := bintest.Client{
Debug: false,
URL: ts.URL,
PID: 1234567,
Args: []string{"/tmp/llamasbin", "llamas"},
Stdout: stdout,
Stderr: stderr,
}
if exitCode := c.Run(); exitCode != 0 {
t.Fatalf("Expected error code of 0, got %d", exitCode)
}
if expected := "Success (stdout)!\n"; stdout.String() != expected {
t.Fatalf("Expected stdout of %q, got %q", expected, stdout.String())
}
if expected := "Success (stdout)!\n"; stdout.String() != expected {
t.Fatalf("Expected stdout of %q, got %q", expected, stdout.String())
}
}