Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristina Pathak committed Apr 17, 2024
1 parent a2fdb95 commit d821f83
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 21 deletions.
18 changes: 10 additions & 8 deletions cmd/builder/internal/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import (

var (
// ErrGoNotFound is returned when a Go binary hasn't been found
ErrGoNotFound = errors.New("go binary not found")
ErrDepNotFound = errors.New("dependency not found in go mod file")
ErrVersionMismatch = errors.New("mismatch in go.mod and builder configuration versions")
errFailedToDownload = errors.New("failed to download go modules")
skipStrictMsg = "Use --skip-strict-versioning to temporarily disable this check. This flag will be removed in a future minor version"
ErrGoNotFound = errors.New("go binary not found")
ErrDepNotFound = errors.New("dependency not found in go mod file")
ErrVersionMismatch = errors.New("mismatch in go.mod and builder configuration versions")
errGoGetFailed = errors.New("failed to go get")
errDownloadFailed = errors.New("failed to download go modules")
errCompileFailed = errors.New("failed to compile the OpenTelemetry Collector distribution")
skipStrictMsg = "Use --skip-strict-versioning to temporarily disable this check. This flag will be removed in a future minor version"
)

func runGoCommand(cfg Config, args ...string) ([]byte, error) {
Expand Down Expand Up @@ -127,7 +129,7 @@ func Compile(cfg Config) error {
args = append(args, "-tags", cfg.Distribution.BuildTags)
}
if _, err := runGoCommand(cfg, args...); err != nil {
return fmt.Errorf("failed to compile the OpenTelemetry Collector distribution: %w", err)
return fmt.Errorf("%w: %s", errCompileFailed, err.Error())
}
cfg.Logger.Info("Compiled", zap.String("binary", fmt.Sprintf("%s/%s", cfg.Distribution.OutputPath, cfg.Distribution.Name)))

Expand All @@ -143,7 +145,7 @@ func GetModules(cfg Config) error {

// ambiguous import: found package cloud.google.com/go/compute/metadata in multiple modules
if _, err := runGoCommand(cfg, "get", "cloud.google.com/go"); err != nil {
return fmt.Errorf("failed to go get: %w", err)
return fmt.Errorf("%w: %s", errGoGetFailed, err.Error())
}

if _, err := runGoCommand(cfg, "mod", "tidy", "-compat=1.21"); err != nil {

Check warning on line 151 in cmd/builder/internal/builder/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/builder/main.go#L151

Added line #L151 was not covered by tests
Expand Down Expand Up @@ -209,7 +211,7 @@ func downloadModules(cfg Config) error {
}
return nil
}
return fmt.Errorf("%w: %s", errFailedToDownload, failReason)
return fmt.Errorf("%w: %s", errDownloadFailed, failReason)
}

func processAndWrite(cfg Config, tmpl *template.Template, outFile string, tmplParams any) error {
Expand Down
108 changes: 105 additions & 3 deletions cmd/builder/internal/builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

var (
goModTestFile = []byte(`// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
module go.opentelemetry.io/collector/cmd/builder/internal/tester
go 1.20
require (
go.opentelemetry.io/collector/component v0.96.0
go.opentelemetry.io/collector/connector v0.94.1
go.opentelemetry.io/collector/exporter v0.94.1
go.opentelemetry.io/collector/extension v0.94.1
go.opentelemetry.io/collector/otelcol v0.94.1
go.opentelemetry.io/collector/processor v0.94.1
go.opentelemetry.io/collector/receiver v0.94.1
go.opentelemetry.io/collector v0.96.0
)`)
)

func TestGenerateDefault(t *testing.T) {
Expand All @@ -34,14 +52,38 @@ func TestVersioning(t *testing.T) {
expectedErr error
}{
{
description: "success",
description: "defaults",
cfgBuilder: func() Config {
cfg := NewDefaultConfig()
cfg.Distribution.Go = "go"
return cfg
},
expectedErr: nil,
},
{
description: "require otelcol",
cfgBuilder: func() Config {
cfg := NewDefaultConfig()
cfg.Distribution.Go = "go"
cfg.Distribution.RequireOtelColModule = true
return cfg
},
expectedErr: nil,
},
{
description: "only gomod file, skip generate",
cfgBuilder: func() Config {
cfg := NewDefaultConfig()
tempDir := t.TempDir()
err := makeModule(tempDir, goModTestFile)
require.NoError(t, err)
cfg.Distribution.OutputPath = tempDir
cfg.SkipGenerate = true
cfg.Distribution.Go = "go"
return cfg
},
expectedErr: ErrDepNotFound,
},
{
description: "old otel version",
cfgBuilder: func() Config {
Expand All @@ -55,6 +97,7 @@ func TestVersioning(t *testing.T) {
description: "old otel version without strict mode",
cfgBuilder: func() Config {
cfg := NewDefaultConfig()
cfg.Verbose = true
cfg.Distribution.Go = "go"
cfg.SkipStrictVersioning = true
cfg.Distribution.OtelColVersion = "0.90.0"
Expand All @@ -63,7 +106,7 @@ func TestVersioning(t *testing.T) {
expectedErr: nil,
},
{
description: "invalid version",
description: "invalid collector version",
cfgBuilder: func() Config {
cfg := NewDefaultConfig()
cfg.Distribution.OtelColVersion = "invalid"
Expand All @@ -72,7 +115,7 @@ func TestVersioning(t *testing.T) {
expectedErr: ErrVersionMismatch,
},
{
description: "invalid version without strict mode, only generate",
description: "invalid collector version without strict mode, only generate",
cfgBuilder: func() Config {
cfg := NewDefaultConfig()
cfg.Distribution.OtelColVersion = "invalid"
Expand All @@ -83,6 +126,16 @@ func TestVersioning(t *testing.T) {
},
expectedErr: nil,
},
{
description: "invalid collector version without strict mode",
cfgBuilder: func() Config {
cfg := NewDefaultConfig()
cfg.Distribution.OtelColVersion = "invalid"
cfg.SkipStrictVersioning = true
return cfg
},
expectedErr: errGoGetFailed,
},
{
description: "old component version",
cfgBuilder: func() Config {
Expand All @@ -98,6 +151,37 @@ func TestVersioning(t *testing.T) {
},
expectedErr: ErrVersionMismatch,
},
{
description: "old component version without strict mode",
cfgBuilder: func() Config {
cfg := NewDefaultConfig()
cfg.Distribution.Go = "go"
cfg.SkipStrictVersioning = true
cfg.Exporters = []Module{
{
Import: "go.opentelemetry.io/collector/receiver/otlpreceiver",
GoMod: "go.opentelemetry.io/collector v0.96.0",
},
}
return cfg
},
expectedErr: errCompileFailed,
},
{
description: "invalid component version",
cfgBuilder: func() Config {
cfg := NewDefaultConfig()
cfg.Distribution.Go = "go"
cfg.Exporters = []Module{
{
Import: "go.opentelemetry.io/collector/receiver/otlpreceiver",
GoMod: "go.opentelemetry.io/collector invalid",
},
}
return cfg
},
expectedErr: errGoGetFailed,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
Expand Down Expand Up @@ -193,6 +277,7 @@ func TestGenerateAndCompile(t *testing.T) {
cfg := NewDefaultConfig()
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
cfg.Logger = zap.NewNop()
cfg.Distribution.DebugCompilation = true
return cfg
},
Expand All @@ -208,3 +293,20 @@ func TestGenerateAndCompile(t *testing.T) {
})
}
}

func makeModule(dir string, fileContents []byte) error {
// if the file does not exist, try to create it
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err = os.Mkdir(dir, 0750); err != nil {
return fmt.Errorf("failed to create output path: %w", err)
}
} else if err != nil {
return fmt.Errorf("failed to create output path: %w", err)
}

err := os.WriteFile(filepath.Clean(filepath.Join(dir, "go.mod")), fileContents, 0600)
if err != nil {
return fmt.Errorf("failed to write go.mod file: %w", err)
}
return nil
}
43 changes: 33 additions & 10 deletions cmd/builder/internal/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,26 @@ func Test_applyCfgFromFile(t *testing.T) {
},
wantErr: false,
},
{
name: "Skip strict versioning true",
args: args{
flags: flag.NewFlagSet("version=1.0.0", 1),
cfgFromFile: builder.Config{
Logger: zap.NewNop(),
SkipCompilation: true,
SkipStrictVersioning: true,
Distribution: testDistribution,
},
},
want: builder.Config{
Logger: zap.NewNop(),
SkipCompilation: true,
SkipGetModules: true,
SkipStrictVersioning: true,
Distribution: testDistribution,
},
wantErr: false,
},
{
name: "Skip generate false",
args: args{
Expand All @@ -191,11 +211,12 @@ func Test_applyCfgFromFile(t *testing.T) {
},
},
want: builder.Config{
Logger: zap.NewNop(),
SkipGenerate: false,
SkipCompilation: true,
SkipGetModules: true,
Distribution: testDistribution,
Logger: zap.NewNop(),
SkipGenerate: false,
SkipCompilation: true,
SkipGetModules: true,
SkipStrictVersioning: true,
Distribution: testDistribution,
},
wantErr: false,
},
Expand All @@ -212,11 +233,12 @@ func Test_applyCfgFromFile(t *testing.T) {
},
},
want: builder.Config{
Logger: zap.NewNop(),
SkipGenerate: true,
SkipCompilation: true,
SkipGetModules: true,
Distribution: testDistribution,
Logger: zap.NewNop(),
SkipGenerate: true,
SkipCompilation: true,
SkipGetModules: true,
SkipStrictVersioning: true,
Distribution: testDistribution,
},
wantErr: false,
},
Expand All @@ -228,6 +250,7 @@ func Test_applyCfgFromFile(t *testing.T) {
assert.Equal(t, tt.want.SkipGenerate, cfg.SkipGenerate)
assert.Equal(t, tt.want.SkipCompilation, cfg.SkipCompilation)
assert.Equal(t, tt.want.SkipGetModules, cfg.SkipGetModules)
assert.Equal(t, tt.want.SkipStrictVersioning, cfg.SkipStrictVersioning)
assert.Equal(t, tt.want.Excludes, cfg.Excludes)
assert.Equal(t, tt.want.Exporters, cfg.Exporters)
assert.Equal(t, tt.want.Receivers, cfg.Receivers)
Expand Down

0 comments on commit d821f83

Please sign in to comment.