Skip to content

Commit 8f224a0

Browse files
tests module: Add testcontainers helpers (open-telemetry#76)
1 parent fbae5b2 commit 8f224a0

File tree

7 files changed

+719
-21
lines changed

7 files changed

+719
-21
lines changed

tests/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ is:
77
1. Building the Collector (`make otelcol` or `make all`)
88
1. Defining your expected [resource metric content](./testutils/README.md#resource-metrics) as a yaml file
99
([see example](./testutils/testdata/resourceMetrics.yaml))
10-
1. Spin up your target resources as docker containers (TODO)
10+
1. Spin up your target resources as [docker containers](./testutils/README.md#test-containers).
1111
1. Stand up an in-memory OTLP Receiver and metric sink capable of detecting if/when desired data are received (TODO).
1212
1. Spin up your Collector as a subprocess configured to report to this OTLP receiver (TODO)
1313

tests/go.mod

+10-12
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@ go 1.15
44

55
require (
66
github.com/docker/go-connections v0.4.0
7-
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jmxreceiver v0.19.0
87
github.com/stretchr/testify v1.7.0
98
github.com/testcontainers/testcontainers-go v0.9.0
10-
go.opentelemetry.io/collector v0.19.0
11-
go.uber.org/zap v1.16.0
9+
go.opentelemetry.io/collector v0.19.1-0.20210127225953-68c5961f7bc2
1210
gopkg.in/yaml.v2 v2.4.0
1311
)
1412

1513
replace (
16-
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter v0.19.0
17-
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/splunkhecexporter v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/exporter/splunkhecexporter v0.19.0
18-
github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer v0.19.0
19-
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.19.0
20-
github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig v0.19.0
21-
github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk v0.19.0
22-
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr v0.19.0
23-
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver v0.19.0
24-
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver v0.19.0
14+
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter v0.19.1-0.20210203200406-65673ad3657b
15+
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/splunkhecexporter v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/exporter/splunkhecexporter v0.19.1-0.20210203200406-65673ad3657b
16+
github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer v0.19.1-0.20210203200406-65673ad3657b
17+
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.19.1-0.20210203200406-65673ad3657b
18+
github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig v0.19.1-0.20210203200406-65673ad3657b
19+
github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk v0.19.1-0.20210203200406-65673ad3657b
20+
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr v0.19.1-0.20210203200406-65673ad3657b
21+
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver v0.19.1-0.20210203200406-65673ad3657b
22+
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver v0.0.0-00010101000000-000000000000 => github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver v0.19.1-0.20210203200406-65673ad3657b
2523
)

tests/go.sum

+2-6
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,6 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
759759
github.com/onsi/gomega v1.10.2 h1:aY/nuoWlKJud2J6U0E3NWsjlg+0GtwXxgEqthRdzlcs=
760760
github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
761761
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
762-
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jmxreceiver v0.19.0 h1:3Nd15kRA3oPMTOwMiD9wenq9gDppCWRnxd01/znf8mE=
763-
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jmxreceiver v0.19.0/go.mod h1:7l21wZY9rt9BsGgX7b0XuPx2bPp9auFBrABQFjdpkbo=
764762
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
765763
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
766764
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
@@ -904,8 +902,6 @@ github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfP
904902
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
905903
github.com/shirou/gopsutil v3.20.12-0.20201210134652-afe0c04c5d5a+incompatible h1:uvFOXu1W/nsxgfbLu0jGTad6j0PbFDsXkwxK/rqT/AA=
906904
github.com/shirou/gopsutil v3.20.12-0.20201210134652-afe0c04c5d5a+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
907-
github.com/shirou/gopsutil v3.20.12+incompatible h1:6VEGkOXP/eP4o2Ilk8cSsX0PhOEfX6leqAnD+urrp9M=
908-
github.com/shirou/gopsutil v3.20.12+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
909905
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
910906
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
911907
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
@@ -1021,8 +1017,8 @@ go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
10211017
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
10221018
go.opencensus.io v0.22.5 h1:dntmOdLpSpHlVqbW5Eay97DelsZHe+55D+xC6i0dDS0=
10231019
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
1024-
go.opentelemetry.io/collector v0.19.0 h1:OIL5jgqnRjDKmg6oNtQCULfQSSBlMF3ptToiOVRteNo=
1025-
go.opentelemetry.io/collector v0.19.0/go.mod h1:KZcMoF4fYwruBKDP0e3Xi4fW6sYnia0xqw+WlVQzeuA=
1020+
go.opentelemetry.io/collector v0.19.1-0.20210127225953-68c5961f7bc2 h1:XE4VegZBlHN+Jab2mITYwd+bJo2CHTmLG2MmL0FoSTU=
1021+
go.opentelemetry.io/collector v0.19.1-0.20210127225953-68c5961f7bc2/go.mod h1:KZcMoF4fYwruBKDP0e3Xi4fW6sYnia0xqw+WlVQzeuA=
10261022
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
10271023
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
10281024
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=

tests/testutils/README.md

+37-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ resource_metrics:
1313
- attributes:
1414
a_resource_attribute: a_value
1515
another_resource_attribute: another_value
16-
ilms:
16+
instrumentation_library_metrics:
1717
- instrumentation_library:
1818
name: a library
1919
version: some version
@@ -32,7 +32,7 @@ resource_metrics:
3232
label_one: value_one
3333
label_two: value_two
3434
value: -123.456
35-
- ilms:
35+
- instrumentation_library_metrics:
3636
- instrumentation_library:
3737
name: an instrumentation library from a different resource without attributes
3838
metrics:
@@ -60,3 +60,38 @@ Using `PDataToResourceMetrics(myReceivedPDataMetrics)` you can use the assertion
6060
`ResourceMetrics` are the same as those received in your test case. `FlattenResourceMetrics()` is a good way to "normalize"
6161
metrics received over time to ensure that only unique datapoints are represented, and that all unique Resources and
6262
Instrumentation Libraries have a single item.
63+
64+
### Test Containers
65+
66+
The Testcontainers project is a popular testing resource for easy container creation and usage for a number of languages
67+
including [Go](https://github.com/testcontainers/testcontainers-go). The `testutils` package provides a helpful [container
68+
builder and wrapper library](./container.go) to avoid needing direct Docker api usage:
69+
70+
```go
71+
import "github.com/signafx/splunk-otel-collector/tests/testutils"
72+
73+
myContainerFromImageName := testutils.NewContainer().WithImage(
74+
"my-docker-image:123.4-alpine",
75+
).WithEnvVar("MY_ENV_VAR", "ENV_VAR_VALUE",
76+
).WithExposedPorts("12345:12345").WillWaitForPorts("12345",
77+
).WillWaitForLogs(
78+
"My container is running and ready for interaction"
79+
).Build()
80+
81+
// After building, `myContainerFromImageName` implements the testscontainer.Container interface
82+
err := myContainerFromImageName.Start(context.Background())
83+
84+
myContainerFromBuildContext := testutils.NewContainer().WithContext(
85+
"./directory_with_dockerfile_and_resources",
86+
).WithEnv(map[string]string{
87+
"MY_ENV_VAR_1": "value1",
88+
"MY_ENV_VAR_2": "value2",
89+
"MY_ENV_VAR_3": "value3",
90+
}).WithExposedPorts("23456:23456", "34567:34567",
91+
).WillWaitForPorts("23456", "34567",
92+
).WillWaitForLogs(
93+
"My container is running.", "My container is ready for interaction"
94+
).Build()
95+
96+
err = myContainerFromBuildContext.Start(context.Background())
97+
```

0 commit comments

Comments
 (0)