From 04ff865a9dbd1ffd30c42537e3c8464583905e09 Mon Sep 17 00:00:00 2001 From: Tigran Najaryan Date: Fri, 22 Jul 2022 11:23:59 -0400 Subject: [PATCH] Move CheckFileFormatField tests to internal --- schema/internal/parser_checks_test.go | 41 ++++++ schema/v1.0/parser_test.go | 200 +++++++++++++------------- schema/v1.1/parser_test.go | 13 -- 3 files changed, 139 insertions(+), 115 deletions(-) create mode 100644 schema/internal/parser_checks_test.go diff --git a/schema/internal/parser_checks_test.go b/schema/internal/parser_checks_test.go new file mode 100644 index 00000000000..dab407de51c --- /dev/null +++ b/schema/internal/parser_checks_test.go @@ -0,0 +1,41 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package internal // import "go.opentelemetry.io/otel/schema/internal" + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCheckFileFormatField(t *testing.T) { + // Invalid file format version numbers. + assert.Error(t, CheckFileFormatField("not a semver", 1, 0)) + assert.Error(t, CheckFileFormatField("2.0.0", 1, 0)) + assert.Error(t, CheckFileFormatField("1.1.0", 1, 0)) + + assert.Error(t, CheckFileFormatField("1.2.0", 1, 1)) + + // Valid cases. + assert.NoError(t, CheckFileFormatField("1.0.0", 1, 0)) + assert.NoError(t, CheckFileFormatField("1.0.1", 1, 0)) + assert.NoError(t, CheckFileFormatField("1.0.10000-alpha+4857", 1, 0)) + + assert.NoError(t, CheckFileFormatField("1.0.0", 1, 1)) + assert.NoError(t, CheckFileFormatField("1.0.1", 1, 1)) + assert.NoError(t, CheckFileFormatField("1.0.10000-alpha+4857", 1, 1)) + assert.NoError(t, CheckFileFormatField("1.1.0", 1, 1)) + assert.NoError(t, CheckFileFormatField("1.1.1", 1, 1)) +} diff --git a/schema/v1.0/parser_test.go b/schema/v1.0/parser_test.go index 15480520b2c..72d20bd1162 100644 --- a/schema/v1.0/parser_test.go +++ b/schema/v1.0/parser_test.go @@ -20,7 +20,6 @@ import ( "github.com/stretchr/testify/assert" - "go.opentelemetry.io/otel/schema/internal" "go.opentelemetry.io/otel/schema/v1.0/ast" "go.opentelemetry.io/otel/schema/v1.0/types" ) @@ -29,123 +28,132 @@ func TestParseSchemaFile(t *testing.T) { ts, err := ParseFile("testdata/valid-example.yaml") assert.NoError(t, err) assert.NotNil(t, ts) - assert.EqualValues(t, &ast.Schema{ - FileFormat: "1.0.0", - SchemaURL: "https://opentelemetry.io/schemas/1.1.0", - Versions: map[types.TelemetryVersion]ast.VersionDef{ - "1.0.0": {}, - - "1.1.0": { - All: ast.Attributes{ - Changes: []ast.AttributeChange{ - {RenameAttributes: &ast.AttributeMap{ - "k8s.cluster.name": "kubernetes.cluster.name", - "k8s.namespace.name": "kubernetes.namespace.name", - "k8s.node.name": "kubernetes.node.name", - "k8s.node.uid": "kubernetes.node.uid", - "k8s.pod.name": "kubernetes.pod.name", - "k8s.pod.uid": "kubernetes.pod.uid", - "k8s.container.name": "kubernetes.container.name", - "k8s.replicaset.name": "kubernetes.replicaset.name", - "k8s.replicaset.uid": "kubernetes.replicaset.uid", - "k8s.cronjob.name": "kubernetes.cronjob.name", - "k8s.cronjob.uid": "kubernetes.cronjob.uid", - "k8s.job.name": "kubernetes.job.name", - "k8s.job.uid": "kubernetes.job.uid", - "k8s.statefulset.name": "kubernetes.statefulset.name", - "k8s.statefulset.uid": "kubernetes.statefulset.uid", - "k8s.daemonset.name": "kubernetes.daemonset.name", - "k8s.daemonset.uid": "kubernetes.daemonset.uid", - "k8s.deployment.name": "kubernetes.deployment.name", - "k8s.deployment.uid": "kubernetes.deployment.uid", - "service.namespace": "service.namespace.name", - }}, + assert.EqualValues( + t, &ast.Schema{ + FileFormat: "1.0.0", + SchemaURL: "https://opentelemetry.io/schemas/1.1.0", + Versions: map[types.TelemetryVersion]ast.VersionDef{ + "1.0.0": {}, + + "1.1.0": { + All: ast.Attributes{ + Changes: []ast.AttributeChange{ + { + RenameAttributes: &ast.AttributeMap{ + "k8s.cluster.name": "kubernetes.cluster.name", + "k8s.namespace.name": "kubernetes.namespace.name", + "k8s.node.name": "kubernetes.node.name", + "k8s.node.uid": "kubernetes.node.uid", + "k8s.pod.name": "kubernetes.pod.name", + "k8s.pod.uid": "kubernetes.pod.uid", + "k8s.container.name": "kubernetes.container.name", + "k8s.replicaset.name": "kubernetes.replicaset.name", + "k8s.replicaset.uid": "kubernetes.replicaset.uid", + "k8s.cronjob.name": "kubernetes.cronjob.name", + "k8s.cronjob.uid": "kubernetes.cronjob.uid", + "k8s.job.name": "kubernetes.job.name", + "k8s.job.uid": "kubernetes.job.uid", + "k8s.statefulset.name": "kubernetes.statefulset.name", + "k8s.statefulset.uid": "kubernetes.statefulset.uid", + "k8s.daemonset.name": "kubernetes.daemonset.name", + "k8s.daemonset.uid": "kubernetes.daemonset.uid", + "k8s.deployment.name": "kubernetes.deployment.name", + "k8s.deployment.uid": "kubernetes.deployment.uid", + "service.namespace": "service.namespace.name", + }, + }, + }, }, - }, - Resources: ast.Attributes{ - Changes: []ast.AttributeChange{ - { - RenameAttributes: &ast.AttributeMap{ - "telemetry.auto.version": "telemetry.auto_instr.version", + Resources: ast.Attributes{ + Changes: []ast.AttributeChange{ + { + RenameAttributes: &ast.AttributeMap{ + "telemetry.auto.version": "telemetry.auto_instr.version", + }, }, }, }, - }, - Spans: ast.Spans{ - Changes: []ast.SpansChange{ - { - RenameAttributes: &ast.AttributeMapForSpans{ - AttributeMap: ast.AttributeMap{ - "peer.service": "peer.service.name", + Spans: ast.Spans{ + Changes: []ast.SpansChange{ + { + RenameAttributes: &ast.AttributeMapForSpans{ + AttributeMap: ast.AttributeMap{ + "peer.service": "peer.service.name", + }, + ApplyToSpans: []types.SpanName{"HTTP GET"}, }, - ApplyToSpans: []types.SpanName{"HTTP GET"}, }, }, }, - }, - SpanEvents: ast.SpanEvents{ - Changes: []ast.SpanEventsChange{ - { - RenameEvents: &ast.RenameSpanEvents{ - EventNameMap: map[string]string{ - "exception.stacktrace": "exception.stack_trace", + SpanEvents: ast.SpanEvents{ + Changes: []ast.SpanEventsChange{ + { + RenameEvents: &ast.RenameSpanEvents{ + EventNameMap: map[string]string{ + "exception.stacktrace": "exception.stack_trace", + }, }, }, - }, - { - RenameAttributes: &ast.RenameSpanEventAttributes{ - ApplyToEvents: []types.EventName{"exception.stack_trace"}, - AttributeMap: ast.AttributeMap{ - "peer.service": "peer.service.name", + { + RenameAttributes: &ast.RenameSpanEventAttributes{ + ApplyToEvents: []types.EventName{"exception.stack_trace"}, + AttributeMap: ast.AttributeMap{ + "peer.service": "peer.service.name", + }, }, }, }, }, - }, - Logs: ast.Logs{Changes: []ast.LogsChange{ - {RenameAttributes: &ast.RenameAttributes{ - AttributeMap: map[string]string{ - "process.executable_name": "process.executable.name", - }, - }}, - }}, - - Metrics: ast.Metrics{ - Changes: []ast.MetricsChange{ - { - RenameAttributes: &ast.AttributeMapForMetrics{ - AttributeMap: map[string]string{ - "http.status_code": "http.response_status_code", + Logs: ast.Logs{ + Changes: []ast.LogsChange{ + { + RenameAttributes: &ast.RenameAttributes{ + AttributeMap: map[string]string{ + "process.executable_name": "process.executable.name", + }, }, - }}, - { - RenameMetrics: map[types.MetricName]types.MetricName{ - "container.cpu.usage.total": "cpu.usage.total", - "container.memory.usage.max": "memory.usage.max", }, }, - { - RenameAttributes: &ast.AttributeMapForMetrics{ - ApplyToMetrics: []types.MetricName{ - "system.cpu.utilization", - "system.memory.usage", - "system.memory.utilization", - "system.paging.usage", + }, + + Metrics: ast.Metrics{ + Changes: []ast.MetricsChange{ + { + RenameAttributes: &ast.AttributeMapForMetrics{ + AttributeMap: map[string]string{ + "http.status_code": "http.response_status_code", + }, }, - AttributeMap: map[string]string{ - "status": "state", + }, + { + RenameMetrics: map[types.MetricName]types.MetricName{ + "container.cpu.usage.total": "cpu.usage.total", + "container.memory.usage.max": "memory.usage.max", + }, + }, + { + RenameAttributes: &ast.AttributeMapForMetrics{ + ApplyToMetrics: []types.MetricName{ + "system.cpu.utilization", + "system.memory.usage", + "system.memory.utilization", + "system.paging.usage", + }, + AttributeMap: map[string]string{ + "status": "state", + }, }, }, }, }, }, }, - }, - }, ts) + }, ts, + ) } func TestFailParseSchemaFile(t *testing.T) { @@ -168,15 +176,3 @@ func TestFailParseSchema(t *testing.T) { _, err = Parse(bytes.NewReader([]byte("file_format: 1.0.0"))) assert.Error(t, err) } - -func TestCheckFileFormatField(t *testing.T) { - // Invalid file format version numbers. - assert.Error(t, internal.CheckFileFormatField("not a semver", supportedFormatMajor, supportedFormatMinor)) - assert.Error(t, internal.CheckFileFormatField("2.0.0", supportedFormatMajor, supportedFormatMinor)) - assert.Error(t, internal.CheckFileFormatField("1.1.0", supportedFormatMajor, supportedFormatMinor)) - - // Valid cases. - assert.NoError(t, internal.CheckFileFormatField("1.0.0", supportedFormatMajor, supportedFormatMinor)) - assert.NoError(t, internal.CheckFileFormatField("1.0.1", supportedFormatMajor, supportedFormatMinor)) - assert.NoError(t, internal.CheckFileFormatField("1.0.10000-alpha+4857", supportedFormatMajor, supportedFormatMinor)) -} diff --git a/schema/v1.1/parser_test.go b/schema/v1.1/parser_test.go index d2fe3498987..5f02a52b225 100644 --- a/schema/v1.1/parser_test.go +++ b/schema/v1.1/parser_test.go @@ -19,7 +19,6 @@ import ( "github.com/stretchr/testify/assert" - "go.opentelemetry.io/otel/schema/internal" ast10 "go.opentelemetry.io/otel/schema/v1.0/ast" types10 "go.opentelemetry.io/otel/schema/v1.0/types" ast11 "go.opentelemetry.io/otel/schema/v1.1/ast" @@ -173,15 +172,3 @@ func TestFailParseSchemaFile(t *testing.T) { assert.Error(t, err) assert.Nil(t, ts) } - -func TestCheckFileFormatField(t *testing.T) { - // Invalid file format version numbers. - assert.Error(t, internal.CheckFileFormatField("1.2.0", supportedFormatMajor, supportedFormatMinor)) - - // Valid cases. - assert.NoError(t, internal.CheckFileFormatField("1.0.0", supportedFormatMajor, supportedFormatMinor)) - assert.NoError(t, internal.CheckFileFormatField("1.0.1", supportedFormatMajor, supportedFormatMinor)) - assert.NoError(t, internal.CheckFileFormatField("1.0.10000-alpha+4857", supportedFormatMajor, supportedFormatMinor)) - assert.NoError(t, internal.CheckFileFormatField("1.1.0", supportedFormatMajor, supportedFormatMinor)) - assert.NoError(t, internal.CheckFileFormatField("1.1.1", supportedFormatMajor, supportedFormatMinor)) -}