-
Notifications
You must be signed in to change notification settings - Fork 59
/
agents_test.go
133 lines (104 loc) · 3.3 KB
/
agents_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
package buildkite
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestAgentsService_List(t *testing.T) {
t.Parallel()
server, client, teardown := newMockServerAndClient(t)
t.Cleanup(teardown)
server.HandleFunc("/v2/organizations/my-great-org/agents", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":"123"},{"id":"1234"}]`)
})
agents, _, err := client.Agents.List(context.Background(), "my-great-org", nil)
if err != nil {
t.Errorf("Agents.List returned error: %v", err)
}
want := []Agent{{ID: "123"}, {ID: "1234"}}
if diff := cmp.Diff(agents, want); diff != "" {
t.Errorf("Agents.List: diff: (-got +want)\n%s", diff)
}
}
func TestAgentsService_Get(t *testing.T) {
t.Parallel()
server, client, teardown := newMockServerAndClient(t)
t.Cleanup(teardown)
server.HandleFunc("/v2/organizations/my-great-org/agents/123", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":"123"}`)
})
agent, _, err := client.Agents.Get(context.Background(), "my-great-org", "123")
if err != nil {
t.Errorf("Agents.Get returned error: %v", err)
}
want := Agent{ID: "123"}
if diff := cmp.Diff(agent, want); diff != "" {
t.Errorf("Agents.List(): diff: (-got +want)\n%s", diff)
}
}
func TestAgentsService_Create(t *testing.T) {
t.Parallel()
server, client, teardown := newMockServerAndClient(t)
t.Cleanup(teardown)
input := Agent{Name: "new_agent_bob"}
server.HandleFunc("/v2/organizations/my-great-org/agents", func(w http.ResponseWriter, r *http.Request) {
var v Agent
err := json.NewDecoder(r.Body).Decode(&v)
if err != nil {
t.Fatalf("Error parsing json body: %v", err)
}
testMethod(t, r, "POST")
if diff := cmp.Diff(v, input); diff != "" {
t.Errorf("Request body diff: (-got +want)\n%s", diff)
}
fmt.Fprint(w, `{"id":"123"}`)
})
agent, _, err := client.Agents.Create(context.Background(), "my-great-org", input)
if err != nil {
t.Errorf("Agents.Create returned error: %v", err)
}
want := Agent{ID: "123"}
if diff := cmp.Diff(agent, want); diff != "" {
t.Errorf("Agents.Create() diff: (-got +want)\n%s", diff)
}
}
func TestAgentsService_Delete(t *testing.T) {
t.Parallel()
server, client, teardown := newMockServerAndClient(t)
t.Cleanup(teardown)
server.HandleFunc("/v2/organizations/my-great-org/agents/123", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
})
_, err := client.Agents.Delete(context.Background(), "my-great-org", "123")
if err != nil {
t.Errorf("Agents.Delete returned error: %v", err)
}
}
func TestAgentsService_Stop(t *testing.T) {
t.Parallel()
server, client, teardown := newMockServerAndClient(t)
t.Cleanup(teardown)
server.HandleFunc("/v2/organizations/my-great-org/agents/123/stop", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
body, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("could not read request body: %v", err)
}
s := strings.TrimSpace(string(body))
expected := `{"force":true}`
if s != expected {
t.Errorf("Request body: %s, want %s", s, expected)
}
})
_, err := client.Agents.Stop(context.Background(), "my-great-org", "123", true)
if err != nil {
t.Errorf("Agents.Stop returned error: %v", err)
}
}