Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Scraper test functionality to seperate package #5755

Merged
merged 23 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/a

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/stanza => ./internal/stanza

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest => ./internal/scrapertest

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/docker => ./internal/docker

replace github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alibabacloudlogserviceexporter => ./exporter/alibabacloudlogserviceexporter
Expand Down
1 change: 1 addition & 0 deletions internal/scrapertest/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
46 changes: 46 additions & 0 deletions internal/scrapertest/expected.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 scrapertest

import (
"encoding/json"
"io/ioutil"

"go.opentelemetry.io/collector/model/otlp"
"go.opentelemetry.io/collector/model/pdata"
)

func ReadExpected(filePath string) (pdata.Metrics, error) {
expectedFileBytes, err := ioutil.ReadFile(filePath)
if err != nil {
return pdata.Metrics{}, err
}
unmarshaller := otlp.NewJSONMetricsUnmarshaler()
return unmarshaller.UnmarshalMetrics(expectedFileBytes)
}

func WriteExpected(filePath string, metrics pdata.Metrics) error {
bytes, err := otlp.NewJSONMetricsMarshaler().MarshalMetrics(metrics)
if err != nil {
return err
}
var jsonVal map[string]interface{}
json.Unmarshal(bytes, &jsonVal)
b, err := json.MarshalIndent(jsonVal, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(filePath, b, 0600)
}
67 changes: 67 additions & 0 deletions internal/scrapertest/expected_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 scrapertest

import (
"io/ioutil"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/model/pdata"
)

func TestWriteExpected(t *testing.T) {
metricslice := baseTestMetrics()
metrics := pdata.NewMetrics()
metricslice.CopyTo(metrics.ResourceMetrics().AppendEmpty().InstrumentationLibraryMetrics().AppendEmpty().Metrics())

tempDir := filepath.Join(t.TempDir(), "metrics.json")
WriteExpected(tempDir, metrics)

actualBytes, err := ioutil.ReadFile(tempDir)
require.NoError(t, err)

expectedFile := filepath.Join("testdata", "roundtrip", "expected.json")
expectedBytes, err := ioutil.ReadFile(expectedFile)
require.NoError(t, err)

require.Equal(t, expectedBytes, actualBytes)
}

func TestReadExpected(t *testing.T) {
metricslice := baseTestMetrics()
expectedMetrics := pdata.NewMetrics()
metricslice.CopyTo(expectedMetrics.ResourceMetrics().AppendEmpty().InstrumentationLibraryMetrics().AppendEmpty().Metrics())

expectedFile := filepath.Join("testdata", "roundtrip", "expected.json")
actualMetrics, err := ReadExpected(expectedFile)
require.NoError(t, err)
require.Equal(t, expectedMetrics, actualMetrics)
}

func TestRoundTrip(t *testing.T) {
metricslice := baseTestMetrics()
expectedMetrics := pdata.NewMetrics()
metricslice.CopyTo(expectedMetrics.ResourceMetrics().AppendEmpty().InstrumentationLibraryMetrics().AppendEmpty().Metrics())

tempDir := filepath.Join(t.TempDir(), "metrics.json")
err := WriteExpected(tempDir, expectedMetrics)
require.NoError(t, err)

actualMetrics, err := ReadExpected(tempDir)
require.NoError(t, err)
require.Equal(t, expectedMetrics, actualMetrics)
}
27 changes: 27 additions & 0 deletions internal/scrapertest/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest

go 1.17

require (
github.com/stretchr/testify v1.7.0
go.opentelemetry.io/collector/model v0.36.1-0.20211004155959-190f8fbb2b9a
go.uber.org/multierr v1.7.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/genproto v0.0.0-20210604141403-392c879c8b08 // indirect
google.golang.org/grpc v1.41.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/scraperhelper => ../../receiver/scraperhelper
Loading