mirror of https://github.com/databricks/cli.git
Always close test HTTP server during cleanup (#2261)
## Changes This PR registers the `server.Close()` function to be run during test cleanup in the server initialization function. This ensures that all test servers are closed as soon as the test they are scoped to finish. Motivated by https://github.com/databricks/cli/pull/2255/files where a regression was introduced where we did not close the test server. ## Tests N/A
This commit is contained in:
parent
ce965b22b2
commit
55c03cc119
|
@ -13,7 +13,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func StartCmdServer(t *testing.T) *testserver.Server {
|
func StartCmdServer(t *testing.T) *testserver.Server {
|
||||||
server := StartServer(t)
|
server := testserver.New(t)
|
||||||
|
|
||||||
server.Handle("/", func(r *http.Request) (any, error) {
|
server.Handle("/", func(r *http.Request) (any, error) {
|
||||||
q := r.URL.Query()
|
q := r.URL.Query()
|
||||||
args := strings.Split(q.Get("args"), " ")
|
args := strings.Split(q.Get("args"), " ")
|
||||||
|
|
|
@ -2,7 +2,6 @@ package acceptance_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/databricks/cli/libs/testserver"
|
"github.com/databricks/cli/libs/testserver"
|
||||||
"github.com/databricks/databricks-sdk-go/service/catalog"
|
"github.com/databricks/databricks-sdk-go/service/catalog"
|
||||||
|
@ -11,14 +10,6 @@ import (
|
||||||
"github.com/databricks/databricks-sdk-go/service/workspace"
|
"github.com/databricks/databricks-sdk-go/service/workspace"
|
||||||
)
|
)
|
||||||
|
|
||||||
func StartServer(t *testing.T) *testserver.Server {
|
|
||||||
server := testserver.New(t)
|
|
||||||
t.Cleanup(func() {
|
|
||||||
server.Close()
|
|
||||||
})
|
|
||||||
return server
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddHandlers(server *testserver.Server) {
|
func AddHandlers(server *testserver.Server) {
|
||||||
server.Handle("GET /api/2.0/policies/clusters/list", func(r *http.Request) (any, error) {
|
server.Handle("GET /api/2.0/policies/clusters/list", func(r *http.Request) (any, error) {
|
||||||
return compute.ListPoliciesResponse{
|
return compute.ListPoliciesResponse{
|
||||||
|
|
|
@ -18,6 +18,7 @@ type Server struct {
|
||||||
func New(t testutil.TestingT) *Server {
|
func New(t testutil.TestingT) *Server {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
server := httptest.NewServer(mux)
|
server := httptest.NewServer(mux)
|
||||||
|
t.Cleanup(server.Close)
|
||||||
|
|
||||||
return &Server{
|
return &Server{
|
||||||
Server: server,
|
Server: server,
|
||||||
|
@ -28,10 +29,6 @@ func New(t testutil.TestingT) *Server {
|
||||||
|
|
||||||
type HandlerFunc func(req *http.Request) (resp any, err error)
|
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) {
|
func (s *Server) Handle(pattern string, handler HandlerFunc) {
|
||||||
s.Mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
|
s.Mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
|
||||||
resp, err := handler(r)
|
resp, err := handler(r)
|
||||||
|
|
Loading…
Reference in New Issue