Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to mock server APIs in acceptance tests #2226

Merged
merged 35 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
07c2558
Add support for mocking servers in acceptance tests
shreyas-goenka Jan 23, 2025
2bcf43b
-
shreyas-goenka Jan 23, 2025
07e1ba2
-
shreyas-goenka Jan 23, 2025
6b8e1df
-
shreyas-goenka Jan 23, 2025
d9685ab
add unit tests
shreyas-goenka Jan 23, 2025
de326bf
-
shreyas-goenka Jan 23, 2025
09791fc
decouple changes
shreyas-goenka Jan 24, 2025
e22b015
wip finer grained server regex
shreyas-goenka Jan 24, 2025
0d14526
move server to libs
shreyas-goenka Jan 24, 2025
accec71
remove request bits
shreyas-goenka Jan 24, 2025
fffc89c
-
shreyas-goenka Jan 24, 2025
2952a73
rm fn
shreyas-goenka Jan 24, 2025
24be18a
merge
shreyas-goenka Jan 24, 2025
af6ae9c
Merge remote-tracking branch 'origin' into validate-response-body
shreyas-goenka Jan 28, 2025
7c5d65f
allow override test to run in parallel
shreyas-goenka Jan 29, 2025
fadd6ed
-
shreyas-goenka Jan 29, 2025
ba55f5b
merge
shreyas-goenka Jan 29, 2025
39477d5
-
shreyas-goenka Jan 29, 2025
3549c22
-
shreyas-goenka Jan 29, 2025
1f1d705
move config to toml
shreyas-goenka Jan 29, 2025
badfb9c
-
shreyas-goenka Jan 29, 2025
005589a
-
shreyas-goenka Jan 29, 2025
3e7acdb
-
shreyas-goenka Jan 29, 2025
ca17487
-
shreyas-goenka Jan 29, 2025
8505abd
-
shreyas-goenka Jan 29, 2025
8da6c57
address comments
shreyas-goenka Jan 29, 2025
bb87563
inlint append
shreyas-goenka Jan 29, 2025
76ca212
-
shreyas-goenka Jan 29, 2025
b93fa07
Revert "-"
shreyas-goenka Jan 29, 2025
c2eaf1a
-
shreyas-goenka Jan 29, 2025
ef6a673
-
shreyas-goenka Jan 29, 2025
b0628d1
return string directly
shreyas-goenka Jan 29, 2025
15c09c6
Merge remote-tracking branch 'origin' into validate-response-body
shreyas-goenka Jan 29, 2025
6ed7f22
-
shreyas-goenka Jan 29, 2025
0f42155
Merge remote-tracking branch 'origin' into validate-response-body
shreyas-goenka Jan 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -108,10 +109,10 @@ func testAccept(t *testing.T, InprocessMode bool, singleTest string) int {
cloudEnv := os.Getenv("CLOUD_ENV")

if cloudEnv == "" {
server := testserver.New(t)
AddHandlers(server)
defaultServer := testserver.New(t)
AddHandlers(defaultServer)
// Redirect API access to local server:
t.Setenv("DATABRICKS_HOST", server.URL)
t.Setenv("DATABRICKS_HOST", defaultServer.URL)
shreyas-goenka marked this conversation as resolved.
Show resolved Hide resolved
t.Setenv("DATABRICKS_TOKEN", "dapi1234")

homeDir := t.TempDir()
Expand Down Expand Up @@ -210,14 +211,30 @@ func runTest(t *testing.T, dir, coverDir string, repls testdiff.ReplacementsCont

args := []string{"bash", "-euo", "pipefail", EntryPointScript}
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = os.Environ()

// Start a new server with a custom configuration if the acceptance test
// specifies a custom server stubs.
if len(config.Server) > 0 {
server := testserver.New(t)

for _, stub := range config.Server {
require.NotEmpty(t, stub.Pattern)
server.Handle(stub.Pattern, func(req *http.Request) (resp any, err error) {
return stub.Response.Body, nil
})
}
cmd.Env = append(cmd.Env, "DATABRICKS_HOST="+server.URL)
}

if coverDir != "" {
// Creating individual coverage directory for each test, because writing to the same one
// results in sporadic failures like this one (only if tests are running in parallel):
// +error: coverage meta-data emit failed: writing ... rename .../tmp.covmeta.b3f... .../covmeta.b3f2c...: no such file or directory
coverDir = filepath.Join(coverDir, strings.ReplaceAll(dir, string(os.PathSeparator), "--"))
err := os.MkdirAll(coverDir, os.ModePerm)
require.NoError(t, err)
cmd.Env = append(os.Environ(), "GOCOVERDIR="+coverDir)
cmd.Env = append(cmd.Env, "GOCOVERDIR="+coverDir)
}

// Write combined output to a file
Expand Down
23 changes: 23 additions & 0 deletions acceptance/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ type TestConfig struct {
// List of additional replacements to apply on this test.
// Old is a regexp, New is a replacement expression.
Repls []testdiff.Replacement

// List of server stubs to load. Example configuration:
//
// [[Server]]
// Pattern = "POST /api/2.1/jobs/create"
// Response.Body = '''
// {
// "job_id": 1111
// }
// '''
Server []ServerStub
}

type ServerStub struct {
// The HTTP method and path to match. Examples:
// 1. /api/2.0/clusters/list (matches all methods)
// 2. GET /api/2.0/clusters/list
Pattern string

// The response body to return.
Response struct {
Body string
}
}

// FindConfig finds the closest config file.
Expand Down
5 changes: 5 additions & 0 deletions acceptance/workspace/jobs/create/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

>>> $CLI jobs create --json {"name":"abc"}
{
"job_id":1111
}
1 change: 1 addition & 0 deletions acceptance/workspace/jobs/create/script
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
trace $CLI jobs create --json '{"name":"abc"}'
7 changes: 7 additions & 0 deletions acceptance/workspace/jobs/create/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[[Server]]
Pattern = "POST /api/2.1/jobs/create"
Response.Body = '''
shreyas-goenka marked this conversation as resolved.
Show resolved Hide resolved
{
"job_id": 1111
}
'''