Skip to content

Commit 0af0690

Browse files
committed
Delete hostname or delete network adapter in Aq
The hostname implicitly will delete the network adapter, so we should shift this to an if-else rather than both always condition
1 parent 2f6a623 commit 0af0690

File tree

2 files changed

+87
-15
lines changed

2 files changed

+87
-15
lines changed

OpenStack-Rabbit-Consumer/rabbit_consumer/message_consumer.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,24 @@ def delete_machine(
8686
# of deletion orders which it enforces...
8787

8888
hostname = aq_api.search_host_by_machine(machine_name)
89+
machine_details = aq_api.get_machine_details(machine_name)
90+
91+
# We have to clean-up all the interfaces and addresses first
92+
# we could have a machine which points to a different hostname
8993
if hostname:
9094
if aq_api.check_host_exists(hostname):
9195
# This is a different hostname to the one we have in the message
9296
# so, we need to delete it
9397
logger.info("Host exists for %s. Deleting old", hostname)
9498
aq_api.delete_host(hostname)
95-
96-
# We have to clean-up all the interfaces and addresses first
97-
machine_details = aq_api.get_machine_details(machine_name)
98-
99-
# First delete the interfaces
100-
ipv4_address = socket.gethostbyname(hostname)
101-
if ipv4_address in machine_details:
102-
aq_api.delete_address(ipv4_address, machine_name)
103-
104-
if "eth0" in machine_details:
105-
aq_api.delete_interface(machine_name)
99+
else:
100+
# Delete the interfaces
101+
ipv4_address = socket.gethostbyname(hostname)
102+
if ipv4_address in machine_details:
103+
aq_api.delete_address(ipv4_address, machine_name)
104+
105+
if "eth0" in machine_details:
106+
aq_api.delete_interface(machine_name)
106107

107108
logger.info("Machine exists for %s. Deleting old", vm_data.virtual_machine_id)
108109

OpenStack-Rabbit-Consumer/test/test_message_consumer.py

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
check_machine_valid,
2323
is_aq_managed_image,
2424
get_aq_build_metadata,
25+
delete_machine,
2526
)
2627
from rabbit_consumer.vm_data import VmData
2728

@@ -221,7 +222,7 @@ def test_consume_create_machine_hostnames_good_path(
221222
patch(
222223
"rabbit_consumer.message_consumer.get_aq_build_metadata"
223224
) as get_image_meta,
224-
patch("rabbit_consumer.message_consumer.delete_machine") as delete_machine,
225+
patch("rabbit_consumer.message_consumer.delete_machine") as delete_machine_mock,
225226
):
226227
check_machine.return_value = True
227228
get_image_meta.return_value = image_metadata
@@ -235,7 +236,7 @@ def test_consume_create_machine_hostnames_good_path(
235236
openstack.get_server_networks.assert_called_with(vm_data)
236237

237238
# Check main Aq Flow
238-
delete_machine.assert_called_once_with(vm_data, network_details[0])
239+
delete_machine_mock.assert_called_once_with(vm_data, network_details[0])
239240
aq_api.create_machine.assert_called_once_with(rabbit_message, vm_data)
240241
machine_name = aq_api.create_machine.return_value
241242

@@ -255,7 +256,7 @@ def test_consume_create_machine_hostnames_good_path(
255256

256257

257258
@patch("rabbit_consumer.message_consumer.delete_machine")
258-
def test_consume_delete_machine_good_path(delete_machine, rabbit_message):
259+
def test_consume_delete_machine_good_path(delete_machine_mock, rabbit_message):
259260
"""
260261
Test that the function calls the correct functions in the correct order to delete a machine
261262
"""
@@ -264,7 +265,9 @@ def test_consume_delete_machine_good_path(delete_machine, rabbit_message):
264265
with patch("rabbit_consumer.message_consumer.VmData") as data_patch:
265266
handle_machine_delete(rabbit_message)
266267

267-
delete_machine.assert_called_once_with(vm_data=data_patch.from_message.return_value)
268+
delete_machine_mock.assert_called_once_with(
269+
vm_data=data_patch.from_message.return_value
270+
)
268271

269272

270273
@patch("rabbit_consumer.message_consumer.is_aq_managed_image")
@@ -361,3 +364,71 @@ def test_get_aq_build_metadata(openstack_api, aq_metadata_class, vm_data):
361364
aq_metadata_obj.override_from_vm_meta.assert_called_once_with(
362365
openstack_api.get_server_metadata.return_value
363366
)
367+
368+
369+
@patch("rabbit_consumer.message_consumer.aq_api")
370+
def test_delete_machine_hostname_only(aq_api, vm_data, openstack_address):
371+
"""
372+
Tests that the function deletes a host then exits if no machine is found
373+
"""
374+
aq_api.check_host_exists.return_value = True
375+
aq_api.search_machine_by_serial.return_value = None
376+
377+
delete_machine(vm_data, openstack_address)
378+
aq_api.delete_host.assert_called_once_with(openstack_address.hostname)
379+
aq_api.delete_machine.assert_not_called()
380+
381+
382+
@patch("rabbit_consumer.message_consumer.aq_api")
383+
def test_delete_machine_by_serial(aq_api, vm_data, openstack_address):
384+
"""
385+
Tests that the function deletes a host then a machine
386+
assuming both were found
387+
"""
388+
# Assume our host address doesn't match the machine record
389+
# but the machine does have a hostname which is valid...
390+
aq_api.check_host_exists.side_effect = [False, True]
391+
392+
aq_api.search_host_by_machine.return_value = "host.example.com"
393+
aq_api.get_machine_details.return_value = ""
394+
395+
delete_machine(vm_data, openstack_address)
396+
397+
aq_api.check_host_exists.assert_has_calls(
398+
[call(openstack_address.hostname), call("host.example.com")]
399+
)
400+
aq_api.delete_host.assert_called_once_with("host.example.com")
401+
402+
403+
@patch("rabbit_consumer.message_consumer.aq_api")
404+
@patch("rabbit_consumer.message_consumer.socket")
405+
def test_delete_machine_no_hostname(socket_api, aq_api, vm_data):
406+
aq_api.check_host_exists.return_value = False
407+
408+
ip_address = "127.0.0.1"
409+
socket_api.gethostbyname.return_value = ip_address
410+
411+
machine_name = aq_api.search_machine_by_serial.return_value
412+
aq_api.get_machine_details.return_value = f"eth0: {ip_address}"
413+
414+
delete_machine(vm_data, NonCallableMock())
415+
aq_api.delete_address.assert_called_once_with(ip_address, machine_name)
416+
aq_api.delete_interface.assert_called_once_with(machine_name)
417+
418+
419+
@patch("rabbit_consumer.message_consumer.aq_api")
420+
@patch("rabbit_consumer.message_consumer.socket")
421+
def test_delete_machine_always_called(socket_api, aq_api, vm_data):
422+
"""
423+
Tests that the function always calls the delete machine function
424+
"""
425+
aq_api.check_host_exists.return_value = False
426+
socket_api.gethostbyname.return_value = "123123"
427+
428+
aq_api.get_machine_details.return_value = "Machine Details"
429+
430+
machine_name = "machine_name"
431+
aq_api.search_machine_by_serial.return_value = machine_name
432+
433+
delete_machine(vm_data, NonCallableMock())
434+
aq_api.delete_machine.assert_called_once_with(machine_name)

0 commit comments

Comments
 (0)