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 request body assertions to acceptance tests #2263

Merged
merged 46 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
46 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
de7540e
rebase
shreyas-goenka Jan 30, 2025
8b568b4
add recordrquests
shreyas-goenka Jan 30, 2025
10c48c3
-
shreyas-goenka Jan 30, 2025
c29dac2
merge
shreyas-goenka Jan 31, 2025
685fd0b
address comments
shreyas-goenka Jan 31, 2025
8edb47a
return body directly
shreyas-goenka Jan 31, 2025
47d344e
address comments
shreyas-goenka Jan 31, 2025
f5f8ee8
-
shreyas-goenka Jan 31, 2025
0e06baf
-
shreyas-goenka Jan 31, 2025
55ea53a
address comments
shreyas-goenka Jan 31, 2025
3d78349
remove append flag
shreyas-goenka Jan 31, 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
28 changes: 26 additions & 2 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package acceptance_test

import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -219,8 +220,21 @@ func runTest(t *testing.T, dir, coverDir string, repls testdiff.ReplacementsCont

// 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)
var server *testserver.Server

// Start a new server for this test if either:
// 1. A custom server spec is defined in the test configuration.
// 2. The test is configured to record requests and assert on them. We need
// a duplicate of the default server to record requests because the default
// server otherwise is a shared resource.
if len(config.Server) > 0 || config.RecordRequests {
shreyas-goenka marked this conversation as resolved.
Show resolved Hide resolved
server = testserver.New(t)
server.RecordRequests = config.RecordRequests

// If no custom server stubs are defined, add the default handlers.
if len(config.Server) == 0 {
AddHandlers(server)
}

for _, stub := range config.Server {
require.NotEmpty(t, stub.Pattern)
Expand Down Expand Up @@ -249,6 +263,16 @@ func runTest(t *testing.T, dir, coverDir string, repls testdiff.ReplacementsCont
cmd.Dir = tmpDir
err = cmd.Run()

// Write the requests made to the server to a output file if the test is
// configured to record requests.
if config.RecordRequests {
for _, req := range server.Requests {
reqJson, err := json.Marshal(req)
require.NoError(t, err)
testutil.AppendFile(t, filepath.Join(tmpDir, "out.requests.txt"), fmt.Sprintf("%s\n", reqJson))
shreyas-goenka marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Include exit code in output (if non-zero)
formatOutput(out, err)
require.NoError(t, out.Close())
Expand Down
4 changes: 4 additions & 0 deletions acceptance/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type TestConfig struct {
// }
// '''
Server []ServerStub

// Record the requests made to the server and write them as output to
// out.requests.txt
RecordRequests bool
}

type ServerStub struct {
Expand Down
1 change: 1 addition & 0 deletions acceptance/workspace/jobs/create/out.requests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"method":"POST","path":"/api/2.1/jobs/create","body":{"name":"abc"}}
2 changes: 2 additions & 0 deletions acceptance/workspace/jobs/create/test.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
RecordRequests = true

[[Server]]
Pattern = "POST /api/2.1/jobs/create"
Response.Body = '''
Expand Down
16 changes: 16 additions & 0 deletions internal/testutil/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ func WriteFile(t TestingT, path, content string) {
require.NoError(t, err)
}

// AppendFile appends content to a file.
// If the file does not exist, it is created.
func AppendFile(t TestingT, path, content string) {
err := os.MkdirAll(filepath.Dir(path), 0o755)
require.NoError(t, err)

f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
require.NoError(t, err)

_, err = f.WriteString(content)
require.NoError(t, err)

err = f.Close()
require.NoError(t, err)
}

// ReadFile reads a file and returns its content as a string.
func ReadFile(t TestingT, path string) string {
b, err := os.ReadFile(path)
Expand Down
25 changes: 25 additions & 0 deletions libs/testserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package testserver

import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"

"github.com/stretchr/testify/assert"

"github.com/databricks/cli/internal/testutil"
)

Expand All @@ -13,6 +16,16 @@ type Server struct {
Mux *http.ServeMux

t testutil.TestingT

RecordRequests bool

Requests []Request
}

type Request struct {
Method string `json:"method"`
Path string `json:"path"`
Body any `json:"body"`
}

func New(t testutil.TestingT) *Server {
Expand All @@ -37,6 +50,18 @@ func (s *Server) Handle(pattern string, handler HandlerFunc) {
return
}

if s.RecordRequests {
body, err := io.ReadAll(r.Body)
assert.NoError(s.t, err)

s.Requests = append(s.Requests, Request{
Method: r.Method,
Path: r.URL.Path,
Body: json.RawMessage(body),
})

}

w.Header().Set("Content-Type", "application/json")

var respBytes []byte
Expand Down