<s> from mock import call , patch <EOL> from nose . tools import istest <EOL> from provy . more . debian import RailsRole , AptitudeRole , GemRole , SupervisorRole , NginxRole <EOL> from provy . more . debian . web . rails import PACKAGES_TO_INSTALL <EOL> from tests . unit . tools . helpers import ProvyTestCase <EOL> class RailsRoleTest ( ProvyTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( RailsRoleTest , self ) . setUp ( ) <EOL> self . role = RailsRole ( prov = None , context = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . supervisor_role = SupervisorRole ( prov = None , context = self . role . context ) <EOL> @ istest <EOL> def installs_necessary_packages_to_provision ( self ) : <EOL> methods_to_mock = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> with self . using_stub ( AptitudeRole ) as aptitude , self . using_stub ( GemRole ) as gem , self . mock_role_methods ( * methods_to_mock ) : <EOL> self . role . remote_exists_dir . return_value = False <EOL> self . role . provision ( ) <EOL> self . role . register_template_loader . assert_called_once_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( aptitude . ensure_package_installed . mock_calls , [ call ( package ) for package in PACKAGES_TO_INSTALL ] ) <EOL> self . assertEqual ( gem . ensure_package_installed . mock_calls , [ call ( '<STR_LIT>' ) , call ( '<STR_LIT>' ) ] ) <EOL> self . role . remote_exists_dir . assert_called_once_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( self . role . ensure_dir . mock_calls , [ <EOL> call ( '<STR_LIT>' , sudo = True ) , <EOL> call ( '<STR_LIT>' , sudo = True ) , <EOL> call ( '<STR_LIT>' , sudo = True ) , <EOL> call ( '<STR_LIT>' , sudo = True ) , <EOL> ] ) <EOL> self . role . execute . assert_called_once_with ( '<STR_LIT>' , sudo = True , stdout = False ) <EOL> self . assertEqual ( self . role . update_file . mock_calls , [ <EOL> call ( '<STR_LIT>' , '<STR_LIT>' , sudo = True ) , <EOL> call ( '<STR_LIT>' , '<STR_LIT>' , sudo = True ) , <EOL> ] ) <EOL> self . role . change_path_mode . assert_called_once_with ( '<STR_LIT>' , <NUM_LIT> ) <EOL> @ istest <EOL> def provisions_even_if_nginx_already_exists ( self ) : <EOL> methods_to_mock = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> with self . using_stub ( AptitudeRole ) , self . using_stub ( GemRole ) , self . mock_role_methods ( * methods_to_mock ) : <EOL> self . role . remote_exists_dir . return_value = True <EOL> self . role . provision ( ) <EOL> self . assertEqual ( self . role . ensure_dir . mock_calls , [ <EOL> call ( '<STR_LIT>' , sudo = True ) , <EOL> call ( '<STR_LIT>' , sudo = True ) , <EOL> call ( '<STR_LIT>' , sudo = True ) , <EOL> ] ) <EOL> @ istest <EOL> def restarts_on_cleanup_if_must_be_restarted ( self ) : <EOL> with patch ( '<STR_LIT>' ) as restart : <EOL> self . role . ensure_restart ( ) <EOL> self . role . cleanup ( ) <EOL> self . assertTrue ( restart . called ) <EOL> @ istest <EOL> def doesnt_restart_on_cleanup_if_doesnt_need_to_be_restarted ( self ) : <EOL> with patch ( '<STR_LIT>' ) as restart : <EOL> self . role . cleanup ( ) <EOL> self . assertFalse ( restart . called ) <EOL> @ istest <EOL> def ensures_site_is_disabled ( self ) : <EOL> site = '<STR_LIT>' <EOL> with self . using_stub ( NginxRole ) as nginx : <EOL> self . role . ensure_site_disabled ( site ) <EOL> nginx . ensure_site_disabled . assert_called_once_with ( site ) <EOL> @ istest <EOL> def ensures_site_is_enabled ( self ) : <EOL> site = '<STR_LIT>' <EOL> with self . using_stub ( NginxRole ) as nginx : <EOL> self . role . ensure_site_enabled ( site ) <EOL> nginx . ensure_site_enabled . assert_called_once_with ( site ) <EOL> @ istest <EOL> def ensures_site_is_created_and_restarted ( self ) : <EOL> owner = self . role . context [ '<STR_LIT>' ] <EOL> site = '<STR_LIT>' <EOL> host = '<STR_LIT>' <EOL> path = '<STR_LIT>' <EOL> options = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } <EOL> expected_options = { '<STR_LIT:foo>' : '<STR_LIT:bar>' , '<STR_LIT:host>' : host , '<STR_LIT:path>' : path } <EOL> with self . mock_role_methods ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . role . update_file . return_value = True <EOL> self . role . create_site ( site , host , path , options = options ) <EOL> self . role . update_file . assert_called_once_with ( '<STR_LIT>' , '<STR_LIT>' , options = expected_options , sudo = True ) <EOL> self . role . ensure_restart . assert_called_once_with ( ) <EOL> self . role . execute . assert_called_once_with ( '<STR_LIT>' , stdout = True , user = owner ) <EOL> @ istest <EOL> def ensures_site_is_created_without_restart_when_already_existant ( self ) : <EOL> owner = self . role . context [ '<STR_LIT>' ] <EOL> site = '<STR_LIT>' <EOL> host = '<STR_LIT>' <EOL> path = '<STR_LIT>' <EOL> options = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } <EOL> expected_options = { '<STR_LIT:foo>' : '<STR_LIT:bar>' , '<STR_LIT:host>' : host , '<STR_LIT:path>' : path } <EOL> with self . mock_role_methods ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . role . update_file . return_value = False <EOL> self . role . create_site ( site , host , path , options = options ) <EOL> self . role . update_file . assert_called_once_with ( '<STR_LIT>' , '<STR_LIT>' , options = expected_options , sudo = True ) <EOL> self . assertFalse ( self . role . ensure_restart . called ) <EOL> self . role . execute . assert_called_once_with ( '<STR_LIT>' , stdout = True , user = owner ) <EOL> @ istest <EOL> def restarts_nginx ( self ) : <EOL> with self . using_stub ( NginxRole ) as nginx : <EOL> self . role . restart ( ) <EOL> nginx . restart . assert_called_once_with ( ) </s>
