1616import os
1717import pytest
1818
19- from newrelic .core .rules_engine import RulesEngine , NormalizationRule
19+ from newrelic .api .application import application_instance
20+ from newrelic .api .background_task import background_task
21+ from newrelic .api .transaction import record_custom_metric
22+ from newrelic .core .rules_engine import RulesEngine
23+
24+ from testing_support .validators .validate_metric_payload import validate_metric_payload
2025
2126CURRENT_DIR = os .path .dirname (os .path .realpath (__file__ ))
2227FIXTURE = os .path .normpath (os .path .join (
2328 CURRENT_DIR , 'fixtures' , 'rules.json' ))
2429
30+
2531def _load_tests ():
2632 with open (FIXTURE , 'r' ) as fh :
2733 js = fh .read ()
2834 return json .loads (js )
2935
30- def _prepare_rules (test_rules ):
31- # ensure all keys are present, if not present set to an empty string
32- for rule in test_rules :
33- for key in NormalizationRule ._fields :
34- rule [key ] = rule .get (key , '' )
35- return test_rules
3636
3737def _make_case_insensitive (rules ):
3838 # lowercase each rule
@@ -42,14 +42,14 @@ def _make_case_insensitive(rules):
4242 rule ['replacement' ] = rule ['replacement' ].lower ()
4343 return rules
4444
45+
4546@pytest .mark .parametrize ('test_group' , _load_tests ())
4647def test_rules_engine (test_group ):
4748
4849 # FIXME: The test fixture assumes that matching is case insensitive when it
4950 # is not. To avoid errors, just lowercase all rules, inputs, and expected
5051 # values.
51- insense_rules = _make_case_insensitive (test_group ['rules' ])
52- test_rules = _prepare_rules (insense_rules )
52+ test_rules = _make_case_insensitive (test_group ['rules' ])
5353 rules_engine = RulesEngine (test_rules )
5454
5555 for test in test_group ['tests' ]:
@@ -66,3 +66,46 @@ def test_rules_engine(test_group):
6666 assert expected == ''
6767 else :
6868 assert result == expected
69+
70+
71+ @pytest .mark .parametrize ('test_group' , _load_tests ())
72+ def test_rules_engine_metric_harvest (test_group ):
73+ # FIXME: The test fixture assumes that matching is case insensitive when it
74+ # is not. To avoid errors, just lowercase all rules, inputs, and expected
75+ # values.
76+ test_rules = _make_case_insensitive (test_group ['rules' ])
77+ rules_engine = RulesEngine (test_rules )
78+
79+ # Set rules engine on core application
80+ api_application = application_instance (activate = False )
81+ api_name = api_application .name
82+ core_application = api_application ._agent .application (api_name )
83+ old_rules = core_application ._rules_engine ["metric" ] # save previoius rules
84+ core_application ._rules_engine ["metric" ] = rules_engine
85+
86+ def send_metrics ():
87+ # Send all metrics in this test batch in one transaction, then harvest so the normalizer is run.
88+ @background_task (name = "send_metrics" )
89+ def _test ():
90+ for test in test_group ['tests' ]:
91+ # lowercase each value
92+ input_str = test ['input' ].lower ()
93+ record_custom_metric (input_str , {"count" : 1 })
94+ _test ()
95+ core_application .harvest ()
96+
97+ try :
98+ # Create a map of all result metrics to validate after harvest
99+ test_metrics = []
100+ for test in test_group ['tests' ]:
101+ expected = (test ['expected' ] or '' ).lower ()
102+ if expected == '' : # Ignored
103+ test_metrics .append ((expected , None ))
104+ else :
105+ test_metrics .append ((expected , 1 ))
106+
107+ # Harvest and validate resulting payload
108+ validate_metric_payload (metrics = test_metrics )(send_metrics )()
109+ finally :
110+ # Replace original rules engine
111+ core_application ._rules_engine ["metric" ] = old_rules
0 commit comments