Skip to content

Commit

Permalink
[chore] [mdatagen] Fix generated tests for include/exclude capability
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryax committed Apr 16, 2024
1 parent f4570d1 commit 6a53350
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 55 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -60,52 +60,68 @@ none_set:
filter_set_include:
resource_attributes:
map.resource.attr:
enabled: true
include:
- regexp: ".*"
optional.resource.attr:
enabled: true
include:
- regexp: ".*"
slice.resource.attr:
enabled: true
include:
- regexp: ".*"
string.enum.resource.attr:
enabled: true
include:
- regexp: ".*"
string.resource.attr:
enabled: true
include:
- regexp: ".*"
string.resource.attr_disable_warning:
enabled: true
include:
- regexp: ".*"
string.resource.attr_remove_warning:
enabled: true
include:
- regexp: ".*"
string.resource.attr_to_be_removed:
enabled: true
include:
- regexp: ".*"
filter_set_exclude:
resource_attributes:
map.resource.attr:
enabled: true
exclude:
- regexp: ".*"
optional.resource.attr:
enabled: true
exclude:
- strict: "optional.resource.attr-val"
slice.resource.attr:
enabled: true
exclude:
- regexp: ".*"
string.enum.resource.attr:
enabled: true
exclude:
- strict: "one"
string.resource.attr:
enabled: true
exclude:
- strict: "string.resource.attr-val"
string.resource.attr_disable_warning:
enabled: true
exclude:
- strict: "string.resource.attr_disable_warning-val"
string.resource.attr_remove_warning:
enabled: true
exclude:
- strict: "string.resource.attr_remove_warning-val"
string.resource.attr_to_be_removed:
enabled: true
exclude:
- strict: "string.resource.attr_to_be_removed-val"
52 changes: 26 additions & 26 deletions cmd/mdatagen/templates/metrics_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,44 @@ import (
)


type metricsSet int
type testDataSet int

const (
metricsSetDefault metricsSet = iota
metricsSetAll
metricsSetNone
testDataSetDefault testDataSet = iota
testDataSetAll
testDataSetNone
)

func TestMetricsBuilder(t *testing.T) {
tests := []struct {
name string
metricsSet metricsSet
resAttrsConfigured bool
metricsSet testDataSet
resAttrsSet testDataSet
expectEmpty bool
}{
{
name: "default",
},
{
name: "all_set",
metricsSet: metricsSetAll,
resAttrsConfigured: true,
name: "all_set",
metricsSet: testDataSetAll,
resAttrsSet: testDataSetAll,
},
{
name: "none_set",
metricsSet: metricsSetNone,
resAttrsConfigured: true,
expectEmpty: true,
name: "none_set",
metricsSet: testDataSetNone,
resAttrsSet: testDataSetNone,
expectEmpty: true,
},
{{- if .ResourceAttributes }}
{
name: "filter_set_include",
resAttrsConfigured: true,
name: "filter_set_include",
resAttrsSet: testDataSetAll,
},
{
name: "filter_set_exclude",
resAttrsConfigured: true,
expectEmpty: true,
name: "filter_set_exclude",
resAttrsSet: testDataSetAll,
expectEmpty: true,
},
{{- end }}
}
Expand All @@ -67,39 +67,39 @@ func TestMetricsBuilder(t *testing.T) {
expectedWarnings := 0
{{- range $name, $metric := .Metrics }}
{{- if and $metric.Enabled $metric.Warnings.IfEnabled }}
if test.metricsSet == metricsSetDefault || test.metricsSet == metricsSetAll {
if test.metricsSet == testDataSetDefault || test.metricsSet == testDataSetAll {
assert.Equal(t, "[WARNING] `{{ $name }}` should not be enabled: {{ $metric.Warnings.IfEnabled }}", observedLogs.All()[expectedWarnings].Message)
expectedWarnings++
}
{{- end }}
{{- if $metric.Warnings.IfEnabledNotSet }}
if test.metricsSet == metricsSetDefault {
if test.metricsSet == testDataSetDefault {
assert.Equal(t, "[WARNING] Please set `enabled` field explicitly for `{{ $name }}`: {{ $metric.Warnings.IfEnabledNotSet }}", observedLogs.All()[expectedWarnings].Message)
expectedWarnings++
}
{{- end }}
{{- if $metric.Warnings.IfConfigured }}
if test.metricsSet == metricsSetAll || test.metricsSet == metricsSetNone {
if test.metricsSet == testDataSetAll || test.metricsSet == testDataSetNone {
assert.Equal(t, "[WARNING] `{{ $name }}` should not be configured: {{ $metric.Warnings.IfConfigured }}", observedLogs.All()[expectedWarnings].Message)
expectedWarnings++
}
{{- end }}
{{- end }}
{{- range $name, $attr := .ResourceAttributes }}
{{- if and $attr.Enabled $attr.Warnings.IfEnabled }}
if test.metricsSet == metricsSetDefault || test.metricsSet == metricsSetAll {
if test.resAttrsSet == testDataSetDefault || test.resAttrsSet == testDataSetAll {
assert.Equal(t, "[WARNING] `{{ $name }}` should not be enabled: {{ $attr.Warnings.IfEnabled }}", observedLogs.All()[expectedWarnings].Message)
expectedWarnings++
}
{{- end }}
{{- if $attr.Warnings.IfEnabledNotSet }}
if test.metricsSet == metricsSetDefault {
if test.resAttrsSet == testDataSetDefault {
assert.Equal(t, "[WARNING] Please set `enabled` field explicitly for `{{ $name }}`: {{ $attr.Warnings.IfEnabledNotSet }}", observedLogs.All()[expectedWarnings].Message)
expectedWarnings++
}
{{- end }}
{{- if $attr.Warnings.IfConfigured }}
if test.resAttrsConfigured {
if test.resAttrsSet == testDataSetAll || test.resAttrsSet == testDataSetNone {
assert.Equal(t, "[WARNING] `{{ $name }}` should not be configured: {{ $attr.Warnings.IfConfigured }}", observedLogs.All()[expectedWarnings].Message)
expectedWarnings++
}
Expand Down Expand Up @@ -146,10 +146,10 @@ func TestMetricsBuilder(t *testing.T) {
assert.Equal(t, res, rm.Resource())
assert.Equal(t, 1, rm.ScopeMetrics().Len())
ms := rm.ScopeMetrics().At(0).Metrics()
if test.metricsSet == metricsSetDefault {
if test.metricsSet == testDataSetDefault {
assert.Equal(t, defaultMetricsCount, ms.Len())
}
if test.metricsSet == metricsSetAll {
if test.metricsSet == testDataSetAll {
assert.Equal(t, allMetricsCount, ms.Len())
}
validatedMetrics := make(map[string]bool)
Expand Down
2 changes: 2 additions & 0 deletions cmd/mdatagen/templates/testdata/config.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ filter_set_include:
resource_attributes:
{{- range $name, $attr := .ResourceAttributes }}
{{ $name }}:
enabled: true
include:
- regexp: ".*"
{{- end }}
filter_set_exclude:
resource_attributes:
{{- range $name, $attr := .ResourceAttributes }}
{{ $name }}:
enabled: true
exclude:
{{- if eq $attr.Type.String "Str" }}
- strict: {{ $attr.TestValue }}
Expand Down

0 comments on commit 6a53350

Please sign in to comment.