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

[SNOW-1462584] Implement setup script validation #1161

Merged
merged 29 commits into from
Jun 12, 2024

Conversation

sfc-gh-fcampbell
Copy link
Collaborator

@sfc-gh-fcampbell sfc-gh-fcampbell commented Jun 5, 2024

Pre-review checklist

  • I've confirmed that instructions included in README.md are still correct after my changes in the codebase.
  • I've added or updated automated unit tests to verify correctness of my new code.
  • I've added or updated integration tests to verify correctness of my new code.
  • I've confirmed that my changes are working by executing CLI's commands manually.
  • I've confirmed that my changes are up-to-date with the target branch.
  • I've described my changes in the release notes.
  • I've described my changes in the section below.

Changes description

Calls system$validate_native_app_setup() to validate the setup script in the stage on snow app deploy, snow app run, and the new snow app validate. The validation is done server-side, all we need to do is raise an error and show messages if the validation failed.

For snow app validate, we use a scratch stage to avoid clobbering the app's dev stage in between deploys. This stage is deleted after every validation run and on snow app teardown to avoid leaving behind any garbage.

Scenarios

App has warning but no errors (exit code 0):
image

App has validation errors (exit code 1):
image

Deploy-time validation error (exit code 1, notice it's using the app's stage and not the scratch stage):
image

Skip validation at deploy time (exit code 0):
image

JSON output (exit code 0 regardless of status):
image

@sfc-gh-fcampbell sfc-gh-fcampbell marked this pull request as ready for review June 6, 2024 15:44
@sfc-gh-fcampbell sfc-gh-fcampbell requested review from a team as code owners June 6, 2024 15:44
RELEASE-NOTES.md Outdated Show resolved Hide resolved
src/snowflake/cli/plugins/nativeapp/commands.py Outdated Show resolved Hide resolved
f"call system$validate_native_app_setup('{stage_name}')"
)
except ProgrammingError as err:
if "does not exist or not authorized" in err.msg:
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the App Pkg does not exist error is a specific error, we should possibly tighten the constraint on this condition with an and on the error number.

Once case I'm thinking of but is not necessarily true today: "does not exist or not authorized" could be thrown if some setup script is missing or permissions change that setup script cannot be accessed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

if a permission issue hides the visibility of the app package, wouldn't that be functionally equivalent to it not existing, from the PoV of the user? I can update the exception's error message to add "or is not authorized" (i'll add the errno check too)

src/snowflake/cli/plugins/nativeapp/manager.py Outdated Show resolved Hide resolved
src/snowflake/cli/plugins/nativeapp/commands.py Outdated Show resolved Hide resolved
tests_integration/nativeapp/test_validate.py Outdated Show resolved Hide resolved
@sfc-gh-fcampbell sfc-gh-fcampbell marked this pull request as draft June 7, 2024 15:22
@sfc-gh-fcampbell sfc-gh-fcampbell changed the title [SNOW-1462584] Implement snow app validate command [SNOW-1462584] Implement setup script validation Jun 7, 2024
@sfc-gh-fcampbell sfc-gh-fcampbell force-pushed the frank-snow-app-validate branch 4 times, most recently from aebd7a0 to 365f311 Compare June 10, 2024 16:42
@sfc-gh-fcampbell sfc-gh-fcampbell marked this pull request as ready for review June 10, 2024 17:09
@sfc-gh-fcampbell sfc-gh-fcampbell marked this pull request as draft June 10, 2024 17:31
@sfc-gh-fcampbell sfc-gh-fcampbell marked this pull request as ready for review June 10, 2024 17:36
Comment on lines 608 to 625
messages = [
_validation_item_to_str(error)
for error in validation_result.get("errors", [])
]
raise SetupScriptFailedValidation(messages)
Copy link
Contributor

Choose a reason for hiding this comment

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

any reason we shouldn't print the errors as we do with the warnings, and fail with a "validation failed" generic message?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

there was no specific reason, but looking back I like it like that since it separates errors and warnings, and it makes the errors really obvious by printing them inside the ASCII box art (see the screenshot in the PR description). I'm not opposed to changing it if it's a blocker

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we'll want to control how we format these messages, and this way of outputting them will prevent us from doing that. I'll do a pass on the overall output this week or next week, it's long overdue. We should sync on that, but I'm fine if validate becomes slightly inconsistent (but better) while the other commands catch up.

IMHO we keep the box small and generic here (something like "Validation failed, re-run with --no-validate to skip this check"). We output errors the same way as we do warnings, but maybe we make them visually stand out in the output (maybe we use shell colors to highlight errors?)

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 updated it to print the messages before the error:
image


def validate(self, use_scratch_stage: bool = False):
"""Validates Native App setup script SQL."""
cc.step(f"Validating Snowflake Native App setup script.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Is validation a phase? It looks like it will output nested messages, hence the question.

Copy link
Collaborator Author

@sfc-gh-fcampbell sfc-gh-fcampbell Jun 10, 2024

Choose a reason for hiding this comment

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

yeah it could be considered a phase, but the output looks a little wonky since the diff log uses "\n.join on a bunch of strings, so they don't come out indented:
image

Copy link
Contributor

Choose a reason for hiding this comment

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

The diff output is bad, I have a fix locally that I will submit in the next few days. So it's not a really good comparison.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed it to a phase

tests_integration/nativeapp/test_validate.py Outdated Show resolved Hide resolved
tests_integration/nativeapp/test_deploy.py Show resolved Hide resolved
@sfc-gh-fcampbell sfc-gh-fcampbell force-pushed the frank-snow-app-validate branch 2 times, most recently from d2d8401 to 323ff2f Compare June 11, 2024 15:07
tests_integration/nativeapp/test_deploy.py Show resolved Hide resolved
Comment on lines 608 to 625
messages = [
_validation_item_to_str(error)
for error in validation_result.get("errors", [])
]
raise SetupScriptFailedValidation(messages)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we'll want to control how we format these messages, and this way of outputting them will prevent us from doing that. I'll do a pass on the overall output this week or next week, it's long overdue. We should sync on that, but I'm fine if validate becomes slightly inconsistent (but better) while the other commands catch up.

IMHO we keep the box small and generic here (something like "Validation failed, re-run with --no-validate to skip this check"). We output errors the same way as we do warnings, but maybe we make them visually stand out in the output (maybe we use shell colors to highlight errors?)

@sfc-gh-fcampbell sfc-gh-fcampbell force-pushed the frank-snow-app-validate branch 3 times, most recently from 1485c46 to f1aa991 Compare June 12, 2024 15:18
sfc-gh-bdufour
sfc-gh-bdufour previously approved these changes Jun 12, 2024
@sfc-gh-fcampbell sfc-gh-fcampbell enabled auto-merge (squash) June 12, 2024 15:38
@sfc-gh-fcampbell sfc-gh-fcampbell merged commit 5519f5a into main Jun 12, 2024
24 checks passed
@sfc-gh-fcampbell sfc-gh-fcampbell deleted the frank-snow-app-validate branch June 12, 2024 15:46
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