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 1105629 git integration tests #865

Merged
merged 34 commits into from
Mar 7, 2024
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a061e22
git copy: get
sfc-gh-pczajka Feb 27, 2024
f3c10d9
git copy: copy
sfc-gh-pczajka Feb 27, 2024
d9029e1
unit tests
sfc-gh-pczajka Feb 27, 2024
6f11594
Merge branch 'git-repository' into SNOW-1105689-git-copy
sfc-gh-pczajka Mar 4, 2024
db62c6c
Setting up api integration
sfc-gh-pczajka Feb 28, 2024
eaf972d
test git flow - in progress
sfc-gh-pczajka Feb 28, 2024
e7661b6
copy: fix error handling
sfc-gh-pczajka Feb 28, 2024
a9b6236
Add unit test for checking error messages
sfc-gh-pczajka Mar 1, 2024
228fb3e
finish flow test
sfc-gh-pczajka Mar 1, 2024
601cd75
fix get command
sfc-gh-pczajka Mar 4, 2024
5be18d8
switch to smaller repo
sfc-gh-pczajka Mar 4, 2024
20e362f
refactor tests
sfc-gh-pczajka Mar 4, 2024
adbef0c
update unit tests
sfc-gh-pczajka Mar 4, 2024
712b07b
remove snapshots for Windows run
sfc-gh-pczajka Mar 4, 2024
9c59659
target help message: a directory; create target if not exists
sfc-gh-pczajka Mar 5, 2024
d11c38e
Use SecurePath
sfc-gh-pczajka Mar 5, 2024
0ef4dcf
fix path type
sfc-gh-pczajka Mar 5, 2024
33d9556
Merge branch 'git-repository' into SNOW-1105689-git-copy
sfc-gh-pczajka Mar 5, 2024
5af7287
review fixes
sfc-gh-pczajka Mar 5, 2024
2f7eb04
Refactor stage.get
sfc-gh-pczajka Mar 5, 2024
c3de383
assert in callback
sfc-gh-pczajka Mar 5, 2024
cdf311d
fix source error
sfc-gh-pczajka Mar 5, 2024
287511c
update help message
sfc-gh-pczajka Mar 5, 2024
2317d29
Review fixes
sfc-gh-pczajka Mar 6, 2024
7265e17
rename test assert
sfc-gh-pczajka Mar 6, 2024
c2172f8
small fix
sfc-gh-pczajka Mar 6, 2024
2e9d7a4
Merge branch 'SNOW-1105689-git-copy' into SNOW-1105629-git-integratio…
sfc-gh-pczajka Mar 6, 2024
a1a79ef
fix merge issues
sfc-gh-pczajka Mar 6, 2024
dd6c1ae
change repo to snowcli; refactor copy tests
sfc-gh-pczajka Mar 6, 2024
d51002b
review fixes
sfc-gh-pczajka Mar 7, 2024
9082ea0
Merge branch 'git-repository' into SNOW-1105629-git-integration-tests
sfc-gh-pczajka Mar 7, 2024
a43d2ba
Merge branch 'git-repository' into SNOW-1105629-git-integration-tests
sfc-gh-pczajka Mar 7, 2024
c8fc64c
rename function
sfc-gh-pczajka Mar 7, 2024
adc7450
remove debug
sfc-gh-pczajka Mar 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
236 changes: 236 additions & 0 deletions tests_integration/test_git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
import pytest
from snowflake.connector.errors import ProgrammingError
from pathlib import Path
import tempfile

FILE_IN_REPO = "RELEASE-NOTES.md"


@pytest.fixture
def sf_git_repository(runner, test_database):
repo_name = "SNOWCLI_TESTING_REPO"
integration_name = "SNOW_GIT_TESTING_API_INTEGRATION"

if not _integration_exists(runner, integration_name=integration_name):
result = runner.invoke_with_connection(
[
"sql",
"-q",
f"""
CREATE API INTEGRATION {integration_name}
API_PROVIDER = git_https_api
API_ALLOWED_PREFIXES = ('https://github.com/snowflakedb/')
ALLOWED_AUTHENTICATION_SECRETS = ()
ENABLED = true
""",
]
)
assert result.exit_code == 0

result = runner.invoke_with_connection(
[
"sql",
"-q",
f"""
CREATE GIT REPOSITORY {repo_name}
API_INTEGRATION = {integration_name}
ORIGIN = 'https://github.com/snowflakedb/snowflake-cli.git'
""",
]
)
assert result.exit_code == 0
return repo_name


@pytest.mark.integration
def test_object_commands(runner, sf_git_repository):
# object list
result = runner.invoke_with_connection_json(["object", "list", "git-repository"])
assert result.exit_code == 0
assert sf_git_repository in _filter_key(result.json, key="name")

# describe
result = runner.invoke_with_connection_json(
["object", "describe", "git-repository", sf_git_repository]
)
assert result.exit_code == 0
assert result.json[0]["name"] == sf_git_repository

# drop
result = runner.invoke_with_connection_json(
["object", "drop", "git-repository", sf_git_repository]
)
assert result.exit_code == 0
assert result.json == [{"status": f"{sf_git_repository} successfully dropped."}]


@pytest.mark.integration
def test_fetch(runner, sf_git_repository):
result = runner.invoke_with_connection_json(["git", "fetch", sf_git_repository])
assert result.exit_code == 0
assert result.json == [
{
"status": f"Git Repository {sf_git_repository} is up to date. No change was fetched."
}
]


@pytest.mark.integration
def test_list_branches_and_tags(runner, sf_git_repository):
# list branches
result = runner.invoke_with_connection_json(
["git", "list-branches", sf_git_repository]
)
assert result.exit_code == 0
assert "main" in _filter_key(result.json, key="name")
Comment on lines +81 to +85
Copy link
Contributor

Choose a reason for hiding this comment

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

HAve you considered using like to reduce the json and do explicit comparison ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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


# list tags
result = runner.invoke_with_connection_json(["git", "list-tags", sf_git_repository])
assert result.exit_code == 0
assert "v2.0.0" in _filter_key(result.json, key="name")


@pytest.mark.integration
def test_list_files(runner, sf_git_repository):
# error messages are passed to the user
result = runner.invoke_with_connection(["git", "list-files", sf_git_repository])
_assert_invalid_repo_path_error_message(result.output)

try:
repository_path = f"@{sf_git_repository}"
runner.invoke_with_connection(["git", "list-files", repository_path])
assert False, "Expected exception"
except ProgrammingError as err:
assert (
err.raw_msg
== "Files paths in git repositories must specify a scope. For example, a branch name, "
"a tag name, or a valid commit hash. Commit hashes are between 6 and 40 characters long."
)

repository_path = f"@{sf_git_repository}/tags/v2.1.0-rc1/"
result = runner.invoke_with_connection_json(["git", "list-files", repository_path])
assert result.exit_code == 0
assert f"{repository_path[1:].lower()}{FILE_IN_REPO}" in _filter_key(
result.json, key="name"
)


@pytest.mark.integration
def test_copy_to_stage(runner, sf_git_repository):
REPO_PATH_PREFIX = f"@{sf_git_repository}/tags/v2.1.0-rc0"
SUBDIR = "tests_integration/config"
SUBDIR_ON_STAGE = "config"
FILE_IN_SUBDIR = "connection_configs.toml"
STAGE_NAME = "a_perfect_stage_for_testing"

def _assert_file_on_stage(file_path):
result = runner.invoke_with_connection_json(
["object", "stage", "list", STAGE_NAME]
)
assert result.exit_code == 0
assert f"{STAGE_NAME.lower()}/{file_path}" in [f["name"] for f in result.json]

# create stage for testing copy
result = runner.invoke_with_connection(["object", "stage", "create", STAGE_NAME])
assert result.exit_code == 0

# copy directory - whole directory
repository_path = f"{REPO_PATH_PREFIX}/{SUBDIR}"
result = runner.invoke_with_connection_json(
["git", "copy", repository_path, f"@{STAGE_NAME}"]
)
assert result.exit_code == 0
_assert_file_on_stage(f"{SUBDIR_ON_STAGE}/{FILE_IN_SUBDIR}") # whole dir is copied

# copy directory - copy contents
repository_path = f"{REPO_PATH_PREFIX}/{SUBDIR}/"
result = runner.invoke_with_connection_json(
["git", "copy", repository_path, f"@{STAGE_NAME}"]
)
assert result.exit_code == 0
_assert_file_on_stage(FILE_IN_SUBDIR) # contents are copied

# copy single file
repository_path = f"{REPO_PATH_PREFIX}/{FILE_IN_REPO}"
result = runner.invoke_with_connection_json(
["git", "copy", repository_path, f"@{STAGE_NAME}"]
)
assert result.exit_code == 0
_assert_file_on_stage(FILE_IN_REPO)

# copy file into directory
repository_path = f"{REPO_PATH_PREFIX}/{FILE_IN_REPO}"
result = runner.invoke_with_connection_json(
["git", "copy", repository_path, f"@{STAGE_NAME}/a_dir/"]
)
assert result.exit_code == 0
_assert_file_on_stage(f"a_dir/{FILE_IN_REPO}")
# error with no '/' at the end should be fixed by snowcli
repository_path = f"{REPO_PATH_PREFIX}/{FILE_IN_REPO}"
result = runner.invoke_with_connection_json(
["git", "copy", repository_path, f"@{STAGE_NAME}/another_dir"]
)
assert result.exit_code == 0
_assert_file_on_stage(f"another_dir/{FILE_IN_REPO}")


@pytest.mark.integration
def test_copy_to_local_file_system(runner, sf_git_repository):
# TODO: change subdir to dedicated one after merging this to main
REPO_PATH_PREFIX = f"@{sf_git_repository}/tags/v2.1.0-rc0"
SUBDIR = "tests_integration/config"
FILE_IN_SUBDIR = "connection_configs.toml"
with tempfile.TemporaryDirectory() as tmp_dir:
LOCAL_DIR = Path(tmp_dir) / "a_dir"
assert not LOCAL_DIR.exists()

# copy directory - GET only copy contents
repository_path = f"{REPO_PATH_PREFIX}/{SUBDIR}"
result = runner.invoke_with_connection_json(
["git", "copy", repository_path, str(LOCAL_DIR)]
)
assert result.exit_code == 0
assert LOCAL_DIR.exists() # create directory if not exists
assert (LOCAL_DIR / FILE_IN_SUBDIR).exists() # contents are copied

# copy single file
repository_path = f"{REPO_PATH_PREFIX}/{FILE_IN_REPO}"
result = runner.invoke_with_connection_json(
["git", "copy", repository_path, str(LOCAL_DIR)]
)
assert result.exit_code == 0
assert (LOCAL_DIR / FILE_IN_REPO).exists()

# error messages are passed to the user
try:
repository_path = f"@{sf_git_repository}/tags/no-such-tag/"
runner.invoke_with_connection(
["git", "copy", repository_path, str(LOCAL_DIR)]
)
assert False, "Expected exception"
except ProgrammingError as err:
assert (
err.raw_msg
== "The specified tag 'no-such-tag' cannot be found in the Git Repository."
)


def _filter_key(objects, *, key):
return [o[key] for o in objects]


def _assert_invalid_repo_path_error_message(output):
assert "Error" in output
assert (
"REPOSITORY_PATH should be a path to git repository stage with scope" in output
)
assert "provided. For example: @my_repo/branches/main" in output


def _integration_exists(runner, integration_name):
result = runner.invoke_with_connection_json(["sql", "-q", "SHOW INTEGRATIONS"])
assert result.exit_code == 0
return any(
integration["name"].upper() == integration_name.upper()
for integration in result.json
)
Loading