2323from osc_lib import exceptions
2424from osc_lib import utils as common_utils
2525from oslo_utils import timeutils
26+ import six
2627
2728from openstackclient .compute .v2 import server
2829from openstackclient .tests .unit .compute .v2 import fakes as compute_fakes
@@ -2565,12 +2566,40 @@ def test_server_migrate_with_disk_overcommit(self):
25652566 self .assertNotCalled (self .servers_mock .live_migrate )
25662567 self .assertNotCalled (self .servers_mock .migrate )
25672568
2569+ def test_server_migrate_with_host (self ):
2570+ # Tests that --host is not allowed for a cold migration.
2571+ arglist = [
2572+ '--host' , 'fakehost' , self .server .id ,
2573+ ]
2574+ verifylist = [
2575+ ('live' , None ),
2576+ ('live_migration' , False ),
2577+ ('host' , 'fakehost' ),
2578+ ('block_migration' , False ),
2579+ ('disk_overcommit' , False ),
2580+ ('wait' , False ),
2581+ ]
2582+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
2583+
2584+ ex = self .assertRaises (exceptions .CommandError , self .cmd .take_action ,
2585+ parsed_args )
2586+
2587+ # Make sure it's the error we expect.
2588+ self .assertIn ("--live-migration must be specified if "
2589+ "--block-migration, --disk-overcommit or --host is "
2590+ "specified" , six .text_type (ex ))
2591+ self .servers_mock .get .assert_called_with (self .server .id )
2592+ self .assertNotCalled (self .servers_mock .live_migrate )
2593+ self .assertNotCalled (self .servers_mock .migrate )
2594+
25682595 def test_server_live_migrate (self ):
25692596 arglist = [
25702597 '--live' , 'fakehost' , self .server .id ,
25712598 ]
25722599 verifylist = [
25732600 ('live' , 'fakehost' ),
2601+ ('live_migration' , False ),
2602+ ('host' , None ),
25742603 ('block_migration' , False ),
25752604 ('disk_overcommit' , False ),
25762605 ('wait' , False ),
@@ -2580,14 +2609,141 @@ def test_server_live_migrate(self):
25802609 self .app .client_manager .compute .api_version = \
25812610 api_versions .APIVersion ('2.24' )
25822611
2583- result = self .cmd .take_action (parsed_args )
2612+ with mock .patch .object (self .cmd .log , 'warning' ) as mock_warning :
2613+ result = self .cmd .take_action (parsed_args )
25842614
25852615 self .servers_mock .get .assert_called_with (self .server .id )
25862616 self .server .live_migrate .assert_called_with (block_migration = False ,
25872617 disk_over_commit = False ,
25882618 host = 'fakehost' )
25892619 self .assertNotCalled (self .servers_mock .migrate )
25902620 self .assertIsNone (result )
2621+ # A warning should have been logged for using --live.
2622+ mock_warning .assert_called_once ()
2623+ self .assertIn ('The --live option has been deprecated.' ,
2624+ six .text_type (mock_warning .call_args [0 ][0 ]))
2625+
2626+ def test_server_live_migrate_host_pre_2_30 (self ):
2627+ # Tests that the --host option is not supported for --live-migration
2628+ # before microversion 2.30 (the test defaults to 2.1).
2629+ arglist = [
2630+ '--live-migration' , '--host' , 'fakehost' , self .server .id ,
2631+ ]
2632+ verifylist = [
2633+ ('live' , None ),
2634+ ('live_migration' , True ),
2635+ ('host' , 'fakehost' ),
2636+ ('block_migration' , False ),
2637+ ('disk_overcommit' , False ),
2638+ ('wait' , False ),
2639+ ]
2640+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
2641+
2642+ ex = self .assertRaises (exceptions .CommandError , self .cmd .take_action ,
2643+ parsed_args )
2644+
2645+ # Make sure it's the error we expect.
2646+ self .assertIn ('--os-compute-api-version 2.30 or greater is required '
2647+ 'when using --host' , six .text_type (ex ))
2648+
2649+ self .servers_mock .get .assert_called_with (self .server .id )
2650+ self .assertNotCalled (self .servers_mock .live_migrate )
2651+ self .assertNotCalled (self .servers_mock .migrate )
2652+
2653+ def test_server_live_migrate_no_host (self ):
2654+ # Tests the --live-migration option without --host or --live.
2655+ arglist = [
2656+ '--live-migration' , self .server .id ,
2657+ ]
2658+ verifylist = [
2659+ ('live' , None ),
2660+ ('live_migration' , True ),
2661+ ('host' , None ),
2662+ ('block_migration' , False ),
2663+ ('disk_overcommit' , False ),
2664+ ('wait' , False ),
2665+ ]
2666+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
2667+
2668+ with mock .patch .object (self .cmd .log , 'warning' ) as mock_warning :
2669+ result = self .cmd .take_action (parsed_args )
2670+
2671+ self .servers_mock .get .assert_called_with (self .server .id )
2672+ self .server .live_migrate .assert_called_with (block_migration = False ,
2673+ disk_over_commit = False ,
2674+ host = None )
2675+ self .assertNotCalled (self .servers_mock .migrate )
2676+ self .assertIsNone (result )
2677+ # Since --live wasn't used a warning shouldn't have been logged.
2678+ mock_warning .assert_not_called ()
2679+
2680+ def test_server_live_migrate_with_host (self ):
2681+ # Tests the --live-migration option with --host but no --live.
2682+ # This requires --os-compute-api-version >= 2.30 so the test uses 2.30.
2683+ arglist = [
2684+ '--live-migration' , '--host' , 'fakehost' , self .server .id ,
2685+ ]
2686+ verifylist = [
2687+ ('live' , None ),
2688+ ('live_migration' , True ),
2689+ ('host' , 'fakehost' ),
2690+ ('block_migration' , False ),
2691+ ('disk_overcommit' , False ),
2692+ ('wait' , False ),
2693+ ]
2694+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
2695+
2696+ self .app .client_manager .compute .api_version = \
2697+ api_versions .APIVersion ('2.30' )
2698+
2699+ result = self .cmd .take_action (parsed_args )
2700+
2701+ self .servers_mock .get .assert_called_with (self .server .id )
2702+ # No disk_overcommit with microversion >= 2.25.
2703+ self .server .live_migrate .assert_called_with (block_migration = False ,
2704+ host = 'fakehost' )
2705+ self .assertNotCalled (self .servers_mock .migrate )
2706+ self .assertIsNone (result )
2707+
2708+ def test_server_live_migrate_without_host_override_live (self ):
2709+ # Tests the --live-migration option without --host and with --live.
2710+ # The --live-migration option will take precedence and a warning is
2711+ # logged for using --live.
2712+ arglist = [
2713+ '--live' , 'fakehost' , '--live-migration' , self .server .id ,
2714+ ]
2715+ verifylist = [
2716+ ('live' , 'fakehost' ),
2717+ ('live_migration' , True ),
2718+ ('host' , None ),
2719+ ('block_migration' , False ),
2720+ ('disk_overcommit' , False ),
2721+ ('wait' , False ),
2722+ ]
2723+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
2724+
2725+ with mock .patch .object (self .cmd .log , 'warning' ) as mock_warning :
2726+ result = self .cmd .take_action (parsed_args )
2727+
2728+ self .servers_mock .get .assert_called_with (self .server .id )
2729+ self .server .live_migrate .assert_called_with (block_migration = False ,
2730+ disk_over_commit = False ,
2731+ host = None )
2732+ self .assertNotCalled (self .servers_mock .migrate )
2733+ self .assertIsNone (result )
2734+ # A warning should have been logged for using --live.
2735+ mock_warning .assert_called_once ()
2736+ self .assertIn ('The --live option has been deprecated.' ,
2737+ six .text_type (mock_warning .call_args [0 ][0 ]))
2738+
2739+ def test_server_live_migrate_live_and_host_mutex (self ):
2740+ # Tests specifying both the --live and --host options which are in a
2741+ # mutex group so argparse should fail.
2742+ arglist = [
2743+ '--live' , 'fakehost' , '--host' , 'fakehost' , self .server .id ,
2744+ ]
2745+ self .assertRaises (utils .ParserException ,
2746+ self .check_parser , self .cmd , arglist , verify_args = [])
25912747
25922748 def test_server_block_live_migrate (self ):
25932749 arglist = [
0 commit comments