|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +from pretend import call, call_recorder, stub |
| 16 | + |
| 17 | +from warehouse import tuf |
| 18 | + |
| 19 | + |
| 20 | +class TestTUF: |
| 21 | + server = "rstuf.api" |
| 22 | + task_id = "123456" |
| 23 | + |
| 24 | + def test_get_task_state(self, monkeypatch): |
| 25 | + state = "SUCCESS" |
| 26 | + |
| 27 | + resp_json = {"data": {"state": state}} |
| 28 | + resp = stub( |
| 29 | + raise_for_status=(lambda *a: None), json=(lambda *a, **kw: resp_json) |
| 30 | + ) |
| 31 | + get = call_recorder(lambda *a: resp) |
| 32 | + monkeypatch.setattr(tuf.requests, "get", get) |
| 33 | + |
| 34 | + result = tuf.get_task_state(self.server, self.task_id) |
| 35 | + |
| 36 | + assert result == state |
| 37 | + assert get.calls == [call(f"{self.server}/api/v1/task?task_id={self.task_id}")] |
| 38 | + |
| 39 | + def test_post_bootstrap(self, monkeypatch): |
| 40 | + payload = ["foo"] |
| 41 | + |
| 42 | + resp_json = {"data": {"task_id": self.task_id}} |
| 43 | + resp = stub( |
| 44 | + raise_for_status=(lambda *a: None), json=(lambda *a, **kw: resp_json) |
| 45 | + ) |
| 46 | + post = call_recorder(lambda *a, **kw: resp) |
| 47 | + monkeypatch.setattr(tuf.requests, "post", post) |
| 48 | + |
| 49 | + # Test success |
| 50 | + result = tuf.post_bootstrap(self.server, payload) |
| 51 | + |
| 52 | + assert result == self.task_id |
| 53 | + assert post.calls == [call(f"{self.server}/api/v1/bootstrap", json=payload)] |
| 54 | + |
| 55 | + # Test fail with incomplete response json |
| 56 | + del resp_json["data"] |
| 57 | + with pytest.raises(tuf.RSTUFError): |
| 58 | + tuf.post_bootstrap(self.server, payload) |
| 59 | + |
| 60 | + def test_wait_for_success(self, monkeypatch): |
| 61 | + get_task_state = call_recorder(lambda *a: "SUCCESS") |
| 62 | + monkeypatch.setattr(tuf, "get_task_state", get_task_state) |
| 63 | + tuf.wait_for_success(self.server, self.task_id) |
| 64 | + |
| 65 | + assert get_task_state.calls == [call(self.server, self.task_id)] |
| 66 | + |
| 67 | + @pytest.mark.parametrize( |
| 68 | + "state, iterations", |
| 69 | + [ |
| 70 | + ("PENDING", 20), |
| 71 | + ("RUNNING", 20), |
| 72 | + ("RECEIVED", 20), |
| 73 | + ("STARTED", 20), |
| 74 | + ("FAILURE", 1), |
| 75 | + ("ERRORED", 1), |
| 76 | + ("REVOKED", 1), |
| 77 | + ("REJECTED", 1), |
| 78 | + ("bogus", 1), |
| 79 | + ], |
| 80 | + ) |
| 81 | + def test_wait_for_success_error(self, state, iterations, monkeypatch): |
| 82 | + monkeypatch.setattr(tuf.time, "sleep", lambda *a: None) |
| 83 | + |
| 84 | + get_task_state = call_recorder(lambda *a: state) |
| 85 | + monkeypatch.setattr(tuf, "get_task_state", get_task_state) |
| 86 | + |
| 87 | + with pytest.raises(tuf.RSTUFError): |
| 88 | + tuf.wait_for_success(self.server, self.task_id) |
| 89 | + |
| 90 | + assert get_task_state.calls == [call(self.server, self.task_id)] * iterations |
0 commit comments