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

Refactor agent integration test API #2764

Merged
merged 2 commits into from
May 2, 2024
Merged

Conversation

moskyb
Copy link
Contributor

@moskyb moskyb commented May 2, 2024

Description

The agent integration tests use a mock server to pretend to be the Buildkite Agent API, but it's extremely brittle, and quite frankly, some of the worst code i've ever written. it's difficult to add new endpoints, and callers of the server can't modify its behaviour in any way, which is frustrating when you need to test against specific behaviour.

This PR refactors that server to use Go 1.22's new http.HandleFunc("GET /some/path/{with}/path/params") gear, cleans up the handler code to not be godawful, and allows callers to add endpoints, and override the existing ones.

It also adds an error return to the runJob helper that gets used in agent integration tests, as we might in future want to test failures of this code

Testing

  • Tests have run locally (with go test ./...). Buildkite employees may check this if the pipeline has run automatically.
  • Code is formatted (with go fmt ./...)

@moskyb moskyb requested a review from DrJosh9000 May 2, 2024 07:06
@moskyb moskyb force-pushed the refactor-agent-integration-test-api branch from 5e3cd05 to 67c7673 Compare May 2, 2024 07:07
@moskyb moskyb changed the title Refactor agent integration test api Refactor agent integration test API May 2, 2024
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
b, _ := io.ReadAll(req.Body)
req.Body.Close()
req.Body = io.NopCloser(bytes.NewBuffer(b))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this?

Copy link
Contributor Author

@moskyb moskyb May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right... this function records the contents of the body of the request so that consumers can look through it later on. unfortunately, the body of an HTTP request is an io.ReadCloser, which is forward-read-only. this means that if we read the body, then tried to read it again, it wouldn't work (it would either return an EOF error, or it would return an empty read body, i can't remember which).

regardless, this little dance is basically:

  • read the body into a []byte caled b
  • close the body (we're done with it)
  • replace the body with a copy of b that is also an io.ReadCloser

i'll add a comment explaining this, it's not super clear just from reading it

@moskyb moskyb force-pushed the refactor-agent-integration-test-api branch 2 times, most recently from 8db8a08 to 8d9360a Compare May 2, 2024 09:33
@moskyb moskyb force-pushed the refactor-agent-integration-test-api branch 2 times, most recently from d381589 to 890da64 Compare May 2, 2024 23:36
@moskyb moskyb enabled auto-merge May 2, 2024 23:49
@moskyb moskyb merged commit 706bf27 into main May 2, 2024
1 check passed
@moskyb moskyb deleted the refactor-agent-integration-test-api branch May 2, 2024 23:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants