Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cassandra jmx test #4479

Merged
merged 2 commits into from Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion docker/cassandra/Dockerfile
Expand Up @@ -7,4 +7,7 @@ ENV LOCAL_JMX=no

RUN echo 'cassandra readonly' > /etc/cassandra/jmxremote.access &&\
echo 'cassandra cassandra' > /etc/cassandra/jmxremote.password &&\
chmod 0400 /etc/cassandra/jmxremote*
echo 'JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.access.file=/etc/cassandra/jmxremote.access"' >> /etc/cassandra/cassandra-env.sh &&\
chmod 0400 /etc/cassandra/jmxremote* &&\
chown cassandra:cassandra /etc/cassandra/jmxremote.* &&\
chown cassandra:cassandra /etc/cassandra/cassandra-env.sh
4 changes: 2 additions & 2 deletions pkg/receiver/smartagentreceiver/output_traces_test.go
Expand Up @@ -30,7 +30,7 @@ import (
func TestSendSpansWithoutNextTracesConsumer(t *testing.T) {
output, err := newOutput(
Config{}, fakeMonitorFiltering(), consumertest.NewNop(), consumertest.NewNop(),
nil, componenttest.NewNopHost(), newReceiverCreateSettings(""),
nil, componenttest.NewNopHost(), newReceiverCreateSettings("", t),
)
require.NoError(t, err)
output.SendSpans(&trace.Span{TraceID: "12345678", ID: "23456789"}) // doesn't panic
Expand All @@ -40,7 +40,7 @@ func TestSendSpansWithConsumerError(t *testing.T) {
obs, logs := observer.New(zap.DebugLevel)
logger := zap.New(obs)

rcs := newReceiverCreateSettings("")
rcs := newReceiverCreateSettings("", t)
rcs.Logger = logger

err := fmt.Errorf("desired error")
Expand Down
141 changes: 68 additions & 73 deletions tests/receivers/smartagent/jmx/jmx_test.go
Expand Up @@ -18,96 +18,91 @@ package tests

import (
"context"
"errors"
"fmt"
"path"
"path/filepath"
"runtime"
"testing"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
docker "github.com/docker/docker/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/otlpreceiver"
"go.opentelemetry.io/collector/receiver/receivertest"
"go.uber.org/zap"

"github.com/signalfx/splunk-otel-collector/tests/testutils"
)

const networkName = "cassandra"

var cassandra = testutils.NewContainer().WithContext(
path.Join(".", "testdata", "server"),
).WithEnv(map[string]string{
"LOCAL_JMX": "no",
}).WithExposedPorts("7199:7199").
WithStartupTimeout(3 * time.Minute).
WithHostConfigModifier(func(cm *container.HostConfig) {
cm.NetworkMode = "bridge"
}).
WithName("cassandra").
WithNetworks("cassandra").
WillWaitForPorts("7199").
WillWaitForLogs("JMX is enabled to receive remote connections on port").
WillWaitForLogs("Startup complete")

func TestJmxReceiverProvidesAllMetrics(t *testing.T) {
t.Skip("Issues with test-containers networking, need to wait for -contrib to update the docker api version for us to update testcontainers-go locally")
testutils.SkipIfNotContainerTest(t)
metricNames := []string{
"cassandra.status",
"cassandra.state",
"cassandra.load",
"cassandra.ownership",
}

tc := testutils.Testcase{TB: t}
_, stopDependentContainers := tc.Containers(cassandra)
defer stopDependentContainers()
checkMetricsPresence(t, metricNames, "all_metrics_config.yaml")
}

endpoint, err := GetDockerNetworkGateway(t, networkName)
func checkMetricsPresence(t *testing.T, metricNames []string, configFile string) {
f := otlpreceiver.NewFactory()
port := testutils.GetAvailablePort(t)
c := f.CreateDefaultConfig().(*otlpreceiver.Config)
c.GRPC.NetAddr.Endpoint = fmt.Sprintf("localhost:%d", port)
sink := &consumertest.MetricsSink{}
receiver, err := f.CreateMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), c, sink)
require.NoError(t, err)
require.NotEmpty(t, endpoint)
sinkBuilder := GetSinkAndLogs(t, endpoint)
sink, err := sinkBuilder.Build()
require.NoError(t, err)
tc.OTLPEndpoint = sink.Endpoint
tc.OTLPEndpointForCollector = sink.Endpoint
require.Contains(t, tc.OTLPEndpointForCollector, ":")
require.NoError(t, sink.Start())
defer func() { require.NoError(t, sink.Shutdown()) }()

_, shutdown := tc.SplunkOtelCollector("all_metrics_config.yaml",
func(collector testutils.Collector) testutils.Collector {
collector = collector.WithEnv(map[string]string{"OTLP_ENDPOINT": tc.OTLPEndpointForCollector})
collector = collector.WithLogLevel("debug")
collectorContainer := collector.(*testutils.CollectorContainer)
collectorContainer.Container = collectorContainer.Container.WithHostConfigModifier(func(chc *container.HostConfig) {
chc.NetworkMode = "bridge"
}).WithName("otelcol-jmxtest").WithNetworks(networkName)
p, err := filepath.Abs(filepath.Join(".", "testdata", "script.groovy"))
require.NoError(t, err)
return collector.WithMount(p, "/opt/script.groovy")
})
defer shutdown()
require.NoError(t, receiver.Start(context.Background(), componenttest.NewNopHost()))
t.Cleanup(func() {
require.NoError(t, receiver.Shutdown(context.Background()))
})
logger, _ := zap.NewDevelopment()

resourceMetrics := tc.ResourceMetrics("all.yaml")
require.NoError(t, sink.AssertAllMetricsReceived(t, *resourceMetrics, time.Minute))

}

func GetDockerNetworkGateway(t testing.TB, dockerNetwork string) (string, error) {
client, err := docker.NewClientWithOpts(docker.FromEnv)
dockerHost := "0.0.0.0"
if runtime.GOOS == "darwin" {
dockerHost = "host.docker.internal"
}
mountDir, err := filepath.Abs(filepath.Join("testdata", "script.groovy"))
require.NoError(t, err)
client.NegotiateAPIVersion(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
network, err := client.NetworkInspect(ctx, dockerNetwork, types.NetworkInspectOptions{})
p, err := testutils.NewCollectorContainer().
WithConfigPath(filepath.Join("testdata", configFile)).
WithLogger(logger).
WithEnv(map[string]string{
"OTLP_ENDPOINT": fmt.Sprintf("%s:%d", dockerHost, port),
"HOST": dockerHost,
}).
WithMount(mountDir, "/opt/script.groovy").
Build()
require.NoError(t, err)
for _, ipam := range network.IPAM.Config {
if ipam.Gateway != "" {
return ipam.Gateway, nil
}
require.NoError(t, p.Start())
t.Cleanup(func() {
require.NoError(t, p.Shutdown())
})

missingMetrics := make(map[string]any, len(metricNames))
for _, m := range metricNames {
missingMetrics[m] = struct{}{}
}
return "", errors.New("Could not find gateway for network " + dockerNetwork)
}

func GetSinkAndLogs(t testing.TB, sinkHost string) testutils.OTLPReceiverSink {
otlpPort := testutils.GetAvailablePort(t)
endpoint := fmt.Sprintf("%s:%d", sinkHost, otlpPort)
sink := testutils.NewOTLPReceiverSink().WithEndpoint(endpoint)
return sink
assert.EventuallyWithT(t, func(tt *assert.CollectT) {
for i := 0; i < len(sink.AllMetrics()); i++ {
m := sink.AllMetrics()[i]
for j := 0; j < m.ResourceMetrics().Len(); j++ {
rm := m.ResourceMetrics().At(j)
for k := 0; k < rm.ScopeMetrics().Len(); k++ {
sm := rm.ScopeMetrics().At(k)
for l := 0; l < sm.Metrics().Len(); l++ {
delete(missingMetrics, sm.Metrics().At(l).Name())
}
}
}
}
msg := "Missing metrics:\n"
for k := range missingMetrics {
msg += fmt.Sprintf("- %q\n", k)
}
assert.Len(tt, missingMetrics, 0, msg)
}, 1*time.Minute, 1*time.Second)
}
Expand Up @@ -3,8 +3,7 @@ config_sources:
receivers:
smartagent/jmx:
type: jmx
host: cassandra
# host: 127.0.0.1
host: "${HOST}"
port: 7199
username: cassandra
password: cassandra
Expand Down
19 changes: 0 additions & 19 deletions tests/receivers/smartagent/jmx/testdata/server/Dockerfile

This file was deleted.