Skip to content

Commit

Permalink
feat: add exceptions source file parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
gsanchezgavier committed Jun 27, 2022
1 parent 850b60c commit cfe1fb9
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 20 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ The spec file for the e2e needs to be a yaml file with the following structure:
- `source` : Relative path to the integration spec file (It defines the entities and metrics) that will be parsed to match the metrics got from NROne.
- `except_entities` : Array of entities whose metrics will be skipped.
- `except_metrics` : Array of metrics to skip.
- `exceptions_source` : Relative (to the spec file) path to a YAML file containing extra exceptions. This metrics are appended to the ones defined in `except_metrics` and `except_entities`.
- `entities` : Array of entities to chek existing in NROne.
- `type` : Type of the entity to look for in NROne
- `data_type` : Name of the table to check for the entity in NROne (If V4 integration, will always be Metric)
Expand Down Expand Up @@ -153,7 +154,16 @@ scenarios:
except_metrics:
- powerdns_authoritative_answers_bytes_total
- powerdns_recursor_cache_lookups_total
# additionals: ""
exceptions_source: "powerdns-custom-exceptions.yml"
```

Extra exceptions file `powerdns-custom-exceptions.yml` example:
```yaml
except_entities:
- POWERDNS_MY_CUSTOM_ENTITY
except_metrics:
- powerdns_metric_removed_on_version_x
```

### Custom Agent image
Expand Down
24 changes: 24 additions & 0 deletions newrelic-integration-e2e/internal/runtime/metrics_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ func (mt MetricsTester) Test(tests spec.Tests, customTagKey, customTagValue stri

func (mt MetricsTester) checkMetrics(entities []spec.Entity, tm spec.TestMetrics, queriedMetrics []string) []error {
var errors []error

if tm.ExceptionsSource != "" {
exceptMetricsPath := filepath.Join(mt.specParentDir, tm.ExceptionsSource)
mt.logger.Debugf("parsing the content of the except metrics source file: %s", exceptMetricsPath)

exceptions, err := parseExceptions(exceptMetricsPath)
if err != nil {
errors = append(errors, fmt.Errorf("reading except metrics source file %s: %w", exceptMetricsPath, err))
return errors
}

tm.ExceptMetrics = append(tm.ExceptMetrics, exceptions.ExceptMetrics...)
tm.ExceptEntities = append(tm.ExceptEntities, exceptions.ExceptEntities...)
}

for _, entity := range entities {
if mt.isEntityException(entity.EntityType, tm.ExceptEntities) {
continue
Expand Down Expand Up @@ -100,3 +115,12 @@ func (mt MetricsTester) containsMetric(metric string, queriedMetricsList []strin
}
return false
}

func parseExceptions(exceptMetricsPath string) (*spec.Exceptions, error) {
content, err := ioutil.ReadFile(exceptMetricsPath)
if err != nil {
return nil, fmt.Errorf("reading except metrics source file %s: %w", exceptMetricsPath, err)
}

return spec.ParseExceptionsFile(content)
}
58 changes: 45 additions & 13 deletions newrelic-integration-e2e/internal/runtime/metrics_tester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ func TestMetricsTester_Test(t *testing.T) {

inputTests := spec.Tests{Metrics: []spec.TestMetrics{
{
Source: "testdata/powerdns.yml",
ExceptEntities: nil,
ExceptMetrics: nil,
Source: "testdata/powerdns.yml",
Exceptions: spec.Exceptions{
ExceptEntities: nil,
ExceptMetrics: nil,
},
},
}}

Expand Down Expand Up @@ -54,37 +56,67 @@ func TestMetricsTester_checkMetrics(t *testing.T) {
queriedMetrics []string
numberOfErrorsExpected int
}{

{
name: "when no metrics it should return 3 errors, one from each missing metric",
testMetrics: spec.TestMetrics{
Source: "",
ExceptEntities: []string{},
ExceptMetrics: []string{},
Source: "",
Exceptions: spec.Exceptions{
ExceptEntities: []string{},
ExceptMetrics: []string{},
},
},
queriedMetrics: []string{},
numberOfErrorsExpected: 3,
},
{
name: "when only metrics from entity B but entity A excluded it shouldn't return errors",
testMetrics: spec.TestMetrics{
Source: "",
ExceptEntities: []string{"ENTITY-A"},
ExceptMetrics: []string{},
Source: "",
Exceptions: spec.Exceptions{
ExceptEntities: []string{"ENTITY-A"},
ExceptMetrics: []string{},
},
},
queriedMetrics: []string{"metric-B1", "metric-B2"},
numberOfErrorsExpected: 0,
},
{
name: "when a metric is not returned but it's excluded it shouldn't return errors",
testMetrics: spec.TestMetrics{
Source: "",
ExceptEntities: []string{},
ExceptMetrics: []string{"metric-A"},
Source: "",
Exceptions: spec.Exceptions{
ExceptEntities: []string{},
ExceptMetrics: []string{"metric-A"},
},
},
queriedMetrics: []string{"metric-B1", "metric-B2"},
numberOfErrorsExpected: 0,
},
{
name: "when a metric is excluded from exceptions file source it shouldn't return errors",
testMetrics: spec.TestMetrics{
Source: "",
Exceptions: spec.Exceptions{
ExceptEntities: []string{},
ExceptMetrics: []string{"metric-B1"},
},
ExceptionsSource: "testdata/exceptions_metric.yml",
},
queriedMetrics: []string{"metric-B2"},
numberOfErrorsExpected: 0,
},
{
name: "when an entity is excluded from except metrics source it shouldn't return errors",
testMetrics: spec.TestMetrics{
Source: "",
Exceptions: spec.Exceptions{
ExceptEntities: []string{"ENTITY-B"},
},
ExceptionsSource: "testdata/exceptions_entity.yml",
},
queriedMetrics: []string{},
numberOfErrorsExpected: 0,
},
}

for _, tt := range tests {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
except_entities:
- ENTITY-A
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
except_metrics:
- metric-A
22 changes: 18 additions & 4 deletions newrelic-integration-e2e/internal/spec/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,24 @@ type TestEntity struct {
}

type TestMetrics struct {
Source string `yaml:"source"`
ExceptEntities []string `yaml:"except_entities"`
ExceptMetrics []string `yaml:"except_metrics"`
ExpectedEntitiesNumber int `yaml:"expected_entities_number"`
Source string `yaml:"source"`
ExceptionsSource string `yaml:"exceptions_source"`
Exceptions
}

type Exceptions struct {
ExceptEntities []string `yaml:"except_entities"`
ExceptMetrics []string `yaml:"except_metrics"`
}

func ParseExceptionsFile(content []byte) (*Exceptions, error) {
exceptions := &Exceptions{}

if err := yaml.Unmarshal(content, exceptions); err != nil {
return nil, err
}

return exceptions, nil
}

func ParseDefinitionFile(content []byte) (*Definition, error) {
Expand Down
25 changes: 23 additions & 2 deletions newrelic-integration-e2e/internal/spec/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ import (
"github.com/stretchr/testify/assert"
)

func Test_ParseExceptionsFile(t *testing.T) {
sample := `
except_metrics:
- metric_a
- metric_b
except_entities:
- entity_a
`
exceptions, err := ParseExceptionsFile([]byte(sample))
assert.Nil(t, err)
assert.Equal(
t,
&Exceptions{
ExceptEntities: []string{"entity_a"},
ExceptMetrics: []string{"metric_a", "metric_b"},
},
exceptions)
}

func Test_ParseDefinitionFile(t *testing.T) {
sample := `
description: |
Expand Down Expand Up @@ -82,8 +101,10 @@ scenarios:
},
Metrics: []TestMetrics{
{
Source: "powerdns.yml",
ExceptMetrics: []string{"powerdns_authoritative_answers_bytes_total"},
Source: "powerdns.yml",
Exceptions: Exceptions{
ExceptMetrics: []string{"powerdns_authoritative_answers_bytes_total"},
},
},
},
},
Expand Down

0 comments on commit cfe1fb9

Please sign in to comment.