diff --git a/acceptance/server_test.go b/acceptance/server_test.go index 21bf76556..be5dafed3 100644 --- a/acceptance/server_test.go +++ b/acceptance/server_test.go @@ -1,7 +1,6 @@ package acceptance_test import ( - "context" "net/http" "testing" @@ -13,7 +12,7 @@ import ( ) func StartServer(t *testing.T) *testserver.Server { - server := testserver.New(context.Background(), t) + server := testserver.New(t) t.Cleanup(func() { server.Close() }) diff --git a/acceptance/workspace/jobs/create/server.json b/acceptance/workspace/jobs/create/server.json index 52c83fe4e..79eb75aac 100644 --- a/acceptance/workspace/jobs/create/server.json +++ b/acceptance/workspace/jobs/create/server.json @@ -2,15 +2,6 @@ { "method": "POST", "path": "/api/2.1/jobs/create", - "request": { - "headers": { - "Authorization": "Bearer acceptance-test-token", - "User-Agent": "cli/[SEMVER] databricks-sdk-go/[SEMVER] go/[SEMVER] os/[OS] cmd/jobs_create cmd-exec-id/<UUID> auth/pat" - }, - "body": { - "name": "abc" - } - }, "response": { "body": { "job_id": 1111 diff --git a/libs/testdiff/replacement.go b/libs/testdiff/replacement.go index 8b75c70b3..ca76b159c 100644 --- a/libs/testdiff/replacement.go +++ b/libs/testdiff/replacement.go @@ -9,13 +9,10 @@ import ( "slices" "strings" - "github.com/databricks/cli/internal/build" "github.com/databricks/cli/internal/testutil" "github.com/databricks/cli/libs/iamutil" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/service/iam" - "github.com/databricks/databricks-sdk-go/version" - "golang.org/x/mod/semver" ) const ( @@ -23,8 +20,6 @@ const ( ) var ( - // source: https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string - semverRegex = regexp.MustCompile(`(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?`) uuidRegex = regexp.MustCompile(`[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}`) numIdRegex = regexp.MustCompile(`[0-9]{3,}`) privatePathRegex = regexp.MustCompile(`(/tmp|/private)(/.*)/([a-zA-Z0-9]+)`) @@ -200,35 +195,3 @@ func PrepareReplacementsTemporaryDirectory(t testutil.TestingT, r *ReplacementsC t.Helper() r.append(privatePathRegex, "/tmp/.../$3") } - -// cli/0.0.0-dev databricks-sdk-go/0.55.0 go/1.23.4 os/darwin cmd/jobs_create cmd-exec-id/2222ca79-6a7e-4e8a-9e89-73392f8a2f09 auth/pat - -func PrepareReplacementVersions(t testutil.TestingT, r *ReplacementsContext) { - t.Helper() - r.Set(version.Version, "$GO_SDK_VERSION") - - buildInfo := build.GetInfo() - // test build versions can contain build metadata in their semver. Add a regex to match that. - // TODO: This does not work. Fix it. - cliVersionRegex := regexp.MustCompile(buildInfo.Version + `(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?`) - r.append(cliVersionRegex, "$CLI_VERSION") - - r.Set(goVersion(), "$GO_VERSION") -} - -func goVersion() string { - gv := runtime.Version() - ssv := strings.ReplaceAll(gv, "go", "v") - sv := semver.Canonical(ssv) - return strings.TrimPrefix(sv, "v") -} - -func PrepareReplacementsSemver(t testutil.TestingT, r *ReplacementsContext) { - t.Helper() - r.append(semverRegex, "[SEMVER]") -} - -func PrepareReplacementOS(t testutil.TestingT, r *ReplacementsContext) { - t.Helper() - r.Set(runtime.GOOS, "[OS]") -} diff --git a/libs/testdiff/replacement_test.go b/libs/testdiff/replacement_test.go index 764f6db79..de247c03e 100644 --- a/libs/testdiff/replacement_test.go +++ b/libs/testdiff/replacement_test.go @@ -1,7 +1,6 @@ package testdiff import ( - "runtime" "testing" "github.com/stretchr/testify/assert" @@ -45,21 +44,3 @@ func TestReplacement_TemporaryDirectory(t *testing.T) { assert.Equal(t, "/tmp/.../tail", repls.Replace("/tmp/foo/bar/qux/tail")) } - -func TestReplacement_OS(t *testing.T) { - var repls ReplacementsContext - - PrepareReplacementOS(t, &repls) - - assert.Equal(t, "[OS]", repls.Replace(runtime.GOOS)) -} - -func TestReplacement_Semver(t *testing.T) { - var repls ReplacementsContext - - PrepareReplacementsSemver(t, &repls) - - assert.Equal(t, "[SEMVER]", repls.Replace("1.2.3")) - assert.Equal(t, "[SEMVER]", repls.Replace("1.2.3-alpha")) - assert.Equal(t, "[SEMVER]", repls.Replace("1.2.3-alpha+build")) -} diff --git a/libs/testserver/server.go b/libs/testserver/server.go index d160f400b..4377ebe71 100644 --- a/libs/testserver/server.go +++ b/libs/testserver/server.go @@ -1,14 +1,11 @@ package testserver import ( - "context" "encoding/json" - "io" "net/http" "net/http/httptest" "github.com/databricks/cli/internal/testutil" - "github.com/databricks/cli/libs/testdiff" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -17,26 +14,21 @@ type Server struct { *httptest.Server Mux *http.ServeMux - t testutil.TestingT - ctx context.Context + t testutil.TestingT // API calls that we expect to be made. calledPatterns map[string]bool } type ApiSpec struct { - Method string `json:"method"` - Path string `json:"path"` - Request struct { - Headers map[string]string `json:"headers"` - Body json.RawMessage `json:"body"` - } `json:"request"` + Method string `json:"method"` + Path string `json:"path"` Response struct { Body json.RawMessage `json:"body"` } `json:"response"` } -func New(ctx context.Context, t testutil.TestingT) *Server { +func New(t testutil.TestingT) *Server { mux := http.NewServeMux() server := httptest.NewServer(mux) @@ -44,7 +36,6 @@ func New(ctx context.Context, t testutil.TestingT) *Server { Server: server, Mux: mux, t: t, - ctx: ctx, calledPatterns: make(map[string]bool), } } @@ -55,13 +46,7 @@ func NewFromConfig(t testutil.TestingT, path string) *Server { err := json.Unmarshal([]byte(content), &apiSpecs) require.NoError(t, err) - ctx, replacements := testdiff.WithReplacementsMap(context.Background()) - testdiff.PrepareReplacementOS(t, replacements) - testdiff.PrepareReplacementsUUID(t, replacements) - testdiff.PrepareReplacementVersions(t, replacements) - // testdiff.PrepareReplacementsSemver(t, replacements) - - server := New(ctx, t) + server := New(t) for _, apiSpec := range apiSpecs { server.MustHandle(apiSpec) } @@ -72,28 +57,18 @@ func NewFromConfig(t testutil.TestingT, path string) *Server { type HandlerFunc func(req *http.Request) (resp any, err error) func (s *Server) MustHandle(apiSpec ApiSpec) { - require.NotEmpty(s.t, apiSpec.Method) - require.NotEmpty(s.t, apiSpec.Path) + assert.NotEmpty(s.t, apiSpec.Method) + assert.NotEmpty(s.t, apiSpec.Path) pattern := apiSpec.Method + " " + apiSpec.Path s.calledPatterns[pattern] = false s.Handle(pattern, func(req *http.Request) (any, error) { - for k, v := range apiSpec.Request.Headers { - testdiff.AssertEqualStrings(s.t, s.ctx, v, req.Header.Get(k)) - } - - b, err := io.ReadAll(req.Body) - require.NoError(s.t, err) - - // Assert that the request body matches the expected body. - assert.JSONEq(s.t, string(apiSpec.Request.Body), string(b)) - // Record the fact that this pattern was called. s.calledPatterns[pattern] = true // Return the expected response body. - return apiSpec.Response.Body, err + return apiSpec.Response.Body, nil }) }