<s> """<STR_LIT>""" <EOL> from nova import exception <EOL> from nova . objects import fields <EOL> from nova . objects import monitor_metric <EOL> from nova . scheduler import weights <EOL> from nova . scheduler . weights import metrics <EOL> from nova import test <EOL> from nova . tests . unit . scheduler import fakes <EOL> idle = fields . MonitorMetricType . CPU_IDLE_TIME <EOL> kernel = fields . MonitorMetricType . CPU_KERNEL_TIME <EOL> user = fields . MonitorMetricType . CPU_USER_TIME <EOL> class MetricsWeigherTestCase ( test . NoDBTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( MetricsWeigherTestCase , self ) . setUp ( ) <EOL> self . weight_handler = weights . HostWeightHandler ( ) <EOL> self . weighers = [ metrics . MetricsWeigher ( ) ] <EOL> def _get_weighed_host ( self , hosts , setting , weight_properties = None ) : <EOL> if not weight_properties : <EOL> weight_properties = { } <EOL> self . flags ( weight_setting = setting , group = '<STR_LIT>' ) <EOL> self . weighers [ <NUM_LIT:0> ] . _parse_setting ( ) <EOL> return self . weight_handler . get_weighed_objects ( self . weighers , <EOL> hosts , weight_properties ) [ <NUM_LIT:0> ] <EOL> def _get_all_hosts ( self ) : <EOL> def fake_metric ( name , value ) : <EOL> return monitor_metric . MonitorMetric ( name = name , value = value ) <EOL> def fake_list ( objs ) : <EOL> m_list = [ fake_metric ( name , val ) for name , val in objs ] <EOL> return monitor_metric . MonitorMetricList ( objects = m_list ) <EOL> host_values = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' , { '<STR_LIT>' : fake_list ( [ ( idle , <NUM_LIT> ) , <EOL> ( kernel , <NUM_LIT:1> ) ] ) } ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , { '<STR_LIT>' : fake_list ( [ ( idle , <NUM_LIT> ) , <EOL> ( kernel , <NUM_LIT:2> ) ] ) } ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , { '<STR_LIT>' : fake_list ( [ ( idle , <NUM_LIT> ) , <EOL> ( kernel , <NUM_LIT:1> ) ] ) } ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , { '<STR_LIT>' : fake_list ( [ ( idle , <NUM_LIT> ) , <EOL> ( kernel , <NUM_LIT:0> ) ] ) } ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , { '<STR_LIT>' : fake_list ( [ ( idle , <NUM_LIT> ) , <EOL> ( kernel , <NUM_LIT:0> ) , <EOL> ( user , <NUM_LIT:1> ) ] ) } ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , { '<STR_LIT>' : fake_list ( [ ( idle , <NUM_LIT> ) , <EOL> ( kernel , <NUM_LIT:0> ) , <EOL> ( user , <NUM_LIT:2> ) ] ) } ) , <EOL> ] <EOL> return [ fakes . FakeHostState ( host , node , values ) <EOL> for host , node , values in host_values ] <EOL> def _do_test ( self , settings , expected_weight , expected_host ) : <EOL> hostinfo_list = self . _get_all_hosts ( ) <EOL> weighed_host = self . _get_weighed_host ( hostinfo_list , settings ) <EOL> self . assertEqual ( expected_weight , weighed_host . weight ) <EOL> self . assertEqual ( expected_host , weighed_host . obj . host ) <EOL> def test_single_resource_no_metrics ( self ) : <EOL> setting = [ idle + '<STR_LIT>' ] <EOL> hostinfo_list = [ fakes . FakeHostState ( '<STR_LIT>' , '<STR_LIT>' , <EOL> { '<STR_LIT>' : None } ) , <EOL> fakes . FakeHostState ( '<STR_LIT>' , '<STR_LIT>' , <EOL> { '<STR_LIT>' : None } ) ] <EOL> self . assertRaises ( exception . ComputeHostMetricNotFound , <EOL> self . _get_weighed_host , hostinfo_list , setting ) <EOL> def test_single_resource ( self ) : <EOL> setting = [ idle + '<STR_LIT>' ] <EOL> self . _do_test ( setting , <NUM_LIT:1.0> , '<STR_LIT>' ) <EOL> def test_multiple_resource ( self ) : <EOL> setting = [ idle + '<STR_LIT>' , kernel + '<STR_LIT>' ] <EOL> self . _do_test ( setting , <NUM_LIT:1.0> , '<STR_LIT>' ) <EOL> def test_single_resource_duplicate_setting ( self ) : <EOL> setting = [ idle + '<STR_LIT>' , idle + '<STR_LIT>' ] <EOL> self . _do_test ( setting , <NUM_LIT:1.0> , '<STR_LIT>' ) <EOL> def test_single_resourcenegtive_ratio ( self ) : <EOL> setting = [ idle + '<STR_LIT>' ] <EOL> self . _do_test ( setting , <NUM_LIT:1.0> , '<STR_LIT>' ) <EOL> def test_multiple_resource_missing_ratio ( self ) : <EOL> setting = [ idle + '<STR_LIT>' , kernel ] <EOL> self . _do_test ( setting , <NUM_LIT:1.0> , '<STR_LIT>' ) <EOL> def test_multiple_resource_wrong_ratio ( self ) : <EOL> setting = [ idle + '<STR_LIT>' , kernel + '<STR_LIT>' ] <EOL> self . _do_test ( setting , <NUM_LIT:1.0> , '<STR_LIT>' ) <EOL> def _check_parsing_result ( self , weigher , setting , results ) : <EOL> self . flags ( weight_setting = setting , group = '<STR_LIT>' ) <EOL> weigher . _parse_setting ( ) <EOL> self . assertEqual ( len ( weigher . setting ) , len ( results ) ) <EOL> for item in results : <EOL> self . assertIn ( item , weigher . setting ) <EOL> def test_parse_setting ( self ) : <EOL> weigher = self . weighers [ <NUM_LIT:0> ] <EOL> self . _check_parsing_result ( weigher , <EOL> [ idle + '<STR_LIT>' ] , <EOL> [ ( idle , <NUM_LIT:1.0> ) ] ) <EOL> self . _check_parsing_result ( weigher , <EOL> [ idle + '<STR_LIT>' , kernel + '<STR_LIT>' ] , <EOL> [ ( idle , <NUM_LIT:1.0> ) , ( kernel , - <NUM_LIT> ) ] ) <EOL> self . _check_parsing_result ( weigher , <EOL> [ idle + '<STR_LIT>' , kernel + '<STR_LIT>' ] , <EOL> [ ( kernel , - <NUM_LIT> ) ] ) <EOL> self . _check_parsing_result ( weigher , <EOL> [ idle , kernel + '<STR_LIT>' ] , <EOL> [ ( kernel , - <NUM_LIT> ) ] ) <EOL> self . _check_parsing_result ( weigher , <EOL> [ '<STR_LIT>' , kernel + '<STR_LIT>' ] , <EOL> [ ( kernel , - <NUM_LIT> ) ] ) <EOL> def test_metric_not_found_required ( self ) : <EOL> setting = [ idle + '<STR_LIT>' , user + '<STR_LIT>' ] <EOL> self . assertRaises ( exception . ComputeHostMetricNotFound , <EOL> self . _do_test , <EOL> setting , <EOL> <NUM_LIT> , <EOL> '<STR_LIT>' ) <EOL> def test_metric_not_found_non_required ( self ) : <EOL> self . flags ( required = False , group = '<STR_LIT>' ) <EOL> setting = [ idle + '<STR_LIT>' , user + '<STR_LIT>' ] <EOL> self . _do_test ( setting , <NUM_LIT:1.0> , '<STR_LIT>' ) </s>
