22
22
check_machine_valid ,
23
23
is_aq_managed_image ,
24
24
get_aq_build_metadata ,
25
+ delete_machine ,
25
26
)
26
27
from rabbit_consumer .vm_data import VmData
27
28
@@ -221,7 +222,7 @@ def test_consume_create_machine_hostnames_good_path(
221
222
patch (
222
223
"rabbit_consumer.message_consumer.get_aq_build_metadata"
223
224
) 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 ,
225
226
):
226
227
check_machine .return_value = True
227
228
get_image_meta .return_value = image_metadata
@@ -235,7 +236,7 @@ def test_consume_create_machine_hostnames_good_path(
235
236
openstack .get_server_networks .assert_called_with (vm_data )
236
237
237
238
# 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 ])
239
240
aq_api .create_machine .assert_called_once_with (rabbit_message , vm_data )
240
241
machine_name = aq_api .create_machine .return_value
241
242
@@ -255,7 +256,7 @@ def test_consume_create_machine_hostnames_good_path(
255
256
256
257
257
258
@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 ):
259
260
"""
260
261
Test that the function calls the correct functions in the correct order to delete a machine
261
262
"""
@@ -264,7 +265,9 @@ def test_consume_delete_machine_good_path(delete_machine, rabbit_message):
264
265
with patch ("rabbit_consumer.message_consumer.VmData" ) as data_patch :
265
266
handle_machine_delete (rabbit_message )
266
267
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
+ )
268
271
269
272
270
273
@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):
361
364
aq_metadata_obj .override_from_vm_meta .assert_called_once_with (
362
365
openstack_api .get_server_metadata .return_value
363
366
)
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