@@ -54,10 +54,10 @@ def setUp(self):
5454
5555class TestKeypairCreate (TestKeypair ):
5656
57- keypair = compute_fakes .FakeKeypair .create_one_keypair ()
58-
5957 def setUp (self ):
60- super (TestKeypairCreate , self ).setUp ()
58+ super ().setUp ()
59+
60+ self .keypair = compute_fakes .FakeKeypair .create_one_keypair ()
6161
6262 self .columns = (
6363 'fingerprint' ,
@@ -77,8 +77,11 @@ def setUp(self):
7777
7878 self .sdk_client .create_keypair .return_value = self .keypair
7979
80- def test_key_pair_create_no_options (self ):
81-
80+ @mock .patch .object (
81+ keypair , '_generate_keypair' ,
82+ return_value = keypair .Keypair ('private' , 'public' ),
83+ )
84+ def test_key_pair_create_no_options (self , mock_generate ):
8285 arglist = [
8386 self .keypair .name ,
8487 ]
@@ -90,18 +93,14 @@ def test_key_pair_create_no_options(self):
9093 columns , data = self .cmd .take_action (parsed_args )
9194
9295 self .sdk_client .create_keypair .assert_called_with (
93- name = self .keypair .name
96+ name = self .keypair .name ,
97+ public_key = mock_generate .return_value .public_key ,
9498 )
9599
96100 self .assertEqual ({}, columns )
97101 self .assertEqual ({}, data )
98102
99103 def test_keypair_create_public_key (self ):
100- # overwrite the setup one because we want to omit private_key
101- self .keypair = compute_fakes .FakeKeypair .create_one_keypair (
102- no_pri = True )
103- self .sdk_client .create_keypair .return_value = self .keypair
104-
105104 self .data = (
106105 self .keypair .fingerprint ,
107106 self .keypair .name ,
@@ -135,7 +134,11 @@ def test_keypair_create_public_key(self):
135134 self .assertEqual (self .columns , columns )
136135 self .assertEqual (self .data , data )
137136
138- def test_keypair_create_private_key (self ):
137+ @mock .patch .object (
138+ keypair , '_generate_keypair' ,
139+ return_value = keypair .Keypair ('private' , 'public' ),
140+ )
141+ def test_keypair_create_private_key (self , mock_generate ):
139142 tmp_pk_file = '/tmp/kp-file-' + uuid .uuid4 ().hex
140143 arglist = [
141144 '--private-key' , tmp_pk_file ,
@@ -156,19 +159,20 @@ def test_keypair_create_private_key(self):
156159
157160 self .sdk_client .create_keypair .assert_called_with (
158161 name = self .keypair .name ,
162+ public_key = mock_generate .return_value .public_key ,
159163 )
160164
161165 mock_open .assert_called_once_with (tmp_pk_file , 'w+' )
162- m_file .write .assert_called_once_with (self .keypair .private_key )
166+ m_file .write .assert_called_once_with (
167+ mock_generate .return_value .private_key ,
168+ )
163169
164170 self .assertEqual (self .columns , columns )
165171 self .assertEqual (self .data , data )
166172
167173 @mock .patch .object (sdk_utils , 'supports_microversion' , return_value = True )
168174 def test_keypair_create_with_key_type (self , sm_mock ):
169175 for key_type in ['x509' , 'ssh' ]:
170- self .keypair = compute_fakes .FakeKeypair .create_one_keypair (
171- no_pri = True )
172176 self .sdk_client .create_keypair .return_value = self .keypair
173177
174178 self .data = (
@@ -233,8 +237,12 @@ def test_keypair_create_with_key_type_pre_v22(self, sm_mock):
233237 '--os-compute-api-version 2.2 or greater is required' ,
234238 str (ex ))
235239
240+ @mock .patch .object (
241+ keypair , '_generate_keypair' ,
242+ return_value = keypair .Keypair ('private' , 'public' ),
243+ )
236244 @mock .patch .object (sdk_utils , 'supports_microversion' , return_value = True )
237- def test_key_pair_create_with_user (self , sm_mock ):
245+ def test_key_pair_create_with_user (self , sm_mock , mock_generate ):
238246 arglist = [
239247 '--user' , identity_fakes .user_name ,
240248 self .keypair .name ,
@@ -250,6 +258,7 @@ def test_key_pair_create_with_user(self, sm_mock):
250258 self .sdk_client .create_keypair .assert_called_with (
251259 name = self .keypair .name ,
252260 user_id = identity_fakes .user_id ,
261+ public_key = mock_generate .return_value .public_key ,
253262 )
254263
255264 self .assertEqual ({}, columns )
@@ -673,9 +682,6 @@ def test_keypair_show_no_options(self):
673682 self .cmd , arglist , verifylist )
674683
675684 def test_keypair_show (self ):
676- # overwrite the setup one because we want to omit private_key
677- self .keypair = compute_fakes .FakeKeypair .create_one_keypair (
678- no_pri = True )
679685 self .sdk_client .find_keypair .return_value = self .keypair
680686
681687 self .data = (
@@ -704,7 +710,6 @@ def test_keypair_show(self):
704710 self .assertEqual (self .data , data )
705711
706712 def test_keypair_show_public (self ):
707-
708713 arglist = [
709714 '--public-key' ,
710715 self .keypair .name
@@ -723,10 +728,6 @@ def test_keypair_show_public(self):
723728
724729 @mock .patch .object (sdk_utils , 'supports_microversion' , return_value = True )
725730 def test_keypair_show_with_user (self , sm_mock ):
726-
727- # overwrite the setup one because we want to omit private_key
728- self .keypair = compute_fakes .FakeKeypair .create_one_keypair (
729- no_pri = True )
730731 self .sdk_client .find_keypair .return_value = self .keypair
731732
732733 self .data = (
0 commit comments