@@ -310,14 +310,6 @@ class TestKeypairList(TestKeypair):
310310 def setUp (self ):
311311 super (TestKeypairList , self ).setUp ()
312312
313- self .users_mock = self .app .client_manager .identity .users
314- self .users_mock .reset_mock ()
315- self .users_mock .get .return_value = fakes .FakeResource (
316- None ,
317- copy .deepcopy (identity_fakes .USER ),
318- loaded = True ,
319- )
320-
321313 self .keypairs_mock .list .return_value = self .keypairs
322314
323315 # Get the command object to test
@@ -378,6 +370,14 @@ def test_keypair_list_with_user(self):
378370 self .app .client_manager .compute .api_version = \
379371 api_versions .APIVersion ('2.10' )
380372
373+ users_mock = self .app .client_manager .identity .users
374+ users_mock .reset_mock ()
375+ users_mock .get .return_value = fakes .FakeResource (
376+ None ,
377+ copy .deepcopy (identity_fakes .USER ),
378+ loaded = True ,
379+ )
380+
381381 arglist = [
382382 '--user' , identity_fakes .user_name ,
383383 ]
@@ -388,7 +388,7 @@ def test_keypair_list_with_user(self):
388388
389389 columns , data = self .cmd .take_action (parsed_args )
390390
391- self . users_mock .get .assert_called_with (identity_fakes .user_name )
391+ users_mock .get .assert_called_with (identity_fakes .user_name )
392392 self .keypairs_mock .list .assert_called_with (
393393 user_id = identity_fakes .user_id ,
394394 )
@@ -423,6 +423,83 @@ def test_keypair_list_with_user_pre_v210(self):
423423 self .assertIn (
424424 '--os-compute-api-version 2.10 or greater is required' , str (ex ))
425425
426+ def test_keypair_list_with_project (self ):
427+
428+ # Filtering by user is support for nova api 2.10 or above
429+ self .app .client_manager .compute .api_version = \
430+ api_versions .APIVersion ('2.10' )
431+
432+ projects_mock = self .app .client_manager .identity .tenants
433+ projects_mock .reset_mock ()
434+ projects_mock .get .return_value = fakes .FakeResource (
435+ None ,
436+ copy .deepcopy (identity_fakes .PROJECT ),
437+ loaded = True ,
438+ )
439+
440+ users_mock = self .app .client_manager .identity .users
441+ users_mock .reset_mock ()
442+ users_mock .list .return_value = [
443+ fakes .FakeResource (
444+ None ,
445+ copy .deepcopy (identity_fakes .USER ),
446+ loaded = True ,
447+ ),
448+ ]
449+
450+ arglist = ['--project' , identity_fakes .project_name ]
451+ verifylist = [('project' , identity_fakes .project_name )]
452+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
453+
454+ columns , data = self .cmd .take_action (parsed_args )
455+
456+ projects_mock .get .assert_called_with (identity_fakes .project_name )
457+ users_mock .list .assert_called_with (tenant_id = identity_fakes .project_id )
458+ self .keypairs_mock .list .assert_called_with (
459+ user_id = identity_fakes .user_id ,
460+ )
461+
462+ self .assertEqual (('Name' , 'Fingerprint' , 'Type' ), columns )
463+ self .assertEqual (
464+ ((
465+ self .keypairs [0 ].name ,
466+ self .keypairs [0 ].fingerprint ,
467+ self .keypairs [0 ].type ,
468+ ), ),
469+ tuple (data )
470+ )
471+
472+ def test_keypair_list_with_project_pre_v210 (self ):
473+
474+ self .app .client_manager .compute .api_version = \
475+ api_versions .APIVersion ('2.9' )
476+
477+ arglist = ['--project' , identity_fakes .project_name ]
478+ verifylist = [('project' , identity_fakes .project_name )]
479+
480+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
481+ ex = self .assertRaises (
482+ exceptions .CommandError ,
483+ self .cmd .take_action ,
484+ parsed_args )
485+ self .assertIn (
486+ '--os-compute-api-version 2.10 or greater is required' , str (ex ))
487+
488+ def test_keypair_list_conflicting_user_options (self ):
489+
490+ # Filtering by user is support for nova api 2.10 or above
491+ self .app .client_manager .compute .api_version = \
492+ api_versions .APIVersion ('2.10' )
493+
494+ arglist = [
495+ '--user' , identity_fakes .user_name ,
496+ '--project' , identity_fakes .project_name ,
497+ ]
498+
499+ self .assertRaises (
500+ tests_utils .ParserException ,
501+ self .check_parser , self .cmd , arglist , None )
502+
426503
427504class TestKeypairShow (TestKeypair ):
428505
0 commit comments