16
16
import os
17
17
import pytest
18
18
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
20
25
21
26
CURRENT_DIR = os .path .dirname (os .path .realpath (__file__ ))
22
27
FIXTURE = os .path .normpath (os .path .join (
23
28
CURRENT_DIR , 'fixtures' , 'rules.json' ))
24
29
30
+
25
31
def _load_tests ():
26
32
with open (FIXTURE , 'r' ) as fh :
27
33
js = fh .read ()
28
34
return json .loads (js )
29
35
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
36
36
37
37
def _make_case_insensitive (rules ):
38
38
# lowercase each rule
@@ -42,14 +42,14 @@ def _make_case_insensitive(rules):
42
42
rule ['replacement' ] = rule ['replacement' ].lower ()
43
43
return rules
44
44
45
+
45
46
@pytest .mark .parametrize ('test_group' , _load_tests ())
46
47
def test_rules_engine (test_group ):
47
48
48
49
# FIXME: The test fixture assumes that matching is case insensitive when it
49
50
# is not. To avoid errors, just lowercase all rules, inputs, and expected
50
51
# 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' ])
53
53
rules_engine = RulesEngine (test_rules )
54
54
55
55
for test in test_group ['tests' ]:
@@ -66,3 +66,46 @@ def test_rules_engine(test_group):
66
66
assert expected == ''
67
67
else :
68
68
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