File: test_invocation.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 (140 lines) | stat: -rw-r--r-- 3,915 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""
System tests for `jenkinsapi.jenkins` module.
"""

import time
import logging
import pytest
from jenkinsapi.build import Build
from jenkinsapi.queue import QueueItem
from jenkinsapi_tests.test_utils.random_strings import random_string
from jenkinsapi_tests.systests.job_configs import LONG_RUNNING_JOB
from jenkinsapi_tests.systests.job_configs import SHORTISH_JOB, EMPTY_JOB
from jenkinsapi.custom_exceptions import BadParams, NotFound


log = logging.getLogger(__name__)


def test_invocation_object(jenkins):
    job_name = "Acreate_%s" % random_string()
    job = jenkins.create_job(job_name, SHORTISH_JOB)
    qq = job.invoke()
    assert isinstance(qq, QueueItem)
    # Let Jenkins catchup
    qq.block_until_building()
    assert qq.get_build_number() == 1


def test_get_block_until_build_running(jenkins):
    job_name = "Bcreate_%s" % random_string()
    job = jenkins.create_job(job_name, LONG_RUNNING_JOB)
    qq = job.invoke()
    time.sleep(3)
    bn = qq.block_until_building(delay=3).get_number()
    assert isinstance(bn, int)

    build = qq.get_build()
    assert isinstance(build, Build)
    assert build.is_running()
    build.stop()
    # if we call next line right away - Jenkins have no time to stop job
    # so we wait a bit
    time.sleep(1)
    assert not build.is_running()
    console = build.get_console()
    assert isinstance(console, str)
    assert "Started by user" in console


def test_get_block_until_build_complete(jenkins):
    job_name = "Ccreate_%s" % random_string()
    job = jenkins.create_job(job_name, SHORTISH_JOB)
    qq = job.invoke()
    qq.block_until_complete()
    assert not qq.get_build().is_running()


def test_mi_and_get_last_build(jenkins):
    job_name = "Dcreate_%s" % random_string()

    job = jenkins.create_job(job_name, SHORTISH_JOB)

    for _ in range(3):
        ii = job.invoke()
        ii.block_until_complete(delay=2)

    build_number = job.get_last_good_buildnumber()
    assert build_number == 3

    build = job.get_build(build_number)
    assert isinstance(build, Build)

    build = job.get_build_metadata(build_number)
    assert isinstance(build, Build)


def test_mi_and_get_build_number(jenkins):
    job_name = "Ecreate_%s" % random_string()

    job = jenkins.create_job(job_name, EMPTY_JOB)

    for invocation in range(3):
        qq = job.invoke()
        qq.block_until_complete(delay=1)
        build_number = qq.get_build_number()
        assert build_number == invocation + 1


def test_mi_and_delete_build(jenkins):
    job_name = "Ecreate_%s" % random_string()

    job = jenkins.create_job(job_name, EMPTY_JOB)

    for invocation in range(3):
        qq = job.invoke()
        qq.block_until_complete(delay=1)
        build_number = qq.get_build_number()
        assert build_number == invocation + 1

    # Delete build using Job.delete_build
    job.get_build(1)
    job.delete_build(1)
    with pytest.raises(NotFound):
        job.get_build(1)

    # Delete build using Job as dictionary of builds
    assert isinstance(job[2], Build)
    del job[2]

    with pytest.raises(NotFound):
        job.get_build(2)

    with pytest.raises(NotFound):
        job.delete_build(99)


def test_give_params_on_non_parameterized_job(jenkins):
    job_name = "Ecreate_%s" % random_string()
    job = jenkins.create_job(job_name, EMPTY_JOB)
    with pytest.raises(BadParams):
        job.invoke(build_params={"foo": "bar", "baz": 99})


def test_keep_build_toggle(jenkins):
    job_name = "Ecreate_%s" % random_string()

    job = jenkins.create_job(job_name, EMPTY_JOB)

    qq = job.invoke()
    qq.block_until_complete(delay=1)

    build = job.get_last_build()
    assert not build.is_kept_forever()
    build.toggle_keep()
    assert build.is_kept_forever()

    build_number = job.get_last_buildnumber()
    job.toggle_keep_build(build_number)
    build = job.get_last_build()
    assert not build.is_kept_forever()