Skip to content

feat: Add DRIFT region tag parser to CI#4506

Merged
tmatsuo merged 28 commits into
masterfrom
drift-parser-2
Aug 26, 2020
Merged

feat: Add DRIFT region tag parser to CI#4506
tmatsuo merged 28 commits into
masterfrom
drift-parser-2

Conversation

@ace-n
Copy link
Copy Markdown

@ace-n ace-n commented Aug 15, 2020

Cleaned-up version of #4402

@jmdobry FYI

@ace-n ace-n requested review from busunkim96 and tmatsuo August 15, 2020 01:42
@ace-n ace-n requested a review from a team as a code owner August 15, 2020 01:42
@google-cla google-cla Bot added the cla: yes This human has signed the Contributor License Agreement. label Aug 15, 2020
@tmatsuo
Copy link
Copy Markdown
Contributor

tmatsuo commented Aug 15, 2020

python-3.7 build has a failure, and the new script is throwing an error:

Command pytest --junitxml=sponge_log.xml failed with exit code -6
Session py-3.7 failed.
cp: cannot stat '/workspace/speech/cloud-client/sponge_log.xml': No such file or directory
cat: /workspace/speech/cloud-client/drift_tmp.xml: No such file or directory
Traceback (most recent call last):
  File "/region-tag-parser/wizard-py/cli.py", line 200, in <module>
    inject_snippet_mapping(args.root_dir, sys.stdin.readlines())
  File "/region-tag-parser/wizard-py/cli.py", line 89, in inject_snippet_mapping
    xunit_tree = etree.fromstring("".join(stdin_lines))
  File "/usr/local/lib/python3.8/xml/etree/ElementTree.py", line 1321, in XML
    return parser.close()
xml.etree.ElementTree.ParseError: no element found: line 1, column 0
Testing failed: Nox returned a non-zero exit code.

Seems like the script needs to be little bit more robust.

Also, since you have changes in the Dockerfile, all the CI builds are full build.
I strongly recommend that you develop the script further locally.

You can emulate the Kokoro full build with the following command:

$ cd python-docs-samples
$ TRAMPOLINE_BUILD_FILE=.kokoro/tests/run_tests.sh .kokoro/trampoline_v2.sh

Full build takes long time, so you may prefer:

$ cd python-docs-samples
$ TRAMPOLINE_BUILD_FILE=.kokoro/tests/run_tests_diff_master.sh .kokoro/trampoline_v2.sh

This command only runs tests for directories that has changes from master. You can change some tests so that run_tests_diff_master.sh will pick up the directory.
Update: Unfortunately, you have a change in Dockerfile, this command will be also full build for you.

You can commit your Dockerfile changes into your local master, then you can deceive the test script.

$ cd python-docs-samples
$ git checkout master
$ git rebase drift-parser-2 # let master have the Dockerfile change
$ git checkout drift-parser-2
# Edit some test files
$ TRAMPOLINE_BUILD_FILE=.kokoro/tests/run_tests_diff_master.sh .kokoro/trampoline_v2.sh

Anyways, full builds on Kokoro consume resources and interfere other PRs, so please be considerable.

Comment thread .kokoro/docker/Dockerfile Outdated
Comment thread .kokoro/docker/Dockerfile Outdated
Comment thread .kokoro/tests/run_single_test.sh Outdated

# Inject region tag data into the test log
XUNIT_PATH="$PWD/sponge_log.xml"
XUNIT_TMP_PATH="$PWD/drift_tmp.xml"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you use temp directory for XUNIT_TMP_PATH?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done for XUNIT_TMP_PATH, though I think XUNIT_PATH needs to stay in $PWD.

Comment thread .kokoro/tests/run_single_test.sh Outdated
Comment thread .kokoro/tests/run_single_test.sh Outdated
Comment thread .kokoro/docker/Dockerfile Outdated
Comment thread .kokoro/tests/run_single_test.sh
Comment thread .kokoro/tests/run_tests.sh Outdated

# install PyYaml (used by the DRIFT region tag parsing system)
pip install --user -q pyyaml

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If we introduce an envvar, can we only install pyyaml if the envvar is set to true? Also I think it is ok to install the parser alongside of pyyaml.

Or even, how about to have requirements.txt in the parser repo and run pip install --user -r ${PARSER_DIR}/requirements.txt or sth?

Copy link
Copy Markdown
Author

@ace-n ace-n Aug 20, 2020

Choose a reason for hiding this comment

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

I got this working by installing pyyaml in a specific directory and adding that to the PYTHONPATH. (I think there are some virtualenv issues here that prevent the usual "pip install and go" approach, and this was the easiest workaround to implement.)

Copy link
Copy Markdown
Contributor

@tmatsuo tmatsuo left a comment

Choose a reason for hiding this comment

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

You need to add INJECT_REGION_TAGS to pass_down_envvars in .trampolinerc

Copy link
Copy Markdown
Contributor

@tmatsuo tmatsuo left a comment

Choose a reason for hiding this comment

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

Code looks good. Maybe we should run py-3.7 periodic build manually against the drift-parser-2 branch before merge. I'll start the build once the presubmit builds finish.

@tmatsuo
Copy link
Copy Markdown
Contributor

tmatsuo commented Aug 19, 2020

I run py37 periodic build against your branch:
https://source.cloud.google.com/results/invocations/34c925d1-5261-466a-826b-6e8bc00c30ad/targets

I think we have little more to fix.

Copy link
Copy Markdown
Contributor

@tmatsuo tmatsuo left a comment

Choose a reason for hiding this comment

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

It's close, but we need little bit more tweak.

Also, can you try this locally and check if the resulted xml files have expected contents?

Comment thread .kokoro/tests/run_single_test.sh Outdated
Comment thread .kokoro/tests/run_single_test.sh Outdated
Comment thread .kokoro/tests/run_tests.sh Outdated
# Setup DRIFT region tag parser
# (only run on *some* builds)
if [ "${INJECT_REGION_TAGS:-}" == "true" ]]; then
# install PyYaml (used by the DRIFT region tag parsing system)
Copy link
Copy Markdown
Contributor

@tmatsuo tmatsuo Aug 19, 2020

Choose a reason for hiding this comment

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

Ditto for missing bracket.

It's not obvious from the log, so can you add info log?

    echo "Downloading region tag parser"

or something?

Also, do you prefer always installing it from HEAD?

If you install with a SHA1 hash or release tag, can you also log the hash or the release tag?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

  1. Fixed missing bracket
  2. Added debug log statements
  3. Yes - installing from HEAD is the intent here (esp. since we don't need to use a commit hash to bust Docker caches!)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok, this raises another question. How is the parser script tested?

Copy link
Copy Markdown
Author

@ace-n ace-n Aug 20, 2020

Choose a reason for hiding this comment

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

The Python script itself is fetched from this repo; there are tests in that folder.

(There aren't any [new] tests for the .sh testing scripts.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you have CI builds setup?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not yet - though we plan to add them in the coming months.

(If you'd prefer to do that immediately after merging this PR though, that can be done.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Well, then the script might be not tested. We'll need extra caution because the script might fail.

Copy link
Copy Markdown
Contributor

@tmatsuo tmatsuo Aug 21, 2020

Choose a reason for hiding this comment

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

Also, the script might empty the xml file. If that happens, we'll lost all the reporting capabilities including build cop bot issues.

I'd like you to setup CI on your repo before merging this.

Also, I don't think we should use redirect (>) here. Because if the script exit 0, but produces no output, then we'll loose all the test logs.

@ace-n
Copy link
Copy Markdown
Author

ace-n commented Aug 19, 2020

Reran the periodic build here

Comment thread .kokoro/tests/run_single_test.sh Outdated
Comment thread .kokoro/tests/run_single_test.sh Outdated
Comment thread .kokoro/tests/run_tests.sh Outdated
# Setup DRIFT region tag parser
# (only run on *some* builds)
if [ "${INJECT_REGION_TAGS:-}" == "true" ]]; then
# install PyYaml (used by the DRIFT region tag parsing system)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok, this raises another question. How is the parser script tested?

@ace-n ace-n requested a review from tmatsuo August 20, 2020 01:21
Comment thread .kokoro/tests/run_single_test.sh Outdated
Comment thread .kokoro/tests/run_single_test.sh Outdated
if [[ -f "$XUNIT_PATH" ]]; then
echo "=== Injecting region tags into XUnit output ==="
echo "Processing XUnit output file: $XUNIT_PATH"
cat "$XUNIT_PATH" | python3.7 "$PARSER_PATH" inject-snippet-mapping "$PWD" > "$XUNIT_TMP_PATH"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What happens when we remove python 3.7 from the docker image?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Would it be better to use python3 here?

Comment thread .kokoro/tests/run_tests.sh Outdated
# Setup DRIFT region tag parser
# (only run on *some* builds)
if [ "${INJECT_REGION_TAGS:-}" == "true" ]]; then
# install PyYaml (used by the DRIFT region tag parsing system)
Copy link
Copy Markdown
Contributor

@tmatsuo tmatsuo Aug 21, 2020

Choose a reason for hiding this comment

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

Also, the script might empty the xml file. If that happens, we'll lost all the reporting capabilities including build cop bot issues.

I'd like you to setup CI on your repo before merging this.

Also, I don't think we should use redirect (>) here. Because if the script exit 0, but produces no output, then we'll loose all the test logs.

@ace-n
Copy link
Copy Markdown
Author

ace-n commented Aug 24, 2020

New periodic build here.

Copy link
Copy Markdown
Contributor

@tmatsuo tmatsuo left a comment

Choose a reason for hiding this comment

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

Also let me know once CI build is set up in the upstream repo.

Comment thread .kokoro/tests/run_single_test.sh Outdated

cat "$XUNIT_PATH" | python3.7 "$PARSER_PATH" inject-snippet-mapping --output_file "$XUNIT_TMP_PATH" "$PWD"
if [[ $? -eq 0 ]]; then
mv $XUNIT_TMP_PATH $XUNIT_PATH
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are you 100% sure that there is a file when the script exit with 0?
If not, please check the file existance.

Also, does it make sense to log something here as well?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

If the file at $XUNIT_TMP_PATH doesn't exist, then the mv command should fail.

IMO, logging something here would be overkill. 🙂

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So if mv fails, the test is marked as failed even if just the injection here failed?
I don't like it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Well, actually, there's no -e flag set in this file. So it may keep running.
I still like the explicit check, but I think I can live with the current code.

Comment thread .kokoro/tests/run_tests.sh Outdated

export REGION_TAG_PARSER_DIR="/tmp/region-tag-parser"
export PARSER_PATH="${REGION_TAG_PARSER_DIR}/wizard-py/cli.py"
export PIP_PATH=/tmp/pyyaml
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why did you need PIP_PATH again?

Did you try:

pip install --user pyyaml -q 

?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

IIRC, I did (and it didn't work).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's the error message?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

If memory serves, it couldn't find the package (so probably something like cannot find module 'yaml')

Copy link
Copy Markdown
Contributor

@tmatsuo tmatsuo Aug 25, 2020

Choose a reason for hiding this comment

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

I think --user flag should work.
Can you try this locally and paste the error log?

Copy link
Copy Markdown
Author

@ace-n ace-n Aug 25, 2020

Choose a reason for hiding this comment

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

If I use that line and remove the PYTHONPATH modification:

Processing XUnit output file: /workspace/functions/env_vars/sponge_log.xml (saving output to /tmp/tmp.I0Hx5IhTZh)
Traceback (most recent call last):
  File "/tmp/region-tag-parser/wizard-py/cli.py", line 17, in <module>
    import analyze
  File "/tmp/region-tag-parser/wizard-py/analyze.py", line 19, in <module>
    import yaml_utils
  File "/tmp/region-tag-parser/wizard-py/yaml_utils.py", line 19, in <module>
    import yaml
ModuleNotFoundError: No module named 'yaml'

If we're going to use --user instead of specifying a target directory, we'll probably need to tell Python what directory --user corresponds to (via PYTHONPATH).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm maybe the package is being installed to a pip associated with a different Python version. Could you try something like python3.7 -m pip install ...?


cat "$XUNIT_PATH" | python3.7 "$PARSER_PATH" inject-snippet-mapping --output_file "$XUNIT_TMP_PATH" "$PWD"
if [[ $? -eq 0 ]]; then
mv $XUNIT_TMP_PATH $XUNIT_PATH
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So if mv fails, the test is marked as failed even if just the injection here failed?
I don't like it.

@ace-n
Copy link
Copy Markdown
Author

ace-n commented Aug 25, 2020

So if mv fails, the test is marked as failed even if just the injection here failed?
I don't like it.

Maybe the cleaner answer here (instead of including a bunch of checks) is to wrap this section like this?

set +e
...
set -e

@tmatsuo
Copy link
Copy Markdown
Contributor

tmatsuo commented Aug 25, 2020

@ace-n
Question: is there any reason you don't want to check the file existence?

@ace-n
Copy link
Copy Markdown
Author

ace-n commented Aug 25, 2020

@ace-n
Question: is there any reason you don't want to check the file existence?

I'm not entirely against it, but explicitly checking for every edge case makes this more complex (and in my view, harder to maintain). Personally, I think the set e approach is a (syntactically) cleaner solution.

@tmatsuo
Copy link
Copy Markdown
Contributor

tmatsuo commented Aug 25, 2020

@ace-n
Actually this file does not -e flag set, so it will keep running even if the mv fails.

However, I think being explicit and verbose logging will help us debug things if something wrong happens. I still recommend my approach.

@ace-n
Copy link
Copy Markdown
Author

ace-n commented Aug 25, 2020

@tmatsuo I went ahead and added a file existence check.

I also added set -e to trap any errors we might not think to explicitly check for.

Copy link
Copy Markdown
Contributor

@busunkim96 busunkim96 left a comment

Choose a reason for hiding this comment

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

LGTM if @tmatsuo approves.

(Stamping to make this auto-mergeable once Takashi approves the changes)

@ace-n
Copy link
Copy Markdown
Author

ace-n commented Aug 25, 2020

FYI @tmatsuo build failures are unrelated to this PR.

Comment thread .kokoro/tests/run_single_test.sh Outdated
if [[ "${INJECT_REGION_TAGS:-}" == "true" ]]; then
export REGION_TAG_PARSER_DIR="/tmp/region-tag-parser"
export PARSER_PATH="${REGION_TAG_PARSER_DIR}/wizard-py/cli.py"
export PIP_PATH=/tmp/pyyaml
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is PIP_PATH used?

@tmatsuo
Copy link
Copy Markdown
Contributor

tmatsuo commented Aug 26, 2020

Test failures are known ones, let's merge this.

@tmatsuo tmatsuo merged commit d1dfc72 into master Aug 26, 2020
@tmatsuo tmatsuo deleted the drift-parser-2 branch August 26, 2020 05:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes This human has signed the Contributor License Agreement. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants