-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudshell_test.go
More file actions
197 lines (178 loc) · 5.61 KB
/
Copy pathcloudshell_test.go
File metadata and controls
197 lines (178 loc) · 5.61 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
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
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"context"
"io"
"net/http"
"path"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
)
// staticCreds is a fixed CredentialsProvider for tests (no real AWS calls).
type staticCreds struct{}
func (staticCreds) Retrieve(context.Context) (aws.Credentials, error) {
return aws.Credentials{AccessKeyID: "AKID", SecretAccessKey: "SECRET", SessionToken: "TOKEN", Source: "test"}, nil
}
// roundTripFunc adapts a function to http.RoundTripper.
type roundTripFunc func(*http.Request) (*http.Response, error)
func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
// handler maps an API action and request body to an HTTP status and response body.
type handler func(action string, body []byte) (int, string)
func testClient(h handler) *Client {
c := NewClient("eu-west-1", staticCreds{})
c.pollInterval = time.Millisecond
c.http = &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
body, _ := io.ReadAll(req.Body)
code, resp := h(path.Base(req.URL.Path), body)
return &http.Response{
StatusCode: code,
Body: io.NopCloser(strings.NewReader(resp)),
Header: make(http.Header),
}, nil
})}
return c
}
func TestDescribeEnvironmentsCoercion(t *testing.T) {
cases := map[string]struct {
body string
want int
}{
"wrapped": {`{"Environments":[{"EnvironmentId":"a"},{"EnvironmentId":"b"}]}`, 2},
"array": {`[{"EnvironmentId":"a"}]`, 1},
"single": {`{"EnvironmentId":"solo"}`, 1},
"empty": {`{"Environments":[]}`, 0},
"unknown": {`{"foo":1}`, 0},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
c := testClient(func(action string, _ []byte) (int, string) {
return 200, tc.body
})
envs, err := c.DescribeEnvironments(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(envs) != tc.want {
t.Fatalf("got %d environments, want %d", len(envs), tc.want)
}
})
}
}
func TestDescribeEnvironmentsWithStatus(t *testing.T) {
c := testClient(func(action string, body []byte) (int, string) {
switch action {
case "describeEnvironments":
return 200, `{"Environments":[{"EnvironmentId":"e1"},{"EnvironmentId":"gone"}]}`
case "getEnvironmentStatus":
if strings.Contains(string(body), "e1") {
return 200, `{"EnvironmentId":"e1","Status":"RUNNING"}`
}
return 404, `{}` // lingering / deleted
}
t.Fatalf("unexpected action %q", action)
return 500, ""
})
envs, err := c.DescribeEnvironmentsWithStatus(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
got := map[string]string{}
for _, e := range envs {
got[e.EnvironmentId] = e.Status
}
if got["e1"] != "RUNNING" {
t.Errorf("e1 status = %q, want RUNNING", got["e1"])
}
if got["gone"] != "DELETED" {
t.Errorf("gone status = %q, want DELETED (unresolvable)", got["gone"])
}
}
func TestWaitForRunningAlreadyRunning(t *testing.T) {
calls := 0
c := testClient(func(action string, _ []byte) (int, string) {
calls++
return 200, `{}`
})
if err := c.WaitForRunning(context.Background(), "e1", "RUNNING", time.Second); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if calls != 0 {
t.Fatalf("expected no API calls when already RUNNING, got %d", calls)
}
}
func TestWaitForRunningResumesSuspendedOnce(t *testing.T) {
starts := 0
statuses := []string{"SUSPENDED", "RUNNING"}
i := 0
c := testClient(func(action string, _ []byte) (int, string) {
switch action {
case "startEnvironment":
starts++
return 200, `{}`
case "getEnvironmentStatus":
s := statuses[min(i, len(statuses)-1)]
i++
return 200, `{"EnvironmentId":"e1","Status":"` + s + `"}`
}
return 500, ""
})
if err := c.WaitForRunning(context.Background(), "e1", "SUSPENDED", time.Second); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if starts != 1 {
t.Fatalf("startEnvironment called %d times, want exactly 1", starts)
}
}
func TestWaitForRunningWaitsThroughCreating(t *testing.T) {
starts := 0
statuses := []string{"CREATING", "CREATING", "RUNNING"}
i := 0
c := testClient(func(action string, _ []byte) (int, string) {
if action == "startEnvironment" {
starts++
return 200, `{}`
}
s := statuses[min(i, len(statuses)-1)]
i++
return 200, `{"Status":"` + s + `"}`
})
if err := c.WaitForRunning(context.Background(), "e1", "CREATING", time.Second); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if starts != 0 {
t.Fatalf("startEnvironment should not be called for CREATING, got %d", starts)
}
}
func TestWaitForRunningDeleting(t *testing.T) {
c := testClient(func(action string, _ []byte) (int, string) {
return 200, `{"Status":"DELETING"}`
})
err := c.WaitForRunning(context.Background(), "e1", "DELETING", time.Second)
if err == nil || !strings.Contains(err.Error(), "DELETING") {
t.Fatalf("expected DELETING error, got %v", err)
}
}
func TestWaitForRunningTimeout(t *testing.T) {
c := testClient(func(action string, _ []byte) (int, string) {
return 200, `{"Status":"CREATING"}`
})
err := c.WaitForRunning(context.Background(), "e1", "CREATING", 20*time.Millisecond)
if err == nil || !strings.Contains(err.Error(), "timed out") {
t.Fatalf("expected timeout error, got %v", err)
}
}
func TestCallSurfacesAPIError(t *testing.T) {
c := testClient(func(action string, _ []byte) (int, string) {
return 400, `{"message":"bad vpc"}`
})
_, err := c.CreateEnvironment(context.Background(), nil)
if err == nil {
t.Fatal("expected an error")
}
for _, want := range []string{"createEnvironment", "400", "bad vpc"} {
if !strings.Contains(err.Error(), want) {
t.Errorf("error %q missing %q", err.Error(), want)
}
}
}