Skip to content

Commit

Permalink
Improved test reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanrolds committed Dec 18, 2024
1 parent b8d5695 commit c313144
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/Microsoft/hcsshim v0.9.4 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/avast/retry-go v3.0.0+incompatible // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/containerd/cgroups v1.0.4 // indirect
github.com/containerd/containerd v1.6.8 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/aws/aws-sdk-go v1.15.11/go.mod h1:mFuSZ37Z9YOHbQEwBWztmVzqXrEkub65tZoCYDt7FT0=
github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down
47 changes: 31 additions & 16 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"path"
"strings"
"testing"
"time"

"github.com/avast/retry-go"
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go"
)
Expand Down Expand Up @@ -212,9 +213,17 @@ func Test_Main_SampleXML(t *testing.T) {
t.Error(err)
}

collectorPort, err := otelCollector.MappedPort(ctx, "4317/tcp")
var collectorPort nat.Port
err = retry.Do(func() error {
collectorPort, err = otelCollector.MappedPort(ctx, "4317/tcp")
if err != nil {
return err
}

return nil
})
if err != nil {
t.Errorf("could not get mapped port for otel-collector: %v", err)
t.Errorf("could not get the collector port: %s", err)
}

os.Setenv(exporterEndpointKey, "http://localhost:"+collectorPort.Port())
Expand Down Expand Up @@ -251,25 +260,31 @@ func Test_Main_SampleXML(t *testing.T) {
t.Error()
}

// TODO: retry until the file is written by the otel-exporter
time.Sleep(time.Second * 30)

// assert using the generated file
jsonBytes, err := os.ReadFile(reportFilePath)
if err != nil {
t.Error(err)
}

// merge both JSON files
// 1. get the spans and metrics JSONs, they are separated by \n
// 2. remote white spaces
// 3. unmarshal each resource separately
// 4. assign each resource to the test report struct
content := string(jsonBytes)

jsons := strings.Split(strings.TrimSpace(content), "\n")
if len(jsons) != 2 {
t.Errorf("expected 2 JSONs, got %d - %s", len(jsons), jsons)
var jsons []string
err = retry.Do(func() error {
// assert using the generated file
jsonBytes, err := os.ReadFile(reportFilePath)
if err != nil {
t.Error(err)
}

content := string(jsonBytes)

jsons = strings.Split(strings.TrimSpace(content), "\n")
if len(jsons) != 2 {
return fmt.Errorf("expected 2 JSONs, got %d - %s", len(jsons), jsons)
}

return nil
})
if err != nil {
t.Errorf("error while waiting for collector output: %s", err)
}

jsonSpans := ""
Expand Down

0 comments on commit c313144

Please sign in to comment.