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

feat(testing): run tests in modified packages only #4214

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

muncus
Copy link
Collaborator

@muncus muncus commented Jun 25, 2024

current testing logic also ensures modules under these packages are
also tested.

current testing logic also ensures modules *under* these packages are
also tested.
@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Jun 25, 2024
@muncus muncus marked this pull request as ready for review June 27, 2024 17:21
@muncus muncus requested a review from a team as a code owner June 27, 2024 17:21
@muncus muncus added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 27, 2024
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 27, 2024
GO_CHANGED_MODULES=${GO_CHANGED_MODULES:-./go.mod}
# Exclude the root module, if present, from the list of sub-modules.
GO_CHANGED_SUBMODULES=${GO_CHANGED_MODULES#./go.mod}
GO_CHANGED_MODULES="$(find ${GO_CHANGED_PKGS:-.} -name go.mod | xargs --no-run-if-empty dirname)"
Copy link
Contributor

Choose a reason for hiding this comment

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

I tried to execute the steps but encountered these problems:

  • GO_CHANGED_PKGS would contain a list of go files, not a list of folders
  • GO_CHANGED_MODULES does not contain the modified modules

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe dirname could help:

$ dirname vertexai/chat/chat.go
vertexai/chat

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for taking a look at this!

I've added dirname to this pipeline, and to make future changes easier, i've allowed us to set $GIT_CHANGES locally to enable better testability here.

So running GIT_CHANGES="functions/functionsv2/firebase/hellofirestore/go.mod compute/instances/foo.go" sh testing/kokoro/system_tests.sh will show (among other things): GO_CHANGED_PKGS='compute/instances functions/functionsv2/firebase/hellofirestore'

the script stops when it tries to read the nonexistant kokoro credentials, but this is still an improvement.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!
Now the value of GO_CHANGED_MODULES looks good, but I can't see it used anywhere in the script?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's correct. the module-based approach was never fully implemented, so these variables are informational only.

@muncus muncus requested a review from Deleplace July 2, 2024 22:17
Copy link
Contributor

@grayside grayside left a comment

Choose a reason for hiding this comment

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

Overall this looks good to me. I wasn't able to get some of the specific script elements working locally on Mac but it makes sense.

# CHANGED_DIRS is the list of significant top-level directories that changed,
# but weren't deleted by the current PR.
# CHANGED_DIRS will be empty when run on main.
CHANGED_DIRS=$(echo "$SIGNIFICANT_CHANGES" | tr ' ' '\n' | grep "/" | cut -d/ -f1 | sort -u | tr '\n' ' ' | xargs --no-run-if-empty ls -d 2>/dev/null || true)
GO_CHANGED_PKGS=$(echo "$SIGNIFICANT_CHANGES" | tr ' ' '\n' | grep "/" | tr '\n' ' ' | xargs --no-run-if-empty dirname | xargs --no-run-if-empty ls -d 2>/dev/null || true)
Copy link
Contributor

Choose a reason for hiding this comment

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

question: Not recognizing the logic indicating these directories are necessarily Go packages. What am I missing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A Package is just a directory that contains go code. a Module is a grouping of packages (see https://go.dev/ref/mod#modules-overview).

We are not looking for the module that contains the modified packages, we are just testing the modified packages themselves. We believe this is safe due to the rarity of inter-package dependencies, and the use of exhaustive nightly testing.

if we do experience breakage, we can move toward a "find the containing module" approach, but that would be more complex.

Copy link
Contributor

@Deleplace Deleplace left a comment

Choose a reason for hiding this comment

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

Marc, I tested the modified changes in bash (macOS) and now I can confirm the env var values look good 👍

Before LGTM, I'd want to understand if GO_CHANGED_MODULES is supposed to be used at all?

@@ -44,25 +44,27 @@ export GOLANG_SAMPLES_E2E_TEST=""
# allow files to be owned by a different user than our current uid.
# Kokoro runs a double-nested container, and UIDs may not match.
git config --global --add safe.directory $(pwd)
GIT_CHANGES=$(git --no-pager diff --name-only main..HEAD)
# Allow $GIT_CHANGES to be set in the env, enabling local testing of the change detection below.
GIT_CHANGES=${GIT_CHANGES:-$(git --no-pager diff --name-only main..HEAD)}
Copy link
Contributor

Choose a reason for hiding this comment

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

The git changes between HEAD and main includes the current changes, but they also includes any very recent changes of main, unrelated to the current changes. Is this intentional?

Copy link
Collaborator Author

@muncus muncus Jul 9, 2024

Choose a reason for hiding this comment

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

main here is using the current status of the main branch in your checkout - which is likely missing some of the newest changes to origin/main. you can make those changes disappear by doing a git pull on your main branch, then switching back to the PR branch (and ensuring the PR branch is up to date with main).

This is not a problem in CI, where we're using a fresh clone of the repo.

We may be able to address this by using origin/main instead of main, but that's probably unnecessary for our usage, and i'd like to minimize the behavior change in this PR. If you think this is an important change, you're welcome to file an issue for it.

if [[ -z $GIT_CHANGES && $KOKORO_JOB_NAME != *"system-tests"* ]]; then
echo "No diffs detected. This is unexpected - check above for additional error messages."
exit 2
fi
SIGNIFICANT_CHANGES=$(git --no-pager diff --name-only main..HEAD | grep -Ev '(\.md$|^\.github)' || true )
SIGNIFICANT_CHANGES=$(echo $GIT_CHANGES | grep -Ev '(\.md$|^\.github)' || true )
# CHANGED_DIRS is the list of significant top-level directories that changed,
# but weren't deleted by the current PR.
# CHANGED_DIRS will be empty when run on main.
CHANGED_DIRS=$(echo "$SIGNIFICANT_CHANGES" | tr ' ' '\n' | grep "/" | cut -d/ -f1 | sort -u | tr '\n' ' ' | xargs --no-run-if-empty ls -d 2>/dev/null || true)
Copy link
Contributor

Choose a reason for hiding this comment

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

CHANGED_DIRS includes new dirs added in the current change, but not the new dirs very recently added in main. Is this intentional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't understand how that's possible, since the git command is the same, and i was not able to reproduce this behavior.
The presence of non-PR changes is dependent on the git clone's current state- could that have changed for you between these commands?

GO_CHANGED_MODULES=${GO_CHANGED_MODULES:-./go.mod}
# Exclude the root module, if present, from the list of sub-modules.
GO_CHANGED_SUBMODULES=${GO_CHANGED_MODULES#./go.mod}
GO_CHANGED_MODULES="$(find ${GO_CHANGED_PKGS:-.} -name go.mod | xargs --no-run-if-empty dirname)"
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!
Now the value of GO_CHANGED_MODULES looks good, but I can't see it used anywhere in the script?

@muncus
Copy link
Collaborator Author

muncus commented Jul 15, 2024

@grayside, @Deleplace Ping. This PR has been awaiting re-review for a week. Could one of you take a look so we can get this speed improvement delivered?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants