Skip to content

Commit

Permalink
Supporting code has been extracted out of test file.
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkajanecek committed Dec 18, 2024
1 parent 524f1c1 commit 275c8ff
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 184 deletions.
115 changes: 115 additions & 0 deletions internal/e2e/instrumentation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package e2e

import (
"context"
"log"
"path/filepath"
"time"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

const (
receivingContainer = "receiver"
testedContainer = "sut"
generatingContainer = "generator"
port = 17016
collectorRunningPeriod = 35 * time.Second
samplesCount = 10
)

func runReceivingSolarWindsOTELCollector(
ctx context.Context,
networkName string,
) (testcontainers.Container, error) {
configPath, err := filepath.Abs(filepath.Join(".", "testdata", "receiving_collector.yaml"))
if err != nil {
return nil, err
}

container, err := runSolarWindsOTELCollector(ctx, networkName, receivingContainer, configPath)
return container, err
}

func runTestedSolarWindsOTELCollector(
ctx context.Context,
networkName string,
) (testcontainers.Container, error) {
configPath, err := filepath.Abs(filepath.Join(".", "testdata", "emitting_collector.yaml"))
if err != nil {
return nil, err
}

container, err := runSolarWindsOTELCollector(ctx, networkName, testedContainer, configPath)
return container, err
}

func runSolarWindsOTELCollector(
ctx context.Context,
networkName string,
containerName string,
configPath string,
) (testcontainers.Container, error) {
lc := new(logConsumer)
lc.Prefix = containerName
req := testcontainers.ContainerRequest{
Image: "solarwinds-otel-collector:latest",
LogConsumerCfg: &testcontainers.LogConsumerConfig{
Consumers: []testcontainers.LogConsumer{lc},
},
Files: []testcontainers.ContainerFile{
{
HostFilePath: configPath,
ContainerFilePath: "/opt/default-config.yaml",
FileMode: 0o440,
},
},
WaitingFor: wait.ForLog("Everything is ready. Begin running and processing data."),
Networks: []string{networkName},
Name: containerName,
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})

return container, err
}

func runGeneratorContainer(
ctx context.Context,
networkName string,
cmd []string,
) (testcontainers.Container, error) {
containerName := generatingContainer

lc := new(logConsumer)
lc.Prefix = containerName

req := testcontainers.ContainerRequest{
Image: "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen:latest",
LogConsumerCfg: &testcontainers.LogConsumerConfig{
Consumers: []testcontainers.LogConsumer{lc},
},
Networks: []string{networkName},
Name: containerName,
Cmd: cmd,
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})

return container, err
}

type logConsumer struct {
Prefix string
}

func (lc *logConsumer) Accept(l testcontainers.Log) {
log.Printf("***%s: %s", lc.Prefix, string(l.Content))
}
Loading

0 comments on commit 275c8ff

Please sign in to comment.