@@ -2,14 +2,19 @@ package v1
22
33import (
44 "context"
5+ "errors"
6+ "io"
7+ "net/http"
58 "os"
9+ "strings"
610 "testing"
711 "time"
812
9- "github.com/brevdev/cloud/internal/ssh"
1013 "github.com/brevdev/cloud/internal/validation"
11- v1 "github.com/brevdev/cloud/v1"
1214 openapi "github.com/brevdev/cloud/v1/providers/shadeform/gen/shadeform"
15+
16+ "github.com/brevdev/cloud/internal/ssh"
17+ v1 "github.com/brevdev/cloud/v1"
1318 "github.com/google/uuid"
1419 "github.com/stretchr/testify/require"
1520)
@@ -118,6 +123,90 @@ func TestInstanceTypeFilter(t *testing.T) {
118123 })
119124}
120125
126+ func TestHandleShadeformAPIErrorResponse (t * testing.T ) {
127+ tests := []struct {
128+ name string
129+ httpResp * http.Response
130+ expectedError error
131+ }{
132+ {
133+ name : "OutOfStockError" ,
134+ httpResp : & http.Response {StatusCode : http .StatusConflict , Body : io .NopCloser (strings .NewReader (`{"error_code": "OUT_OF_STOCK", "error": "Out of stock"}` ))},
135+ expectedError : v1 .ErrInsufficientResources ,
136+ },
137+ {
138+ name : "OtherShadeformError" ,
139+ httpResp : & http.Response {StatusCode : http .StatusConflict , Body : io .NopCloser (strings .NewReader (`{"error_code": "INTERNAL_SERVER_ERROR", "error": "Internal server error"}` ))},
140+ expectedError : errors .New (`shadeform API error: error code: INTERNAL_SERVER_ERROR, error: Internal server error` ),
141+ },
142+ {
143+ name : "OtherHTTPError" ,
144+ httpResp : & http.Response {StatusCode : http .StatusInternalServerError , Body : io .NopCloser (strings .NewReader ("error" ))},
145+ expectedError : errors .New ("shadeform HTTP error: [500], error" ),
146+ },
147+ }
148+
149+ for _ , tt := range tests {
150+ t .Run (tt .name , func (t * testing.T ) {
151+ err := handleShadeformAPIErrorResponse (tt .httpResp )
152+ require .Contains (t , err .Error (), tt .expectedError .Error ())
153+ })
154+ }
155+ }
156+
157+ func TestOutOfStockError (t * testing.T ) {
158+ checkSkip (t )
159+ apiKey := getAPIKey ()
160+
161+ ctx , cancel := context .WithTimeout (context .Background (), 15 * time .Minute )
162+ t .Cleanup (cancel )
163+
164+ client := NewShadeformClient ("validation-test" , apiKey )
165+ client .WithConfiguration (Configuration {})
166+
167+ _ , err := client .CreateInstance (ctx , v1.CreateInstanceAttrs {
168+ RefID : uuid .New ().String (),
169+ InstanceType : "datacrunch_H200x8" ,
170+ Location : "abc123" , // Put a region that is not valid
171+ PublicKey : ssh .GetTestPublicKey (),
172+ Name : "test_name" ,
173+ FirewallRules : v1.FirewallRules {
174+ EgressRules : []v1.FirewallRule {
175+ {
176+ ID : "test-rule1" ,
177+ FromPort : 80 ,
178+ ToPort : 8080 ,
179+ IPRanges : []string {"127.0.0.1" , "10.0.0.0/24" },
180+ },
181+ {
182+ ID : "test-rule2" ,
183+ FromPort : 5432 ,
184+ ToPort : 5432 ,
185+ IPRanges : []string {"127.0.0.1" , "10.0.0.0/24" },
186+ },
187+ },
188+ IngressRules : []v1.FirewallRule {
189+ {
190+ ID : "test-rule3" ,
191+ FromPort : 80 ,
192+ ToPort : 8080 ,
193+ IPRanges : []string {"127.0.0.1" , "10.0.0.0/24" },
194+ },
195+ {
196+ ID : "test-rule4" ,
197+ FromPort : 5432 ,
198+ ToPort : 5432 ,
199+ IPRanges : []string {"127.0.0.1" , "10.0.0.0/24" },
200+ },
201+ },
202+ },
203+ })
204+ if err == nil {
205+ t .Fatalf ("ValidateCreateInstance failed: Should have resulted in an insufficientResourcesError" )
206+ }
207+ require .True (t , errors .Is (err , v1 .ErrInsufficientResources ), "Error must be ErrInsufficientResources" )
208+ }
209+
121210func checkSkip (t * testing.T ) {
122211 apiKey := getAPIKey ()
123212 isValidationTest := os .Getenv ("VALIDATION_TEST" )
0 commit comments