Skip to content

Commit 924dab6

Browse files
authored
chore(grafana): use Run function (#3412)
* chore(grafana): use Run function * chore: add test for endpoints
1 parent 5941323 commit 924dab6

File tree

2 files changed

+72
-21
lines changed

2 files changed

+72
-21
lines changed

modules/grafana-lgtm/grafana.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package grafanalgtm
33
import (
44
"context"
55
"fmt"
6+
"time"
67

78
"github.com/testcontainers/testcontainers-go"
89
"github.com/testcontainers/testcontainers-go/log"
@@ -25,30 +26,30 @@ type GrafanaLGTMContainer struct {
2526

2627
// Run creates an instance of the Grafana LGTM container type
2728
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*GrafanaLGTMContainer, error) {
28-
req := testcontainers.ContainerRequest{
29-
Image: img,
30-
ExposedPorts: []string{GrafanaPort, LokiPort, TempoPort, OtlpGrpcPort, OtlpHttpPort, PrometheusPort},
31-
WaitingFor: wait.ForLog(".*The OpenTelemetry collector and the Grafana LGTM stack are up and running.*\\s").AsRegexp().WithOccurrence(1),
29+
moduleOpts := []testcontainers.ContainerCustomizer{
30+
testcontainers.WithExposedPorts(GrafanaPort, LokiPort, TempoPort, OtlpGrpcPort, OtlpHttpPort, PrometheusPort),
31+
testcontainers.WithWaitStrategyAndDeadline(2*time.Minute,
32+
wait.ForLog(".*The OpenTelemetry collector and the Grafana LGTM stack are up and running.*\\s").AsRegexp().WithOccurrence(1),
33+
wait.ForListeningPort(GrafanaPort),
34+
wait.ForListeningPort(LokiPort),
35+
wait.ForListeningPort(TempoPort),
36+
wait.ForListeningPort(OtlpGrpcPort),
37+
wait.ForListeningPort(OtlpHttpPort),
38+
wait.ForListeningPort(PrometheusPort),
39+
),
40+
}
41+
42+
moduleOpts = append(moduleOpts, opts...)
43+
44+
var c *GrafanaLGTMContainer
45+
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)
46+
if ctr != nil {
47+
c = &GrafanaLGTMContainer{Container: ctr}
3248
}
33-
34-
genericContainerReq := testcontainers.GenericContainerRequest{
35-
ContainerRequest: req,
36-
Started: true,
37-
}
38-
39-
for _, opt := range opts {
40-
if err := opt.Customize(&genericContainerReq); err != nil {
41-
return nil, fmt.Errorf("customize: %w", err)
42-
}
43-
}
44-
45-
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
4649
if err != nil {
47-
return nil, fmt.Errorf("generic container: %w", err)
50+
return nil, fmt.Errorf("run grafana lgtm: %w", err)
4851
}
4952

50-
c := &GrafanaLGTMContainer{Container: container}
51-
5253
url, err := c.OtlpHttpEndpoint(ctx)
5354
if err != nil {
5455
// return the container instance to allow the caller to clean up

modules/grafana-lgtm/grafana_test.go

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"net"
78
"net/http"
89
"net/url"
910
"testing"
@@ -23,7 +24,7 @@ func TestGrafanaLGTM(t *testing.T) {
2324

2425
// perform assertions
2526

26-
t.Run("container is running with right version", func(t *testing.T) {
27+
t.Run("right-version", func(t *testing.T) {
2728
healthURL, err := url.Parse(fmt.Sprintf("http://%s/api/health", grafanaLgtmContainer.MustHttpEndpoint(ctx)))
2829
require.NoError(t, err)
2930

@@ -46,4 +47,53 @@ func TestGrafanaLGTM(t *testing.T) {
4647
require.NoError(t, err)
4748
require.Equal(t, "11.0.0", body["version"])
4849
})
50+
51+
availableURL := func(t *testing.T, url string) {
52+
t.Helper()
53+
54+
conn, err := net.Dial("tcp", url)
55+
defer func() {
56+
if conn != nil {
57+
err := conn.Close()
58+
require.NoError(t, err)
59+
}
60+
}()
61+
require.NoError(t, err)
62+
}
63+
64+
t.Run("loki-endpoint", func(t *testing.T) {
65+
lokiEndpoint := grafanaLgtmContainer.MustLokiEndpoint(ctx)
66+
require.NotEmpty(t, lokiEndpoint)
67+
availableURL(t, lokiEndpoint)
68+
})
69+
70+
t.Run("tempo-endpoint", func(t *testing.T) {
71+
tempoEndpoint := grafanaLgtmContainer.MustTempoEndpoint(ctx)
72+
require.NotEmpty(t, tempoEndpoint)
73+
availableURL(t, tempoEndpoint)
74+
})
75+
76+
t.Run("otlp-http-endpoint", func(t *testing.T) {
77+
otlpHTTPEndpoint := grafanaLgtmContainer.MustOtlpHttpEndpoint(ctx)
78+
require.NotEmpty(t, otlpHTTPEndpoint)
79+
availableURL(t, otlpHTTPEndpoint)
80+
})
81+
82+
t.Run("otlp-grpc-endpoint", func(t *testing.T) {
83+
otlpGrpcEndpoint := grafanaLgtmContainer.MustOtlpGrpcEndpoint(ctx)
84+
require.NotEmpty(t, otlpGrpcEndpoint)
85+
availableURL(t, otlpGrpcEndpoint)
86+
})
87+
88+
t.Run("prometheus-http-endpoint", func(t *testing.T) {
89+
prometheusHTTPEndpoint := grafanaLgtmContainer.MustPrometheusHttpEndpoint(ctx)
90+
require.NotEmpty(t, prometheusHTTPEndpoint)
91+
availableURL(t, prometheusHTTPEndpoint)
92+
})
93+
94+
t.Run("grafana-endpoint", func(t *testing.T) {
95+
grafanaEndpoint := grafanaLgtmContainer.MustHttpEndpoint(ctx)
96+
require.NotEmpty(t, grafanaEndpoint)
97+
availableURL(t, grafanaEndpoint)
98+
})
4999
}

0 commit comments

Comments
 (0)