|
18 | 18 | from datetime import datetime |
19 | 19 |
|
20 | 20 | import pytest |
| 21 | +from zunclient.exceptions import Conflict |
21 | 22 |
|
22 | 23 | from chi.container import Container, download, upload |
| 24 | +from chi.exception import ContainerCreateWaitError, ResourceError |
23 | 25 |
|
24 | 26 |
|
25 | 27 | @pytest.fixture() |
@@ -110,3 +112,97 @@ def test_download_extracts_tar_and_writes_file(mocker): |
110 | 112 | assert os.path.exists(dest_path) |
111 | 113 | with open(dest_path, "rb") as f: |
112 | 114 | assert f.read() == file_content |
| 115 | + |
| 116 | + |
| 117 | +def test_submit_idempotent_returns_existing_without_create(mocker): |
| 118 | + # idempotent=true, wait=true |
| 119 | + chi_container = Container(name="dup-name", image_ref="img") |
| 120 | + existing_zun_container = mocker.Mock(uuid="existing-uuid", status="Running") |
| 121 | + |
| 122 | + mocker.patch("chi.container.get_container", return_value=existing_zun_container) |
| 123 | + create_mock = mocker.patch("chi.container.create_container") |
| 124 | + |
| 125 | + submit_result = chi_container.submit( |
| 126 | + idempotent=True, wait_for_active=True, wait_timeout=123, show="text" |
| 127 | + ) |
| 128 | + create_mock.assert_not_called() |
| 129 | + existing_zun_container.wait.assert_called_once_with(status="Running", timeout=123) |
| 130 | + existing_zun_container.show.assert_called_once_with( |
| 131 | + type="text", wait_for_active=True |
| 132 | + ) |
| 133 | + |
| 134 | + # submit returns only on idempotent=true |
| 135 | + assert submit_result is existing_zun_container |
| 136 | + |
| 137 | + |
| 138 | +def test_submit_idempotent_returns_existing_without_create_no_wait(mocker): |
| 139 | + # idempotent=true, wait=false |
| 140 | + chi_container = Container(name="dup-name", image_ref="img") |
| 141 | + existing_zun_container = mocker.Mock(uuid="existing-uuid", status="Running") |
| 142 | + |
| 143 | + mocker.patch("chi.container.get_container", return_value=existing_zun_container) |
| 144 | + create_mock = mocker.patch("chi.container.create_container") |
| 145 | + |
| 146 | + submit_result = chi_container.submit( |
| 147 | + idempotent=True, wait_for_active=False, show=None |
| 148 | + ) |
| 149 | + create_mock.assert_not_called() |
| 150 | + existing_zun_container.wait.assert_not_called() |
| 151 | + existing_zun_container.show.assert_not_called() |
| 152 | + |
| 153 | + # submit returns only on idempotent=true |
| 154 | + assert submit_result is existing_zun_container |
| 155 | + |
| 156 | + |
| 157 | +def test_submit_preserves_reference_on_create_wait_failure(mocker): |
| 158 | + """Ensure that we keep the zun container id, even if create fails. |
| 159 | +
|
| 160 | + This case can arise because the container moves to an error state, or if |
| 161 | + the wait times out for another reason. |
| 162 | + """ |
| 163 | + chi_container = Container(name="test", image_ref="img") |
| 164 | + leaked_zun_container = mocker.Mock(uuid="leaked-uuid", status="Error") |
| 165 | + |
| 166 | + mocker.patch( |
| 167 | + "chi.container.create_container", |
| 168 | + side_effect=ContainerCreateWaitError( |
| 169 | + zun_container=leaked_zun_container, cause=RuntimeError |
| 170 | + ), |
| 171 | + ) |
| 172 | + zun_mock = mocker.patch("chi.container.zun")() |
| 173 | + zun_mock.containers.get.return_value = leaked_zun_container |
| 174 | + |
| 175 | + with pytest.raises(ResourceError): |
| 176 | + chi_container.submit(wait_for_active=False, show=None) |
| 177 | + |
| 178 | + assert chi_container.id == "leaked-uuid" |
| 179 | + assert chi_container._status == "Error" |
| 180 | + |
| 181 | + |
| 182 | +def test_submit_duplicate_name_tracks_created_uuid(mocker): |
| 183 | + """Test the case where we re-run submit after a failure. |
| 184 | +
|
| 185 | + An "old" container alreday already exists, with name = "dup-name". |
| 186 | + If idempotent=false, it should be possible to make a new container with the same name. |
| 187 | + However, name-based lookups will fail with a 409. |
| 188 | + """ |
| 189 | + |
| 190 | + chi_container = Container(name="dup-name", image_ref="img") |
| 191 | + new_zun_container = mocker.Mock(uuid="new-uuid", status="Running") |
| 192 | + |
| 193 | + def _get_side_effect(ref): |
| 194 | + if ref == "dup-name": |
| 195 | + raise Conflict( |
| 196 | + "Multiple containers exist with same name. Please use the container uuid instead." |
| 197 | + ) |
| 198 | + if ref == "new-uuid": |
| 199 | + return new_zun_container |
| 200 | + raise AssertionError(ref) |
| 201 | + |
| 202 | + mocker.patch("chi.container.create_container", return_value=new_zun_container) |
| 203 | + zun_mock = mocker.patch("chi.container.zun")() |
| 204 | + zun_mock.containers.get.side_effect = _get_side_effect |
| 205 | + |
| 206 | + # disable optional behavor from wait_for_active and show |
| 207 | + chi_container.submit(wait_for_active=False, show=None) |
| 208 | + assert chi_container.id == "new-uuid" |
0 commit comments