@@ -4,11 +4,16 @@ import (
44 "context"
55 "encoding/json"
66 "errors"
7+ "io"
78 "net/http"
89 "net/http/httptest"
910 "net/url"
11+ "strings"
12+ "sync/atomic"
1013 "testing"
14+ "time"
1115
16+ "github.com/databricks/cli/libs/cmdio"
1217 "github.com/databricks/databricks-sdk-go"
1318 "github.com/databricks/databricks-sdk-go/apierr"
1419 "github.com/stretchr/testify/assert"
@@ -48,6 +53,38 @@ func validationTestWorkspaceClient(t *testing.T, host string) *databricks.Worksp
4853 return w
4954}
5055
56+ func validationTestWorkspaceClientWithTransport (t * testing.T , transport validationRoundTripFunc ) * databricks.WorkspaceClient {
57+ t .Helper ()
58+ w , err := databricks .NewWorkspaceClient (& databricks.Config {
59+ Host : "https://example.test" ,
60+ Token : "token" ,
61+ HTTPTransport : transport ,
62+ })
63+ require .NoError (t , err )
64+ return w
65+ }
66+
67+ type validationRoundTripFunc func (* http.Request ) (* http.Response , error )
68+
69+ func (fn validationRoundTripFunc ) RoundTrip (req * http.Request ) (* http.Response , error ) {
70+ return fn (req )
71+ }
72+
73+ type validationErrorReadCloser struct {}
74+
75+ func (validationErrorReadCloser ) Read ([]byte ) (int , error ) { return 0 , errors .New ("body read failed" ) }
76+ func (validationErrorReadCloser ) Close () error { return nil }
77+
78+ type validationContextReadCloser struct {
79+ ctx context.Context
80+ }
81+
82+ func (r validationContextReadCloser ) Read ([]byte ) (int , error ) {
83+ <- r .ctx .Done ()
84+ return 0 , r .ctx .Err ()
85+ }
86+ func (validationContextReadCloser ) Close () error { return nil }
87+
5188func TestPreflightValidateFailsOpenWhenBackendUnavailable (t * testing.T ) {
5289 tests := []struct {
5390 name string
@@ -67,6 +104,154 @@ func TestPreflightValidateFailsOpenWhenBackendUnavailable(t *testing.T) {
67104 }
68105}
69106
107+ func TestPreflightValidationUnavailableWarnsWithoutRetry (t * testing.T ) {
108+ var requests atomic.Int32
109+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
110+ if r .URL .Path == validateConfigPath {
111+ requests .Add (1 )
112+ w .WriteHeader (http .StatusServiceUnavailable )
113+ _ , _ = w .Write ([]byte (`{"error_code":"TEMPORARILY_UNAVAILABLE","message":"try again"}` ))
114+ return
115+ }
116+ _ , _ = w .Write ([]byte (`{}` ))
117+ }))
118+ t .Cleanup (srv .Close )
119+ ctx , stderr := cmdio .NewTestContextWithStderr (t .Context ())
120+
121+ err := preflightValidate (ctx , validationTestWorkspaceClient (t , srv .URL ), baseRunConfig (), "/Workspace/Users/me/cmd.sh" , nil , "token" )
122+ require .NoError (t , err )
123+ assert .Equal (t , int32 (1 ), requests .Load ())
124+ assert .Equal (t , "Warning: server-side config validation was unavailable; continuing with submission.\n " , stderr .String ())
125+ }
126+
127+ func TestPreflightValidationDoesNotRetryTransportFailure (t * testing.T ) {
128+ var requests atomic.Int32
129+ w := validationTestWorkspaceClientWithTransport (t , func (req * http.Request ) (* http.Response , error ) {
130+ if req .URL .Path == validateConfigPath {
131+ requests .Add (1 )
132+ }
133+ return nil , errors .New ("connection failed" )
134+ })
135+
136+ err := preflightValidate (t .Context (), w , baseRunConfig (), "/Workspace/Users/me/cmd.sh" , nil , "token" )
137+ require .NoError (t , err )
138+ assert .Equal (t , int32 (1 ), requests .Load ())
139+ }
140+
141+ func TestPreflightValidationFailsOpenOnInvalidSuccessResponse (t * testing.T ) {
142+ tests := []struct {
143+ name string
144+ body func () io.ReadCloser
145+ }{
146+ {"body read failure" , func () io.ReadCloser { return validationErrorReadCloser {} }},
147+ {"malformed JSON" , func () io.ReadCloser { return io .NopCloser (strings .NewReader ("{" )) }},
148+ }
149+
150+ for _ , tt := range tests {
151+ t .Run (tt .name , func (t * testing.T ) {
152+ var requests atomic.Int32
153+ w := validationTestWorkspaceClientWithTransport (t , func (req * http.Request ) (* http.Response , error ) {
154+ body := io .NopCloser (strings .NewReader (`{}` ))
155+ if req .URL .Path == validateConfigPath {
156+ requests .Add (1 )
157+ body = tt .body ()
158+ }
159+ return & http.Response {
160+ StatusCode : http .StatusOK ,
161+ Header : http.Header {"Content-Type" : []string {"application/json" }},
162+ Body : body ,
163+ Request : req ,
164+ }, nil
165+ })
166+
167+ err := preflightValidate (t .Context (), w , baseRunConfig (), "/Workspace/Users/me/cmd.sh" , nil , "token" )
168+ require .NoError (t , err )
169+ assert .Equal (t , int32 (1 ), requests .Load ())
170+ })
171+ }
172+ }
173+
174+ func TestPreflightValidationBodyReadFailureOnCallerErrorBlocks (t * testing.T ) {
175+ w := validationTestWorkspaceClientWithTransport (t , func (req * http.Request ) (* http.Response , error ) {
176+ body := io .NopCloser (strings .NewReader (`{}` ))
177+ status := http .StatusOK
178+ if req .URL .Path == validateConfigPath {
179+ body = validationErrorReadCloser {}
180+ status = http .StatusUnauthorized
181+ }
182+ return & http.Response {
183+ StatusCode : status ,
184+ Header : http.Header {"Content-Type" : []string {"application/json" }},
185+ Body : body ,
186+ Request : req ,
187+ }, nil
188+ })
189+
190+ err := preflightValidate (t .Context (), w , baseRunConfig (), "/Workspace/Users/me/cmd.sh" , nil , "token" )
191+ require .Error (t , err )
192+ }
193+
194+ func TestPreflightValidationTimeoutFailsOpen (t * testing.T ) {
195+ releaseRequest := make (chan struct {})
196+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
197+ if r .URL .Path == validateConfigPath {
198+ <- releaseRequest
199+ return
200+ }
201+ _ , _ = w .Write ([]byte (`{}` ))
202+ }))
203+ t .Cleanup (srv .Close )
204+
205+ ctx , cancel := context .WithTimeout (t .Context (), 50 * time .Millisecond )
206+ defer cancel ()
207+ started := time .Now ()
208+ err := preflightValidate (ctx , validationTestWorkspaceClient (t , srv .URL ), baseRunConfig (), "/Workspace/Users/me/cmd.sh" , nil , "token" )
209+ close (releaseRequest )
210+ require .NoError (t , err )
211+ assert .Less (t , time .Since (started ), time .Second )
212+ }
213+
214+ func TestPreflightValidationPropagatesCancellation (t * testing.T ) {
215+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
216+ if r .URL .Path == validateConfigPath {
217+ <- r .Context ().Done ()
218+ return
219+ }
220+ _ , _ = w .Write ([]byte (`{}` ))
221+ }))
222+ t .Cleanup (srv .Close )
223+
224+ ctx , cancel := context .WithCancel (t .Context ())
225+ cancel ()
226+ err := preflightValidate (ctx , validationTestWorkspaceClient (t , srv .URL ), baseRunConfig (), "/Workspace/Users/me/cmd.sh" , nil , "token" )
227+ require .ErrorIs (t , err , context .Canceled )
228+ }
229+
230+ func TestPreflightValidationPropagatesCancellationWhileReadingResponse (t * testing.T ) {
231+ validationStarted := make (chan struct {})
232+ w := validationTestWorkspaceClientWithTransport (t , func (req * http.Request ) (* http.Response , error ) {
233+ body := io .NopCloser (strings .NewReader (`{}` ))
234+ if req .URL .Path == validateConfigPath {
235+ close (validationStarted )
236+ body = validationContextReadCloser {ctx : req .Context ()}
237+ }
238+ return & http.Response {
239+ StatusCode : http .StatusOK ,
240+ Header : http.Header {"Content-Type" : []string {"application/json" }},
241+ Body : body ,
242+ Request : req ,
243+ }, nil
244+ })
245+
246+ ctx , cancel := context .WithCancel (t .Context ())
247+ go func () {
248+ <- validationStarted
249+ cancel ()
250+ }()
251+ err := preflightValidate (ctx , w , baseRunConfig (), "/Workspace/Users/me/cmd.sh" , nil , "token" )
252+ require .ErrorIs (t , err , context .Canceled )
253+ }
254+
70255func TestPreflightValidateBlocksOnCallerError (t * testing.T ) {
71256 for _ , status := range []int {http .StatusBadRequest , http .StatusUnauthorized , http .StatusForbidden } {
72257 t .Run (http .StatusText (status ), func (t * testing.T ) {
0 commit comments