File: test_jenkins_artifacts.py

package info (click to toggle)
python-jenkinsapi 0.3.17-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,500 kB
  • sloc: python: 10,001; xml: 50; makefile: 31; sh: 26
file content (65 lines) | stat: -rw-r--r-- 2,057 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
"""
System tests for `jenkinsapi.jenkins` module.
"""

import os
from posixpath import join
import re
import time
import gzip
import shutil
import tempfile
import logging
from jenkinsapi_tests.systests.job_configs import JOB_WITH_ARTIFACTS
from jenkinsapi_tests.test_utils.random_strings import random_string

log = logging.getLogger(__name__)


def test_artifacts(jenkins):
    job_name = "create_%s" % random_string()
    job = jenkins.create_job(job_name, JOB_WITH_ARTIFACTS)
    job.invoke(block=True)

    build = job.get_last_build()

    while build.is_running():
        time.sleep(1)

    artifacts = build.get_artifact_dict()
    assert isinstance(artifacts, dict) is True

    text_artifact = artifacts["out.txt"]
    binary_artifact = artifacts["out.gz"]

    tempDir = tempfile.mkdtemp()

    try:
        # Verify that we can handle text artifacts
        text_artifact.save_to_dir(tempDir, strict_validation=True)
        text_file_path = join(tempDir, text_artifact.filename)
        assert os.path.exists(text_file_path)
        with open(text_file_path, "rb") as f:
            read_back_text = f.read().strip()
            read_back_text = read_back_text.decode("ascii")
            log.info("Text artifact: %s", read_back_text)
            assert (
                re.match(r"^PING \S+ \(127.0.0.1\)", read_back_text)
                is not None
            )
            assert read_back_text.endswith("ms") is True

        # Verify that we can hande binary artifacts
        binary_artifact.save_to_dir(tempDir, strict_validation=True)
        bin_file_path = join(tempDir, binary_artifact.filename)
        assert os.path.exists(bin_file_path)
        with gzip.open(bin_file_path, "rb") as f:
            read_back_text = f.read().strip()
            read_back_text = read_back_text.decode("ascii")
            assert (
                re.match(r"^PING \S+ \(127.0.0.1\)", read_back_text)
                is not None
            )
            assert read_back_text.endswith("ms") is True
    finally:
        shutil.rmtree(tempDir)