Move TestServer from acceptance to libs/testserver (#2255)

## Changes
Just a move, no changes. As recommended here:
https://github.com/databricks/cli/pull/2226#discussion_r1932152627

## Tests
N/A
This commit is contained in:
shreyas-goenka 2025-01-29 16:12:21 +05:30 committed by GitHub
parent 413ca5c134
commit 124515e8d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 71 additions and 54 deletions

View File

@ -20,6 +20,7 @@ import (
"github.com/databricks/cli/internal/testutil"
"github.com/databricks/cli/libs/env"
"github.com/databricks/cli/libs/testdiff"
"github.com/databricks/cli/libs/testserver"
"github.com/databricks/databricks-sdk-go"
"github.com/stretchr/testify/require"
)
@ -107,7 +108,7 @@ func testAccept(t *testing.T, InprocessMode bool, singleTest string) int {
cloudEnv := os.Getenv("CLOUD_ENV")
if cloudEnv == "" {
server := StartServer(t)
server := testserver.New(t)
AddHandlers(server)
// Redirect API access to local server:
t.Setenv("DATABRICKS_HOST", server.URL)

View File

@ -8,10 +8,11 @@ import (
"testing"
"github.com/databricks/cli/internal/testcli"
"github.com/databricks/cli/libs/testserver"
"github.com/stretchr/testify/require"
)
func StartCmdServer(t *testing.T) *TestServer {
func StartCmdServer(t *testing.T) *testserver.Server {
server := StartServer(t)
server.Handle("/", func(r *http.Request) (any, error) {
q := r.URL.Query()

View File

@ -1,73 +1,25 @@
package acceptance_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/databricks/cli/libs/testserver"
"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/databricks-sdk-go/service/compute"
"github.com/databricks/databricks-sdk-go/service/iam"
"github.com/databricks/databricks-sdk-go/service/workspace"
)
type TestServer struct {
*httptest.Server
Mux *http.ServeMux
}
type HandlerFunc func(r *http.Request) (any, error)
func NewTestServer() *TestServer {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
return &TestServer{
Server: server,
Mux: mux,
}
}
func (s *TestServer) Handle(pattern string, handler HandlerFunc) {
s.Mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
resp, err := handler(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
var respBytes []byte
respString, ok := resp.(string)
if ok {
respBytes = []byte(respString)
} else {
respBytes, err = json.MarshalIndent(resp, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
if _, err := w.Write(respBytes); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
}
func StartServer(t *testing.T) *TestServer {
server := NewTestServer()
func StartServer(t *testing.T) *testserver.Server {
server := testserver.New(t)
t.Cleanup(func() {
server.Close()
})
return server
}
func AddHandlers(server *TestServer) {
func AddHandlers(server *testserver.Server) {
server.Handle("GET /api/2.0/policies/clusters/list", func(r *http.Request) (any, error) {
return compute.ListPoliciesResponse{
Policies: []compute.Policy{

63
libs/testserver/server.go Normal file
View File

@ -0,0 +1,63 @@
package testserver
import (
"encoding/json"
"net/http"
"net/http/httptest"
"github.com/databricks/cli/internal/testutil"
)
type Server struct {
*httptest.Server
Mux *http.ServeMux
t testutil.TestingT
}
func New(t testutil.TestingT) *Server {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
return &Server{
Server: server,
Mux: mux,
t: t,
}
}
type HandlerFunc func(req *http.Request) (resp any, err error)
func (s *Server) Close() {
s.Server.Close()
}
func (s *Server) Handle(pattern string, handler HandlerFunc) {
s.Mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
resp, err := handler(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
var respBytes []byte
respString, ok := resp.(string)
if ok {
respBytes = []byte(respString)
} else {
respBytes, err = json.MarshalIndent(resp, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
if _, err := w.Write(respBytes); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
}