def test_ruleset_duplicated_rule(self): rules_test = yaml.safe_load(""" - name: rules_default ruleset: - metric: request_cpu price: 0.005 unit: core-hours - metric: usage_cpu price: 0.015 unit: core-hours - metric: request_memory price: 0.004 unit: GiB-hours - metric: usage_memory price: 0.012 unit: GiB-hours - metric: usage_memory price: 0.012 unit: GiB-hours """) with self.assertRaisesRegex(utils.ConfigurationException, 'Duplicated'): rules.ensure_rules_config(rules_test)
def test_ruleset_wrong_key_rules(self): rules_test = yaml.safe_load(""" - name: rules_large labelSet: instance_type: large storage_type: hdd gpu_accel: "false" ruleset: - pokemon: ivysaur price: 0.0009 unit: core-hours - metric: usage_cpu price: 0.0026 unit: core-hours - metric: request_memory price: 0.0008 unit: GiB-hours - metric: usage_memory price: 0.0022 unit: GiB-hours """) with self.assertRaisesRegex(utils.ConfigurationException, ('Wrong key in rules')): rules.ensure_rules_config(rules_test)
def test_ruleset_with_labelset(self): rules_test = yaml.safe_load(""" - name: rules_large labelSet: instance_type: large storage_type: hdd gpu_accel: "false" ruleset: - metric: request_cpu price: 0.0009 unit: core-hours - metric: usage_cpu price: 0.0026 unit: core-hours - metric: request_memory price: 0.0008 unit: GiB-hours - metric: usage_memory price: 0.0022 unit: GiB-hours """) rules.ensure_rules_config(rules_test)
def test_ruleset_no_rules(self): rules_test = yaml.safe_load(""" - name: rules_default labelSet: thisis: atest """) with self.assertRaisesRegex(utils.ConfigurationException, 'No rules'): rules.ensure_rules_config(rules_test)
def test_ruleset_wrong_value(self): rules_test = yaml.safe_load(""" - ruleset: - metric: request_memory; price: 0.012 unit: GiB-hours """) with self.assertRaisesRegex(utils.ConfigurationException, 'Invalid value'): rules.ensure_rules_config(rules_test)
def report_event(body, spec, logger, **kwargs): metadata = body['metadata'] if kwargs["type"] not in ['ADDED', 'MODIFIED']: return configurations = retrieve_configurations_from_API() if not configurations: raise utils.ConfigurationMissing( 'Bad response from API, no configuration found.' ) begin = rated_or_not(metadata['name']) configs = tuple(ts['valid_from'] for ts in configurations) choosen_config = get_closest_configs_bisect( begin.strftime('%s'), configs) table = kwargs['status'].get('tableRef') if not table: return metric_config = check_rating_conditions(metadata['name'], table['name'], begin, configurations[choosen_config]) if not metric_config: return logger.info(f'using config with timestamp {configs[choosen_config]}') logger.info( 'rating for {metric} in {table} for period {begin} to {end} started..' .format(metric=metric_config['metric'], table=metric_config['presto_table'], begin=metric_config['begin'], end=metric_config['end']) ) rules.ensure_rules_config(configurations[choosen_config]['rules']['rules']) rated_metrics.retrieve_data( configurations[choosen_config]['rules']['rules'], metric_config, logger)
def test_ruleset_without_labelset(self): rules_test = yaml.safe_load(""" - name: rules_default ruleset: - metric: request_cpu price: 0.005 unit: core-hours - metric: usage_cpu price: 0.015 unit: core-hours - metric: request_memory price: 0.004 unit: GiB-hours - metric: usage_memory price: 0.012 unit: GiB-hours """) rules.ensure_rules_config(rules_test)
def test_ruleset_base_config(self): rules_test = yaml.safe_load(""" - name: rules_small labelSet: instance_type: small storage_type: ssd gpu_accel: "false" ruleset: - metric: request_cpu price: 0.00075 unit: core-hours - metric: usage_cpu price: 0.0015 unit: core-hours - metric: request_memory price: 0.0007 unit: GiB-hours - metric: usage_memory price: 0.0014 unit: GiB-hours - name: rules_medium labelSet: instance_type: medium storage_type: hdd gpu_accel: "true" ruleset: - metric: request_cpu price: 0.0008 unit: core-hours - metric: usage_cpu price: 0.0025 unit: core-hours - metric: request_memory price: 0.0007 unit: GiB-hours - metric: usage_memory price: 0.002 unit: GiB-hours - name: rules_large labelSet: instance_type: large storage_type: hdd gpu_accel: "false" ruleset: - metric: request_cpu price: 0.0009 unit: core-hours - metric: usage_cpu price: 0.0026 unit: core-hours - metric: request_memory price: 0.0008 unit: GiB-hours - metric: usage_memory price: 0.0022 unit: GiB-hours - name: rules_default ruleset: - metric: request_cpu price: 0.005 unit: core-hours - metric: usage_cpu price: 0.015 unit: core-hours - metric: request_memory price: 0.004 unit: GiB-hours - metric: usage_memory price: 0.012 unit: GiB-hours """) rules.ensure_rules_config(rules_test)