1313# under the License.
1414#
1515
16+ import copy
1617from unittest import mock
1718from unittest .mock import call
1819import uuid
2324
2425from openstackclient .compute .v2 import keypair
2526from openstackclient .tests .unit .compute .v2 import fakes as compute_fakes
27+ from openstackclient .tests .unit import fakes
28+ from openstackclient .tests .unit .identity .v2_0 import fakes as identity_fakes
2629from openstackclient .tests .unit import utils as tests_utils
2730
2831
@@ -307,6 +310,14 @@ class TestKeypairList(TestKeypair):
307310 def setUp (self ):
308311 super (TestKeypairList , self ).setUp ()
309312
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+
310321 self .keypairs_mock .list .return_value = self .keypairs
311322
312323 # Get the command object to test
@@ -334,8 +345,8 @@ def test_keypair_list_no_options(self):
334345 )
335346
336347 def test_keypair_list_v22 (self ):
337- self .app .client_manager .compute .api_version = api_versions . APIVersion (
338- '2.2' )
348+ self .app .client_manager .compute .api_version = \
349+ api_versions . APIVersion ( '2.2' )
339350
340351 arglist = []
341352 verifylist = []
@@ -361,6 +372,57 @@ def test_keypair_list_v22(self):
361372 tuple (data )
362373 )
363374
375+ def test_keypair_list_with_user (self ):
376+
377+ # Filtering by user is support for nova api 2.10 or above
378+ self .app .client_manager .compute .api_version = \
379+ api_versions .APIVersion ('2.10' )
380+
381+ arglist = [
382+ '--user' , identity_fakes .user_name ,
383+ ]
384+ verifylist = [
385+ ('user' , identity_fakes .user_name ),
386+ ]
387+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
388+
389+ columns , data = self .cmd .take_action (parsed_args )
390+
391+ self .users_mock .get .assert_called_with (identity_fakes .user_name )
392+ self .keypairs_mock .list .assert_called_with (
393+ user_id = identity_fakes .user_id ,
394+ )
395+
396+ self .assertEqual (('Name' , 'Fingerprint' , 'Type' ), columns )
397+ self .assertEqual (
398+ ((
399+ self .keypairs [0 ].name ,
400+ self .keypairs [0 ].fingerprint ,
401+ self .keypairs [0 ].type ,
402+ ), ),
403+ tuple (data )
404+ )
405+
406+ def test_keypair_list_with_user_pre_v210 (self ):
407+
408+ self .app .client_manager .compute .api_version = \
409+ api_versions .APIVersion ('2.9' )
410+
411+ arglist = [
412+ '--user' , identity_fakes .user_name ,
413+ ]
414+ verifylist = [
415+ ('user' , identity_fakes .user_name ),
416+ ]
417+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
418+
419+ ex = self .assertRaises (
420+ exceptions .CommandError ,
421+ self .cmd .take_action ,
422+ parsed_args )
423+ self .assertIn (
424+ '--os-compute-api-version 2.10 or greater is required' , str (ex ))
425+
364426
365427class TestKeypairShow (TestKeypair ):
366428
0 commit comments