Skip to content

Commit 0777080

Browse files
authored
test: introduce integration test package (#819)
1 parent 0707f70 commit 0777080

26 files changed

+233
-100
lines changed

pkg/test/integration/config.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package integration
5+
6+
// TestConfig holds project-specific configuration for integration tests
7+
type TestConfig struct {
8+
// Environment variable prefixes
9+
EnvPrefix string
10+
11+
// Docker compose configuration
12+
ComposeServiceName string
13+
ComposeImageName string
14+
DockerfilePath string
15+
ConfigPath string
16+
17+
// Metric naming
18+
MetricPrefix string
19+
IPAttribute string
20+
21+
// Telemetry SDK attributes
22+
SDKName string
23+
VersionPkg string
24+
}
25+
26+
// DefaultOBIConfig returns the default OBI test configuration
27+
func DefaultOBIConfig() *TestConfig {
28+
return &TestConfig{
29+
EnvPrefix: "OTEL_EBPF_",
30+
ComposeServiceName: "obi",
31+
ComposeImageName: "hatest-obi",
32+
DockerfilePath: "ebpf-instrument/Dockerfile",
33+
ConfigPath: "obi-config.yml",
34+
MetricPrefix: "obi",
35+
IPAttribute: "obi.ip",
36+
SDKName: "opentelemetry-ebpf-instrumentation",
37+
VersionPkg: "obibuildinfo.Version",
38+
}
39+
}

pkg/test/integration/constants.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package integration
5+
6+
import "time"
7+
8+
const (
9+
instrumentedServiceStdURL = "http://localhost:8080"
10+
testTimeout = 60 * time.Second
11+
)

pkg/test/integration/doc.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package integration provides reusable integration test utilities
5+
// for downstream projects, whilst allowing each project to maintain their own
6+
// project-specific configuration.
7+
//
8+
// Example usage:
9+
//
10+
// config := &integration.TestConfig{
11+
// EnvPrefix: "MY_PROJECT_",
12+
// ComposeServiceName: "my-service",
13+
// MetricPrefix: "myproject",
14+
// // ... other project-specific settings
15+
// }
16+
// integration.InternalPrometheusExport(t, config)
17+
//
18+
// For OBI, use the provided DefaultOBIConfig() function.
19+
package integration

test/integration/internals_test.go renamed to pkg/test/integration/internals.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//go:build integration
5-
64
package integration
75

86
import (
@@ -18,16 +16,21 @@ import (
1816
)
1917

2018
const (
21-
flushesMetricName = "obi_ebpf_tracer_flushes"
22-
promRequestsMetricName = "obi_prometheus_http_requests_total"
19+
flushesMetricName = "_ebpf_tracer_flushes"
20+
promRequestsMetricName = "_prometheus_http_requests_total"
2321
internalPrometheusMetricsURL = "http://localhost:8999/internal/metrics"
2422
)
2523

26-
func testInternalPrometheusExport(t *testing.T) {
24+
// InternalPrometheusExport tests that internal metrics are properly exposed and updated
25+
func InternalPrometheusExport(t *testing.T, config *TestConfig) {
26+
// Use config-specific metric names
27+
flushesMetricName := config.MetricPrefix + flushesMetricName
28+
promRequestsMetricName := config.MetricPrefix + promRequestsMetricName
29+
2730
// tests that internal metrics are properly exposed and updated
2831
initialFlushedRecords := metricValue(t, flushesMetricName, nil)
2932
for i := 0; i < 7; i++ {
30-
doHTTPGet(t, instrumentedServiceStdURL+"/testing/some/flushes", http.StatusOK)
33+
DoHTTPGet(t, instrumentedServiceStdURL+"/testing/some/flushes", http.StatusOK)
3134
}
3235
eventuallyIterations := 0
3336
test.Eventually(t, testTimeout, func(t require.TestingT) {

pkg/test/integration/utils.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package integration
5+
6+
import (
7+
"bytes"
8+
"crypto/tls"
9+
"net/http"
10+
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
// HTTP client for testing
15+
var testHTTPClient = &http.Client{
16+
Transport: &http.Transport{
17+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
18+
},
19+
}
20+
21+
func DoHTTPGet(t require.TestingT, path string, status int) {
22+
// Random fake body to cause the request to have some size (38 bytes)
23+
jsonBody := []byte(`{"productId": 123456, "quantity": 100}`)
24+
25+
req, err := http.NewRequest(http.MethodGet, path, bytes.NewReader(jsonBody))
26+
require.NoError(t, err)
27+
req.Header.Set("Content-Type", "application/json")
28+
29+
r, err := testHTTPClient.Do(req)
30+
require.NoError(t, err)
31+
require.Equal(t, status, r.StatusCode)
32+
}

test/integration/constants.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//go:build integration
5+
6+
package integration
7+
8+
import "time"
9+
10+
const (
11+
instrumentedServiceStdURL = "http://localhost:8080"
12+
instrumentedServiceGinURL = "http://localhost:8081"
13+
instrumentedServiceGorillaURL = "http://localhost:8082"
14+
instrumentedServiceGorillaMidURL = "http://localhost:8083"
15+
instrumentedServiceGorillaMid2URL = "http://localhost:8087"
16+
instrumentedServiceStdTLSURL = "https://localhost:8383"
17+
instrumentedServiceJSONRPCURL = "http://localhost:8088"
18+
prometheusHostPort = "localhost:9090"
19+
jaegerQueryURL = "http://localhost:16686/api/traces"
20+
21+
testTimeout = 60 * time.Second
22+
)

test/integration/discovery_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/mariomac/guara/pkg/test"
1717
"github.com/stretchr/testify/require"
1818

19+
ti "go.opentelemetry.io/obi/pkg/test/integration"
1920
"go.opentelemetry.io/obi/test/integration/components/docker"
2021
"go.opentelemetry.io/obi/test/integration/components/jaeger"
2122
"go.opentelemetry.io/obi/test/integration/components/prom"
@@ -48,16 +49,16 @@ func testSelectiveExports(t *testing.T) {
4849
// this with a proper check to see if the target process has finished
4950
// being instrumented
5051
test.Eventually(t, 3*time.Minute, func(t require.TestingT) {
51-
doHTTPGet(t, "http://localhost:5001/b", 200)
52+
ti.DoHTTPGet(t, "http://localhost:5001/b", 200)
5253
bTraces := getTraces("service-b", "/b")
5354
require.NotNil(t, bTraces)
5455
})
5556

5657
// Run couple of requests to make sure we flush out any transactions that might be
5758
// stuck because of our tracking of full request times
5859
for i := 0; i < 10; i++ {
59-
doHTTPGet(t, "http://localhost:5000/a", 200)
60-
doHTTPGet(t, "http://localhost:5001/b", 200)
60+
ti.DoHTTPGet(t, "http://localhost:5000/a", 200)
61+
ti.DoHTTPGet(t, "http://localhost:5001/b", 200)
6162
}
6263

6364
test.Eventually(t, testTimeout, func(t require.TestingT) {

test/integration/http_go_otel_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ import (
1717
"github.com/stretchr/testify/assert"
1818
"github.com/stretchr/testify/require"
1919

20+
ti "go.opentelemetry.io/obi/pkg/test/integration"
2021
"go.opentelemetry.io/obi/test/integration/components/docker"
2122
"go.opentelemetry.io/obi/test/integration/components/jaeger"
2223
"go.opentelemetry.io/obi/test/integration/components/prom"
2324
)
2425

2526
func testForHTTPGoOTelLibrary(t *testing.T, route, svcNs string) {
2627
for i := 0; i < 4; i++ {
27-
doHTTPGet(t, "http://localhost:8080"+route, 200)
28+
ti.DoHTTPGet(t, "http://localhost:8080"+route, 200)
2829
}
2930

3031
// Eventually, Prometheus would make this query visible
@@ -80,7 +81,7 @@ func testForHTTPGoOTelLibrary(t *testing.T, route, svcNs string) {
8081

8182
func testInstrumentationMissing(t *testing.T, route, svcNs string) {
8283
for i := 0; i < 4; i++ {
83-
doHTTPGet(t, "http://localhost:8080"+route, 200)
84+
ti.DoHTTPGet(t, "http://localhost:8080"+route, 200)
8485
}
8586

8687
test.Eventually(t, testTimeout, func(t require.TestingT) {

test/integration/internal_metrics_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/stretchr/testify/assert"
1919
"github.com/stretchr/testify/require"
2020

21+
ti "go.opentelemetry.io/obi/pkg/test/integration"
2122
"go.opentelemetry.io/obi/test/integration/components/docker"
2223
"go.opentelemetry.io/obi/test/integration/components/prom"
2324
)
@@ -67,7 +68,7 @@ func TestAvoidedServicesMetrics(t *testing.T) {
6768

6869
// Make additional requests to ensure OTLP endpoints are hit
6970
for i := 0; i < 3; i++ {
70-
doHTTPGet(t, "http://localhost:8080/rolldice", 200)
71+
ti.DoHTTPGet(t, "http://localhost:8080/rolldice", 200)
7172
time.Sleep(1 * time.Second)
7273
}
7374

test/integration/nodejs_multiproc_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/stretchr/testify/assert"
1717
"github.com/stretchr/testify/require"
1818

19+
ti "go.opentelemetry.io/obi/pkg/test/integration"
1920
"go.opentelemetry.io/obi/test/integration/components/docker"
2021
"go.opentelemetry.io/obi/test/integration/components/jaeger"
2122
)
@@ -33,7 +34,7 @@ func testNestedTraces(t *testing.T) {
3334
// being instrumented
3435
t.Log("checking instrumentation status")
3536
test.Eventually(t, 2*time.Minute, func(t require.TestingT) {
36-
doHTTPGet(t, "http://localhost:5000/a", 200)
37+
ti.DoHTTPGet(t, "http://localhost:5000/a", 200)
3738

3839
resp, err := http.Get(jaegerQueryURL + "?service=service-a&limit=1")
3940
if err != nil || resp == nil || resp.StatusCode != http.StatusOK {
@@ -52,7 +53,7 @@ func testNestedTraces(t *testing.T) {
5253
// Run couple of requests to make sure we flush out any transactions that might be
5354
// stuck because of our tracking of full request times
5455
for i := 0; i < 10; i++ {
55-
doHTTPGet(t, "http://localhost:5000/a", 200)
56+
ti.DoHTTPGet(t, "http://localhost:5000/a", 200)
5657
}
5758

5859
// Get the first 5 traces

0 commit comments

Comments
 (0)