Skip to content

Commit 59edf11

Browse files
Run a CLI acceptance test against DMS in Litebox (#6780)
## Changes Add opt-in routing from the acceptance runner to local DMS and its shared workspace/ACL fakes in Litebox. DMS, workspace files, workspace objects, and their permissions use the Litebox endpoint; unrelated APIs retain their existing testserver mocks. ## Usage With `DMS_LITEBOX_URL`, `DMS_LITEBOX_WORKSPACE_URL`, `DMS_LITEBOX_CERT`, `DMS_LITEBOX_KEY`, and `DMS_LITEBOX_CA` configured: ```sh go test ./acceptance -run '^TestDmsAcc$/bundle/dms/no-resources$' -count=1 ``` Companion Universe PR: https://github.com/databricks-eng/universe/pull/2653507.
1 parent af5bc3c commit 59edf11

3 files changed

Lines changed: 168 additions & 0 deletions

File tree

‎acceptance/acceptance_test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ func TestAccept(t *testing.T) {
154154
testAccept(t, InprocessMode, nil, false)
155155
}
156156

157+
// TestDmsAcc runs acceptance tests against local DMS, selected with go test -run.
158+
// The in-process runner skips unrelated tool builds and old CLI downloads.
159+
func TestDmsAcc(t *testing.T) {
160+
if os.Getenv("DMS_LITEBOX_URL") == "" {
161+
t.Skip("DMS_LITEBOX_URL is not set")
162+
}
163+
require.Empty(t, os.Getenv("CLOUD_ENV"), "Litebox must not use a cloud workspace")
164+
testAccept(t, true, nil, true)
165+
}
166+
157167
func TestInprocessMode(t *testing.T) {
158168
if InprocessMode && !Forcerun {
159169
t.Skip("Already tested by TestAccept")

‎acceptance/internal/dms_litebox.go‎

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package internal
2+
3+
import (
4+
"bytes"
5+
"crypto/tls"
6+
"crypto/x509"
7+
"io"
8+
"net"
9+
"net/http"
10+
"net/url"
11+
"os"
12+
"strings"
13+
"testing"
14+
"time"
15+
16+
"github.com/databricks/cli/libs/env"
17+
"github.com/databricks/cli/libs/testserver"
18+
"github.com/stretchr/testify/require"
19+
)
20+
21+
const dmsAPIPath = "/api/2.0/bundle/deployments"
22+
23+
// Deployment TreeNodes use the generic workspace-objects permissions API.
24+
var liteboxPermissionTypes = []string{"directories", "files", "notebooks", "workspace-objects"}
25+
26+
// configureDMSLitebox routes DMS to the local service and workspace files/ACLs
27+
// to its shared fakes, retaining request recording and mocks for other APIs.
28+
func configureDMSLitebox(t *testing.T, server *testserver.Server) {
29+
t.Helper()
30+
endpoint := env.Get(t.Context(), "DMS_LITEBOX_URL")
31+
if endpoint == "" {
32+
return
33+
}
34+
dmsURL := liteboxURL(t, "DMS_LITEBOX_URL")
35+
workspaceURL := liteboxURL(t, "DMS_LITEBOX_WORKSPACE_URL")
36+
37+
certFile, keyFile := env.Get(t.Context(), "DMS_LITEBOX_CERT"), env.Get(t.Context(), "DMS_LITEBOX_KEY")
38+
caFile := env.Get(t.Context(), "DMS_LITEBOX_CA")
39+
require.NotEmpty(t, certFile, "DMS_LITEBOX_CERT is required with DMS_LITEBOX_URL")
40+
require.NotEmpty(t, keyFile, "DMS_LITEBOX_KEY is required with DMS_LITEBOX_URL")
41+
require.NotEmpty(t, caFile, "DMS_LITEBOX_CA is required with DMS_LITEBOX_URL")
42+
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
43+
require.NoError(t, err, "load Litebox client certificate and key")
44+
pem, err := os.ReadFile(caFile)
45+
require.NoError(t, err, "read DMS_LITEBOX_CA")
46+
roots := x509.NewCertPool()
47+
require.True(t, roots.AppendCertsFromPEM(pem), "invalid Litebox certificate")
48+
// Both upstreams must use LITE's test certificate for *.svc.cluster.local.
49+
transport := &http.Transport{
50+
TLSClientConfig: &tls.Config{
51+
MinVersion: tls.VersionTLS12,
52+
Certificates: []tls.Certificate{cert},
53+
RootCAs: roots,
54+
ServerName: "deployment-metadata-service.svc.cluster.local",
55+
},
56+
ForceAttemptHTTP2: true,
57+
}
58+
t.Cleanup(transport.CloseIdleConnections)
59+
client := &http.Client{
60+
Transport: transport,
61+
Timeout: 30 * time.Second,
62+
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
63+
return http.ErrUseLastResponse
64+
},
65+
}
66+
handler := func(r testserver.Request) any {
67+
target := *workspaceURL
68+
if isDMSPath(r.URL.Path) {
69+
target = *dmsURL
70+
}
71+
target.Path, target.RawPath, target.RawQuery = r.URL.Path, r.URL.RawPath, r.URL.RawQuery
72+
req, err := http.NewRequestWithContext(r.Context, r.Method, target.String(), bytes.NewReader(r.Body))
73+
if err != nil {
74+
return liteboxFailure(err)
75+
}
76+
req.Header = r.Headers.Clone()
77+
req.Header.Del("Authorization")
78+
req.Header.Del("Accept-Encoding")
79+
// Fixed test identity; this adapter only connects to a local LITE fixture.
80+
req.Header.Set("X-Databricks-Org-Id", "456")
81+
req.Header.Set("X-Databricks-Workspace-Id", "456")
82+
req.Header.Set("X-Databricks-User-Id", testserver.TestUser.Id)
83+
req.Header.Set("X-Databricks-User-Name", testserver.TestUser.UserName)
84+
resp, err := client.Do(req)
85+
if err != nil {
86+
return liteboxFailure(err)
87+
}
88+
defer resp.Body.Close()
89+
body, err := io.ReadAll(resp.Body)
90+
if err != nil {
91+
return liteboxFailure(err)
92+
}
93+
return testserver.Response{StatusCode: resp.StatusCode, Headers: resp.Header, Body: body}
94+
}
95+
routeLitebox(server, handler)
96+
}
97+
98+
func liteboxURL(t *testing.T, name string) *url.URL {
99+
t.Helper()
100+
endpoint := env.Get(t.Context(), name)
101+
require.NotEmpty(t, endpoint, "%s is required with DMS_LITEBOX_URL", name)
102+
u, err := url.Parse(endpoint)
103+
require.NoError(t, err, "invalid %s", name)
104+
ip := net.ParseIP(u.Hostname())
105+
require.True(t, u.Scheme == "https" && (u.Hostname() == "localhost" || (ip != nil && ip.IsLoopback())) &&
106+
u.User == nil && u.RawQuery == "" && u.Fragment == "" && (u.Path == "" || u.Path == "/"),
107+
"%s must be an HTTPS loopback origin", name)
108+
return u
109+
}
110+
111+
func isDMSPath(path string) bool {
112+
return path == dmsAPIPath || strings.HasPrefix(path, dmsAPIPath+"/")
113+
}
114+
115+
func isLiteboxPath(path string) bool {
116+
if isDMSPath(path) ||
117+
strings.HasPrefix(path, "/api/2.0/workspace/") ||
118+
strings.HasPrefix(path, "/api/2.0/workspace-files/") {
119+
return true
120+
}
121+
for _, objectType := range liteboxPermissionTypes {
122+
if strings.HasPrefix(path, "/api/2.0/permissions/"+objectType+"/") {
123+
return true
124+
}
125+
}
126+
return false
127+
}
128+
129+
func routeLitebox(server *testserver.Server, handler testserver.HandlerFunc) {
130+
// Without a PATCH route, ServeMux returns 405 before Dispatch or NotFound.
131+
for _, objectType := range liteboxPermissionTypes {
132+
server.Handle(http.MethodPatch, "/api/2.0/permissions/"+objectType+"/{object_id}", handler)
133+
}
134+
dispatch := server.Dispatch
135+
server.Dispatch = func(w http.ResponseWriter, r *http.Request, h testserver.HandlerFunc, vars map[string]string) {
136+
if isLiteboxPath(r.URL.Path) {
137+
h = handler
138+
}
139+
dispatch(w, r, h, vars)
140+
}
141+
notFound := server.NotFound
142+
server.NotFound = func(w http.ResponseWriter, r *http.Request) {
143+
if isLiteboxPath(r.URL.Path) {
144+
dispatch(w, r, handler, nil)
145+
return
146+
}
147+
notFound(w, r)
148+
}
149+
}
150+
151+
func liteboxFailure(err error) testserver.Response {
152+
return testserver.Response{StatusCode: http.StatusBadGateway, Body: map[string]string{
153+
"error_code": "LITEBOX_CONNECTION_ERROR",
154+
"message": err.Error(),
155+
}}
156+
}

‎acceptance/internal/prepare_server.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
func StartDefaultServer(t *testing.T, logRequests bool) {
2929
s := testserver.New(t)
3030
testserver.AddDefaultHandlers(s)
31+
configureDMSLitebox(t, s)
3132

3233
// Log API responses if the -logrequests flag is set.
3334
if logRequests {
@@ -270,6 +271,7 @@ func startLocalServer(t *testing.T,
270271
// The first handler registered for a given pattern wins, so default
271272
// handlers registered last serve as fallbacks.
272273
testserver.AddDefaultHandlers(s)
274+
configureDMSLitebox(t, s)
273275
return s.URL
274276
}
275277

0 commit comments

Comments
 (0)