1313# under the License.
1414#
1515
16+ import copy
17+
1618from openstackclient .common import exceptions
1719from openstackclient .common import utils
1820from openstackclient .compute .v2 import flavor
1921from openstackclient .tests .compute .v2 import fakes as compute_fakes
22+ from openstackclient .tests import fakes
23+ from openstackclient .tests .identity .v3 import fakes as identity_fakes
2024from openstackclient .tests import utils as tests_utils
2125
2226
@@ -29,6 +33,13 @@ def setUp(self):
2933 self .flavors_mock = self .app .client_manager .compute .flavors
3034 self .flavors_mock .reset_mock ()
3135
36+ # Get a shortcut to the FlavorAccessManager Mock
37+ self .flavor_access_mock = self .app .client_manager .compute .flavor_access
38+ self .flavor_access_mock .reset_mock ()
39+
40+ self .projects_mock = self .app .client_manager .identity .projects
41+ self .projects_mock .reset_mock ()
42+
3243
3344class TestFlavorCreate (TestFlavor ):
3445
@@ -427,16 +438,23 @@ def test_flavor_list_long(self):
427438class TestFlavorSet (TestFlavor ):
428439
429440 # Return value of self.flavors_mock.find().
430- flavor = compute_fakes .FakeFlavor .create_one_flavor ()
441+ flavor = compute_fakes .FakeFlavor .create_one_flavor (
442+ attrs = {'os-flavor-access:is_public' : False })
431443
432444 def setUp (self ):
433445 super (TestFlavorSet , self ).setUp ()
434446
435447 self .flavors_mock .find .return_value = self .flavor
436448 self .flavors_mock .get .side_effect = exceptions .NotFound (None )
449+ # Return a project
450+ self .projects_mock .get .return_value = fakes .FakeResource (
451+ None ,
452+ copy .deepcopy (identity_fakes .PROJECT ),
453+ loaded = True ,
454+ )
437455 self .cmd = flavor .SetFlavor (self .app , None )
438456
439- def test_flavor_set (self ):
457+ def test_flavor_set_property (self ):
440458 arglist = [
441459 '--property' , 'FOO="B A R"' ,
442460 'baremetal'
@@ -452,6 +470,83 @@ def test_flavor_set(self):
452470 is_public = None )
453471 self .assertIsNone (result )
454472
473+ def test_flavor_set_project (self ):
474+ arglist = [
475+ '--project' , identity_fakes .project_id ,
476+ self .flavor .id ,
477+ ]
478+ verifylist = [
479+ ('project' , identity_fakes .project_id ),
480+ ('flavor' , self .flavor .id ),
481+ ]
482+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
483+
484+ result = self .cmd .take_action (parsed_args )
485+ self .assertIsNone (result )
486+
487+ self .flavor_access_mock .add_tenant_access .assert_called_with (
488+ self .flavor .id ,
489+ identity_fakes .project_id ,
490+ )
491+
492+ def test_flavor_set_no_project (self ):
493+ arglist = [
494+ '--project' , '' ,
495+ self .flavor .id ,
496+ ]
497+ verifylist = [
498+ ('project' , '' ),
499+ ('flavor' , self .flavor .id ),
500+ ]
501+
502+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
503+ self .assertRaises (exceptions .CommandError , self .cmd .take_action ,
504+ parsed_args )
505+
506+ def test_flavor_set_no_flavor (self ):
507+ arglist = [
508+ '--project' , identity_fakes .project_id ,
509+ ]
510+ verifylist = [
511+ ('project' , identity_fakes .project_id ),
512+ ]
513+
514+ self .assertRaises (tests_utils .ParserException ,
515+ self .check_parser ,
516+ self .cmd ,
517+ arglist ,
518+ verifylist )
519+
520+ def test_flavor_set_with_unexist_flavor (self ):
521+ self .flavors_mock .get .side_effect = exceptions .NotFound (None )
522+ self .flavors_mock .find .side_effect = exceptions .NotFound (None )
523+
524+ arglist = [
525+ '--project' , identity_fakes .project_id ,
526+ 'unexist_flavor' ,
527+ ]
528+ verifylist = [
529+ ('project' , identity_fakes .project_id ),
530+ ('flavor' , 'unexist_flavor' ),
531+ ]
532+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
533+
534+ self .assertRaises (exceptions .CommandError ,
535+ self .cmd .take_action ,
536+ parsed_args )
537+
538+ def test_flavor_set_nothing (self ):
539+ arglist = [
540+ self .flavor .id ,
541+ ]
542+ verifylist = [
543+ ('flavor' , self .flavor .id ),
544+ ]
545+
546+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
547+ self .assertRaises (exceptions .CommandError , self .cmd .take_action ,
548+ parsed_args )
549+
455550
456551class TestFlavorShow (TestFlavor ):
457552
0 commit comments