|
1 | 1 | import pytest
|
| 2 | +import logging |
2 | 3 |
|
3 | 4 | from pubtools.pulplib import Repository, YumRepository, DetachedException
|
4 | 5 |
|
@@ -193,3 +194,58 @@ def test_related_repositories_detached_client():
|
193 | 194 |
|
194 | 195 | with pytest.raises(DetachedException):
|
195 | 196 | repo_binary_test.get_binary_repository()
|
| 197 | + |
| 198 | + |
| 199 | +def test_create_repository(client, requests_mocker): |
| 200 | + repo = YumRepository(id="yum_repo") |
| 201 | + repo.__dict__["_client"] = client |
| 202 | + |
| 203 | + requests_mocker.post( |
| 204 | + "https://pulp.example.com/pulp/api/v2/repositories/", |
| 205 | + json={}, |
| 206 | + ) |
| 207 | + |
| 208 | + out = client.create_repository(repo) |
| 209 | + # check return value of create_repository() call |
| 210 | + assert out.result() is None |
| 211 | + |
| 212 | + hist = requests_mocker.request_history |
| 213 | + # there should be exactly one request sent |
| 214 | + assert len(hist) == 1 |
| 215 | + |
| 216 | + query = hist[0].json() |
| 217 | + # check id of repository sent in request body |
| 218 | + assert query["id"] == "yum_repo" |
| 219 | + # check importer data sent in request body that |
| 220 | + # are automatically added for yum repository |
| 221 | + assert query["importer_type_id"] == "yum_importer" |
| 222 | + assert query["importer_config"] is None |
| 223 | + |
| 224 | + |
| 225 | +def test_create_repository_already_exists(client, requests_mocker, caplog): |
| 226 | + repo = YumRepository(id="yum_repo") |
| 227 | + repo.__dict__["_client"] = client |
| 228 | + |
| 229 | + requests_mocker.post( |
| 230 | + "https://pulp.example.com/pulp/api/v2/repositories/", |
| 231 | + status_code=409, |
| 232 | + text="Conflict 409 status", |
| 233 | + ) |
| 234 | + with caplog.at_level(logging.WARNING): |
| 235 | + out = client.create_repository(repo) |
| 236 | + # check return value of create_repository() call |
| 237 | + assert out.result() is None |
| 238 | + |
| 239 | + hist = requests_mocker.request_history |
| 240 | + # there should are exactly one request sent, 409 status is never retried |
| 241 | + assert len(hist) == 1 |
| 242 | + |
| 243 | + query = hist[0].json() |
| 244 | + # check id of repository sent in request body |
| 245 | + assert query["id"] == "yum_repo" |
| 246 | + # check importer data sent in request body that |
| 247 | + # are automatically added for yum repository |
| 248 | + assert query["importer_type_id"] == "yum_importer" |
| 249 | + assert query["importer_config"] is None |
| 250 | + # check logged information about existing repository |
| 251 | + assert "Repository yum_repo already exists" in caplog.text |
0 commit comments