-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathintegration_test.go
161 lines (143 loc) · 5.11 KB
/
integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:build integration
package jmxreceiver
import (
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/receiver/receivertest"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/scraperinttest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
)
const jmxPort = "7199"
var jmxJarReleases = map[string]string{
"1.26.0-alpha": "https://repo1.maven.org/maven2/io/opentelemetry/contrib/opentelemetry-jmx-metrics/1.26.0-alpha/opentelemetry-jmx-metrics-1.26.0-alpha.jar",
"1.10.0-alpha": "https://repo1.maven.org/maven2/io/opentelemetry/contrib/opentelemetry-jmx-metrics/1.10.0-alpha/opentelemetry-jmx-metrics-1.10.0-alpha.jar",
}
type JMXIntegrationSuite struct {
suite.Suite
VersionToJar map[string]string
}
// It is recommended that this test be run locally with a longer timeout than the default 30s
// go test -timeout 60s -run ^TestJMXIntegration$ github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jmxreceiver
func TestJMXIntegration(t *testing.T) {
suite.Run(t, new(JMXIntegrationSuite))
}
func (suite *JMXIntegrationSuite) SetupSuite() {
suite.VersionToJar = make(map[string]string)
for version, url := range jmxJarReleases {
jarPath, err := downloadJMXMetricGathererJAR(url)
suite.VersionToJar[version] = jarPath
suite.Require().NoError(err)
}
}
func (suite *JMXIntegrationSuite) TearDownSuite() {
for _, path := range suite.VersionToJar {
suite.Require().NoError(os.Remove(path))
}
}
func downloadJMXMetricGathererJAR(url string) (string, error) {
resp, err := http.Get(url) //nolint:gosec
if err != nil {
return "", err
}
defer resp.Body.Close()
file, err := os.CreateTemp("", "jmx-metrics.jar")
if err != nil {
return "", err
}
defer file.Close()
_, err = io.Copy(file, resp.Body)
return file.Name(), err
}
func (suite *JMXIntegrationSuite) TestJMXReceiverHappyPath() {
for version, jar := range suite.VersionToJar {
suite.T().Run(version, integrationTest(version, jar))
}
}
func integrationTest(version string, jar string) func(*testing.T) {
return scraperinttest.NewIntegrationTest(
NewFactory(),
scraperinttest.WithContainerRequest(
testcontainers.ContainerRequest{
Image: "cassandra:3.11",
Env: map[string]string{
"LOCAL_JMX": "no",
"JVM_OPTS": "-Djava.rmi.server.hostname=0.0.0.0",
},
Files: []testcontainers.ContainerFile{{
HostFilePath: filepath.Join("testdata", "integration", "jmxremote.password"),
ContainerFilePath: "/etc/cassandra/jmxremote.password",
FileMode: 400,
}},
ExposedPorts: []string{jmxPort + ":" + jmxPort},
WaitingFor: wait.ForListeningPort(jmxPort),
}),
scraperinttest.AllowHardcodedHostPort(),
scraperinttest.WithCustomConfig(
func(t *testing.T, cfg component.Config, ci *scraperinttest.ContainerInfo) {
rCfg := cfg.(*Config)
rCfg.CollectionInterval = 3 * time.Second
rCfg.JARPath = jar
rCfg.Endpoint = fmt.Sprintf("%v:%s", ci.Host(t), ci.MappedPort(t, jmxPort))
rCfg.TargetSystem = "cassandra"
rCfg.Username = "cassandra"
rCfg.Password = "cassandra"
rCfg.ResourceAttributes = map[string]string{
"myattr": "myvalue",
"myotherattr": "myothervalue",
}
rCfg.OTLPExporterConfig = otlpExporterConfig{
Endpoint: "127.0.0.1:0",
TimeoutSettings: exporterhelper.TimeoutConfig{
Timeout: time.Second,
},
}
}),
scraperinttest.WithExpectedFile(filepath.Join("testdata", "integration", version, "expected.yaml")),
scraperinttest.WithCompareOptions(
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreResourceMetricsOrder(),
pmetrictest.IgnoreMetricValues(),
pmetrictest.IgnoreMetricsOrder(),
pmetrictest.IgnoreMetricDataPointsOrder(),
),
).Run
}
func TestJMXReceiverInvalidOTLPEndpointIntegration(t *testing.T) {
params := receivertest.NewNopSettings()
cfg := &Config{
CollectionInterval: 100 * time.Millisecond,
Endpoint: "service:jmx:rmi:///jndi/rmi://localhost:7199/jmxrmi",
JARPath: "/notavalidpath",
TargetSystem: "jvm",
OTLPExporterConfig: otlpExporterConfig{
Endpoint: "<invalid>:123",
TimeoutSettings: exporterhelper.TimeoutConfig{
Timeout: 1000 * time.Millisecond,
},
},
}
receiver := newJMXMetricReceiver(params, cfg, consumertest.NewNop())
require.NotNil(t, receiver)
defer func() {
require.EqualError(t, receiver.Shutdown(context.Background()), "no subprocess.cancel(). Has it been started properly?")
}()
err := receiver.Start(context.Background(), componenttest.NewNopHost())
require.ErrorContains(t, err, "listen tcp: lookup <invalid>:")
}