Skip to content

Commit 6919ab6

Browse files
committed
Fix up a few of the tests
1 parent 151e4b2 commit 6919ab6

File tree

1 file changed

+33
-44
lines changed

1 file changed

+33
-44
lines changed

capi_janitor/tests/openstack/test_operator.py

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,21 @@
66
import easykube
77
from easykube.rest.util import PropertyDict
88

9-
from capi_janitor.openstack import operator, openstack
10-
from capi_janitor.openstack.operator import OPENSTACK_USER_VOLUMES_RECLAIM_PROPERTY
119
from capi_janitor.openstack import openstack
10+
from capi_janitor.openstack import operator
11+
from capi_janitor.openstack.operator import OPENSTACK_USER_VOLUMES_RECLAIM_PROPERTY
1212

1313

1414
# Helper to create an async iterable
15-
def aiter(iterable):
16-
class AsyncIter:
17-
def __init__(self, it):
18-
self.it = iter(it)
19-
20-
def __aiter__(self):
21-
return self
15+
class AsyncIterList:
16+
def __init__(self, items):
17+
self.items = items
18+
self.kwargs = None
2219

23-
async def __anext__(self):
24-
try:
25-
return next(self.it)
26-
except StopIteration:
27-
raise StopAsyncIteration
28-
29-
return AsyncIter(iterable)
20+
async def list(self, **kwargs):
21+
self.kwargs = kwargs
22+
for item in self.items:
23+
yield item
3024

3125

3226
class TestOperator(unittest.IsolatedAsyncioTestCase):
@@ -39,55 +33,50 @@ async def test_operator(self):
3933
mock_easykube.aclose.assert_awaited_once_with()
4034

4135
async def test_fips_for_cluster(self):
36+
prefix = "Floating IP for Kubernetes external service from cluster"
4237
fips = [
4338
mock.Mock(
44-
description="Floating IP for Kubernetes external service from cluster mycluster",
45-
id=1,
39+
description=f"{prefix} mycluster",
4640
),
4741
mock.Mock(
48-
description="Floating IP for Kubernetes external service from cluster othercluster",
49-
id=2,
42+
description=f"{prefix} asdf",
5043
),
51-
mock.Mock(description="Some other description", id=3),
44+
mock.Mock(description="Some other description"),
5245
mock.Mock(
53-
description="Floating IP for Kubernetes external service from cluster mycluster",
54-
id=4,
46+
description=f"{prefix} mycluster",
5547
),
5648
]
49+
resource_mock = AsyncIterList(fips)
5750

58-
resource_mock = mock.Mock()
59-
resource_mock.list = mock.AsyncMock(return_value=aiter(fips))
60-
61-
result = []
62-
async for fip in operator.fips_for_cluster(resource_mock, "mycluster"):
63-
result.append(fip)
51+
result = [
52+
fip async for fip in operator.fips_for_cluster(resource_mock, "mycluster")
53+
]
6454

6555
self.assertEqual(len(result), 2)
66-
self.assertEqual(result[0].id, 1)
67-
self.assertEqual(result[1].id, 4)
56+
self.assertEqual(result[0], fips[0])
57+
self.assertEqual(result[1], fips[3])
6858

6959
async def test_lbs_for_cluster(self):
7060
lbs = [
71-
mock.Mock(name="lb1", id=1),
72-
mock.Mock(name="lb2", id=2),
73-
mock.Mock(name="lb3", id=3),
74-
mock.Mock(name="lb4", id=4),
61+
mock.Mock(name="lb0"),
62+
mock.Mock(name="lb1"),
63+
mock.Mock(name="lb2"),
64+
mock.Mock(name="lb3"),
7565
]
66+
# can't pass name into mock.Mock() to do this
7667
lbs[0].name = "kube_service_mycluster_api"
7768
lbs[1].name = "kube_service_othercluster_api"
7869
lbs[2].name = "fake_service_mycluster_api"
7970
lbs[3].name = "kube_service_mycluster_ui"
71+
resource_mock = AsyncIterList(lbs)
8072

81-
resource_mock = mock.Mock()
82-
resource_mock.list = mock.AsyncMock(return_value=aiter(lbs))
83-
84-
result = []
85-
async for lb in operator.lbs_for_cluster(resource_mock, "mycluster"):
86-
result.append(lb)
87-
73+
result = [
74+
lb async for lb in operator.lbs_for_cluster(resource_mock, "mycluster")
75+
]
76+
8877
self.assertEqual(len(result), 2)
89-
self.assertEqual(result[0].id, 1)
90-
self.assertEqual(result[1].id, 4)
78+
self.assertEqual(result[0], lbs[0])
79+
self.assertEqual(result[1], lbs[3])
9180

9281
async def test_secgroups_for_cluster(self):
9382
secgroups = [

0 commit comments

Comments
 (0)