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

End-to-end tests for service registration #858

Merged
merged 13 commits into from
May 10, 2022
Merged

End-to-end tests for service registration #858

merged 13 commits into from
May 10, 2022

Conversation

mkam
Copy link
Contributor

@mkam mkam commented May 10, 2022

Adds e2e tests for service registration and various additional Consul testutil methods to verify the registered service and health check.

Sorry for the extensive PR, there were a lot of changes needed to support the various use cases. An overview of the non-test changes are:

Consul testutil additions:

  • Listing all instances for a service and filtering by name (for the case where ID is generated and not known)
  • Retrieving a health check and waiting until it has a certain status
  • Getting a specific service instance by ID
  • Waiting for a service to be registered and deregistered

Config changes:

  • Add options to the basic Consul config
  • Add append method for global id configuration

Module additions:

  • Added a module that fails on runtime (rather than on initialization)

And this wasn't really a part of the tests, but I wanted to make some improvements to the retry logging which I noted while testing the use case where registration fails with a non-retryable error.

  • Fixed the log system name
  • Added more details for logging a non-retryable error
  • Removed this error is not retryable since the user

Closes #860

@mkam mkam requested review from a team and wilkermichael and removed request for a team May 10, 2022 19:03
func TestE2E_ServiceRegistration_RegistrationFailure(t *testing.T) {
// TestE2E_ServiceRegistration_RegistrationSkipped tests scenarios where registration does
// not happen, but CTS continues to run.
func TestE2E_ServiceRegistration_RegistrationSkipped(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The diff for this commit is hard to read, but I basically just put the old test in a for loop where the only difference between the test cases is the service_registration config. From a test perspective, all the setup and validation was the same whether the config disabled the registration or the config caused an error with registration.

return checks
}

func WaitForCheckStatus(tb testing.TB, srv *testutil.TestServer, checkID, status string, wait time.Duration) (Check, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Something I've noted is that we have a lot of WaitFor methods that all have the same structure. I'd like to refactor this in the future so that they all use a common method for polling, kind of in the same way we have a generic retry structure in the CTS code. I'll be making an issue for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opened #859 to address this.

params.Add("filter", filter)
u = fmt.Sprintf("%s?%s", u, params.Encode())
}
resp := RequestHTTP(tb, http.MethodGet, u, "")
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't have to for this PR, but might be useful to wrap this in a retry

Copy link
Contributor

Choose a reason for hiding this comment

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

Could also be useful to add ListServices to the Consul client, and use the API library instead of the raw get query: https://pkg.go.dev/github.com/hashicorp/consul/api#Agent.ServicesWithFilter

Copy link
Contributor

Choose a reason for hiding this comment

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

Might also be useful to call this ConsulListServices since it's testutils.ListServices currently

testutils/consul.go Outdated Show resolved Hide resolved
testutils/consul.go Outdated Show resolved Hide resolved
testutils/consul.go Show resolved Hide resolved
testutils/consul.go Outdated Show resolved Hide resolved
retry/retry.go Outdated Show resolved Hide resolved
@@ -150,6 +151,51 @@ func TestE2E_ServiceRegistration_DeregisterWhenStopped(t *testing.T) {
assert.False(t, registered)
}

// TestE2E_ServiceRegistration_RegistrationFailure tests that if CTS is unable
// to register itself as a service, it will not exit.
func TestE2E_ServiceRegistration_RegistrationFailure(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe include the test case being tested in the name?

TestE2E_ServiceRegistration_ContinueOnRegistrationFailure or something like that

e2e/registration_test.go Show resolved Hide resolved
e2e/registration_test.go Show resolved Hide resolved
e2e/registration_test.go Outdated Show resolved Hide resolved
Also adds comments to the exported methods and structs.
@mkam mkam force-pushed the self-registration-e2e branch from 8a4787a to 7d665c2 Compare May 10, 2022 22:49
@mkam mkam force-pushed the self-registration-e2e branch from 7d665c2 to 357fb07 Compare May 10, 2022 22:50
Copy link
Contributor

@wilkermichael wilkermichael left a comment

Choose a reason for hiding this comment

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

Thanks for addressing everything Melissa!

testutils/consul.go Outdated Show resolved Hide resolved
@mkam mkam force-pushed the self-registration-e2e branch from 357fb07 to 4d7b280 Compare May 10, 2022 23:08
@mkam mkam merged commit b97b917 into main May 10, 2022
@mkam mkam deleted the self-registration-e2e branch May 10, 2022 23:18
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.

Register CTS as a service with a health check to Consul
2 participants