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

test: add end-to-end tests using fake upstreams #67

Merged
merged 12 commits into from
Oct 4, 2022

Conversation

IvanVergiliev
Copy link
Contributor

Description

This PR adds basic support for running end-to-end tests that cover the whole routing. It uses in-process servers from the net/http/httptest package to bring up fake upstreams, and returns legitimately-looking responses to health check requests. Each test can then configure additional handlers for other RPC methods and check that they are routed as expected.

Notable changes:

  1. I added IsInitialized methods to Router and HealthCheckManager. I needed a way to confirm that the gateway is fully up and running - in particular, that at least one iteration of the health checks has already executed. Without this, we can't be sure if the behaviour we're testing actually respects the health checks.
  2. I changed the /health endpoint to reflect the above. I thought I was going to need that for the test, but I realized I can directly poll on the router instead. However, this still seemed like the correct thing to do - for example, if I'm doing a blue-green deploy, I want to make sure that my newly deployed gateway instance is fully up and running before I redirect traffic to it. The previous health check didn't support this.
  3. Added two test cases for starters - one healthy node; and full vs archive depending on method. The first one is a nice sanity check that everything works, and is also a simple example of how to get started writing new e2e tests. The second one tests the behaviour I added most recently.

Type of change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • 😎 New feature (non-breaking change which adds functionality)
  • ⁉️ Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • ⚒️ Refactor (no functional changes)
  • 📖 Documentation (updating or adding docs)
  • 🚨 Tests

How Has This Been Tested?

Added new tests and confirmed they pass (and also confirmed that they break accordingly when I break the underlying behaviour).

@IvanVergiliev
Copy link
Contributor Author

cc @dianwen - these are the end-to-end tests I mentioned. Cross-posting from Slack to make sure they don't get lost in the noise and you have a chance to take a look if you'd like.

Copy link
Member

@brianluong brianluong left a comment

Choose a reason for hiding this comment

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

🥳

defer upstream.Close()

upstreamConfigs := []config.UpstreamConfig{
{ID: "testNode", HTTPURL: upstream.URL},
Copy link
Member

Choose a reason for hiding this comment

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

curious to what these test URLs looks like 👀

Copy link
Member

Choose a reason for hiding this comment

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

it listens to 127.0.0.1:0 by default

TIL: Port 0 is a wildcard port that tells the system to find a suitable port number.

"github.com/stretchr/testify/require"
)

func TestServeHTTP_ForwardsToSoleHealthyUpstream(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

These kind of remind me of a halfway point between unit tests and an integration tests!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, they don't bring up a full set of infrastructure (real ETH nodes, etc.) so they're not 100% realistic, but it's super convenient that they're all written in the same language (i.e. more control, better tooling) and they're also reasonably efficient to run, unlike a full-blown setup.

upstream := setUpHealthyUpstream(t, map[string]func(t *testing.T, w http.ResponseWriter){})
defer upstream.Close()

upstreamConfigs := []config.UpstreamConfig{
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to include one unhealthy node in this config?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added!

@IvanVergiliev IvanVergiliev merged commit 7807e91 into main Oct 4, 2022
@IvanVergiliev IvanVergiliev deleted the end-to-end-tests branch October 4, 2022 07:02
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