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: Replace t.fatalf with testify require #16038

Merged
merged 9 commits into from
Jun 14, 2024
Merged

test: Replace t.fatalf with testify require #16038

merged 9 commits into from
Jun 14, 2024

Conversation

Ari1009
Copy link
Contributor

@Ari1009 Ari1009 commented Jun 2, 2024

Description

This PR replaces some t.fatalf instances with testify's require

Related Issue(s)

#15182

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Signed-off-by: Ari1009 <arihant0pal@gmail.com>
Copy link
Contributor

vitess-bot bot commented Jun 2, 2024

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Jun 2, 2024
@github-actions github-actions bot added this to the v20.0.0 milestone Jun 2, 2024
Copy link
Contributor

@shlomi-noach shlomi-noach left a comment

Choose a reason for hiding this comment

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

Thank you for this cleanup PR! While we're doing this, let's do it in the most idiomatic way. Please see inline comments for suggestions and issues!

if err != ErrServerEOF {
t.Errorf("unexpected error: %v", err)
}
require.Equal(t, ErrServerEOF, err, "unexpected error: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we use EqualError?

if applyIdx == 0 && cloneIdx == 0 {
t.Fatalf("file doesn't contain expected contents")
}
assert.False(t, applyIdx == 0 && cloneIdx == 0, "file doesn't contain expected contents")
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like a redaction of severity from Fatalf to assert. Should this not be at least require?

for i, fbt := range want {
t.Errorf("Want(%v)=%v", i, fbt.statements)
}
assert.True(t, reflect.DeepEqual(got, want), "binlogConnStreamer.parseEvents(): got:\n%+v\nwant:\n%+v", got, want)
Copy link
Contributor

Choose a reason for hiding this comment

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

assert.Equal uses reflect.DeepEqual under the hood. We should just use assert.Equal(t, want, got).

Comment on lines 528 to 529
for i, fbt := range got {
assert.Equal(t, fbt.statements, want[i].statements, "Got (%v)=%v, want (%v)=%v", i, fbt.statements, i, want[i].statements)
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems superfluous given we've validated the equality of the entire slice, above?

if want != got {
t.Errorf("want\n%s, got\n%s", want, got)
}
assert.Equal(t, want, got, "binlogConnStreamer.tablesFilterFunc(): got:\n%+v\nwant:\n%+v", got, want)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should remove the got:\n%+v\nwant:\n%+v output, since testify prints the same kind of output anyway.

Comment on lines 125 to 126
require.Error(t, err, "DeleteTablet on primary: want error")
require.Contains(t, err.Error(), wantError, "DeleteTablet on primary: want specific error message")
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's use a single ErrorContains test instead of the two tests above.

if !testCase.result.Equal(result) {
t.Errorf("mismatched result:\nwant: %v\ngot: %v", testCase.result, result)
}
assert.True(t, testCase.result.Equal(result), "mismatched result:\nwant: %v\ngot: %v", testCase.result, result)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should remove the got:\n%+v\nwant:\n%+v output, since testify prints the same kind of output anyway.

if !strings.Contains(err.Error(), testCase.errorString) {
t.Fatalf("Wrong error, want %s, got %s", testCase.errorString, err.Error())
}
assert.Contains(t, err.Error(), testCase.errorString, "Wrong error, want %s, got %s", testCase.errorString, err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's use ErrorContains() instead.

}
zkObservedConf, err := MakeZooCfg([]string{zkConf.ConfigFile()}, zkConf, "header")
require.NoError(t, err, "MakeZooCfg err: %v", err)
assert.Contains(t, zkObservedConf, fmt.Sprintf("\n%s\n", tpcKeepAliveCfg), "Expected tpcKeepAliveCfg in zkObservedConf")
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be require given the original Fatalf?

zkObservedConf, err := MakeZooCfg([]string{zkConf.ConfigFile()}, zkConf, "header")
require.NoError(t, err, "MakeZooCfg err: %v", err)
assert.Contains(t, zkObservedConf, fmt.Sprintf("\n%s\n", tpcKeepAliveCfg), "Expected tpcKeepAliveCfg in zkObservedConf")
assert.Contains(t, zkObservedConf, fmt.Sprintf("\n%s\n", adminServerCfg), "Expected adminServerCfg in zkObservedConf")
Copy link
Contributor

Choose a reason for hiding this comment

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

should be require?

@shlomi-noach shlomi-noach added Component: Build/CI Type: Testing and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Jun 2, 2024
Signed-off-by: Ari1009 <arihant0pal@gmail.com>
@Ari1009
Copy link
Contributor Author

Ari1009 commented Jun 3, 2024

Thanks, @shlomi-noach, for the review. I have made the necessary changes. Could you please check it out?

Signed-off-by: Ari1009 <arihant0pal@gmail.com>
Copy link

codecov bot commented Jun 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 68.23%. Comparing base (b49494e) to head (ccdc5f1).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #16038   +/-   ##
=======================================
  Coverage   68.23%   68.23%           
=======================================
  Files        1543     1543           
  Lines      197555   197555           
=======================================
+ Hits       134796   134808   +12     
+ Misses      62759    62747   -12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@shlomi-noach
Copy link
Contributor

Static code checks fails on:

Run out=$(goimports -local vitess.io/vitess -l -w $(find . -name "*.go" | grep -v ".pb.go"))
The following files are malformatted:
./go/vt/wrangler/tablet_test.go
./go/vt/binlog/binlog_streamer_rbr_test.go
./go/vt/binlog/tables_filter_test.go

Can you please format those files via goimports?

Copy link
Contributor

@shlomi-noach shlomi-noach left a comment

Choose a reason for hiding this comment

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

A couple more changes needed, as well as formatting. Thank you!

}

if tracer.name != serviceName {
t.Fatalf("expected the name to be `%v` but it was `%v`", serviceName, tracer.name)
require.FailNow(t, fmt.Sprintf("expected the name to be `%v` but it was `%v`", serviceName, tracer.name))
Copy link
Contributor

Choose a reason for hiding this comment

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

Shall we replace with require.Equal(t, serviceName, tracer.name)?

@@ -62,11 +62,11 @@ func TestRegisterService(t *testing.T) {
closer := StartTracing(serviceName)
tracer, ok := closer.(*fakeTracer)
if !ok {
t.Fatalf("did not get the expected tracer, got %+v (%T)", tracer, tracer)
require.FailNow(t, fmt.Sprintf("did not get the expected tracer, got %+v (%T)", tracer, tracer))
Copy link
Contributor

Choose a reason for hiding this comment

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

shall we replace this if statement with require.True(t, ok, ...)?

go/vt/binlog/binlog_streamer_rbr_test.go Show resolved Hide resolved
}
}
assert.EqualError(t, err, ErrServerEOF.Error(), "unexpected error")
assert.Equal(t, want, got, "binlogConnStreamer.parseEvents(): got:\n%+v\nwant:\n%+v", got, want)
Copy link
Contributor

Choose a reason for hiding this comment

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

let's get rid of got:\n%+v\nwant:\n%+v

}
}
assert.EqualError(t, err, ErrServerEOF.Error(), "unexpected error")
assert.Equal(t, want, got, "binlogConnStreamer.parseEvents(): got:\n%+v\nwant:\n%+v", got, want)
Copy link
Contributor

Choose a reason for hiding this comment

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

let's get rid of got:\n%+v\nwant:\n%+v

Signed-off-by: Ari1009 <arihant0pal@gmail.com>
@Ari1009
Copy link
Contributor Author

Ari1009 commented Jun 3, 2024

@shlomi-noach made changes. Thanks for the quick updates.

go/tools/asthelpergen/asthelpergen_test.go Outdated Show resolved Hide resolved
go/trace/trace_test.go Outdated Show resolved Hide resolved
go/vt/wrangler/tablet_test.go Outdated Show resolved Hide resolved
go/vt/zkctl/zkctl_test.go Outdated Show resolved Hide resolved
go/vt/zkctl/zkctl_test.go Outdated Show resolved Hide resolved
Signed-off-by: Ari1009 <arihant0pal@gmail.com>
@Ari1009
Copy link
Contributor Author

Ari1009 commented Jun 4, 2024

@dbussink , Just made those changes you mentioned. ‍ Sorry about the weird formatting with the license. Seems an extension I had messed things up when saving. My bad!

@dbussink
Copy link
Contributor

dbussink commented Jun 4, 2024

@dbussink , Just made those changes you mentioned. ‍ Sorry about the weird formatting with the license. Seems an extension I had messed things up when saving. My bad!

Commented, but there's many other cases of require.NoError(t, err) that can be cleaned up and don't really need any context.

Signed-off-by: Ari1009 <arihant0pal@gmail.com>
Signed-off-by: Ari1009 <arihant0pal@gmail.com>
@Ari1009
Copy link
Contributor Author

Ari1009 commented Jun 4, 2024

@dbussink, I removed strings that didn't seem to have any relevant context. Are there any other strings you think might be unnecessary?
There is:
GetTablet failed
DeleteTablet failed
UpdateShardFields failed
DeleteShard failed

t.Fatalf("DeleteTablet failed: %v", err)
}
err = wr.DeleteTablet(context.Background(), tablet.Alias, true)
require.NoError(t, err, "DeleteTablet failed")
Copy link
Contributor

Choose a reason for hiding this comment

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

@Ari1009 I think you can remove this one.

go/vt/wrangler/tablet_test.go Outdated Show resolved Hide resolved
t.Fatalf("UpdateShardFields failed: %v", err)
}
})
require.NoError(t, err, "UpdateShardFields failed")
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for the last string here either.

t.Fatalf("UpdateShardFields failed: %v", err)
}
})
require.NoError(t, err, "UpdateShardFields failed")
Copy link
Contributor

Choose a reason for hiding this comment

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

Here as well.

t.Fatalf("DeleteTablet failed: %v", err)
}
err = wr.DeleteTablet(context.Background(), tablet1.Alias, false)
require.NoError(t, err, "DeleteTablet failed")
Copy link
Contributor

Choose a reason for hiding this comment

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

Here too.

t.Fatalf("Expected adminServerCfg in zkObservedConf")
}
zkObservedConf, err := MakeZooCfg([]string{zkConf.ConfigFile()}, zkConf, "header")
require.NoError(t, err, "MakeZooCfg err: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't seem useful to print the same error again.

t.Fatalf("Init() err: %v", err)
}
err = zkd.Init()
require.NoError(t, err, "Init() err: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, not very useful to print the same error again.

t.Fatalf("Shutdown() err: %v", err)
}
err = zkd.Shutdown()
require.NoError(t, err, "Shutdown() err: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

No need here either.

t.Fatalf("Teardown() err: %v", err)
}
err = zkd.Start()
require.NoError(t, err, "Start() err: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

No need here either.


err = zkd.Teardown()
require.NoError(t, err, "Teardown() err: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

No need here either.

@dbussink
Copy link
Contributor

dbussink commented Jun 4, 2024

@dbussink, I removed strings that didn't seem to have any relevant context. Are there any other strings you think might be unnecessary?

I don't think it's needed anywhere, except if other / additional information is provided beyond the actual error. But I don't think that's anywhere here, so we don't really need it. Just a plain string isn't that useful either, it already points at the line where it fails so it's obvious then which operation failed, no need to repeat that in the string.

Signed-off-by: Ari1009 <arihant0pal@gmail.com>
@Ari1009
Copy link
Contributor Author

Ari1009 commented Jun 5, 2024

@dbussink, I have completed the necessary cleanups.

@systay systay merged commit 9d63ab4 into vitessio:main Jun 14, 2024
95 checks passed
@systay
Copy link
Collaborator

systay commented Jun 14, 2024

Thank you for your contribution, @Ari1009!

@Ari1009 Ari1009 deleted the fix branch June 29, 2024 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants