<s> """<STR_LIT>""" <EOL> from mock import patch , Mock <EOL> import mock <EOL> import os_client_config <EOL> from shade import _utils <EOL> from shade import meta <EOL> from shade import OpenStackCloud <EOL> from shade . exc import ( OpenStackCloudException , OpenStackCloudTimeout ) <EOL> from shade . tests import base , fakes <EOL> class TestCreateServer ( base . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestCreateServer , self ) . setUp ( ) <EOL> config = os_client_config . OpenStackConfig ( ) <EOL> self . client = OpenStackCloud ( <EOL> cloud_config = config . get_one_cloud ( validate = False ) ) <EOL> self . client . _SERVER_AGE = <NUM_LIT:0> <EOL> def test_create_server_with_create_exception ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( "<STR_LIT>" ) : <EOL> config = { <EOL> "<STR_LIT>" : Exception ( "<STR_LIT>" ) , <EOL> } <EOL> OpenStackCloud . nova_client = Mock ( ** config ) <EOL> self . assertRaises ( <EOL> OpenStackCloudException , self . client . create_server , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_create_server_with_get_exception ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( "<STR_LIT>" ) : <EOL> config = { <EOL> "<STR_LIT>" : Mock ( status = "<STR_LIT>" ) , <EOL> "<STR_LIT>" : Exception ( "<STR_LIT>" ) <EOL> } <EOL> OpenStackCloud . nova_client = Mock ( ** config ) <EOL> self . assertRaises ( <EOL> OpenStackCloudException , self . client . create_server , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_create_server_with_server_error ( self ) : <EOL> """<STR_LIT>""" <EOL> build_server = fakes . FakeServer ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> error_server = fakes . FakeServer ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> with patch ( "<STR_LIT>" ) : <EOL> config = { <EOL> "<STR_LIT>" : build_server , <EOL> "<STR_LIT>" : error_server , <EOL> } <EOL> OpenStackCloud . nova_client = Mock ( ** config ) <EOL> self . assertRaises ( <EOL> OpenStackCloudException , self . client . create_server , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_create_server_wait_server_error ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( "<STR_LIT>" ) : <EOL> build_server = fakes . FakeServer ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> error_server = fakes . FakeServer ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> config = { <EOL> "<STR_LIT>" : build_server , <EOL> "<STR_LIT>" : build_server , <EOL> "<STR_LIT>" : [ <EOL> [ build_server ] , [ error_server ] ] <EOL> } <EOL> OpenStackCloud . nova_client = Mock ( ** config ) <EOL> self . assertRaises ( <EOL> OpenStackCloudException , <EOL> self . client . create_server , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , wait = True ) <EOL> def test_create_server_with_timeout ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( "<STR_LIT>" ) : <EOL> fake_server = fakes . FakeServer ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> config = { <EOL> "<STR_LIT>" : fake_server , <EOL> "<STR_LIT>" : fake_server , <EOL> "<STR_LIT>" : [ fake_server ] , <EOL> } <EOL> OpenStackCloud . nova_client = Mock ( ** config ) <EOL> self . assertRaises ( <EOL> OpenStackCloudTimeout , <EOL> self . client . create_server , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , wait = True , timeout = <NUM_LIT:1> ) <EOL> def test_create_server_no_wait ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( "<STR_LIT>" ) : <EOL> fake_server = fakes . FakeServer ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> config = { <EOL> "<STR_LIT>" : fake_server , <EOL> "<STR_LIT>" : fake_server <EOL> } <EOL> OpenStackCloud . nova_client = Mock ( ** config ) <EOL> self . assertEqual ( <EOL> _utils . normalize_server ( <EOL> meta . obj_to_dict ( fake_server ) , <EOL> cloud_name = self . client . name , <EOL> region_name = self . client . region_name ) , <EOL> self . client . create_server ( <EOL> name = '<STR_LIT>' , image = '<STR_LIT>' , <EOL> flavor = '<STR_LIT>' ) ) <EOL> def test_create_server_with_admin_pass_no_wait ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch ( "<STR_LIT>" ) : <EOL> fake_server = fakes . FakeServer ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> fake_create_server = fakes . FakeServer ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> adminPass = '<STR_LIT>' ) <EOL> config = { <EOL> "<STR_LIT>" : fake_create_server , <EOL> "<STR_LIT>" : fake_server <EOL> } <EOL> OpenStackCloud . nova_client = Mock ( ** config ) <EOL> self . assertEqual ( <EOL> _utils . normalize_server ( <EOL> meta . obj_to_dict ( fake_create_server ) , <EOL> cloud_name = self . client . name , <EOL> region_name = self . client . region_name ) , <EOL> self . client . create_server ( <EOL> name = '<STR_LIT>' , image = '<STR_LIT>' , <EOL> flavor = '<STR_LIT>' , admin_pass = '<STR_LIT>' ) ) <EOL> @ patch . object ( OpenStackCloud , "<STR_LIT>" ) <EOL> @ patch . object ( OpenStackCloud , "<STR_LIT>" ) <EOL> def test_create_server_with_admin_pass_wait ( self , mock_nova , mock_wait ) : <EOL> """<STR_LIT>""" <EOL> fake_server = fakes . FakeServer ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> fake_server_with_pass = fakes . FakeServer ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> adminPass = '<STR_LIT>' ) <EOL> mock_nova . servers . create . return_value = fake_server <EOL> mock_nova . servers . get . return_value = fake_server <EOL> mock_wait . return_value = _utils . normalize_server ( <EOL> meta . obj_to_dict ( fake_server ) , None , None ) <EOL> server = self . client . create_server ( <EOL> name = '<STR_LIT>' , image = '<STR_LIT>' , <EOL> flavor = '<STR_LIT>' , admin_pass = '<STR_LIT>' , wait = True ) <EOL> self . assertTrue ( mock_wait . called ) <EOL> self . assertEqual ( <EOL> server , <EOL> _utils . normalize_server ( meta . obj_to_dict ( fake_server_with_pass ) , <EOL> None , None ) <EOL> ) <EOL> @ patch . object ( OpenStackCloud , "<STR_LIT>" ) <EOL> @ patch . object ( OpenStackCloud , "<STR_LIT>" ) <EOL> def test_wait_for_server ( self , mock_get_server , mock_get_active_server ) : <EOL> """<STR_LIT>""" <EOL> building_server = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:status>' : '<STR_LIT>' } <EOL> active_server = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:status>' : '<STR_LIT>' } <EOL> mock_get_server . side_effect = iter ( [ building_server , active_server ] ) <EOL> mock_get_active_server . side_effect = iter ( [ <EOL> building_server , active_server ] ) <EOL> server = self . client . wait_for_server ( building_server ) <EOL> self . assertEqual ( <NUM_LIT:2> , mock_get_server . call_count ) <EOL> mock_get_server . assert_has_calls ( [ <EOL> mock . call ( building_server [ '<STR_LIT:id>' ] ) , <EOL> mock . call ( active_server [ '<STR_LIT:id>' ] ) , <EOL> ] ) <EOL> self . assertEqual ( <NUM_LIT:2> , mock_get_active_server . call_count ) <EOL> mock_get_active_server . assert_has_calls ( [ <EOL> mock . call ( server = building_server , reuse = True , auto_ip = True , <EOL> ips = None , ip_pool = None , wait = True , timeout = mock . ANY , <EOL> nat_destination = None ) , <EOL> mock . call ( server = active_server , reuse = True , auto_ip = True , <EOL> ips = None , ip_pool = None , wait = True , timeout = mock . ANY , <EOL> nat_destination = None ) , <EOL> ] ) <EOL> self . assertEqual ( '<STR_LIT>' , server [ '<STR_LIT:status>' ] ) <EOL> @ patch . object ( OpenStackCloud , '<STR_LIT>' ) <EOL> @ patch . object ( OpenStackCloud , '<STR_LIT>' ) <EOL> def test_create_server_wait ( self , mock_nova , mock_wait ) : <EOL> """<STR_LIT>""" <EOL> fake_server = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:status>' : '<STR_LIT>' } <EOL> mock_nova . servers . create . return_value = fake_server <EOL> self . client . create_server ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , wait = True ) , <EOL> mock_wait . assert_called_once_with ( <EOL> fake_server , auto_ip = True , ips = None , <EOL> ip_pool = None , reuse = True , timeout = <NUM_LIT> , <EOL> nat_destination = None , <EOL> ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_create_server_no_addresses ( self , mock_sleep ) : <EOL> """<STR_LIT>""" <EOL> with patch ( "<STR_LIT>" ) : <EOL> build_server = fakes . FakeServer ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> fake_server = fakes . FakeServer ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> config = { <EOL> "<STR_LIT>" : build_server , <EOL> "<STR_LIT>" : [ build_server , None ] , <EOL> "<STR_LIT>" : [ <EOL> [ build_server ] , [ fake_server ] ] , <EOL> "<STR_LIT>" : None , <EOL> } <EOL> OpenStackCloud . nova_client = Mock ( ** config ) <EOL> self . client . _SERVER_AGE = <NUM_LIT:0> <EOL> with patch . object ( OpenStackCloud , "<STR_LIT>" , <EOL> return_value = fake_server ) : <EOL> self . assertRaises ( <EOL> OpenStackCloudException , self . client . create_server , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> wait = True ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_create_server_network_with_no_nics ( self , mock_get_network , <EOL> mock_nova ) : <EOL> """<STR_LIT>""" <EOL> self . client . create_server ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> network = '<STR_LIT>' ) <EOL> mock_get_network . assert_called_once_with ( name_or_id = '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_create_server_network_with_empty_nics ( self , <EOL> mock_get_network , <EOL> mock_nova ) : <EOL> """<STR_LIT>""" <EOL> self . client . create_server ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> network = '<STR_LIT>' , nics = [ ] ) <EOL> mock_get_network . assert_called_once_with ( name_or_id = '<STR_LIT>' ) </s>
