Skip to content

Improve error handling in Connect gateway integration test#19145

Merged
ravicious merged 3 commits intomasterfrom
ravicious/proxy-teleterm-err
Dec 15, 2022
Merged

Improve error handling in Connect gateway integration test#19145
ravicious merged 3 commits intomasterfrom
ravicious/proxy-teleterm-err

Conversation

@ravicious
Copy link
Copy Markdown
Member

A few small things that I missed when I added this test in #17950.

mockTSHDEventsClient will most likely be called outside of the test goroutine, so we shouldn't use require.NoError there.

helpers.SetupUserCreds returns an error which I did not inspect. The linter didn't warn me about it for some reason.

mockTSHDEventsClient will most likely be called outside of the test
goroutine, so we shouldn't use require.NoError there.

helpers.SetupUserCreds returns an error which I did not inspect. The linter
didn't warn me about it for some reason.
Copy link
Copy Markdown
Contributor

@espadolini espadolini left a comment

Choose a reason for hiding this comment

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

Is this better than using assert.NoError instead?

@ravicious
Copy link
Copy Markdown
Member Author

I suppose assert.NoError would be better at communicating where exactly the failure has happened.

If I understand correctly, assert.NoError continues the execution if the error is present but the test will ultimately fail. Does this mean that the implementation of Relogin should take the return value of assert.NoError into account?

That is, I'm not sure if I should do this:

	err = helpers.SetupUserCreds(c.tc, c.pack.Root.Cluster.Config.Proxy.SSHAddr.Addr, *creds)
	assert.NoError(c.t, err)

	return &api.ReloginResponse{}, nil

or

	err = helpers.SetupUserCreds(c.tc, c.pack.Root.Cluster.Config.Proxy.SSHAddr.Addr, *creds)
	if !assert.NoError(c.t, err) {
		return nil, trace.Wrap(err)
	}

	return &api.ReloginResponse{}, nil

From what I see, in the project we seem to use the first approach.

@ravicious
Copy link
Copy Markdown
Member Author

This also reminds me that asserting something without failing the test immediately would also be helpful when checking the number of calls to the mock. I had a test that was failing on this check:

client, err = mysql.MakeTestClientWithoutTLS(
net.JoinHostPort(gateway.LocalAddress(), gateway.LocalPort()),
gateway.RouteToDatabase())
require.NoError(t, err)

but if these two were performed as assertions before that require.NoError, it'd have made debugging much easier:

require.Equal(t, 1, tshdEventsClient.callCounts["Relogin"],
"Unexpected number of calls to TSHDEventsClient.Relogin")
require.Equal(t, 0, tshdEventsClient.callCounts["SendNotification"],
"Unexpected number of calls to TSHDEventsClient.SendNotification")

Would that be an okay approach or would you rather say it's misusing the assert package?

@ravicious
Copy link
Copy Markdown
Member Author

@espadolini Could you offer an advice or an opinion on using assert.NoError inside a mocked function vs returning an error from that function? #19145 (comment)

@espadolini
Copy link
Copy Markdown
Contributor

I don't know if mocks should have access to the *testing.T, that seems kind of an overreach. The testing environment should set up and control the mock instead, so I think the change here is good.

@ravicious ravicious enabled auto-merge (squash) December 14, 2022 16:49
@ravicious ravicious merged commit 519c9aa into master Dec 15, 2022
@github-actions
Copy link
Copy Markdown
Contributor

@ravicious See the table below for backport results.

Branch Result
branch/v11 Create PR

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.

4 participants