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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
"""
GitLab API: https://docs.gitlab.com/ee/api/pipelines.html
"""
import pytest
import responses
from gitlab.v4.objects import (
ProjectPipeline,
ProjectPipelineTestReport,
ProjectPipelineTestReportSummary,
)
pipeline_content = {
"id": 46,
"project_id": 1,
"status": "pending",
"ref": "main",
"sha": "a91957a858320c0e17f3a0eca7cfacbff50ea29a",
"before_sha": "a91957a858320c0e17f3a0eca7cfacbff50ea29a",
"tag": False,
"yaml_errors": None,
"user": {
"name": "Administrator",
"username": "root",
"id": 1,
"state": "active",
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
"web_url": "http://localhost:3000/root",
},
"created_at": "2016-08-11T11:28:34.085Z",
"updated_at": "2016-08-11T11:32:35.169Z",
"started_at": None,
"finished_at": "2016-08-11T11:32:35.145Z",
"committed_at": None,
"duration": None,
"queued_duration": 0.010,
"coverage": None,
"web_url": "https://example.com/foo/bar/pipelines/46",
}
test_report_content = {
"total_time": 5,
"total_count": 1,
"success_count": 1,
"failed_count": 0,
"skipped_count": 0,
"error_count": 0,
"test_suites": [
{
"name": "Secure",
"total_time": 5,
"total_count": 1,
"success_count": 1,
"failed_count": 0,
"skipped_count": 0,
"error_count": 0,
"test_cases": [
{
"status": "success",
"name": "Security Reports can create an auto-remediation MR",
"classname": "vulnerability_management_spec",
"execution_time": 5,
"system_output": None,
"stack_trace": None,
}
],
}
],
}
test_report_summary_content = {
"total": {
"time": 1904,
"count": 3363,
"success": 3351,
"failed": 0,
"skipped": 12,
"error": 0,
"suite_error": None,
},
"test_suites": [
{
"name": "test",
"total_time": 1904,
"total_count": 3363,
"success_count": 3351,
"failed_count": 0,
"skipped_count": 12,
"error_count": 0,
"build_ids": [66004],
"suite_error": None,
}
],
}
@pytest.fixture
def resp_get_pipeline():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.GET,
url="http://localhost/api/v4/projects/1/pipelines/1",
json=pipeline_content,
content_type="application/json",
status=200,
)
yield rsps
@pytest.fixture
def resp_cancel_pipeline():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.POST,
url="http://localhost/api/v4/projects/1/pipelines/1/cancel",
json=pipeline_content,
content_type="application/json",
status=201,
)
yield rsps
@pytest.fixture
def resp_retry_pipeline():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.POST,
url="http://localhost/api/v4/projects/1/pipelines/1/retry",
json=pipeline_content,
content_type="application/json",
status=201,
)
yield rsps
@pytest.fixture
def resp_get_pipeline_test_report():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.GET,
url="http://localhost/api/v4/projects/1/pipelines/1/test_report",
json=test_report_content,
content_type="application/json",
status=200,
)
yield rsps
@pytest.fixture
def resp_get_pipeline_test_report_summary():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.GET,
url="http://localhost/api/v4/projects/1/pipelines/1/test_report_summary",
json=test_report_summary_content,
content_type="application/json",
status=200,
)
yield rsps
def test_get_project_pipeline(project, resp_get_pipeline):
pipeline = project.pipelines.get(1)
assert isinstance(pipeline, ProjectPipeline)
assert pipeline.ref == "main"
def test_cancel_project_pipeline(project, resp_cancel_pipeline):
pipeline = project.pipelines.get(1, lazy=True)
output = pipeline.cancel()
assert output["ref"] == "main"
def test_retry_project_pipeline(project, resp_retry_pipeline):
pipeline = project.pipelines.get(1, lazy=True)
output = pipeline.retry()
assert output["ref"] == "main"
def test_get_project_pipeline_test_report(project, resp_get_pipeline_test_report):
pipeline = project.pipelines.get(1, lazy=True)
test_report = pipeline.test_report.get()
assert isinstance(test_report, ProjectPipelineTestReport)
assert test_report.total_time == 5
assert test_report.test_suites[0]["name"] == "Secure"
def test_get_project_pipeline_test_report_summary(
project, resp_get_pipeline_test_report_summary
):
pipeline = project.pipelines.get(1, lazy=True)
test_report_summary = pipeline.test_report_summary.get()
assert isinstance(test_report_summary, ProjectPipelineTestReportSummary)
assert test_report_summary.total["count"] == 3363
assert test_report_summary.test_suites[0]["name"] == "test"
|