Skip to content

Commit 28d0768

Browse files
committed
cleanup
1 parent 8700d6d commit 28d0768

File tree

1 file changed

+47
-31
lines changed

1 file changed

+47
-31
lines changed

services/autoscaling/tests/unit/test_modules_auto_scaling_dynamic.py

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,8 @@ async def test_cluster_adapts_machines_on_the_fly( # noqa: PLR0915
13051305
assert analyzed_cluster.active_nodes
13061306
assert not analyzed_cluster.drained_nodes
13071307

1308-
# now we simulate that some of the services in the 1st batch have completed and that we are 1 below the max
1308+
#
1309+
# 4.now we simulate that some of the services in the 1st batch have completed and that we are 1 below the max
13091310
# a machine should switch off and another type should be started
13101311
completed_services_to_stop = random.sample(
13111312
first_batch_services,
@@ -1329,16 +1330,19 @@ async def test_cluster_adapts_machines_on_the_fly( # noqa: PLR0915
13291330
await auto_scale_cluster(
13301331
app=initialized_app, auto_scaling_mode=DynamicAutoscaling()
13311332
)
1332-
assert isinstance(spied_cluster_analysis.spy_return, Cluster)
1333-
assert spied_cluster_analysis.spy_return.active_nodes
1334-
assert not spied_cluster_analysis.spy_return.drained_nodes
1335-
1336-
# the last machine is found empty
1337-
mock_docker_set_node_found_empty.assert_called_with(
1338-
mock.ANY,
1339-
spied_cluster_analysis.spy_return.active_nodes[-1].node,
1340-
empty=True,
1341-
)
1333+
analyzed_cluster = assert_cluster_state(
1334+
spied_cluster_analysis,
1335+
expected_calls=1,
1336+
expected_num_machines=app_settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_MAX_INSTANCES,
1337+
)
1338+
assert analyzed_cluster.active_nodes
1339+
assert not analyzed_cluster.drained_nodes
1340+
# the last machine is found empty
1341+
mock_docker_set_node_found_empty.assert_called_with(
1342+
mock.ANY,
1343+
analyzed_cluster.active_nodes[-1].node,
1344+
empty=True,
1345+
)
13421346

13431347
# now we mock the get_node_found_empty so the next call will actually drain the machine
13441348
with mock.patch(
@@ -1351,22 +1355,29 @@ async def test_cluster_adapts_machines_on_the_fly( # noqa: PLR0915
13511355
await auto_scale_cluster(
13521356
app=initialized_app, auto_scaling_mode=DynamicAutoscaling()
13531357
)
1354-
mocked_get_node_empty_since.assert_called_once()
1355-
assert isinstance(spied_cluster_analysis.spy_return, Cluster)
1356-
assert spied_cluster_analysis.spy_return.active_nodes
1357-
assert not spied_cluster_analysis.spy_return.drained_nodes
1358-
# now the drained machine
1359-
drained_machine_instance_id = spied_cluster_analysis.spy_return.active_nodes[
1360-
-1
1361-
].ec2_instance.id
1358+
mocked_get_node_empty_since.assert_called_once()
1359+
analyzed_cluster = assert_cluster_state(
1360+
spied_cluster_analysis,
1361+
expected_calls=1,
1362+
expected_num_machines=app_settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_MAX_INSTANCES,
1363+
)
1364+
assert analyzed_cluster.active_nodes
1365+
assert not analyzed_cluster.drained_nodes
1366+
# now scaling again should find the drained machine
1367+
drained_machine_instance_id = analyzed_cluster.active_nodes[-1].ec2_instance.id
13621368
mocked_associate_ec2_instances_with_nodes.side_effect = create_fake_association(
13631369
create_fake_node, drained_machine_instance_id, None
13641370
)
13651371
await auto_scale_cluster(
13661372
app=initialized_app, auto_scaling_mode=DynamicAutoscaling()
13671373
)
1368-
assert spied_cluster_analysis.spy_return.active_nodes
1369-
assert spied_cluster_analysis.spy_return.drained_nodes
1374+
analyzed_cluster = assert_cluster_state(
1375+
spied_cluster_analysis,
1376+
expected_calls=1,
1377+
expected_num_machines=app_settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_MAX_INSTANCES,
1378+
)
1379+
assert analyzed_cluster.active_nodes
1380+
assert analyzed_cluster.drained_nodes
13701381

13711382
# this will initiate termination now
13721383
with mock.patch(
@@ -1380,23 +1391,28 @@ async def test_cluster_adapts_machines_on_the_fly( # noqa: PLR0915
13801391
await auto_scale_cluster(
13811392
app=initialized_app, auto_scaling_mode=DynamicAutoscaling()
13821393
)
1383-
mock_docker_tag_node.assert_called_with(
1384-
mock.ANY,
1385-
spied_cluster_analysis.spy_return.drained_nodes[-1].node,
1386-
tags=mock.ANY,
1387-
available=False,
1388-
)
1394+
mock_docker_tag_node.assert_called_with(
1395+
mock.ANY,
1396+
analyzed_cluster.drained_nodes[-1].node,
1397+
tags=mock.ANY,
1398+
available=False,
1399+
)
13891400

1390-
# and this should now recognize the node as terminating
1401+
# scaling again should find the terminating machine
13911402
mocked_associate_ec2_instances_with_nodes.side_effect = create_fake_association(
13921403
create_fake_node, drained_machine_instance_id, drained_machine_instance_id
13931404
)
13941405
await auto_scale_cluster(
13951406
app=initialized_app, auto_scaling_mode=DynamicAutoscaling()
13961407
)
1397-
assert spied_cluster_analysis.spy_return.active_nodes
1398-
assert not spied_cluster_analysis.spy_return.drained_nodes
1399-
assert spied_cluster_analysis.spy_return.terminating_nodes
1408+
analyzed_cluster = assert_cluster_state(
1409+
spied_cluster_analysis,
1410+
expected_calls=1,
1411+
expected_num_machines=app_settings.AUTOSCALING_EC2_INSTANCES.EC2_INSTANCES_MAX_INSTANCES,
1412+
)
1413+
assert analyzed_cluster.active_nodes
1414+
assert not analyzed_cluster.drained_nodes
1415+
assert analyzed_cluster.terminating_nodes
14001416

14011417
# now this will terminate it and straight away start a new machine type
14021418
with mock.patch(

0 commit comments

Comments
 (0)