diff --git a/go.mod b/go.mod index 73aacff..efbd4d6 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 40a7574..573aa4e 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main_test.go b/main_test.go index f51de8c..4ebfc46 100644 --- a/main_test.go +++ b/main_test.go @@ -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" ) @@ -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()) @@ -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 := ""