Skip to content

Commit

Permalink
Add feature to mock server APIs in acceptance tests (#2226)
Browse files Browse the repository at this point in the history
## Changes
This PR allows us to define custom server stubs in a `test.toml` file. 

Note: A followup PR will add functionality to do assertions on the API
request itself.

## Tests
New acceptance test.
  • Loading branch information
shreyas-goenka authored Jan 30, 2025
1 parent f1efbd7 commit 3c6eacb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
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 @@ -112,10 +113,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)
t.Setenv("DATABRICKS_TOKEN", "dapi1234")

homeDir := t.TempDir()
Expand Down Expand Up @@ -214,14 +215,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 = '''
{
"job_id": 1111
}
'''

0 comments on commit 3c6eacb

Please sign in to comment.