<s> import mock <EOL> from sahara . plugins import base as base_plugins <EOL> from sahara . service import ops <EOL> from sahara . tests . unit import base <EOL> from sahara . utils import cluster as c_u <EOL> class FakeCluster ( object ) : <EOL> id = '<STR_LIT:id>' <EOL> status = "<STR_LIT>" <EOL> name = "<STR_LIT>" <EOL> class FakeNodeGroup ( object ) : <EOL> id = '<STR_LIT:id>' <EOL> count = <NUM_LIT:2> <EOL> instances = [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> class FakePlugin ( mock . Mock ) : <EOL> node_groups = [ FakeNodeGroup ( ) ] <EOL> is_transient = False <EOL> def update_infra ( self , cluster ) : <EOL> TestOPS . SEQUENCE . append ( '<STR_LIT>' ) <EOL> def configure_cluster ( self , cluster ) : <EOL> TestOPS . SEQUENCE . append ( '<STR_LIT>' ) <EOL> def start_cluster ( self , cluster ) : <EOL> TestOPS . SEQUENCE . append ( '<STR_LIT>' ) <EOL> def on_terminate_cluster ( self , cluster ) : <EOL> TestOPS . SEQUENCE . append ( '<STR_LIT>' ) <EOL> def decommission_nodes ( self , cluster , instances_to_delete ) : <EOL> TestOPS . SEQUENCE . append ( '<STR_LIT>' ) <EOL> def scale_cluster ( self , cluster , node_group_id_map ) : <EOL> TestOPS . SEQUENCE . append ( '<STR_LIT>' ) <EOL> def cluster_destroy ( self , ctx , cluster ) : <EOL> TestOPS . SEQUENCE . append ( '<STR_LIT>' ) <EOL> class FakeINFRA ( object ) : <EOL> def create_cluster ( self , cluster ) : <EOL> TestOPS . SEQUENCE . append ( '<STR_LIT>' ) <EOL> def scale_cluster ( self , cluster , node_group_id_map ) : <EOL> TestOPS . SEQUENCE . append ( '<STR_LIT>' ) <EOL> return True <EOL> def shutdown_cluster ( self , cluster ) : <EOL> TestOPS . SEQUENCE . append ( '<STR_LIT>' ) <EOL> def rollback_cluster ( self , cluster , reason ) : <EOL> TestOPS . SEQUENCE . append ( '<STR_LIT>' ) <EOL> class TestOPS ( base . SaharaWithDbTestCase ) : <EOL> SEQUENCE = [ ] <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = FakeCluster ( ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = ( mock . Mock ( ) , mock . Mock ( ) , FakePlugin ( ) ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_provision_cluster ( self , p_run_job , p_job_exec , p_create_trust , <EOL> p_conf , p_cluster_get , p_change_status , <EOL> p_prep_provisioning , p_update_sahara_info , <EOL> p_change_cluster_status_desc , refresh ) : <EOL> del self . SEQUENCE [ : ] <EOL> ops . INFRA = FakeINFRA ( ) <EOL> ops . _provision_cluster ( '<STR_LIT>' ) <EOL> self . assertEqual ( [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] , self . SEQUENCE , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:1> , refresh . call_count ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = ( mock . Mock ( ) , mock . Mock ( ) , FakePlugin ( ) ) ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> return_value = FakePlugin ( ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_provision_scaled_cluster ( self , p_get_instances , p_change_status , <EOL> p_prep_provisioning , p_conf , p_ntp , <EOL> refresh ) : <EOL> del self . SEQUENCE [ : ] <EOL> ops . INFRA = FakeINFRA ( ) <EOL> p_conf . use_identity_api_v3 = True <EOL> ops . _provision_scaled_cluster ( '<STR_LIT>' , { '<STR_LIT:id>' : <NUM_LIT:1> } ) <EOL> self . assertEqual ( [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] , self . SEQUENCE , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:1> , refresh . call_count ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_terminate_cluster ( self , p_ctx , p_delete_trust , p_conf , p_set ) : <EOL> del self . SEQUENCE [ : ] <EOL> base_plugins . PLUGINS = FakePlugin ( ) <EOL> base_plugins . PLUGINS . get_plugin . return_value = FakePlugin ( ) <EOL> ops . INFRA = FakeINFRA ( ) <EOL> ops . conductor = FakePlugin ( ) <EOL> ops . terminate_cluster ( '<STR_LIT>' ) <EOL> self . assertEqual ( [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] , self . SEQUENCE , <EOL> '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_ops_error_hadler_success_rollback ( <EOL> self , p_cluster_get , p_rollback_cluster , p_change_cluster_status , <EOL> p__prepare_provisioning , p_change_cluster_status_desc ) : <EOL> fake_cluster = FakeCluster ( ) <EOL> p_change_cluster_status_desc . return_value = FakeCluster ( ) <EOL> p_rollback_cluster . return_value = True <EOL> p_cluster_get . return_value = fake_cluster <EOL> p__prepare_provisioning . side_effect = ValueError ( '<STR_LIT>' ) <EOL> expected = [ <EOL> mock . call ( fake_cluster , c_u . CLUSTER_STATUS_ACTIVE , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> ] <EOL> ops . _provision_scaled_cluster ( fake_cluster . id , { '<STR_LIT:id>' : <NUM_LIT:1> } ) <EOL> self . assertEqual ( expected , p_change_cluster_status . call_args_list ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_ops_error_hadler_failed_rollback ( <EOL> self , p_cluster_get , p_rollback_cluster , p_change_cluster_status , <EOL> p__prepare_provisioning , p_change_cluster_status_desc ) : <EOL> fake_cluster = FakeCluster ( ) <EOL> p_change_cluster_status_desc . return_value = FakeCluster ( ) <EOL> p__prepare_provisioning . side_effect = ValueError ( '<STR_LIT>' ) <EOL> p_rollback_cluster . side_effect = ValueError ( '<STR_LIT>' ) <EOL> p_cluster_get . return_value = fake_cluster <EOL> expected = [ <EOL> mock . call ( <EOL> fake_cluster , '<STR_LIT>' , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> ] <EOL> ops . _provision_scaled_cluster ( fake_cluster . id , { '<STR_LIT:id>' : <NUM_LIT:1> } ) <EOL> self . assertEqual ( expected , p_change_cluster_status . call_args_list ) </s>
