Skip to content

Commit

Permalink
SNOW-1636849 Auto-teardown Native App in integration tests (#1478)
Browse files Browse the repository at this point in the history
Changes `with project_directory()` to `with nativeapp_project_directory()`, which automatically runs `snow app teardown` before exiting the project. This allows us to remove the `try`/`finally` in most tests. For tests that were using `with pushd(test_project)`, this has been changed to `with nativeapp_teardown()`, which is what `with nativeapp_project_directory()` uses under the hood.
  • Loading branch information
sfc-gh-fcampbell authored Aug 23, 2024
1 parent 784936c commit 9c81c52
Show file tree
Hide file tree
Showing 13 changed files with 498 additions and 772 deletions.
63 changes: 63 additions & 0 deletions tests_integration/nativeapp/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from __future__ import annotations

from contextlib import contextmanager
from pathlib import Path
from typing import Any

import pytest

from tests_integration.conftest import SnowCLIRunner


@pytest.fixture
def nativeapp_project_directory(project_directory, nativeapp_teardown):
"""Wrapper around the project_directory fixture specific to Native App testing.
This fixture provides a context manager that does the following:
- Automatically calls `snow app teardown` before exiting
Parameters for the returned context manager:
:param name: The name of the directory in tests_integration/test_data/projects to use.
"""

@contextmanager
def _nativeapp_project_directory(name):
with project_directory(name) as d:
with nativeapp_teardown(project_dir=d):
yield d

return _nativeapp_project_directory


@pytest.fixture
def nativeapp_teardown(runner: SnowCLIRunner):
"""Runs `snow app teardown` before exiting.
This fixture provides a context manager that runs
`snow app teardown --force --cascade` before exiting,
regardless of any exceptions raised.
Parameters for the returned context manager:
:param project_dir: Path to the project directory (optional)
:param env: Environment variables to replace os.environ (optional)
"""

@contextmanager
def _nativeapp_teardown(
*,
project_dir: Path | None = None,
env: dict | None = None,
):
try:
yield
finally:
args = ["--force", "--cascade"]
if project_dir:
args += ["--project", str(project_dir)]
kwargs: dict[str, Any] = {}
if env:
kwargs["env"] = env
result = runner.invoke_with_connection(["app", "teardown", *args], **kwargs)
assert result.exit_code == 0

return _nativeapp_teardown
4 changes: 2 additions & 2 deletions tests_integration/nativeapp/test_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@


@pytest.fixture(scope="function", params=["napp_init_v1", "napp_init_v2"])
def template_setup(runner, project_directory, request):
def template_setup(runner, nativeapp_project_directory, request):
test_project = request.param
with enable_definition_v2_feature_flag:
with project_directory(test_project) as project_root:
with nativeapp_project_directory(test_project) as project_root:
# Vanilla bundle on the unmodified template
result = runner.invoke_json(["app", "bundle"])
assert result.exit_code == 0
Expand Down
12 changes: 2 additions & 10 deletions tests_integration/nativeapp/test_debug_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def test_nativeapp_controlled_debug_mode(
snowflake_session,
default_username,
resource_suffix,
nativeapp_teardown,
project_definition_files: List[Path],
):
project_name = "integration"
Expand All @@ -110,7 +111,7 @@ def test_nativeapp_controlled_debug_mode(
result = runner.invoke_with_connection_json(["app", "run"])
assert result.exit_code == 0

try:
with nativeapp_teardown():
# debug mode should be true by default on first app deploy,
# because snowflake.yml doesn't set it explicitly either way ("uncontrolled")
assert is_debug_mode(snowflake_session, app_name)
Expand All @@ -135,12 +136,3 @@ def test_nativeapp_controlled_debug_mode(
result = runner.invoke_with_connection_json(["app", "run"])
assert result.exit_code == 0
assert is_debug_mode(snowflake_session, app_name)

# make sure we always delete the app
result = runner.invoke_with_connection_json(["app", "teardown"])
assert result.exit_code == 0

finally:
# teardown is idempotent, so we can execute it again with no ill effects
result = runner.invoke_with_connection_json(["app", "teardown", "--force"])
assert result.exit_code == 0
Loading

0 comments on commit 9c81c52

Please sign in to comment.