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

Fail Stream Replicator on startup if source or target isn't reachable #183

Closed
wants to merge 3 commits into from

Conversation

TiganeteaRobert
Copy link
Contributor

@TiganeteaRobert TiganeteaRobert commented Aug 4, 2022

Added connection checks to:

  • PubSub source (also added check for existing subscription in project)
  • PubSub target (also added check for existing topic in project)
  • Eventhub target
  • HTTP target (added HEAD request on start-up to check for a connection to the host)

Tested the rest of the sources and targets and they are checked on start-up.

@TiganeteaRobert TiganeteaRobert force-pushed the startup-fail branch 2 times, most recently from cc541a4 to e5e7c65 Compare August 4, 2022 13:17
config/config.go Outdated Show resolved Hide resolved
@TiganeteaRobert TiganeteaRobert requested review from colmsnowplow and adatzer and removed request for colmsnowplow August 4, 2022 13:31
@TiganeteaRobert TiganeteaRobert marked this pull request as ready for review August 4, 2022 13:31
@colmsnowplow colmsnowplow changed the base branch from release/1.0.0 to develop August 11, 2022 12:29
@colmsnowplow
Copy link
Collaborator

There are tests commented out for most of the sources and targets, which were written as a means of documenting the requirement.

Even where we don't have any code changes, we should uncomment those tests to ensure that for every source and target behaves as we expect.

If the pre-existing tests themselves require changes that's fine - but let's document why. Ofc it's always fine to add more tests too.

@TiganeteaRobert
Copy link
Contributor Author

@colmsnowplow I have went through all sources and targets and uncommented and fixed those tests.

@@ -395,19 +395,3 @@ func TestNewEventHubTarget_ConnVarsErr(t *testing.T) {
assert.EqualError(err, `Error initialising EventHub client: could not reach Event Hub: dial tcp: lookup test.servicebus.windows.net: no such host`)
assert.Nil(tgt)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test was removed as the same behaviour is tested in the unit test above it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It doesn't look like the same behaviour to me...

There are two ways to authenticate against EH - connection string or key value. In the scenario where no valid combination of the required env vars for one of the two methods is present, the client doesn't provide a good debugging experience - so we manually check this in the code. The existing unit tests are for that check.

If the correct env vars are provided, however, we can still fail to connect to the client - it might be unavailable, the credentials provided might be incorrect, etc. So another test should be needed to check this.

Copy link
Contributor Author

@TiganeteaRobert TiganeteaRobert Aug 22, 2022

Choose a reason for hiding this comment

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

I’m having a hard time understanding the issue here, it seems to me that the test that I deleted was not even correct after all. It sets env vars for EVENTHUB_KEY_NAME and EVENTHUB_KEY_VALUE and expects a No valid combination of authentication Env vars found error, which would not be the case. That error would appear if no env vars have been set, either for the connection string or the eventhub key. This possibility is tested above in TestNewEventHubTarget_CredentialsNotFound.

I might be missing something here so please if you could provide a bit more context on this, or even book a quick chat about it it would be really helpful.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Taking a second look, you're right, the different possible failures are indeed accounted for. I think I just looked in the wrong place and confused myself - these tests are indeed fine.

@colmsnowplow
Copy link
Collaborator

colmsnowplow commented Aug 19, 2022

v1 has been merged, develop branch and this branch will both need to be rebased

@@ -77,7 +76,6 @@ func TestNewPubSubSource_Failure(t *testing.T) {
assert.Nil(pubsubSource)
// This should return an error when we can't connect, rather than proceeding to the Write() function before we hit a problem.
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit - would be nice to check for the error message as you have done for other sources

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.

@@ -61,10 +61,12 @@ func TestNewSQSSourceWithInterfaces_Failure(t *testing.T) {

source, err := newSQSSourceWithInterfaces(client, "00000000000", 10, testutil.AWSLocalstackRegion, "nonexistent-queue")

assert.Nil(source)
assert.NotNil(err)
assert.NotNil(source)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the rationale for doing 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.

As newSqsSourceWithInterfaces just returns a new sqsSource regardless of the parameters it gets and never errors, the assertion we had there was wrong.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right but the issue this PR is for is to establish behaviour where functions like this one fail when we can't connect. The test is meant to illustrate that desired behaviour - so the test should stay as it was and the behaviour of the function should change in this PR

assert.Nil(err)

err = source.Read(nil)
assert.True(strings.HasPrefix(err.Error(), `Failed to get SQS queue URL`))
Copy link
Collaborator

Choose a reason for hiding this comment

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

If err is nil this will panic - see the rest of the tests in the project on (on master at least) for pattern that avoids 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.

Fixed.

assert.Equal("Hello Server!!", string(result))
}

assert.Equal(int64(10), ackOps)
}

func TestHttpWrite_Failure(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This test was intended for a separate case to connection error - providing a bad path was just an easy way to achieve it.

It's to test the behaviour of stream replicator when requests fail.

Rather than removing a test for this case, we should amend the test to fail some other way.

For example, a malformed request body would achieve it. Or, we could add a case to the http server to respond to a given path with a success for the HEAD request, but a failure for all other requests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great idea with the path, I've added an exception in the http client to look for the /error path and return a 500 error if the request does not have the HEAD method. This allows us to test that the HEAD connection test can be successful but other requests can fail and test the behaviour.

assert.NotNil(err)
pubsubTarget, err := newPubSubTarget(`project-test`, `test-topic-wrong`)

assert.EqualError(err, `Connection to PubSub failed, topic does not exist`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Damn, wish I knew about EqualError before I spent ages wrapping stuff across the project in if statements 🤦

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