Skip to content

Commit

Permalink
don't pass post deploy/package scripts when validating and convert te…
Browse files Browse the repository at this point in the history
…sts to use factories (#1743)

* don't pass post deploy/package scripts when validating and convert tests to use factories

* update test_manager for deploy in validate with no post deploy/package scripts

* remove bad comment

* nativeapp_project_directory -> nativeapp_teardown
  • Loading branch information
sfc-gh-mchok authored Oct 18, 2024
1 parent f82f51a commit 25ffb1a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,6 @@ def action_validate(
package_warehouse=(
(model.meta and model.meta.warehouse) or workspace_ctx.default_warehouse
),
post_deploy_hooks=model.meta and model.meta.post_deploy,
package_scripts=[], # Package scripts are not supported in PDFv2
policy=policy,
use_scratch_stage=use_scratch_stage,
scratch_stage_fqn=f"{package_name}.{model.scratch_stage}",
Expand Down Expand Up @@ -479,8 +477,6 @@ def deploy(
paths=paths,
stage_fqn=stage_fqn,
package_warehouse=package_warehouse,
post_deploy_hooks=post_deploy_hooks,
package_scripts=package_scripts,
policy=policy,
use_scratch_stage=False,
scratch_stage_fqn="",
Expand Down Expand Up @@ -1201,8 +1197,6 @@ def validate_setup_script(
recursive: bool,
paths: List[Path] | None,
stage_fqn: str,
post_deploy_hooks: list[PostDeployHook] | None,
package_scripts: List[str],
policy: PolicyBase,
use_scratch_stage: bool,
scratch_stage_fqn: str,
Expand All @@ -1224,8 +1218,6 @@ def validate_setup_script(
paths=paths,
stage_fqn=stage_fqn,
package_warehouse=package_warehouse,
post_deploy_hooks=post_deploy_hooks,
package_scripts=package_scripts,
policy=policy,
use_scratch_stage=use_scratch_stage,
scratch_stage_fqn=scratch_stage_fqn,
Expand Down Expand Up @@ -1268,8 +1260,6 @@ def get_validation_result(self, use_scratch_stage: bool = True):
package_warehouse=(
(model.meta and model.meta.warehouse) or workspace_ctx.default_warehouse
),
post_deploy_hooks=model.meta and model.meta.post_deploy,
package_scripts=[], # Package scripts are not supported in PDFv2
policy=AllowAlwaysPolicy(),
use_scratch_stage=use_scratch_stage,
scratch_stage_fqn=f"{package_name}.{model.scratch_stage}",
Expand All @@ -1292,8 +1282,6 @@ def get_validation_result_static(
recursive: bool,
paths: List[Path] | None,
stage_fqn: str,
post_deploy_hooks: list[PostDeployHook] | None,
package_scripts: List[str],
policy: PolicyBase,
use_scratch_stage: bool,
scratch_stage_fqn: str,
Expand All @@ -1319,8 +1307,8 @@ def get_validation_result_static(
validate=False,
stage_fqn=stage_fqn,
package_warehouse=package_warehouse,
post_deploy_hooks=post_deploy_hooks,
package_scripts=package_scripts,
post_deploy_hooks=[],
package_scripts=[],
policy=policy,
)
prefixed_stage_fqn = StageManager.get_standard_stage_prefix(stage_fqn)
Expand Down
4 changes: 0 additions & 4 deletions src/snowflake/cli/_plugins/nativeapp/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,6 @@ def validate(self, use_scratch_stage: bool = False):
paths=[],
stage_fqn=self.stage_fqn,
package_warehouse=self.package_warehouse,
post_deploy_hooks=self.package_post_deploy_hooks,
package_scripts=self.package_scripts,
policy=AllowAlwaysPolicy(),
use_scratch_stage=use_scratch_stage,
scratch_stage_fqn=self.scratch_stage_fqn,
Expand All @@ -348,8 +346,6 @@ def get_validation_result(self, use_scratch_stage: bool = False):
paths=[],
stage_fqn=self.stage_fqn,
package_warehouse=self.package_warehouse,
post_deploy_hooks=self.package_post_deploy_hooks,
package_scripts=self.package_scripts,
policy=AllowAlwaysPolicy(),
use_scratch_stage=use_scratch_stage,
scratch_stage_fqn=self.scratch_stage_fqn,
Expand Down
4 changes: 2 additions & 2 deletions tests/nativeapp/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,7 @@ def test_validate_use_scratch_stage(mock_execute, mock_deploy, temp_dir, mock_cu
validate=False,
stage_fqn=f"{pkg_model.fqn.name}.{pkg_model.scratch_stage}",
package_warehouse=pkg_model.meta.warehouse,
post_deploy_hooks=pkg_model.meta.post_deploy,
post_deploy_hooks=[],
package_scripts=[],
policy=AllowAlwaysPolicy(),
)
Expand Down Expand Up @@ -1470,7 +1470,7 @@ def test_validate_failing_drops_scratch_stage(
validate=False,
stage_fqn=f"{pkg_model.fqn.name}.{pkg_model.scratch_stage}",
package_warehouse=pkg_model.meta.warehouse,
post_deploy_hooks=pkg_model.meta.post_deploy,
post_deploy_hooks=[],
package_scripts=[],
policy=AllowAlwaysPolicy(),
)
Expand Down
128 changes: 108 additions & 20 deletions tests_integration/nativeapp/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,63 @@
# limitations under the License.

from shlex import split
from textwrap import dedent

from tests.nativeapp.factories import (
ProjectV2Factory,
ApplicationPackageEntityModelFactory,
ApplicationEntityModelFactory,
ProjectV10Factory,
)
from tests.project.fixtures import *


@pytest.mark.integration
def test_nativeapp_validate_v1(nativeapp_teardown, runner, temp_dir):
ProjectV10Factory(
pdf__native_app__name="myapp",
pdf__native_app__artifacts=[
{"src": "app/*", "dest": "./"},
],
files={
"app/setup.sql": "CREATE OR ALTER VERSIONED SCHEMA core;",
"app/README.md": "\n",
"app/manifest.yml": "\n",
},
)
with nativeapp_teardown(project_dir=Path(temp_dir)):
# validate the app's setup script
result = runner.invoke_with_connection(["app", "validate"])
assert result.exit_code == 0, result.output
assert "Native App validation succeeded." in result.output


@pytest.mark.integration
@pytest.mark.parametrize(
"command,test_project",
"command",
[
["app validate", "napp_init_v1"],
["app validate", "napp_init_v2"],
["ws validate --entity-id=pkg", "napp_init_v2"],
"app validate",
"ws validate --entity-id=pkg",
],
)
def test_nativeapp_validate(command, test_project, nativeapp_project_directory, runner):
with nativeapp_project_directory(test_project):
def test_nativeapp_validate_v2(command, nativeapp_teardown, runner, temp_dir):
ProjectV2Factory(
pdf__entities=dict(
pkg=ApplicationPackageEntityModelFactory(
identifier="myapp_pkg",
),
app=ApplicationEntityModelFactory(
identifier="myapp",
fromm__target="pkg",
),
),
files={
"setup.sql": "CREATE OR ALTER VERSIONED SCHEMA core;",
"README.md": "\n",
"manifest.yml": "\n",
},
)
with nativeapp_teardown(project_dir=Path(temp_dir)):
# validate the app's setup script
result = runner.invoke_with_connection(split(command))
assert result.exit_code == 0, result.output
Expand All @@ -37,22 +80,67 @@ def test_nativeapp_validate(command, test_project, nativeapp_project_directory,


@pytest.mark.integration
@pytest.mark.parametrize(
"command,test_project",
[
["app validate", "napp_init_v2"],
],
)
def test_nativeapp_validate_failing(
command, test_project, nativeapp_project_directory, runner
):
with nativeapp_project_directory(test_project):
# Create invalid SQL file
Path("app/setup_script.sql").write_text("Lorem ipsum dolor sit amet")

def test_nativeapp_validate_failing(nativeapp_teardown, runner, temp_dir):
ProjectV2Factory(
pdf__entities=dict(
pkg=ApplicationPackageEntityModelFactory(
identifier="myapp_pkg",
),
app=ApplicationEntityModelFactory(
identifier="myapp",
fromm__target="pkg",
),
),
files={
# Create invalid SQL file
"setup.sql": dedent(
"""\
CREATE OR ALTER VERSIONED SCHEMA core;
Lorem ipsum dolor sit amet
"""
),
"README.md": "\n",
"manifest.yml": "\n",
},
)
with nativeapp_teardown(project_dir=Path(temp_dir)):
# validate the app's setup script, this will fail
# because we include an empty file
result = runner.invoke_with_connection(split(command))
result = runner.invoke_with_connection(["app", "validate"])
assert result.exit_code == 1, result.output
assert "Snowflake Native App setup script failed validation." in result.output
assert "syntax error" in result.output


@pytest.mark.integration
def test_nativeapp_validate_with_post_deploy_hooks(
nativeapp_teardown, runner, temp_dir
):
ProjectV2Factory(
pdf__entities=dict(
pkg=ApplicationPackageEntityModelFactory(
identifier="myapp_pkg",
meta__post_deploy=[
{"sql_script": "pkg_post_deploy1.sql"},
],
),
app=ApplicationEntityModelFactory(
identifier="myapp",
fromm__target="pkg",
meta__post_deploy=[
{"sql_script": "app_post_deploy1.sql"},
],
),
),
files={
"app_post_deploy1.sql": "\n",
"pkg_post_deploy1.sql": "\n",
"setup.sql": "CREATE OR ALTER VERSIONED SCHEMA core;",
"README.md": "\n",
"manifest.yml": "\n",
},
)

with nativeapp_teardown(project_dir=Path(temp_dir)):
result = runner.invoke_with_connection(["app", "validate"])
assert result.exit_code == 0, result.output

0 comments on commit 25ffb1a

Please sign in to comment.