Repository navigation
Expand file tree
/
Copy pathbehavior_test.go
More file actions
282 lines (250 loc) · 9.7 KB
/
Copy pathbehavior_test.go
File metadata and controls
282 lines (250 loc) · 9.7 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
Copyright 2026 Hyperbolic Labs, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package hyperbolic_test
import (
"context"
"errors"
"net/http"
"testing"
"time"
hyperbolic "github.com/HyperbolicLabs/hyperbolic-go"
"github.com/HyperbolicLabs/hyperbolic-go/hyperbolictest"
)
// These tests run the client end to end against the in-memory fake, which
// can fail after it has already applied a request: the failure that rents
// GPUs twice if a client gets it wrong.
func h100x1(label string) hyperbolic.CreateRentalParams {
p := hyperbolic.CreateRentalParams{
RentalType: hyperbolic.RentalTypeVirtualMachine,
GPUType: hyperbolic.Ptr("h100"),
GPUCount: 1,
Region: hyperbolic.Ptr("us-central-1"),
}
if label != "" {
p.Label = hyperbolic.Ptr(label)
}
return p
}
// findByLabel lists every active rental carrying label.
func findByLabel(ctx context.Context, t *testing.T, c *hyperbolic.Client, label string) []hyperbolic.RentalWithStatus {
t.Helper()
var out []hyperbolic.RentalWithStatus
for r, err := range c.Rentals.ListActiveAll(ctx, hyperbolic.ListActiveRentalsParams{}) {
if err != nil {
t.Fatalf("list: %v", err)
}
if l, err := r.Label.Get(); err == nil && l == label {
out = append(out, r)
}
}
return out
}
func TestRentalLifecycle(t *testing.T) {
srv := hyperbolictest.NewServer(t)
c := srv.Client(t)
ctx := context.Background()
created, err := c.Rentals.Create(ctx, h100x1("train-run-42"))
if err != nil {
t.Fatalf("create: %v", err)
}
if created.RentalID == "" {
t.Fatalf("create returned no id: %+v", created)
}
active := func() hyperbolic.RentalWithStatus {
t.Helper()
got, err := c.Rentals.Get(ctx, hyperbolic.GetRentalParams{RentalID: created.RentalID})
if err != nil {
t.Fatalf("get: %v", err)
}
a, err := got.AsActiveRentalLookup()
if err != nil || a.State != hyperbolic.ActiveRentalLookupStateActive {
t.Fatalf("get: state %q, err %v; want active", a.State, err)
}
return a.Rental
}
if r := active(); r.Status != hyperbolic.RentalStatusPending {
t.Fatalf("status = %q, want Pending", r.Status)
}
srv.SetRentalStatus(created.RentalID, hyperbolic.RentalStatusRunning)
r := active()
if len(r.SSHEndpoints) != 1 || r.SSHEndpoints[0].Host == "" {
t.Fatalf("ssh endpoints = %+v, want one", r.SSHEndpoints)
}
term, err := r.CurrentTerm.AsOnDemandTerm()
if err != nil || term.CostPerHour != 275 {
t.Fatalf("term = %+v, err %v; want on-demand at 275 cents/hr", term, err)
}
if _, err := c.Rentals.Terminate(ctx, hyperbolic.TerminateRentalParams{RentalID: created.RentalID}); err != nil {
t.Fatalf("terminate: %v", err)
}
got, err := c.Rentals.Get(ctx, hyperbolic.GetRentalParams{RentalID: created.RentalID})
if err != nil {
t.Fatalf("get after terminate: %v", err)
}
if state, err := got.Discriminator(); err != nil || state != "terminated" {
t.Fatalf("get after terminate: state %q, err %v; want terminated", state, err)
}
}
func TestGetUnknownRentalIsNotFound(t *testing.T) {
srv := hyperbolictest.NewServer(t)
c := srv.Client(t)
_, err := c.Rentals.Get(context.Background(), hyperbolic.GetRentalParams{RentalID: "rnt_unknown"})
if !errors.Is(err, hyperbolic.ErrNotFound) || !hyperbolic.IsNotFound(err) {
t.Fatalf("err = %v, want ErrNotFound", err)
}
}
// Terminate reports what the API says. Ending a rental twice is a definite
// "not found", which callers that want idempotence treat as done.
func TestTerminatingAGoneRentalIsNotFound(t *testing.T) {
srv := hyperbolictest.NewServer(t)
c := srv.Client(t)
ctx := context.Background()
created, err := c.Rentals.Create(ctx, h100x1(""))
if err != nil {
t.Fatal(err)
}
if _, err := c.Rentals.Terminate(ctx, hyperbolic.TerminateRentalParams{RentalID: created.RentalID}); err != nil {
t.Fatalf("terminate: %v", err)
}
for _, id := range []string{created.RentalID, "rnt_unknown"} {
_, err := c.Rentals.Terminate(ctx, hyperbolic.TerminateRentalParams{RentalID: id})
if !hyperbolic.IsNotFound(err) || hyperbolic.IsOutcomeUnknown(err) {
t.Fatalf("terminate %s: err = %v, want a definite not found", id, err)
}
}
}
// The core safety property: a create that fails after the server applied it
// must not be retried, and must be reported as outcome-unknown so the caller
// looks the rental up instead of renting twice.
func TestCreateFailingAfterCommitIsOutcomeUnknownAndNotRetried(t *testing.T) {
for _, status := range []int{http.StatusInternalServerError, http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout} {
t.Run(http.StatusText(status), func(t *testing.T) {
srv := hyperbolictest.NewServer(t)
c := srv.Client(t)
srv.InjectFault(hyperbolictest.Fault{Method: http.MethodPost, Path: hyperbolictest.PathRentals, Status: status, AfterCommit: true})
_, err := c.Rentals.Create(context.Background(), h100x1("k8s-abc123"))
if !errors.Is(err, hyperbolic.ErrOutcomeUnknown) {
t.Fatalf("err = %v, want ErrOutcomeUnknown", err)
}
if hyperbolic.StatusCode(err) != status {
t.Fatalf("wrapped status = %d, want %d", hyperbolic.StatusCode(err), status)
}
if n := srv.Requests(http.MethodPost, hyperbolictest.PathRentals); n != 1 {
t.Fatalf("create sent %d times, want exactly 1", n)
}
if found := findByLabel(context.Background(), t, c, "k8s-abc123"); len(found) != 1 {
t.Fatalf("lookup by label: %d rentals; want exactly the one that was created", len(found))
}
})
}
}
func TestCreateTimeoutIsOutcomeUnknown(t *testing.T) {
srv := hyperbolictest.NewServer(t)
c := srv.Client(t, hyperbolic.WithHTTPClient(&http.Client{Timeout: 50 * time.Millisecond}))
srv.InjectFault(hyperbolictest.Fault{Method: http.MethodPost, Path: hyperbolictest.PathRentals, Delay: 500 * time.Millisecond, AfterCommit: true})
_, err := c.Rentals.Create(context.Background(), h100x1("slow"))
if !hyperbolic.IsOutcomeUnknown(err) {
t.Fatalf("err = %v, want outcome unknown", err)
}
if n := srv.Requests(http.MethodPost, hyperbolictest.PathRentals); n != 1 {
t.Fatalf("create sent %d times, want 1", n)
}
if len(srv.ActiveRentals()) != 1 {
t.Fatalf("the server did not create the rental, so the test setup is wrong")
}
}
func TestCreateRetriedOnRateLimit(t *testing.T) {
srv := hyperbolictest.NewServer(t)
c := srv.Client(t)
srv.InjectFault(hyperbolictest.Fault{Method: http.MethodPost, Path: hyperbolictest.PathRentals, Status: http.StatusTooManyRequests})
if _, err := c.Rentals.Create(context.Background(), h100x1("")); err != nil {
t.Fatalf("create: %v", err)
}
if n := srv.Requests(http.MethodPost, hyperbolictest.PathRentals); n != 2 {
t.Fatalf("create sent %d times, want 2 (one 429, one success)", n)
}
if n := len(srv.ActiveRentals()); n != 1 {
t.Fatalf("%d rentals exist, want 1", n)
}
}
func TestCreateClientErrorsAreDefinite(t *testing.T) {
srv := hyperbolictest.NewServer(t)
c := srv.Client(t)
srv.SetBalance(100, 0) // $1, less than an hour of anything
_, err := c.Rentals.Create(context.Background(), h100x1(""))
if !hyperbolic.HasCode(err, hyperbolic.CodeInsufficientBalance) {
t.Fatalf("err = %v, want INSUFFICIENT_BALANCE", err)
}
if hyperbolic.IsOutcomeUnknown(err) {
t.Fatal("a 4xx is a definite failure, not outcome unknown")
}
if n := srv.Requests(http.MethodPost, hyperbolictest.PathRentals); n != 1 {
t.Fatalf("create sent %d times, want 1 (4xx must not be retried)", n)
}
}
func TestReadsRetryTransientFailures(t *testing.T) {
srv := hyperbolictest.NewServer(t)
c := srv.Client(t)
srv.InjectFault(hyperbolictest.Fault{Method: http.MethodGet, Path: hyperbolictest.PathActiveRentals, Status: http.StatusServiceUnavailable, Times: 2})
if _, err := c.Rentals.ListActive(context.Background(), hyperbolic.ListActiveRentalsParams{}); err != nil {
t.Fatalf("list: %v", err)
}
if n := srv.Requests(http.MethodGet, hyperbolictest.PathActiveRentals); n != 3 {
t.Fatalf("list sent %d times, want 3", n)
}
}
func TestExternalTerminationIsVisible(t *testing.T) {
srv := hyperbolictest.NewServer(t)
c := srv.Client(t)
ctx := context.Background()
created, err := c.Rentals.Create(ctx, h100x1("ext"))
if err != nil {
t.Fatal(err)
}
srv.TerminateExternally(created.RentalID)
if found := findByLabel(ctx, t, c, "ext"); len(found) != 0 {
t.Fatalf("found %d, but a rental the platform ended is no longer listed", len(found))
}
}
func TestUnauthorizedAgainstFake(t *testing.T) {
srv := hyperbolictest.NewServer(t)
c, err := hyperbolic.NewClient("sk_wrong", hyperbolic.WithBaseURL(srv.URL))
if err != nil {
t.Fatal(err)
}
if _, err := c.Balances.Get(context.Background()); !hyperbolic.IsUnauthorized(err) {
t.Fatalf("err = %v, want unauthorized", err)
}
if n := srv.Requests(http.MethodGet, hyperbolictest.PathBalance); n != 1 {
t.Fatalf("sent %d times, want 1", n)
}
}
func TestRentalOptionsNeedNoKey(t *testing.T) {
srv := hyperbolictest.NewServer(t)
c, err := hyperbolic.NewClient("", hyperbolic.WithBaseURL(srv.URL))
if err != nil {
t.Fatal(err)
}
opts, err := c.Rentals.Options(context.Background())
if err != nil || len(opts) == 0 {
t.Fatalf("options = %d, err = %v", len(opts), err)
}
}
func TestUnmodeledOperationsFailLoudly(t *testing.T) {
srv := hyperbolictest.NewServer(t)
c := srv.Client(t)
_, err := c.StorageVolumes.List(context.Background())
if hyperbolic.StatusCode(err) != http.StatusNotImplemented {
t.Fatalf("err = %v, want 501 from the fake", err)
}
}