Skip to content

Commit

Permalink
testutils: add Collector.WillFail(), connect Args, and specify config…
Browse files Browse the repository at this point in the history
… dynamically
  • Loading branch information
Ryan Fitzpatrick committed Jun 3, 2021
1 parent d16c4ce commit 626e348
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
1 change: 1 addition & 0 deletions tests/testutils/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Collector interface {
WithEnv(env map[string]string) Collector
WithLogger(logger *zap.Logger) Collector
WithLogLevel(level string) Collector
WillFail(fail bool) Collector
Build() (Collector, error)
Start() error
Shutdown() error
Expand Down
50 changes: 41 additions & 9 deletions tests/testutils/collector_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"os"
"strings"

"github.com/testcontainers/testcontainers-go"
"go.uber.org/zap"
Expand All @@ -35,6 +36,7 @@ type CollectorContainer struct {
Args []string
Logger *zap.Logger
LogLevel string
Fail bool
Container Container
contextArchive io.Reader
logConsumer collectorLogConsumer
Expand All @@ -57,7 +59,7 @@ func (collector CollectorContainer) WithConfigPath(path string) Collector {
return &collector
}

// []string{} by default, but currently a noop
// []string{} by default
func (collector CollectorContainer) WithArgs(args ...string) Collector {
collector.Args = args
return &collector
Expand All @@ -81,6 +83,12 @@ func (collector CollectorContainer) WithLogLevel(level string) Collector {
return &collector
}

func (collector CollectorContainer) WillFail(fail bool) Collector {
collector.Fail = fail
return &collector
}


func (collector CollectorContainer) Build() (Collector, error) {
if collector.Image == "" {
collector.Image = "quay.io/signalfx/splunk-otel-collector:latest"
Expand All @@ -101,7 +109,18 @@ func (collector CollectorContainer) Build() (Collector, error) {
}
collector.Container = collector.Container.WithContextArchive(
collector.contextArchive,
).WithNetworkMode("host").WillWaitForLogs("Everything is ready. Begin running and processing data.")
).WithNetworkMode("host")

if collector.Fail {
collector.Container = collector.Container.WillWaitForLogs("")
} else {
collector.Container = collector.Container.WillWaitForLogs("Everything is ready. Begin running and processing data.")
}

if len(collector.Args) > 0 {
collector.Container = collector.Container.WithCmd(collector.Args...)
}

collector.Container = *(collector.Container.Build())

return &collector, nil
Expand All @@ -128,7 +147,7 @@ func (collector *CollectorContainer) Shutdown() error {
return collector.Container.Terminate(context.Background())
}

func (collector CollectorContainer) buildContextArchive() (io.Reader, error) {
func (collector *CollectorContainer) buildContextArchive() (io.Reader, error) {
var buf bytes.Buffer
tarWriter := tar.NewWriter(&buf)

Expand All @@ -152,13 +171,26 @@ func (collector CollectorContainer) buildContextArchive() (io.Reader, error) {
return nil, err
}

dockerfile += `
COPY config.yaml /etc/config.yaml
ENV SPLUNK_CONFIG=/etc/config.yaml
dockerfile += "COPY config.yaml /etc/config.yaml\n"

ENV SPLUNK_ACCESS_TOKEN=12345
ENV SPLUNK_REALM=us0
`
// We need to tell the Collector to use the provided config
// but only if not already done so in the test
var configSetByArgs bool
for _, c := range collector.Args {
if strings.Contains(c, "--config") {
configSetByArgs = true
}
}
_, configSetByEnvVar := collector.Container.Env["SPLUNK_CONFIG"]
if !configSetByArgs && !configSetByEnvVar {
// only specify w/ args if none are used in the test
if len(collector.Args) == 0 {
collector.Args = append(collector.Args, "--config", "/etc/config.yaml")
} else {
// fallback to env var
collector.Container.Env["SPLUNK_CONFIG"] = "/etc/config.yaml"
}
}
}

header := tar.Header{
Expand Down
10 changes: 10 additions & 0 deletions tests/testutils/collector_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ func TestCollectorContainerBuilders(t *testing.T) {
assert.Equal(t, []string{"arg_one", "arg_two", "arg_three"}, withArgs.Args)
assert.Empty(t, builder.Args)

willFail, ok := builder.WillFail(true).(*CollectorContainer)
require.True(t, ok)
assert.True(t, willFail.Fail)
assert.False(t, builder.Fail)

wontFail, ok := willFail.WillFail(false).(*CollectorContainer)
require.True(t, ok)
assert.False(t, wontFail.Fail)
assert.True(t, willFail.Fail)

logger := zap.NewNop()
withLogger, ok := builder.WithLogger(logger).(*CollectorContainer)
require.True(t, ok)
Expand Down
7 changes: 7 additions & 0 deletions tests/testutils/collector_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type CollectorProcess struct {
Env map[string]string
Logger *zap.Logger
LogLevel string
Fail bool
Process *subprocess.Subprocess
subprocessConfig *subprocess.Config
}
Expand Down Expand Up @@ -81,6 +82,12 @@ func (collector CollectorProcess) WithLogLevel(level string) Collector {
return &collector
}

// noop at this time
func (collector CollectorProcess) WillFail(fail bool) Collector {
collector.Fail = fail
return &collector
}

func (collector CollectorProcess) Build() (Collector, error) {
if collector.ConfigPath == "" && collector.Args == nil {
return nil, fmt.Errorf("you must specify a ConfigPath for your CollectorProcess before building")
Expand Down

0 comments on commit 626e348

Please sign in to comment.