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

roachtest: UT for test selection #130079

Merged
merged 1 commit into from
Sep 24, 2024

Conversation

nameisbhaskar
Copy link
Contributor

@nameisbhaskar nameisbhaskar commented Sep 4, 2024

This PR has the unit tests for the test selection changes. The tests the selector and the main code. This does not validate the snowflake query, but, simulates the response for other tests.

With this change, we have a coverage of 96% for the test selection.

Fixes: #129786
Epic: None

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@nameisbhaskar nameisbhaskar force-pushed the bhaskar-ut-test-selection branch 4 times, most recently from 2c9a239 to e26b555 Compare September 5, 2024 15:53
@nameisbhaskar nameisbhaskar marked this pull request as ready for review September 6, 2024 05:56
@nameisbhaskar nameisbhaskar requested a review from a team as a code owner September 6, 2024 05:56
@nameisbhaskar nameisbhaskar requested review from herkolategan and DarrylWong and removed request for a team September 6, 2024 05:56
@nameisbhaskar nameisbhaskar force-pushed the bhaskar-ut-test-selection branch 2 times, most recently from b3e692e to 1c05103 Compare September 9, 2024 05:59
@nameisbhaskar nameisbhaskar marked this pull request as draft September 9, 2024 13:23
Copy link
Contributor

@DarrylWong DarrylWong left a comment

Choose a reason for hiding this comment

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

I haven't taken a deep look at this PR yet, but wrt #130330, it's good that it caught that bug, but the fact it was just an occasional flake makes me think we should add a bit more. We should have all the parts to do a full simulation (in addition to these hard coded edge cases).

i.e. generate some random starting state, run test selection, randomly pass/fail/preempt some tests, mock writing back the data, then repeat over N days. You can then check invariants at each step, i.e. preempted test is selected the next day, all tests have been run in last 4 days, etc.

Wdyt? Could also be a followup, since it might be a non trivial amount of code.

@nameisbhaskar
Copy link
Contributor Author

I haven't taken a deep look at this PR yet, but wrt #130330, it's good that it caught that bug, but the fact it was just an occasional flake makes me think we should add a bit more. We should have all the parts to do a full simulation (in addition to these hard coded edge cases).

i.e. generate some random starting state, run test selection, randomly pass/fail/preempt some tests, mock writing back the data, then repeat over N days. You can then check invariants at each step, i.e. preempted test is selected the next day, all tests have been run in last 4 days, etc.

Wdyt? Could also be a followup, since it might be a non trivial amount of code.

The current tests covers all known scenarios. But, randomised test is a good idea to generate Fuzzy kind of test cases - I will check how this can be achieved.

@nameisbhaskar
Copy link
Contributor Author

nameisbhaskar commented Sep 10, 2024

@DarrylWong randomised test is making it complex for the assertion, but it should be doable. But, I am thinking of raising another PR later for that. Please do let me know if that sound ok to you.

pkg/cmd/roachtest/main_test.go Outdated Show resolved Hide resolved
pkg/cmd/roachtest/main_test.go Outdated Show resolved Hide resolved
testselector.SqlConnectorFunc = func(_, _ string) (*gosql.DB, error) {
return db, err
}
t.Run("expect CategoriseTests to fail", func(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.

From my understanding we aren't testing to see if CategoriseTests will fail, but rather that if it does we should expect the test selector to fall back to all tests selected. Could we make that more clear through the test name + comments?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will change the test name to "expect CategoriseTests to fail which causes fall back to run all tests". Also, will add comment.

for _, s := range specs {
s.Suites = registry.Suites(registry.Nightly, registry.Weekly)
}
data := [][]string{
Copy link
Contributor

Choose a reason for hiding this comment

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

For those less familiar with the snowflake query, can you leave a comment explaining what the four fields are below?

// all tests which contains "_randomized" are set in spec as Randomized=true
func getMockData() ([]registry.TestSpec, *sqlmock.Rows) {
specs := []registry.TestSpec{
{Name: "t2_selected"},
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: what's the naming convention of these test names? 😅 Some have a number after t, some have it at the end of the name, some don't have a number, some are out of order. If there's a reason to not make it standardized lets add a comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I will add a comments. The entries have been manually shuffled around and the some names were added later, so, is the mismatch in names.

pkg/cmd/roachtest/main_test.go Outdated Show resolved Hide resolved
// all tests which contains "new_test" are present in specs, but missing in snowflake rows
// all tests which contains "_randomized" are set in spec as Randomized=true
func getMockData() ([]registry.TestSpec, *sqlmock.Rows) {
specs := []registry.TestSpec{
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we leave comments explaining what case we are actually testing for? i.e. for t_new_test_3 we expect that it's still selected as there's no data in snowflake even though it is not part of the 30% selection rate.

@nameisbhaskar
Copy link
Contributor Author

Thanks @DarrylWong for your review. I have addressed the comments. Please do take a look.

Copy link
Contributor

@DarrylWong DarrylWong left a comment

Choose a reason for hiding this comment

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

This mostly looks good to me. My remaining comments are mostly just noting that some tests seem redundant to me.

t.Run("expect Prepare to fail", func(t *testing.T) {
db, mock, err = sqlmock.New()
require.Nil(t, err)
mock.ExpectPrepare(regexp.QuoteMeta(PreparedQuery)).WillReturnError(fmt.Errorf("failed to prepare"))
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 like it's testing the sql mock more than it is the CategoriseTests. No harm in keeping it but I feel like expect CategoriseTests to fail which causes fall back to run all tests test above already covers this case.

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 is to test that we are returning the error correctly from the code.

pkg/cmd/roachtest/testselector/selector_test.go Outdated Show resolved Hide resolved
require.Equal(t, defaultLastRunOn, req.LastRunOn)
}

func Test_getSFCreds(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.

Similar comment to above, these tests seem to be already covered by TestCategoriseTests? Not sure how much extra coverage we get.

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 is for the coverage of all error scenarios and ensure that we are returning the errors.

pkg/cmd/roachtest/main_test.go Outdated Show resolved Hide resolved
@nameisbhaskar
Copy link
Contributor Author

Thanks @DarrylWong ! I have replied to your comments. Please do let me know if it sounds good.

This PR has the unit tests for the test selection
changes. The tests the selector and the main code.
This does not validate the snowflake query, but,
simulates the response for other tests.

Fixes: cockroachdb#129786
Epic: None
@nameisbhaskar
Copy link
Contributor Author

Thanks @DarrylWong for reviewing this.

@nameisbhaskar
Copy link
Contributor Author

bors r=@DarrylWong

@craig craig bot merged commit a2bbbd6 into cockroachdb:master Sep 24, 2024
23 checks passed
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.

Unit Test for roachtest test selector
3 participants