<s> import mock <EOL> from rally . plugins . openstack . context . nova import keypairs <EOL> from tests . unit import test <EOL> CTX = "<STR_LIT>" <EOL> class KeyPairContextTestCase ( test . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( KeyPairContextTestCase , self ) . setUp ( ) <EOL> self . users = <NUM_LIT:2> <EOL> task = { "<STR_LIT>" : "<STR_LIT>" } <EOL> self . ctx_with_keys = { <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT:key>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT:key>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> ] , <EOL> "<STR_LIT>" : task <EOL> } <EOL> self . ctx_without_keys = { <EOL> "<STR_LIT>" : [ { "<STR_LIT>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT>" } ] , <EOL> "<STR_LIT>" : task <EOL> } <EOL> def test_keypair_setup ( self ) : <EOL> keypair_ctx = keypairs . Keypair ( self . ctx_without_keys ) <EOL> keypair_ctx . _generate_keypair = mock . Mock ( side_effect = [ <EOL> { "<STR_LIT:id>" : "<STR_LIT>" , "<STR_LIT:key>" : "<STR_LIT>" , "<STR_LIT:name>" : "<STR_LIT>" } , <EOL> { "<STR_LIT:id>" : "<STR_LIT>" , "<STR_LIT:key>" : "<STR_LIT>" , "<STR_LIT:name>" : "<STR_LIT>" } , <EOL> ] ) <EOL> keypair_ctx . setup ( ) <EOL> self . assertEqual ( keypair_ctx . context , self . ctx_with_keys ) <EOL> keypair_ctx . _generate_keypair . assert_has_calls ( <EOL> [ mock . call ( "<STR_LIT>" ) , mock . call ( "<STR_LIT>" ) ] ) <EOL> @ mock . patch ( "<STR_LIT>" % CTX ) <EOL> def test_keypair_cleanup ( self , mock_cleanup ) : <EOL> keypair_ctx = keypairs . Keypair ( self . ctx_with_keys ) <EOL> keypair_ctx . cleanup ( ) <EOL> mock_cleanup . assert_called_once_with ( names = [ "<STR_LIT>" ] , <EOL> users = self . ctx_with_keys [ "<STR_LIT>" ] ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_keypair_generate ( self , mock_clients ) : <EOL> mock_keypairs = mock_clients . return_value . nova . return_value . keypairs <EOL> mock_keypair = mock_keypairs . create . return_value <EOL> mock_keypair . public_key = "<STR_LIT>" <EOL> mock_keypair . private_key = "<STR_LIT>" <EOL> mock_keypair . id = "<STR_LIT>" <EOL> keypair_ctx = keypairs . Keypair ( self . ctx_without_keys ) <EOL> keypair_ctx . generate_random_name = mock . Mock ( ) <EOL> key = keypair_ctx . _generate_keypair ( "<STR_LIT>" ) <EOL> self . assertEqual ( { <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : keypair_ctx . generate_random_name . return_value , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , key ) <EOL> mock_clients . assert_has_calls ( [ <EOL> mock . call ( ) . nova ( ) . keypairs . delete ( <EOL> keypair_ctx . generate_random_name . return_value ) , <EOL> mock . call ( ) . nova ( ) . keypairs . create ( <EOL> keypair_ctx . generate_random_name . return_value ) <EOL> ] ) </s>
