1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"encoding/json"
"testing"
"github.com/newrelic/go-agent/internal/crossagent"
)
func TestMetricRules(t *testing.T) {
var tcs []struct {
Testname string `json:"testname"`
Rules metricRules `json:"rules"`
Tests []struct {
Input string `json:"input"`
Expected string `json:"expected"`
} `json:"tests"`
}
err := crossagent.ReadJSON("rules.json", &tcs)
if err != nil {
t.Fatal(err)
}
for _, tc := range tcs {
// This test relies upon Perl-specific regex syntax (negative
// lookahead assertions) which are not implemented in Go's
// regexp package. We believe these types of rules are
// exceedingly rare in practice, so we're skipping
// implementation of this exotic syntax for now.
if tc.Testname == "saxon's test" {
continue
}
for _, x := range tc.Tests {
out := tc.Rules.Apply(x.Input)
if out != x.Expected {
t.Fatal(tc.Testname, x.Input, out, x.Expected)
}
}
}
}
func TestMetricRuleWithNegativeLookaheadAssertion(t *testing.T) {
js := `[{
"match_expression":"^(?!account|application).*",
"replacement":"*",
"ignore":false,
"eval_order":0,
"each_segment":true
}]`
var rules metricRules
err := json.Unmarshal([]byte(js), &rules)
if nil != err {
t.Fatal(err)
}
if 0 != rules.Len() {
t.Fatal(rules)
}
}
func TestNilApplyRules(t *testing.T) {
var rules metricRules
input := "hello"
out := rules.Apply(input)
if input != out {
t.Fatal(input, out)
}
}
func TestAmbiguousReplacement(t *testing.T) {
js := `[{
"match_expression":"(.*)/[^/]*.(bmp|css|gif|ico|jpg|jpeg|js|png)",
"replacement":"\\\\1/*.\\2",
"ignore":false,
"eval_order":0
}]`
var rules metricRules
err := json.Unmarshal([]byte(js), &rules)
if nil != err {
t.Fatal(err)
}
if 0 != rules.Len() {
t.Fatal(rules)
}
}
func TestBadMetricRulesJSON(t *testing.T) {
js := `{}`
var rules metricRules
err := json.Unmarshal([]byte(js), &rules)
if nil == err {
t.Fatal("missing bad json error")
}
}
|