Skip to content

Commit

Permalink
Add step to make a new commit when running test_private_repos
Browse files Browse the repository at this point in the history
To prevent cachito from caching private source code and serving it without trying to access the repository.

STONEBLD-661

Signed-off-by: Felipe de Almeida <fdealmei@redhat.com>
  • Loading branch information
fepas authored and taylormadore committed Mar 16, 2023
1 parent c40d50b commit 27161e4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 26 deletions.
4 changes: 4 additions & 0 deletions test_env_vars.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,7 @@ various_packages:
repo: https://github.com/cachito-testing/cachito-gomod-test
ref: 1827221d787cbd1e979b339cfbbf59728eddf0d4
dependencies_count: 49
# test repo user
git_user: "Arthur Dent"
# test repo user email
git_email: "dent42@cachito.rocks"
3 changes: 1 addition & 2 deletions tests/integration/test_data/private_repo_packages.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Test data for the private repos test
# The test will only be performed if $JOB_NAME includes one of the following

private_repo_test_envs:
- cachito-prod
- cachito-stage
private_repo_https:
repo: https://github.com/cachito-testing/cachito-no-package-manager-private.git
ref: 31e242befe367f940f878f27158d9cd036ba3b23
private_repo_ssh:
repo: git@github.com:cachito-testing/cachito-no-package-manager-private.git
ref: 31e242befe367f940f878f27158d9cd036ba3b23
78 changes: 54 additions & 24 deletions tests/integration/test_private_repos.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-License-Identifier: GPL-3.0-or-later

import os
import random
import string
from pathlib import Path
from typing import Any

Expand All @@ -16,6 +18,9 @@ def test_private_repos(env_package: str, test_env: dict[str, Any], tmp_path: Pat
Validate a cachito request with no package managers to a private repo.
Process:
Create new commit at "cachito-no-package-manager-private" repo
(To prevent cachito from caching private source code and serving
it without trying to access the repository, more info: STONEBLD-661)
Send new request to the Cachito API
Send request to check status of existing request
Expand All @@ -31,7 +36,8 @@ def test_private_repos(env_package: str, test_env: dict[str, Any], tmp_path: Pat
test_data = utils.load_test_data("private_repo_packages.yaml")
private_repo_test_envs = test_data["private_repo_test_envs"]
env_data = test_data[env_package]
is_supported_env = any(x in str(os.environ.get("JOB_NAME")) for x in private_repo_test_envs)
job_name = str(os.environ.get("JOB_NAME"))
is_supported_env = any(x in job_name for x in private_repo_test_envs)
if not is_supported_env:
pytest.skip(
(
Expand All @@ -41,26 +47,50 @@ def test_private_repos(env_package: str, test_env: dict[str, Any], tmp_path: Pat
)
)

client = utils.Client(test_env["api_url"], test_env["api_auth_type"], test_env.get("timeout"))
payload = {
"repo": env_data["repo"],
"ref": env_data["ref"],
"pkg_managers": [],
"flags": ["include-git-dir"],
}

initial_response = client.create_new_request(payload=payload)
completed_response = client.wait_for_complete_request(initial_response)

utils.assert_properly_completed_response(completed_response)
assert completed_response.data["packages"] == []
assert completed_response.data["dependencies"] == []

client.download_and_extract_archive(completed_response.id, tmp_path)
source_path = tmp_path / f"download_{str(completed_response.id)}"
repo = Repo(source_path / "app")
assert repo.head.commit.hexsha == env_data["ref"]
assert not repo.git.diff()
assert not os.listdir(source_path / "deps")

utils.assert_content_manifest(client, completed_response.id, [])
repo = Repo.clone_from(test_data["private_repo_ssh"]["repo"], tmp_path)

repo.config_writer().set_value("user", "name", test_env["git_user"]).release()
repo.config_writer().set_value("user", "email", test_env["git_email"]).release()

generated_suffix = "".join(
random.choice(string.ascii_letters + string.digits) for x in range(10)
)
branch_name = f"tmp-branch-{generated_suffix}"

try:
repo.create_head(branch_name).checkout()

message = "Committed by Cachito integration test (test_private_repos)"
repo.git.commit("--allow-empty", m=message)
repo.git.push("-u", "origin", branch_name)

ref = repo.head.commit.hexsha

client = utils.Client(
test_env["api_url"], test_env["api_auth_type"], test_env.get("timeout")
)
payload = {
"repo": env_data["repo"],
"ref": ref,
"pkg_managers": [],
"flags": ["include-git-dir"],
}

initial_response = client.create_new_request(payload=payload)
completed_response = client.wait_for_complete_request(initial_response)

utils.assert_properly_completed_response(completed_response)
assert completed_response.data["packages"] == []
assert completed_response.data["dependencies"] == []

client.download_and_extract_archive(completed_response.id, tmp_path)
source_path = tmp_path / f"download_{str(completed_response.id)}"
downloaded_repo = Repo(source_path / "app")
assert downloaded_repo.head.commit.hexsha == ref
assert not downloaded_repo.git.diff()
assert not os.listdir(source_path / "deps")

utils.assert_content_manifest(client, completed_response.id, [])

finally:
repo.git.push("--delete", "origin", branch_name)

0 comments on commit 27161e4

Please sign in to comment.