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

importccl: Bazelify importccl test #56127

Merged
merged 3 commits into from
Nov 3, 2020
Merged

Conversation

miretskiy
Copy link
Contributor

Make importccl test hermetic by using correct directories
and avoiding test data regeneration.

Use 16 shards for the test to speed it up.

Release Notes: None

@miretskiy miretskiy requested review from irfansharif, adityamaru and a team October 29, 2020 18:31
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@irfansharif irfansharif left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for trying it out Yevgeniy! TIL.

(Irrelevant aside, from https://cockroachlabs.atlassian.net/wiki/spaces/CRDB/pages/73072807/Git+Commit+Messages, for our commit titles I think we generally don't "end the title with a period".)

pkg/ccl/importccl/BUILD.bazel Show resolved Hide resolved

// RunningUnderBazel returns true if the test is executed by bazel.
func RunningUnderBazel() bool {
return os.Getenv(testSrcDirEnv) != ""
Copy link
Contributor

Choose a reason for hiding this comment

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

We've soured on using os.Getenv directly in favor or envutil.

// If testSrcDirEnv is not set, it means we are not running under bazel,
// and so we can use "" as our directory which should point to the
// src root.
return os.Getenv(testSrcDirEnv)
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit] Mild preference for explicitly returning an empty string if not running under bazel. We could just piggy back our newly added RunningUnderBazel helper here. It would look similar to TestTempDir below.

DEPS.bzl Outdated
sum = "h1:KQqSlwJmTr7NmxtVOl6nPNSZQEk8XuLoLyNDYcmniBo=",
version = "v1.0.1-0.20200826112548-92602d883b11",
sum = "h1:p4AOBShzCogHTl1n5Ff16j5a24tvk1lc7fzRqduSpKM=",
version = "v1.0.1-0.20201022032720-3e27adf87325",
Copy link
Contributor

Choose a reason for hiding this comment

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

James will take a look, thanks for picking it up: #56128

pkg/testutils/bazel.go Show resolved Hide resolved
Copy link
Contributor Author

@miretskiy miretskiy left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @adityamaru, @irfansharif, @jlinder, and @miretskiy)


pkg/ccl/importccl/BUILD.bazel, line 110 at r2 (raw file):

Previously, otan (Oliver Tan) wrote…

i think gazelle only modifies some fields -- shard_count is not one of them given i don't see it as MergeableAttrs anywhere

https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/bazelbuild/bazel-gazelle%24+shard_count&patternType=literal

Ahh.. Cool. I'll keep it off, but I'll leave the comment.


pkg/testutils/bazel.go, line 23 at r1 (raw file):

Previously, irfansharif (irfan sharif) wrote…

We've soured on using os.Getenv directly in favor or envutil.

Done.


pkg/testutils/bazel.go, line 45 at r1 (raw file):

Previously, irfansharif (irfan sharif) wrote…

[nit] Mild preference for explicitly returning an empty string if not running under bazel. We could just piggy back our newly added RunningUnderBazel helper here. It would look similar to TestTempDir below.

Done.


pkg/testutils/bazel.go, line 49 at r1 (raw file):

Previously, irfansharif (irfan sharif) wrote…

Am I correct in understanding that for any test running under bazel, we'll eventually want to use TestTempDir instead of just TempDir?

Correct.

Copy link
Contributor Author

@miretskiy miretskiy left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @adityamaru, @irfansharif, @jlinder, @miretskiy, and @otan)


pkg/testutils/bazel.go, line 45 at r1 (raw file):

Previously, miretskiy (Yevgeniy Miretskiy) wrote…

Done.

Sorry... I decided to just use envutil directly... Don't like calling getenv on the same var if it can be helped.

@miretskiy miretskiy force-pushed the bazel branch 2 times, most recently from df625e2 to ad0321a Compare October 30, 2020 13:04
@miretskiy
Copy link
Contributor Author

FYI: I reverted the use of envutil in favor of OS due to failing tests: https://teamcity.cockroachdb.com/buildConfiguration/Cockroach_UnitTests/2405846?
It appears that envutil requries env variables be named with COCKROACH_ prefix. Clearly, that's not the case for bazel.

@miretskiy miretskiy force-pushed the bazel branch 8 times, most recently from a6922ef to 45d13cb Compare November 2, 2020 19:58
@miretskiy
Copy link
Contributor Author

@irfansharif Can you take another look? I had to make some changes to bazel.go
In particular, I was having some issues w/ getting importccl tests to pass; I wound up adding TestDataPath method
to address this issue.

Copy link
Contributor

@irfansharif irfansharif left a comment

Choose a reason for hiding this comment

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

LGTM mod comments below. Thanks for breaking new ground here, I'm hopeful these testutils APIs will see further usage as we start measuring test times of other packages in earnest. +cc @alan-mas.

if v := os.Getenv(env); v != "" {
return v
}
panic("expected value for env " + env)
Copy link
Contributor

Choose a reason for hiding this comment

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

We generally avoid direct string concatenation (citations needed), in favor of something like

panic(errors.AssertionFailedf("missing env var: %s", env))

// TestSrcDir returns the path to the "source" tree.
//
// If running under bazel, this will point to a private, *readonly*
// directory containing symlinks (or copies) of the test data depencies.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/depencies/dependencies

// TestDataPath returns a path to the directory containing test data files.
//
// Test files are usually checked into the repository under "testdata" directory.
// If we are not using bazel, then the test executes with current working directory of
Copy link
Contributor

Choose a reason for hiding this comment

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

"executes in the directory of the actual tests"

// the actual test, so the files can be referenced via "testdata/subdir/file" relative path.
//
// However, if we are running under bazel, the data files are specified
// via go_test "data" attribute. These files, in turn, are available under run files directory.
Copy link
Contributor

Choose a reason for hiding this comment

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

"are available within the runfiles directory" (looks like it's one word: https://docs.bazel.build/versions/master/skylark/lib/runfiles.html)

//
// However, if we are running under bazel, the data files are specified
// via go_test "data" attribute. These files, in turn, are available under run files directory.
// This helper attempts to construct appropriate path to the run files directory
Copy link
Contributor

Choose a reason for hiding this comment

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

`s/run files/runfiles/

func TestDataPath(relative ...string) string {
if RunningUnderBazel() {
pieces := append(bazelRelativeTargetPath(), relative...)
return path.Join(TestSrcDir(), os.Getenv(testWorkspaceEnv), path.Join(pieces...))
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 want to use requireEnv here for testWorkspaceEnv?

Copy link
Contributor

Choose a reason for hiding this comment

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

Feel free to ignore, but a comment reference to https://docs.bazel.build/versions/master/test-encyclopedia.html here would make this all a bit more friendly to future readers.

The initial working directory shall be $TEST_SRCDIR/$TEST_WORKSPACE.

// Strip out leading package "//"
target = strings.TrimPrefix(target, "//")

// Strip out ":XXX" from the last component
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not split on ":", drop the last name, and then split on "/" to get the relative target path?

It would also make it easier to add a comment up top showing the transformation taking place here. If I've understood correctly, it's the following: //pkg/ccl/importccl:importccl_test -> pkg/ccl/importccl (?).

I was actually a bit surprised that this was not something provided out of the box, or at least easier. I assume we've looked at https://docs.bazel.build/versions/master/test-encyclopedia.html#location and found it inapplicable?

//foo/bar:unittest is the directory $(WORKSPACE)/$(BINDIR)/foo/bar/unittest.runfiles

Copy link
Contributor

Choose a reason for hiding this comment

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

Somewhat related, I couldn't find official documentation on it, but https://github.com/bazelbuild/bazel/blob/c266ac966761c4b3d8a408a03e407505c93effdd/tools/bash/runfiles/runfiles.bash#L57 has a reference to RUNFILES_DIR. I'm curious if that would help. Feel free to ignore though.

Yevgeniy Miretskiy added 2 commits November 2, 2020 19:25
Add a bazel testutils file to help tests use correct paths.

Release Notes: None
Make importccl test hermetic by using correct directories
and avoiding test data regeneration.

Use 16 shards for the test to speed it up.

Release Notes: None
@miretskiy miretskiy requested a review from irfansharif November 3, 2020 00:26
Copy link
Contributor Author

@miretskiy miretskiy left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @adityamaru, @irfansharif, @jlinder, @miretskiy, and @otan)


pkg/testutils/bazel.go, line 34 at r4 (raw file):

Previously, irfansharif (irfan sharif) wrote…

We generally avoid direct string concatenation (citations needed), in favor of something like

panic(errors.AssertionFailedf("missing env var: %s", env))

Ty. Done.


pkg/testutils/bazel.go, line 40 at r4 (raw file):

Previously, irfansharif (irfan sharif) wrote…

s/depencies/dependencies

Done.


pkg/testutils/bazel.go, line 74 at r4 (raw file):

Previously, irfansharif (irfan sharif) wrote…

Somewhat related, I couldn't find official documentation on it, but https://github.com/bazelbuild/bazel/blob/c266ac966761c4b3d8a408a03e407505c93effdd/tools/bash/runfiles/runfiles.bash#L57 has a reference to RUNFILES_DIR. I'm curious if that would help. Feel free to ignore though.

Re splitting: You're right -- i'm trying to transform //pkg/cmd/foo:bar -> pkg/cmd/foo.
I was worried about path containing multiple ":", but that would break many things...
So, yeah, thanks for the suggestion -- changed. I actually, don't even need to split on "/".
Just return that as a string ("pkg/ccl/...")

Yeah -- I tried not to use things not mentioned explicitly in the test encyclopedial.
Added large comment block with the above link.


pkg/testutils/bazel.go, line 89 at r4 (raw file):

Previously, irfansharif (irfan sharif) wrote…

"executes in the directory of the actual tests"

Done


pkg/testutils/bazel.go, line 93 at r4 (raw file):

Previously, irfansharif (irfan sharif) wrote…

"are available within the runfiles directory" (looks like it's one word: https://docs.bazel.build/versions/master/skylark/lib/runfiles.html)

Added large comment block on top to introduce the concept of RUNFILES.


pkg/testutils/bazel.go, line 94 at r4 (raw file):

Previously, irfansharif (irfan sharif) wrote…

`s/run files/runfiles/

Done.


pkg/testutils/bazel.go, line 99 at r4 (raw file):

https://docs.bazel.build/versions/master/test-encyclopedia.html
I could have used requireEnv... It's just for whatever reason, WORKSPACE is marked optional.
Though, I suppose, for all intents and purposes it should exist in our environment.
Changed to requireEnv.

Copy link
Contributor

@irfansharif irfansharif left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 5 files at r3, 1 of 4 files at r5.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @adityamaru, @irfansharif, @jlinder, @miretskiy, and @otan)

@miretskiy
Copy link
Contributor Author

bors r+

@craig
Copy link
Contributor

craig bot commented Nov 3, 2020

Build succeeded:

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