Skip to content

Commit

Permalink
[mdatagen] Extract test data yaml to files (open-telemetry#6470)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryax authored and jamesmoessis committed Dec 6, 2021
1 parent d436189 commit 06e4890
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 64 deletions.
72 changes: 8 additions & 64 deletions cmd/mdatagen/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,70 +15,12 @@
package main

import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

const (
allOptions = `
name: metricreceiver
attributes:
freeFormAttribute:
description: Attribute that can take on any value.
freeFormAttributeWithValue:
value: state
description: Attribute that has alternate value set.
enumAttribute:
description: Attribute with a known set of values.
enum: [red, green, blue]
metrics:
system.cpu.time:
description: Total CPU seconds broken down by different states.
extended_documentation: Additional information on CPU Time can be found [here](https://en.wikipedia.org/wiki/CPU_time).
unit: s
sum:
monotonic: true
aggregation: cumulative
attributes: [freeFormAttribute, freeFormAttributeWithValue, enumAttribute]
`

unknownMetricAttribute = `
name: metricreceiver
metrics:
system.cpu.time:
description: Total CPU seconds broken down by different states.
unit: s
sum:
monotonic: true
aggregation: cumulative
attributes: [missing]
`
noMetricType = `
name: metricreceiver
metrics:
system.cpu.time:
description: Total CPU seconds broken down by different states.
unit: s
attributes:
`
twoMetricTypes = `
name: metricreceiver
metrics:
system.cpu.time:
description: Total CPU seconds broken down by different states.
unit: s
gauge: {}
sum:
monotonic: true
aggregation: cumulative
attributes:
`
)

func Test_loadMetadata(t *testing.T) {
tests := []struct {
name string
Expand All @@ -88,7 +30,7 @@ func Test_loadMetadata(t *testing.T) {
}{
{
name: "all options",
yml: allOptions,
yml: "testdata/all_options.yaml",
want: metadata{
Name: "metricreceiver",
Attributes: map[attributeName]attribute{
Expand Down Expand Up @@ -118,29 +60,31 @@ func Test_loadMetadata(t *testing.T) {
},
{
name: "unknown metric attribute",
yml: unknownMetricAttribute,
yml: "testdata/unknown_metric_attribute.yaml",
want: metadata{},
wantErr: "error validating struct:\n\tmetadata.Metrics[system.cpu.time]." +
"Attributes[missing]: unknown attribute value\n",
},
{
name: "no metric type",
yml: noMetricType,
yml: "testdata/no_metric_type.yaml",
want: metadata{},
wantErr: "metric system.cpu.time doesn't have a metric type key, " +
"one of the following has to be specified: sum, gauge, histogram",
},
{
name: "two metric types",
yml: twoMetricTypes,
yml: "testdata/two_metric_types.yaml",
want: metadata{},
wantErr: "metric system.cpu.time has more than one metric type keys, " +
"only one of the following has to be specified: sum, gauge, histogram",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := loadMetadata([]byte(tt.yml))
ymlData, err := os.ReadFile(tt.yml)
require.NoError(t, err)
got, err := loadMetadata(ymlData)
if tt.wantErr != "" {
require.Error(t, err)
require.EqualError(t, err, tt.wantErr)
Expand Down
22 changes: 22 additions & 0 deletions cmd/mdatagen/testdata/all_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: metricreceiver
attributes:
freeFormAttribute:
description: Attribute that can take on any value.

freeFormAttributeWithValue:
value: state
description: Attribute that has alternate value set.

enumAttribute:
description: Attribute with a known set of values.
enum: [red, green, blue]

metrics:
system.cpu.time:
description: Total CPU seconds broken down by different states.
extended_documentation: Additional information on CPU Time can be found [here](https://en.wikipedia.org/wiki/CPU_time).
unit: s
sum:
monotonic: true
aggregation: cumulative
attributes: [freeFormAttribute, freeFormAttributeWithValue, enumAttribute]
6 changes: 6 additions & 0 deletions cmd/mdatagen/testdata/no_metric_type.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: metricreceiver
metrics:
system.cpu.time:
description: Total CPU seconds broken down by different states.
unit: s
attributes:
10 changes: 10 additions & 0 deletions cmd/mdatagen/testdata/two_metric_types.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: metricreceiver
metrics:
system.cpu.time:
description: Total CPU seconds broken down by different states.
unit: s
gauge: {}
sum:
monotonic: true
aggregation: cumulative
attributes:
9 changes: 9 additions & 0 deletions cmd/mdatagen/testdata/unknown_metric_attribute.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: metricreceiver
metrics:
system.cpu.time:
description: Total CPU seconds broken down by different states.
unit: s
sum:
monotonic: true
aggregation: cumulative
attributes: [missing]

0 comments on commit 06e4890

Please sign in to comment.