diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index 0d273e16..a9155295 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -78,8 +78,8 @@ jobs: fail-fast: false matrix: usecase: - - testbed/integration/k8senrichment - - testbed/integration/prometheus + - k8senrichment + - prometheus runs-on: ubuntu-latest needs: docker-build steps: @@ -132,7 +132,7 @@ jobs: - name: Run e2e tests run: | - cd ${{ matrix.usecase }} + cd internal/testbed/integration/${{ matrix.usecase }} go test -v --tags=e2e eec-confmap-provider: @@ -170,5 +170,5 @@ jobs: - name: Run e2e tests run: | - cd confmap/provider/eecprovider + cd internal/confmap/provider/eecprovider go test -v --tags=e2e diff --git a/Makefile b/Makefile index 1c9b2e34..ec46f0e9 100644 --- a/Makefile +++ b/Makefile @@ -11,9 +11,9 @@ SRC_ROOT := $(shell git rev-parse --show-toplevel) # ALL_MODULES includes ./* dirs (excludes . dir) ALL_MODULES := $(shell find . -type f -name "go.mod" -not -path "./build/*" -not -path "./internal/tools/*" -exec dirname {} \; | sort | grep -E '^./' ) # Append root module to all modules -GOMODULES = $(ALL_MODULES) $(PWD) +GOMODULES = $(ALL_MODULES) -SOURCES := $(shell find confmap -type f | sort ) +SOURCES := $(shell find internal/confmap -type f | sort ) BIN = $(BIN_DIR)/dynatrace-otel-collector diff --git a/confmap/provider/eecprovider/README.md b/internal/confmap/provider/eecprovider/README.md similarity index 100% rename from confmap/provider/eecprovider/README.md rename to internal/confmap/provider/eecprovider/README.md diff --git a/confmap/provider/eecprovider/config.go b/internal/confmap/provider/eecprovider/config.go similarity index 100% rename from confmap/provider/eecprovider/config.go rename to internal/confmap/provider/eecprovider/config.go diff --git a/confmap/provider/eecprovider/config_test.go b/internal/confmap/provider/eecprovider/config_test.go similarity index 100% rename from confmap/provider/eecprovider/config_test.go rename to internal/confmap/provider/eecprovider/config_test.go diff --git a/confmap/provider/eecprovider/e2e_test.go b/internal/confmap/provider/eecprovider/e2e_test.go similarity index 89% rename from confmap/provider/eecprovider/e2e_test.go rename to internal/confmap/provider/eecprovider/e2e_test.go index 4df2f1bb..c535079e 100644 --- a/confmap/provider/eecprovider/e2e_test.go +++ b/internal/confmap/provider/eecprovider/e2e_test.go @@ -15,12 +15,14 @@ import ( "github.com/stretchr/testify/require" ) +const CollectorBinary = "../../../../bin/dynatrace-otel-collector" + func TestGetsConfig(t *testing.T) { f, err := os.ReadFile("./testdata/otel-config.yaml") require.NoError(t, err) fs := &fileserver{config: f} ts := httptest.NewServer(http.HandlerFunc(fs.HandleRequest)) - cmd := exec.Command("../../../bin/dynatrace-otel-collector", fmt.Sprintf("--config=%s", configureProvider(t, ts.URL))) + cmd := exec.Command(CollectorBinary, fmt.Sprintf("--config=%s", configureProvider(t, ts.URL))) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err = cmd.Start() @@ -51,7 +53,7 @@ func TestReloadsConfig(t *testing.T) { require.NoError(t, err) fs := &fileserver{config: f} ts := httptest.NewServer(http.HandlerFunc(fs.HandleRequest)) - cmd := exec.Command("../../../bin/dynatrace-otel-collector", fmt.Sprintf("--config=%s", configureProvider(t, ts.URL))) + cmd := exec.Command(CollectorBinary, fmt.Sprintf("--config=%s", configureProvider(t, ts.URL))) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err = cmd.Start() @@ -96,7 +98,7 @@ func TestReloadsConfig(t *testing.T) { func TestFailsOnBadConfig(t *testing.T) { fs := &fileserver{} ts := httptest.NewServer(http.HandlerFunc(fs.HandleRequest)) - cmd := exec.Command("../../../bin/dynatrace-otel-collector", fmt.Sprintf("--config=%s", configureProvider(t, ts.URL))) + cmd := exec.Command(CollectorBinary, fmt.Sprintf("--config=%s", configureProvider(t, ts.URL))) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err := cmd.Start() diff --git a/confmap/provider/eecprovider/go.mod b/internal/confmap/provider/eecprovider/go.mod similarity index 88% rename from confmap/provider/eecprovider/go.mod rename to internal/confmap/provider/eecprovider/go.mod index b7962ea0..2a55b096 100644 --- a/confmap/provider/eecprovider/go.mod +++ b/internal/confmap/provider/eecprovider/go.mod @@ -1,4 +1,4 @@ -module github.com/Dynatrace/dynatrace-otel-collector/confmap/provider/eecprovider +module github.com/Dynatrace/dynatrace-otel-collector/internal/confmap/provider/eecprovider go 1.21 diff --git a/confmap/provider/eecprovider/go.sum b/internal/confmap/provider/eecprovider/go.sum similarity index 100% rename from confmap/provider/eecprovider/go.sum rename to internal/confmap/provider/eecprovider/go.sum diff --git a/confmap/provider/eecprovider/package_test.go b/internal/confmap/provider/eecprovider/package_test.go similarity index 100% rename from confmap/provider/eecprovider/package_test.go rename to internal/confmap/provider/eecprovider/package_test.go diff --git a/confmap/provider/eecprovider/provider.go b/internal/confmap/provider/eecprovider/provider.go similarity index 100% rename from confmap/provider/eecprovider/provider.go rename to internal/confmap/provider/eecprovider/provider.go diff --git a/confmap/provider/eecprovider/provider_test.go b/internal/confmap/provider/eecprovider/provider_test.go similarity index 100% rename from confmap/provider/eecprovider/provider_test.go rename to internal/confmap/provider/eecprovider/provider_test.go diff --git a/confmap/provider/eecprovider/testdata/otel-config-updated.yaml b/internal/confmap/provider/eecprovider/testdata/otel-config-updated.yaml similarity index 100% rename from confmap/provider/eecprovider/testdata/otel-config-updated.yaml rename to internal/confmap/provider/eecprovider/testdata/otel-config-updated.yaml diff --git a/confmap/provider/eecprovider/testdata/otel-config.yaml b/internal/confmap/provider/eecprovider/testdata/otel-config.yaml similarity index 100% rename from confmap/provider/eecprovider/testdata/otel-config.yaml rename to internal/confmap/provider/eecprovider/testdata/otel-config.yaml diff --git a/confmap/provider/eecprovider/watcher.go b/internal/confmap/provider/eecprovider/watcher.go similarity index 100% rename from confmap/provider/eecprovider/watcher.go rename to internal/confmap/provider/eecprovider/watcher.go diff --git a/confmap/provider/eecprovider/watcher_test.go b/internal/confmap/provider/eecprovider/watcher_test.go similarity index 100% rename from confmap/provider/eecprovider/watcher_test.go rename to internal/confmap/provider/eecprovider/watcher_test.go diff --git a/confmap/provider/eecprovider/yaml.go b/internal/confmap/provider/eecprovider/yaml.go similarity index 100% rename from confmap/provider/eecprovider/yaml.go rename to internal/confmap/provider/eecprovider/yaml.go diff --git a/confmap/provider/eecprovider/yaml_test.go b/internal/confmap/provider/eecprovider/yaml_test.go similarity index 100% rename from confmap/provider/eecprovider/yaml_test.go rename to internal/confmap/provider/eecprovider/yaml_test.go diff --git a/testbed/.gitignore b/internal/testbed/.gitignore similarity index 100% rename from testbed/.gitignore rename to internal/testbed/.gitignore diff --git a/testbed/chainsaw/prometheus-node-exporter/00-assert.yaml b/internal/testbed/chainsaw/prometheus-node-exporter/00-assert.yaml similarity index 100% rename from testbed/chainsaw/prometheus-node-exporter/00-assert.yaml rename to internal/testbed/chainsaw/prometheus-node-exporter/00-assert.yaml diff --git a/testbed/chainsaw/prometheus-node-exporter/chainsaw-test.yaml b/internal/testbed/chainsaw/prometheus-node-exporter/chainsaw-test.yaml similarity index 100% rename from testbed/chainsaw/prometheus-node-exporter/chainsaw-test.yaml rename to internal/testbed/chainsaw/prometheus-node-exporter/chainsaw-test.yaml diff --git a/testbed/chainsaw/syslog-receiver/00-assert.yaml b/internal/testbed/chainsaw/syslog-receiver/00-assert.yaml similarity index 100% rename from testbed/chainsaw/syslog-receiver/00-assert.yaml rename to internal/testbed/chainsaw/syslog-receiver/00-assert.yaml diff --git a/testbed/chainsaw/syslog-receiver/chainsaw-test.yaml b/internal/testbed/chainsaw/syslog-receiver/chainsaw-test.yaml similarity index 100% rename from testbed/chainsaw/syslog-receiver/chainsaw-test.yaml rename to internal/testbed/chainsaw/syslog-receiver/chainsaw-test.yaml diff --git a/go.mod b/internal/testbed/go.mod similarity index 99% rename from go.mod rename to internal/testbed/go.mod index 28e01953..1be5c161 100644 --- a/go.mod +++ b/internal/testbed/go.mod @@ -1,4 +1,4 @@ -module github.com/Dynatrace/dynatrace-otel-collector +module github.com/Dynatrace/dynatrace-otel-collector/internal/testbed go 1.21.0 @@ -210,4 +210,4 @@ require ( replace cloud.google.com/go => cloud.google.com/go v0.112.2 -replace github.com/Dynatrace/dynatrace-otel-collector/internal/k8stest v0.0.0 => ./internal/k8stest +replace github.com/Dynatrace/dynatrace-otel-collector/internal/k8stest v0.0.0 => ../k8stest diff --git a/go.sum b/internal/testbed/go.sum similarity index 100% rename from go.sum rename to internal/testbed/go.sum diff --git a/testbed/integration/config_examples_test.go b/internal/testbed/integration/config_examples_test.go similarity index 94% rename from testbed/integration/config_examples_test.go rename to internal/testbed/integration/config_examples_test.go index c31f659e..a66c6651 100644 --- a/testbed/integration/config_examples_test.go +++ b/internal/testbed/integration/config_examples_test.go @@ -2,6 +2,7 @@ package integration import ( "os" + "path" "strings" "testing" "time" @@ -13,16 +14,20 @@ import ( "go.opentelemetry.io/collector/pdata/pmetric" "go.opentelemetry.io/collector/pdata/ptrace" semconv "go.opentelemetry.io/collector/semconv/v1.18.0" + + "github.com/Dynatrace/dynatrace-otel-collector/internal/testbed/testutil" ) +const ConfigExamplesDir = "../../../config_examples" + func TestConfigTailSampling(t *testing.T) { // arrange col := testbed.NewChildProcessCollector(testbed.WithAgentExePath(CollectorTestsExecPath)) - cfg, err := os.ReadFile("../../config_examples/tail_sampling.yaml") + cfg, err := os.ReadFile(path.Join(ConfigExamplesDir, "tail_sampling.yaml")) require.NoError(t, err) - receiverPort := testbed.GetAvailablePort(t) - exporterPort := testbed.GetAvailablePort(t) + receiverPort := testutil.GetAvailablePort(t) + exporterPort := testutil.GetAvailablePort(t) parsedConfig := string(cfg) parsedConfig = replaceOtlpGrpcReceiverPort(parsedConfig, receiverPort) @@ -115,11 +120,11 @@ func TestConfigTailSampling(t *testing.T) { func TestConfigJaegerGrpc(t *testing.T) { // arrange col := testbed.NewChildProcessCollector(testbed.WithAgentExePath(CollectorTestsExecPath)) - cfg, err := os.ReadFile("../../config_examples/jaeger.yaml") + cfg, err := os.ReadFile(path.Join(ConfigExamplesDir, "jaeger.yaml")) require.NoError(t, err) - grpcReceiverPort := testbed.GetAvailablePort(t) - exporterPort := testbed.GetAvailablePort(t) + grpcReceiverPort := testutil.GetAvailablePort(t) + exporterPort := testutil.GetAvailablePort(t) parsedConfig := string(cfg) parsedConfig = replaceJaegerGrpcReceiverPort(parsedConfig, grpcReceiverPort) @@ -208,11 +213,11 @@ func TestConfigJaegerGrpc(t *testing.T) { func TestConfigHistogramTransform(t *testing.T) { // arrange col := testbed.NewChildProcessCollector(testbed.WithAgentExePath(CollectorTestsExecPath)) - cfg, err := os.ReadFile("../../config_examples/split_histogram.yaml") + cfg, err := os.ReadFile(path.Join(ConfigExamplesDir, "split_histogram.yaml")) require.NoError(t, err) - receiverPort := testbed.GetAvailablePort(t) - exporterPort := testbed.GetAvailablePort(t) + receiverPort := testutil.GetAvailablePort(t) + exporterPort := testutil.GetAvailablePort(t) parsedConfig := string(cfg) parsedConfig = replaceOtlpGrpcReceiverPort(parsedConfig, receiverPort) @@ -318,11 +323,11 @@ func TestConfigHistogramTransform(t *testing.T) { func TestConfigMetricsFromPreSampledTraces(t *testing.T) { // arrange col := testbed.NewChildProcessCollector(testbed.WithAgentExePath(CollectorTestsExecPath)) - cfg, err := os.ReadFile("../../config_examples/spanmetrics.yaml") + cfg, err := os.ReadFile(path.Join(ConfigExamplesDir, "spanmetrics.yaml")) require.NoError(t, err) - receiverPort := testbed.GetAvailablePort(t) - exporterPort := testbed.GetAvailablePort(t) + receiverPort := testutil.GetAvailablePort(t) + exporterPort := testutil.GetAvailablePort(t) parsedConfig := string(cfg) parsedConfig = replaceOtlpGrpcReceiverPort(parsedConfig, receiverPort) diff --git a/testbed/integration/data_providers.go b/internal/testbed/integration/data_providers.go similarity index 100% rename from testbed/integration/data_providers.go rename to internal/testbed/integration/data_providers.go diff --git a/testbed/integration/k8senrichment/README.md b/internal/testbed/integration/k8senrichment/README.md similarity index 100% rename from testbed/integration/k8senrichment/README.md rename to internal/testbed/integration/k8senrichment/README.md diff --git a/testbed/integration/k8senrichment/e2e_test.go b/internal/testbed/integration/k8senrichment/e2e_test.go similarity index 100% rename from testbed/integration/k8senrichment/e2e_test.go rename to internal/testbed/integration/k8senrichment/e2e_test.go diff --git a/testbed/integration/k8senrichment/testdata/collector/clusterrole.yaml b/internal/testbed/integration/k8senrichment/testdata/collector/clusterrole.yaml similarity index 100% rename from testbed/integration/k8senrichment/testdata/collector/clusterrole.yaml rename to internal/testbed/integration/k8senrichment/testdata/collector/clusterrole.yaml diff --git a/testbed/integration/k8senrichment/testdata/collector/clusterrolebinding.yaml b/internal/testbed/integration/k8senrichment/testdata/collector/clusterrolebinding.yaml similarity index 100% rename from testbed/integration/k8senrichment/testdata/collector/clusterrolebinding.yaml rename to internal/testbed/integration/k8senrichment/testdata/collector/clusterrolebinding.yaml diff --git a/testbed/integration/k8senrichment/testdata/collector/configmap.yaml b/internal/testbed/integration/k8senrichment/testdata/collector/configmap.yaml similarity index 100% rename from testbed/integration/k8senrichment/testdata/collector/configmap.yaml rename to internal/testbed/integration/k8senrichment/testdata/collector/configmap.yaml diff --git a/testbed/integration/k8senrichment/testdata/collector/deployment.yaml b/internal/testbed/integration/k8senrichment/testdata/collector/deployment.yaml similarity index 100% rename from testbed/integration/k8senrichment/testdata/collector/deployment.yaml rename to internal/testbed/integration/k8senrichment/testdata/collector/deployment.yaml diff --git a/testbed/integration/k8senrichment/testdata/collector/service.yaml b/internal/testbed/integration/k8senrichment/testdata/collector/service.yaml similarity index 100% rename from testbed/integration/k8senrichment/testdata/collector/service.yaml rename to internal/testbed/integration/k8senrichment/testdata/collector/service.yaml diff --git a/testbed/integration/k8senrichment/testdata/collector/serviceaccount.yaml b/internal/testbed/integration/k8senrichment/testdata/collector/serviceaccount.yaml similarity index 100% rename from testbed/integration/k8senrichment/testdata/collector/serviceaccount.yaml rename to internal/testbed/integration/k8senrichment/testdata/collector/serviceaccount.yaml diff --git a/testbed/integration/k8senrichment/testdata/namespace.yaml b/internal/testbed/integration/k8senrichment/testdata/namespace.yaml similarity index 100% rename from testbed/integration/k8senrichment/testdata/namespace.yaml rename to internal/testbed/integration/k8senrichment/testdata/namespace.yaml diff --git a/testbed/integration/k8senrichment/testdata/telemetrygen/daemonset.yaml b/internal/testbed/integration/k8senrichment/testdata/telemetrygen/daemonset.yaml similarity index 100% rename from testbed/integration/k8senrichment/testdata/telemetrygen/daemonset.yaml rename to internal/testbed/integration/k8senrichment/testdata/telemetrygen/daemonset.yaml diff --git a/testbed/integration/k8senrichment/testdata/telemetrygen/deployment.yaml b/internal/testbed/integration/k8senrichment/testdata/telemetrygen/deployment.yaml similarity index 100% rename from testbed/integration/k8senrichment/testdata/telemetrygen/deployment.yaml rename to internal/testbed/integration/k8senrichment/testdata/telemetrygen/deployment.yaml diff --git a/testbed/integration/k8senrichment/testdata/telemetrygen/job.yaml b/internal/testbed/integration/k8senrichment/testdata/telemetrygen/job.yaml similarity index 100% rename from testbed/integration/k8senrichment/testdata/telemetrygen/job.yaml rename to internal/testbed/integration/k8senrichment/testdata/telemetrygen/job.yaml diff --git a/testbed/integration/k8senrichment/testdata/telemetrygen/statefulset.yaml b/internal/testbed/integration/k8senrichment/testdata/telemetrygen/statefulset.yaml similarity index 100% rename from testbed/integration/k8senrichment/testdata/telemetrygen/statefulset.yaml rename to internal/testbed/integration/k8senrichment/testdata/telemetrygen/statefulset.yaml diff --git a/testbed/integration/metrics_validation.go b/internal/testbed/integration/metrics_validation.go similarity index 100% rename from testbed/integration/metrics_validation.go rename to internal/testbed/integration/metrics_validation.go diff --git a/testbed/integration/prometheus/README.md b/internal/testbed/integration/prometheus/README.md similarity index 100% rename from testbed/integration/prometheus/README.md rename to internal/testbed/integration/prometheus/README.md diff --git a/testbed/integration/prometheus/e2e_test.go b/internal/testbed/integration/prometheus/e2e_test.go similarity index 100% rename from testbed/integration/prometheus/e2e_test.go rename to internal/testbed/integration/prometheus/e2e_test.go diff --git a/testbed/integration/prometheus/testdata/collector/configmap.yaml b/internal/testbed/integration/prometheus/testdata/collector/configmap.yaml similarity index 100% rename from testbed/integration/prometheus/testdata/collector/configmap.yaml rename to internal/testbed/integration/prometheus/testdata/collector/configmap.yaml diff --git a/testbed/integration/prometheus/testdata/collector/deployment.yaml b/internal/testbed/integration/prometheus/testdata/collector/deployment.yaml similarity index 100% rename from testbed/integration/prometheus/testdata/collector/deployment.yaml rename to internal/testbed/integration/prometheus/testdata/collector/deployment.yaml diff --git a/testbed/integration/prometheus/testdata/collector/service.yaml b/internal/testbed/integration/prometheus/testdata/collector/service.yaml similarity index 100% rename from testbed/integration/prometheus/testdata/collector/service.yaml rename to internal/testbed/integration/prometheus/testdata/collector/service.yaml diff --git a/testbed/integration/prometheus/testdata/collector/serviceaccount.yaml b/internal/testbed/integration/prometheus/testdata/collector/serviceaccount.yaml similarity index 100% rename from testbed/integration/prometheus/testdata/collector/serviceaccount.yaml rename to internal/testbed/integration/prometheus/testdata/collector/serviceaccount.yaml diff --git a/testbed/integration/prometheus/testdata/namespace.yaml b/internal/testbed/integration/prometheus/testdata/namespace.yaml similarity index 100% rename from testbed/integration/prometheus/testdata/namespace.yaml rename to internal/testbed/integration/prometheus/testdata/namespace.yaml diff --git a/testbed/integration/prometheus/testdata/prometheus/install.sh b/internal/testbed/integration/prometheus/testdata/prometheus/install.sh similarity index 100% rename from testbed/integration/prometheus/testdata/prometheus/install.sh rename to internal/testbed/integration/prometheus/testdata/prometheus/install.sh diff --git a/testbed/integration/testutils.go b/internal/testbed/integration/testutils.go similarity index 93% rename from testbed/integration/testutils.go rename to internal/testbed/integration/testutils.go index 69bd5c15..9cb60cf8 100644 --- a/testbed/integration/testutils.go +++ b/internal/testbed/integration/testutils.go @@ -9,7 +9,7 @@ import ( "go.opentelemetry.io/collector/pdata/pcommon" ) -const CollectorTestsExecPath string = "../../bin/dynatrace-otel-collector" +const CollectorTestsExecPath string = "../../../bin/dynatrace-otel-collector" func replaceOtlpGrpcReceiverPort(cfg string, receiverPort int) string { return strings.Replace(cfg, "4317", strconv.Itoa(receiverPort), 1) diff --git a/testbed/integration/trace_validation.go b/internal/testbed/integration/trace_validation.go similarity index 100% rename from testbed/integration/trace_validation.go rename to internal/testbed/integration/trace_validation.go diff --git a/testbed/scripts/collector-helm-values.yaml b/internal/testbed/scripts/collector-helm-values.yaml similarity index 100% rename from testbed/scripts/collector-helm-values.yaml rename to internal/testbed/scripts/collector-helm-values.yaml diff --git a/testbed/scripts/get_dt_logs.sh b/internal/testbed/scripts/get_dt_logs.sh similarity index 100% rename from testbed/scripts/get_dt_logs.sh rename to internal/testbed/scripts/get_dt_logs.sh diff --git a/testbed/scripts/get_dt_metrics.sh b/internal/testbed/scripts/get_dt_metrics.sh similarity index 100% rename from testbed/scripts/get_dt_metrics.sh rename to internal/testbed/scripts/get_dt_metrics.sh diff --git a/testbed/scripts/install_dt_collector_helm.sh b/internal/testbed/scripts/install_dt_collector_helm.sh similarity index 100% rename from testbed/scripts/install_dt_collector_helm.sh rename to internal/testbed/scripts/install_dt_collector_helm.sh diff --git a/testbed/scripts/install_prometheus_node_exporter.sh b/internal/testbed/scripts/install_prometheus_node_exporter.sh similarity index 100% rename from testbed/scripts/install_prometheus_node_exporter.sh rename to internal/testbed/scripts/install_prometheus_node_exporter.sh diff --git a/testbed/smoke/smoke_test.go b/internal/testbed/smoke/smoke_test.go similarity index 97% rename from testbed/smoke/smoke_test.go rename to internal/testbed/smoke/smoke_test.go index 36ba94e9..2e2cceb3 100644 --- a/testbed/smoke/smoke_test.go +++ b/internal/testbed/smoke/smoke_test.go @@ -16,7 +16,7 @@ import ( "gopkg.in/yaml.v3" ) -var execPath = "../../bin/dynatrace-otel-collector" +var execPath = "../../../bin/dynatrace-otel-collector" func TestCollectorStarts(t *testing.T) { tests := []struct { @@ -106,7 +106,7 @@ func TestCollectorStarts(t *testing.T) { func TestCollectorIsBuiltFromManifest(t *testing.T) { components := getComponents(t) - b, err := os.ReadFile("../../manifest.yaml") + b, err := os.ReadFile("../../../manifest.yaml") require.NoError(t, err) manifestComponents := manifest{} err = yaml.Unmarshal(b, &manifestComponents) diff --git a/testbed/testdata/config-allcomponents.yaml b/internal/testbed/testdata/config-allcomponents.yaml similarity index 100% rename from testbed/testdata/config-allcomponents.yaml rename to internal/testbed/testdata/config-allcomponents.yaml diff --git a/testbed/testdata/config-smoke.yaml b/internal/testbed/testdata/config-smoke.yaml similarity index 100% rename from testbed/testdata/config-smoke.yaml rename to internal/testbed/testdata/config-smoke.yaml diff --git a/internal/testbed/testutil/testutil.go b/internal/testbed/testutil/testutil.go new file mode 100644 index 00000000..1f958995 --- /dev/null +++ b/internal/testbed/testutil/testutil.go @@ -0,0 +1,147 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package testutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil" + +import ( + "net" + "os/exec" + "runtime" + "strconv" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/featuregate" +) + +type portpair struct { + first string + last string +} + +// GetAvailableLocalAddress finds an available local port on tcp network and returns an endpoint +// describing it. The port is available for opening when this function returns +// provided that there is no race by some other code to grab the same port +// immediately. +func GetAvailableLocalAddress(t testing.TB) string { + return GetAvailableLocalNetworkAddress(t, "tcp") +} + +// GetAvailableLocalNetworkAddress finds an available local port on specified network and returns an endpoint +// describing it. The port is available for opening when this function returns +// provided that there is no race by some other code to grab the same port +// immediately. +func GetAvailableLocalNetworkAddress(t testing.TB, network string) string { + // Retry has been added for windows as net.Listen can return a port that is not actually available. Details can be + // found in https://github.com/docker/for-win/issues/3171 but to summarize Hyper-V will reserve ranges of ports + // which do not show up under the "netstat -ano" but can only be found by + // "netsh interface ipv4 show excludedportrange protocol=tcp". We'll use []exclusions to hold those ranges and + // retry if the port returned by GetAvailableLocalAddress falls in one of those them. + var exclusions []portpair + + portFound := false + if runtime.GOOS == "windows" { + exclusions = getExclusionsList(t) + } + + var endpoint string + for !portFound { + endpoint = findAvailableAddress(t, network) + _, port, err := net.SplitHostPort(endpoint) + require.NoError(t, err) + portFound = true + if runtime.GOOS == "windows" { + for _, pair := range exclusions { + if port >= pair.first && port <= pair.last { + portFound = false + break + } + } + } + } + + return endpoint +} + +func findAvailableAddress(t testing.TB, network string) string { + switch network { + // net.Listen supported network strings + case "tcp", "tcp4", "tcp6", "unix", "unixpacket": + ln, err := net.Listen(network, "localhost:0") + require.NoError(t, err, "Failed to get a free local port") + // There is a possible race if something else takes this same port before + // the test uses it, however, that is unlikely in practice. + defer func() { + assert.NoError(t, ln.Close()) + }() + return ln.Addr().String() + // net.ListenPacket supported network strings + case "udp", "udp4", "udp6", "unixgram": + ln, err := net.ListenPacket(network, "localhost:0") + require.NoError(t, err, "Failed to get a free local port") + // There is a possible race if something else takes this same port before + // the test uses it, however, that is unlikely in practice. + defer func() { + assert.NoError(t, ln.Close()) + }() + return ln.LocalAddr().String() + } + return "" +} + +// Get excluded ports on Windows from the command: netsh interface ipv4 show excludedportrange protocol=tcp +func getExclusionsList(t testing.TB) []portpair { + cmdTCP := exec.Command("netsh", "interface", "ipv4", "show", "excludedportrange", "protocol=tcp") + outputTCP, errTCP := cmdTCP.CombinedOutput() + require.NoError(t, errTCP) + exclusions := createExclusionsList(t, string(outputTCP)) + + cmdUDP := exec.Command("netsh", "interface", "ipv4", "show", "excludedportrange", "protocol=udp") + outputUDP, errUDP := cmdUDP.CombinedOutput() + require.NoError(t, errUDP) + exclusions = append(exclusions, createExclusionsList(t, string(outputUDP))...) + + return exclusions +} + +func createExclusionsList(t testing.TB, exclusionsText string) []portpair { + var exclusions []portpair + + parts := strings.Split(exclusionsText, "--------") + require.Equal(t, len(parts), 3) + portsText := strings.Split(parts[2], "*") + require.Greater(t, len(portsText), 1) // original text may have a suffix like " - Administered port exclusions." + lines := strings.Split(portsText[0], "\n") + for _, line := range lines { + if strings.TrimSpace(line) != "" { + entries := strings.Fields(strings.TrimSpace(line)) + require.Equal(t, len(entries), 2) + pair := portpair{entries[0], entries[1]} + exclusions = append(exclusions, pair) + } + } + return exclusions +} + +// Force the state of feature gate for a test +// usage: defer SetFeatureGateForTest("gateName", true)() +func SetFeatureGateForTest(t testing.TB, gate *featuregate.Gate, enabled bool) func() { + originalValue := gate.IsEnabled() + require.NoError(t, featuregate.GlobalRegistry().Set(gate.ID(), enabled)) + return func() { + require.NoError(t, featuregate.GlobalRegistry().Set(gate.ID(), originalValue)) + } +} + +func GetAvailablePort(t testing.TB) int { + endpoint := GetAvailableLocalAddress(t) + _, port, err := net.SplitHostPort(endpoint) + require.NoError(t, err) + + portInt, err := strconv.Atoi(port) + require.NoError(t, err) + + return portInt +} diff --git a/manifest.yaml b/manifest.yaml index 0d13289c..e8914210 100644 --- a/manifest.yaml +++ b/manifest.yaml @@ -46,7 +46,7 @@ providers: - gomod: go.opentelemetry.io/collector/confmap/provider/httpprovider v0.100.0 - gomod: go.opentelemetry.io/collector/confmap/provider/httpsprovider v0.100.0 - gomod: go.opentelemetry.io/collector/confmap/provider/yamlprovider v0.100.0 - - gomod: github.com/Dynatrace/dynatrace-otel-collector/confmap/provider/eecprovider v0.0.0 + - gomod: github.com/Dynatrace/dynatrace-otel-collector/internal/confmap/provider/eecprovider v0.0.0 replaces: - - github.com/Dynatrace/dynatrace-otel-collector/confmap/provider/eecprovider => ../confmap/provider/eecprovider + - github.com/Dynatrace/dynatrace-otel-collector/internal/confmap/provider/eecprovider => ../internal/confmap/provider/eecprovider