Skip to content

Commit

Permalink
test(objects): add tests for project artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
nejch authored and JohnVillalovos committed Feb 2, 2022
1 parent c8c2fa7 commit 8ce0336
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 7 deletions.
106 changes: 101 additions & 5 deletions tests/functional/cli/test_cli_artifacts.py
Expand Up @@ -4,6 +4,8 @@
from io import BytesIO
from zipfile import is_zipfile

import pytest

content = textwrap.dedent(
"""\
test-artifact:
Expand All @@ -20,25 +22,29 @@
}


def test_cli_artifacts(capsysbinary, gitlab_config, gitlab_runner, project):
@pytest.fixture(scope="module")
def job_with_artifacts(gitlab_runner, project):
project.files.create(data)

jobs = None
while not jobs:
jobs = project.jobs.list(scope="success")
time.sleep(0.5)
jobs = project.jobs.list(scope="success")

job = project.jobs.get(jobs[0].id)
return project.jobs.get(jobs[0].id)


def test_cli_job_artifacts(capsysbinary, gitlab_config, job_with_artifacts):
cmd = [
"gitlab",
"--config-file",
gitlab_config,
"project-job",
"artifacts",
"--id",
str(job.id),
str(job_with_artifacts.id),
"--project-id",
str(project.id),
str(job_with_artifacts.pipeline["project_id"]),
]

with capsysbinary.disabled():
Expand All @@ -47,3 +53,93 @@ def test_cli_artifacts(capsysbinary, gitlab_config, gitlab_runner, project):

artifacts_zip = BytesIO(artifacts)
assert is_zipfile(artifacts_zip)


def test_cli_project_artifact_download(gitlab_config, job_with_artifacts):
cmd = [
"gitlab",
"--config-file",
gitlab_config,
"project-artifact",
"download",
"--project-id",
str(job_with_artifacts.pipeline["project_id"]),
"--ref-name",
job_with_artifacts.ref,
"--job",
job_with_artifacts.name,
]

artifacts = subprocess.run(cmd, capture_output=True, check=True)
assert isinstance(artifacts.stdout, bytes)

artifacts_zip = BytesIO(artifacts.stdout)
assert is_zipfile(artifacts_zip)


def test_cli_project_artifacts_warns_deprecated(gitlab_config, job_with_artifacts):
cmd = [
"gitlab",
"--config-file",
gitlab_config,
"project",
"artifacts",
"--id",
str(job_with_artifacts.pipeline["project_id"]),
"--ref-name",
job_with_artifacts.ref,
"--job",
job_with_artifacts.name,
]

artifacts = subprocess.run(cmd, capture_output=True, check=True)
assert isinstance(artifacts.stdout, bytes)
assert b"DeprecationWarning" in artifacts.stderr

artifacts_zip = BytesIO(artifacts.stdout)
assert is_zipfile(artifacts_zip)


def test_cli_project_artifact_raw(gitlab_config, job_with_artifacts):
cmd = [
"gitlab",
"--config-file",
gitlab_config,
"project-artifact",
"raw",
"--project-id",
str(job_with_artifacts.pipeline["project_id"]),
"--ref-name",
job_with_artifacts.ref,
"--job",
job_with_artifacts.name,
"--artifact-path",
"artifact.txt",
]

artifacts = subprocess.run(cmd, capture_output=True, check=True)
assert isinstance(artifacts.stdout, bytes)
assert artifacts.stdout == b"test\n"


def test_cli_project_artifact_warns_deprecated(gitlab_config, job_with_artifacts):
cmd = [
"gitlab",
"--config-file",
gitlab_config,
"project",
"artifact",
"--id",
str(job_with_artifacts.pipeline["project_id"]),
"--ref-name",
job_with_artifacts.ref,
"--job",
job_with_artifacts.name,
"--artifact-path",
"artifact.txt",
]

artifacts = subprocess.run(cmd, capture_output=True, check=True)
assert isinstance(artifacts.stdout, bytes)
assert b"DeprecationWarning" in artifacts.stderr
assert artifacts.stdout == b"test\n"
15 changes: 13 additions & 2 deletions tests/unit/objects/test_job_artifacts.py
Expand Up @@ -24,7 +24,18 @@ def resp_artifacts_by_ref_name(binary_content):
yield rsps


def test_download_artifacts_by_ref_name(gl, binary_content, resp_artifacts_by_ref_name):
def test_project_artifacts_download_by_ref_name(
gl, binary_content, resp_artifacts_by_ref_name
):
project = gl.projects.get(1, lazy=True)
artifacts = project.artifacts(ref_name=ref_name, job=job)
artifacts = project.artifacts.download(ref_name=ref_name, job=job)
assert artifacts == binary_content


def test_project_artifacts_by_ref_name_warns(
gl, binary_content, resp_artifacts_by_ref_name
):
project = gl.projects.get(1, lazy=True)
with pytest.warns(DeprecationWarning):
artifacts = project.artifacts(ref_name=ref_name, job=job)
assert artifacts == binary_content

0 comments on commit 8ce0336

Please sign in to comment.