remove request bits

This commit is contained in:
Shreyas Goenka 2025-01-24 14:17:56 +01:00
parent 0d14526098
commit accec7118c
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
5 changed files with 9 additions and 100 deletions

View File

@ -1,7 +1,6 @@
package acceptance_test package acceptance_test
import ( import (
"context"
"net/http" "net/http"
"testing" "testing"
@ -13,7 +12,7 @@ import (
) )
func StartServer(t *testing.T) *testserver.Server { func StartServer(t *testing.T) *testserver.Server {
server := testserver.New(context.Background(), t) server := testserver.New(t)
t.Cleanup(func() { t.Cleanup(func() {
server.Close() server.Close()
}) })

View File

@ -2,15 +2,6 @@
{ {
"method": "POST", "method": "POST",
"path": "/api/2.1/jobs/create", "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": { "response": {
"body": { "body": {
"job_id": 1111 "job_id": 1111

View File

@ -9,13 +9,10 @@ import (
"slices" "slices"
"strings" "strings"
"github.com/databricks/cli/internal/build"
"github.com/databricks/cli/internal/testutil" "github.com/databricks/cli/internal/testutil"
"github.com/databricks/cli/libs/iamutil" "github.com/databricks/cli/libs/iamutil"
"github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/iam"
"github.com/databricks/databricks-sdk-go/version"
"golang.org/x/mod/semver"
) )
const ( const (
@ -23,8 +20,6 @@ const (
) )
var ( 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}`) 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,}`) numIdRegex = regexp.MustCompile(`[0-9]{3,}`)
privatePathRegex = regexp.MustCompile(`(/tmp|/private)(/.*)/([a-zA-Z0-9]+)`) privatePathRegex = regexp.MustCompile(`(/tmp|/private)(/.*)/([a-zA-Z0-9]+)`)
@ -200,35 +195,3 @@ func PrepareReplacementsTemporaryDirectory(t testutil.TestingT, r *ReplacementsC
t.Helper() t.Helper()
r.append(privatePathRegex, "/tmp/.../$3") 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]")
}

View File

@ -1,7 +1,6 @@
package testdiff package testdiff
import ( import (
"runtime"
"testing" "testing"
"github.com/stretchr/testify/assert" "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")) 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"))
}

View File

@ -1,14 +1,11 @@
package testserver package testserver
import ( import (
"context"
"encoding/json" "encoding/json"
"io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"github.com/databricks/cli/internal/testutil" "github.com/databricks/cli/internal/testutil"
"github.com/databricks/cli/libs/testdiff"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -18,7 +15,6 @@ type Server struct {
Mux *http.ServeMux Mux *http.ServeMux
t testutil.TestingT t testutil.TestingT
ctx context.Context
// API calls that we expect to be made. // API calls that we expect to be made.
calledPatterns map[string]bool calledPatterns map[string]bool
@ -27,16 +23,12 @@ type Server struct {
type ApiSpec struct { type ApiSpec struct {
Method string `json:"method"` Method string `json:"method"`
Path string `json:"path"` Path string `json:"path"`
Request struct {
Headers map[string]string `json:"headers"`
Body json.RawMessage `json:"body"`
} `json:"request"`
Response struct { Response struct {
Body json.RawMessage `json:"body"` Body json.RawMessage `json:"body"`
} `json:"response"` } `json:"response"`
} }
func New(ctx context.Context, t testutil.TestingT) *Server { func New(t testutil.TestingT) *Server {
mux := http.NewServeMux() mux := http.NewServeMux()
server := httptest.NewServer(mux) server := httptest.NewServer(mux)
@ -44,7 +36,6 @@ func New(ctx context.Context, t testutil.TestingT) *Server {
Server: server, Server: server,
Mux: mux, Mux: mux,
t: t, t: t,
ctx: ctx,
calledPatterns: make(map[string]bool), calledPatterns: make(map[string]bool),
} }
} }
@ -55,13 +46,7 @@ func NewFromConfig(t testutil.TestingT, path string) *Server {
err := json.Unmarshal([]byte(content), &apiSpecs) err := json.Unmarshal([]byte(content), &apiSpecs)
require.NoError(t, err) require.NoError(t, err)
ctx, replacements := testdiff.WithReplacementsMap(context.Background()) server := New(t)
testdiff.PrepareReplacementOS(t, replacements)
testdiff.PrepareReplacementsUUID(t, replacements)
testdiff.PrepareReplacementVersions(t, replacements)
// testdiff.PrepareReplacementsSemver(t, replacements)
server := New(ctx, t)
for _, apiSpec := range apiSpecs { for _, apiSpec := range apiSpecs {
server.MustHandle(apiSpec) 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) type HandlerFunc func(req *http.Request) (resp any, err error)
func (s *Server) MustHandle(apiSpec ApiSpec) { func (s *Server) MustHandle(apiSpec ApiSpec) {
require.NotEmpty(s.t, apiSpec.Method) assert.NotEmpty(s.t, apiSpec.Method)
require.NotEmpty(s.t, apiSpec.Path) assert.NotEmpty(s.t, apiSpec.Path)
pattern := apiSpec.Method + " " + apiSpec.Path pattern := apiSpec.Method + " " + apiSpec.Path
s.calledPatterns[pattern] = false s.calledPatterns[pattern] = false
s.Handle(pattern, func(req *http.Request) (any, error) { 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. // Record the fact that this pattern was called.
s.calledPatterns[pattern] = true s.calledPatterns[pattern] = true
// Return the expected response body. // Return the expected response body.
return apiSpec.Response.Body, err return apiSpec.Response.Body, nil
}) })
} }