File: test_cli_artifacts.py

package info (click to toggle)
python-gitlab 1%3A8.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,884 kB
  • sloc: python: 25,823; makefile: 171; ruby: 27; javascript: 3
file content (108 lines) | stat: -rw-r--r-- 2,792 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import logging
import subprocess
import textwrap
import time
from io import BytesIO
from zipfile import is_zipfile

import pytest

content = textwrap.dedent("""\
    test-artifact:
      script: echo "test" > artifact.txt
      artifacts:
        untracked: true
    """)
data = {
    "file_path": ".gitlab-ci.yml",
    "branch": "main",
    "content": content,
    "commit_message": "Initial commit",
}


@pytest.fixture(scope="module")
def job_with_artifacts(gitlab_runner, project):
    start_time = time.time()

    project.files.create(data)

    jobs = None
    while not jobs:
        time.sleep(0.5)
        jobs = project.jobs.list(scope="success")
        if time.time() - start_time < 60:
            continue
        logging.error("job never succeeded")
        for job in project.jobs.list():
            job = project.jobs.get(job.id)
            logging.info(f"{job.status} job: {job.pformat()}")
            logging.info(f"job log:\n{job.trace()}\n")
        pytest.fail("Fixture 'job_with_artifact' failed")

    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_with_artifacts.id),
        "--project-id",
        str(job_with_artifacts.pipeline["project_id"]),
    ]

    with capsysbinary.disabled():
        artifacts = subprocess.check_output(cmd)
    assert isinstance(artifacts, bytes)

    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_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"