Skip to content

Commit

Permalink
[chore][scraperinttest] Always print errors on failure (#22717)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski authored May 24, 2023
1 parent c27d96f commit 0801d00
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 59 deletions.
19 changes: 14 additions & 5 deletions internal/coreinternal/golden/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,33 @@ func WriteMetrics(t *testing.T, filePath string, metrics pmetric.Metrics) error
return nil
}

// writeMetrics writes a pmetric.Metrics to the specified file in YAML format.
func writeMetrics(filePath string, metrics pmetric.Metrics) error {
// MarshalMetricsYAML marshals a pmetric.Metrics to YAML format.
func MarshalMetricsYAML(metrics pmetric.Metrics) ([]byte, error) {
unmarshaler := &pmetric.JSONMarshaler{}
fileBytes, err := unmarshaler.MarshalMetrics(metrics)
if err != nil {
return err
return nil, err
}
var jsonVal map[string]interface{}
if err = json.Unmarshal(fileBytes, &jsonVal); err != nil {
return err
return nil, err
}
b := &bytes.Buffer{}
enc := yaml.NewEncoder(b)
enc.SetIndent(2)
if err := enc.Encode(jsonVal); err != nil {
return nil, err
}
return b.Bytes(), nil
}

// writeMetrics writes a pmetric.Metrics to the specified file in YAML format.
func writeMetrics(filePath string, metrics pmetric.Metrics) error {
b, err := MarshalMetricsYAML(metrics)
if err != nil {
return err
}
if err := os.WriteFile(filePath, b.Bytes(), 0600); err != nil {
if err := os.WriteFile(filePath, b, 0600); err != nil {
return err
}
return nil
Expand Down
61 changes: 18 additions & 43 deletions internal/coreinternal/scraperinttest/scraperint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
package scraperinttest // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/scraperinttest"

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -25,7 +23,6 @@ import (
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/receivertest"
"gopkg.in/yaml.v3"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
Expand Down Expand Up @@ -58,36 +55,41 @@ type IntegrationTest struct {
compareOptions []pmetrictest.CompareMetricsOption
compareTimeout time.Duration

writeExpected bool
dumpActualOnFailure bool
writeExpected bool
}

func (it *IntegrationTest) Run(t *testing.T) {
ctx := context.Background()

var ci *ContainerInfo
var ciErr error
if it.containerRequest != nil {
require.NoError(t, it.containerRequest.Validate())
for _, port := range it.containerRequest.ExposedPorts {
require.False(t, strings.ContainsRune(port, ':'), errExposedPort)
}

var container testcontainers.Container
var err error
var containerErr error
defer func() {
if t.Failed() && containerErr != nil {
t.Error(containerErr.Error())
}
}()
require.Eventually(t, func() bool {
container, err = testcontainers.GenericContainer(ctx,
container, containerErr = testcontainers.GenericContainer(ctx,
testcontainers.GenericContainerRequest{
ContainerRequest: *it.containerRequest,
Started: true,
})
return err == nil
return containerErr == nil
}, it.createContainerTimeout, time.Second)

defer func() {
require.NoError(t, container.Terminate(context.Background()))
}()

ci, err = containerInfo(ctx, container, it.containerRequest.ExposedPorts)
require.NoError(t, err)
ci, ciErr = containerInfo(ctx, container, it.containerRequest.ExposedPorts)
require.NoError(t, ciErr)
}

cfg := it.factory.CreateDefaultConfig()
Expand All @@ -111,36 +113,15 @@ func (it *IntegrationTest) Run(t *testing.T) {
// Defined outside of Eventually so it can be printed if the test fails
var validateErr error
defer func() {
if t.Failed() {
if t.Failed() && validateErr != nil {
t.Error(validateErr.Error())
numResults := len(sink.AllMetrics())
if numResults == 0 {
if len(sink.AllMetrics()) == 0 {
t.Error("no data emitted by scraper")
return
}
if it.dumpActualOnFailure {
// TODO copied from golden package - expose elsewhere
unmarshaler := &pmetric.JSONMarshaler{}
fileBytes, err := unmarshaler.MarshalMetrics(sink.AllMetrics()[numResults-1])
if err != nil {
t.Errorf("failed to marshal actual metrics to JSON: %v", err)
return
}

var jsonVal map[string]interface{}
if err = json.Unmarshal(fileBytes, &jsonVal); err != nil {
t.Errorf("failed to unmarshal actual metrics JSON: %v", err)
return
}
b := &bytes.Buffer{}
enc := yaml.NewEncoder(b)
enc.SetIndent(2)
if err := enc.Encode(jsonVal); err != nil {
t.Errorf("failed to encode actual metrics to YAML: %v", err)
return
}
t.Errorf("latest result:\n%s", b.Bytes())
}
metricBytes, err := golden.MarshalMetricsYAML(sink.AllMetrics()[len(sink.AllMetrics())-1])
require.NoError(t, err)
t.Errorf("latest result:\n%s", metricBytes)
}
}()

Expand Down Expand Up @@ -204,12 +185,6 @@ func WriteExpected() TestOption {
}
}

func WithDumpActualOnFailure() TestOption {
return func(it *IntegrationTest) {
it.dumpActualOnFailure = true
}
}

type customConfigFunc func(*testing.T, component.Config, *ContainerInfo)

type ContainerInfo struct {
Expand Down
2 changes: 0 additions & 2 deletions receiver/elasticsearchreceiver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,5 @@ func integrationTest(name string) func(*testing.T) {
pmetrictest.IgnoreScopeMetricsOrder(),
pmetrictest.IgnoreResourceMetricsOrder(),
),
// See https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/19755
scraperinttest.WithDumpActualOnFailure(),
).Run
}
2 changes: 0 additions & 2 deletions receiver/mongodbreceiver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,5 @@ func integrationTest(name string, script []string, cfgMod func(*Config)) func(*t
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp(),
),
// See https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/16273
scraperinttest.WithDumpActualOnFailure(),
).Run
}
2 changes: 0 additions & 2 deletions receiver/mysqlreceiver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,5 @@ func TestMySQLIntegration(t *testing.T) {
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp(),
),
// See https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/18286
scraperinttest.WithDumpActualOnFailure(),
).Run(t)
}
2 changes: 0 additions & 2 deletions receiver/rabbitmqreceiver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,5 @@ func TestRabbitmqIntegration(t *testing.T) {
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreMetricValues(),
),
// See https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/17201
scraperinttest.WithDumpActualOnFailure(),
).Run(t)
}
2 changes: 0 additions & 2 deletions receiver/riakreceiver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,5 @@ func TestRiakIntegration(t *testing.T) {
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp(),
),
// See https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/17556
scraperinttest.WithDumpActualOnFailure(),
).Run(t)
}
1 change: 0 additions & 1 deletion receiver/sqlqueryreceiver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ func TestPostgresIntegration(t *testing.T) {
func TestOracleDBIntegration(t *testing.T) {
scraperinttest.NewIntegrationTest(
NewFactory(),
scraperinttest.WithDumpActualOnFailure(),
scraperinttest.WithContainerRequest(
testcontainers.ContainerRequest{
FromDockerfile: testcontainers.FromDockerfile{
Expand Down

0 comments on commit 0801d00

Please sign in to comment.