| |
@@ -0,0 +1,62 @@
|
| |
+ import pytest
|
| |
+ import mock
|
| |
+ from munch import Munch
|
| |
+ from copr.v3.helpers import wait, succeeded
|
| |
+ from copr.v3 import BuildProxy, CoprException
|
| |
+
|
| |
+
|
| |
+ class TestHelpers(object):
|
| |
+ def test_succeeded(self):
|
| |
+ b1 = Munch(state="succeeded")
|
| |
+ b2 = Munch(state="succeeded")
|
| |
+ b3 = Munch(state="running")
|
| |
+ b4 = Munch(state="failed")
|
| |
+
|
| |
+ assert succeeded(b1)
|
| |
+ assert succeeded([b1, b2])
|
| |
+ assert not succeeded(b3)
|
| |
+ assert not succeeded([b1, b2, b4])
|
| |
+
|
| |
+
|
| |
+ class TestWait(object):
|
| |
+ @mock.patch("copr.v3.proxies.build.BuildProxy.get")
|
| |
+ def test_wait(self, mock_get):
|
| |
+ build = MunchMock(id=1, state="importing")
|
| |
+
|
| |
+ mock_get.return_value = MunchMock(id=1, state="succeeded")
|
| |
+ assert wait(build)
|
| |
+
|
| |
+ mock_get.return_value = MunchMock(id=1, state="unknown")
|
| |
+ with pytest.raises(CoprException) as ex:
|
| |
+ wait(build)
|
| |
+ assert "Unknown status" in str(ex)
|
| |
+
|
| |
+ @mock.patch("copr.v3.proxies.build.BuildProxy.get")
|
| |
+ def test_wait_list(self, mock_get):
|
| |
+ builds = [MunchMock(id=1, state="succeeded"), MunchMock(id=2, state="failed")]
|
| |
+ mock_get.side_effect = lambda id: builds[id-1]
|
| |
+ assert wait(builds)
|
| |
+
|
| |
+ @mock.patch("time.time")
|
| |
+ @mock.patch("copr.v3.proxies.build.BuildProxy.get")
|
| |
+ def test_wait_timeout(self, mock_get, mock_time):
|
| |
+ build = MunchMock(id=1, state="importing")
|
| |
+
|
| |
+ mock_get.return_value = MunchMock(id=1, state="running")
|
| |
+ mock_time.return_value = 0
|
| |
+ with pytest.raises(CoprException) as ex:
|
| |
+ wait(build, interval=0, timeout=-10)
|
| |
+ assert "Timeouted" in str(ex)
|
| |
+
|
| |
+ @mock.patch("copr.v3.proxies.build.BuildProxy.get")
|
| |
+ def test_wait_callback(self, mock_get):
|
| |
+ build = MunchMock(id=1, state="importing")
|
| |
+
|
| |
+ callback = mock.Mock()
|
| |
+ mock_get.return_value = MunchMock(id=1, state="failed")
|
| |
+ wait(build, interval=0, callback=callback)
|
| |
+ assert callback.called
|
| |
+
|
| |
+
|
| |
+ class MunchMock(Munch):
|
| |
+ __proxy__ = BuildProxy({})
|
| |
This is supposed to solve RhBug: 1258970
https://bugzilla.redhat.com/show_bug.cgi?id=1258970
It is just a preview, I want to add also unit tests before this gets merged.
There are a couple of things, that I want to discuss.
1) Name of the function -
wait
is very general, which can be good and bad. It could be modified in the future, to wait for various things, such as modules, containers, whatever. Do you prefer this, or rather more explicitwatch_builds
,wait_for_builds
, etc?2)
build_proxy
parameter. We agreed, that this function would fit in helpers file. However, in such case, it will require abuild_proxy
,config
orclient
parameter, because otherwise there is no way to request the current build state from frontend. The other option is not having it as a helper, but rather inBuildProxy
(i.e.BuildProxy.wait(...)
).Also, example usage is here: