|
16 | 16 | from podman.domain.containers import Container |
17 | 17 | from podman.domain.containers_create import CreateMixin |
18 | 18 | from podman.domain.containers_manager import ContainersManager |
19 | | -from podman.errors import ImageNotFound, NotFound |
| 19 | +from podman.errors import NotFound |
20 | 20 |
|
21 | 21 | FIRST_CONTAINER = { |
22 | 22 | "Id": "87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd", |
@@ -338,17 +338,45 @@ def test_create(self, mock): |
338 | 338 |
|
339 | 339 | @requests_mock.Mocker() |
340 | 340 | def test_create_404(self, mock): |
| 341 | + # mock the first POST to return 404, |
| 342 | + # then the second POST (after pulling the image) to return 201 |
341 | 343 | mock.post( |
342 | 344 | tests.LIBPOD_URL + "/containers/create", |
343 | | - status_code=404, |
344 | | - json={ |
345 | | - "cause": "Image not found", |
346 | | - "message": "Image not found", |
347 | | - "response": 404, |
348 | | - }, |
| 345 | + [ |
| 346 | + { |
| 347 | + "status_code": 404, |
| 348 | + "json": { |
| 349 | + "cause": "Image not found", |
| 350 | + "message": "Image not found", |
| 351 | + "response": 404, |
| 352 | + }, |
| 353 | + }, |
| 354 | + { |
| 355 | + "status_code": 201, |
| 356 | + "json": { |
| 357 | + "Id": "87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd", |
| 358 | + "Warnings": [], |
| 359 | + }, |
| 360 | + }, |
| 361 | + ], |
| 362 | + ) |
| 363 | + self.client.images.pull = MagicMock() |
| 364 | + mock.get( |
| 365 | + tests.LIBPOD_URL + f"/containers/{FIRST_CONTAINER['Id']}/json", |
| 366 | + json=FIRST_CONTAINER, |
| 367 | + ) |
| 368 | + actual = self.client.containers.create("fedora", "/usr/bin/ls", cpu_count=9999) |
| 369 | + self.client.images.pull.assert_called_once_with( |
| 370 | + "fedora", |
| 371 | + auth_config=None, |
| 372 | + platform=None, |
| 373 | + policy="missing", |
349 | 374 | ) |
350 | | - with self.assertRaises(ImageNotFound): |
351 | | - self.client.containers.create("fedora", "/usr/bin/ls", cpu_count=9999) |
| 375 | + self.assertIsInstance(actual, Container) |
| 376 | + self.assertEqual(actual.id, FIRST_CONTAINER['Id']) |
| 377 | + # 2 POSTs for create |
| 378 | + # 1 GET for container json |
| 379 | + self.assertEqual(mock.call_count, 3) |
352 | 380 |
|
353 | 381 | @requests_mock.Mocker() |
354 | 382 | def test_create_parse_host_port(self, mock): |
@@ -644,6 +672,67 @@ def test_run(self, mock): |
644 | 672 | self.assertEqual(next(actual), b"This is a unittest - line 1") |
645 | 673 | self.assertEqual(next(actual), b"This is a unittest - line 2") |
646 | 674 |
|
| 675 | + @requests_mock.Mocker() |
| 676 | + def test_run_404(self, mock): |
| 677 | + # mock the first POST to return 404, |
| 678 | + # then the second POST (after pulling the image) to return 201 |
| 679 | + mock.post( |
| 680 | + tests.LIBPOD_URL + "/containers/create", |
| 681 | + [ |
| 682 | + { |
| 683 | + "status_code": 404, |
| 684 | + "json": { |
| 685 | + "cause": "Image not found", |
| 686 | + "message": "Image not found", |
| 687 | + "response": 404, |
| 688 | + }, |
| 689 | + }, |
| 690 | + { |
| 691 | + "status_code": 201, |
| 692 | + "json": { |
| 693 | + "Id": "87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd", |
| 694 | + "Warnings": [], |
| 695 | + }, |
| 696 | + }, |
| 697 | + ], |
| 698 | + ) |
| 699 | + self.client.images.pull = MagicMock() |
| 700 | + mock.post( |
| 701 | + tests.LIBPOD_URL |
| 702 | + + "/containers/87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd/start", |
| 703 | + status_code=204, |
| 704 | + ) |
| 705 | + mock.get( |
| 706 | + tests.LIBPOD_URL + f"/containers/{FIRST_CONTAINER['Id']}/json", |
| 707 | + json=FIRST_CONTAINER, |
| 708 | + ) |
| 709 | + |
| 710 | + mock_logs = ( |
| 711 | + b"This is a unittest - line 1", |
| 712 | + b"This is a unittest - line 2", |
| 713 | + ) |
| 714 | + |
| 715 | + with patch.multiple(Container, logs=DEFAULT, wait=DEFAULT, autospec=True) as mock_container: |
| 716 | + mock_container["wait"].return_value = 0 |
| 717 | + mock_container["logs"].return_value = iter(mock_logs) |
| 718 | + |
| 719 | + actual = self.client.containers.run("fedora", "/usr/bin/ls") |
| 720 | + self.client.images.pull.assert_called_once_with( |
| 721 | + "fedora", |
| 722 | + auth_config=None, |
| 723 | + platform=None, |
| 724 | + policy="missing", |
| 725 | + ) |
| 726 | + self.assertIsInstance(actual, bytes) |
| 727 | + self.assertEqual(actual, b"This is a unittest - line 1This is a unittest - line 2") |
| 728 | + # 2 POSTs for create |
| 729 | + # 1 POST for start |
| 730 | + # 1 GET for container json |
| 731 | + # 1 GET for reload |
| 732 | + for r in mock.request_history: |
| 733 | + print(r) |
| 734 | + self.assertEqual(mock.call_count, 5) |
| 735 | + |
647 | 736 |
|
648 | 737 | if __name__ == "__main__": |
649 | 738 | unittest.main() |
0 commit comments