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

[cmd/builder] Generate replace directives when path is specified for provider #10851

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/builder-providers-path.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: ocb

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Generate replace directives when path is specified for provider

# One or more tracking issues or pull requests related to the change
issues: ["https://github.com/open-telemetry/opentelemetry-collector/pull/10851"]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
200 changes: 197 additions & 3 deletions cmd/builder/internal/builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
)

const (
Expand Down Expand Up @@ -125,6 +126,194 @@ func TestGenerateInvalidOutputPath(t *testing.T) {
require.Contains(t, err.Error(), "failed to create output path")
}

func TestGenerateReplaceDirectives(t *testing.T) {
cases := []struct {
description string
cfgBuilder func() Config
expectedReplacements []modfile.Replace
}{
{
description: "no replacements generated",
cfgBuilder: func() Config {
return newTestConfig()
},
},
{
description: "provider replacements generated",
cfgBuilder: func() Config {
cfg := newTestConfig()
cfg.Providers = &[]Module{
{GoMod: "old/1 v0.0.0", Path: "/new/1"},
{GoMod: "old/2 v0.0.0", Path: "/new/2"},
}
return cfg
},
expectedReplacements: []modfile.Replace{
{
Old: module.Version{Path: "old/1", Version: "v0.0.0"},
New: module.Version{Path: "/new/1"},
},
{
Old: module.Version{Path: "old/2", Version: "v0.0.0"},
New: module.Version{Path: "/new/2"},
},
},
},
{
description: "connector replacements generated",
cfgBuilder: func() Config {
cfg := newTestConfig()
cfg.Connectors = []Module{
{GoMod: "old/1 v0.0.0", Path: "/new/1"},
{GoMod: "old/2 v0.0.0", Path: "/new/2"},
}
return cfg
},
expectedReplacements: []modfile.Replace{
{
Old: module.Version{Path: "old/1", Version: "v0.0.0"},
New: module.Version{Path: "/new/1"},
},
{
Old: module.Version{Path: "old/2", Version: "v0.0.0"},
New: module.Version{Path: "/new/2"},
},
},
},
{
description: "extension replacements generated",
cfgBuilder: func() Config {
cfg := newTestConfig()
cfg.Extensions = []Module{
{GoMod: "old/1 v0.0.0", Path: "/new/1"},
{GoMod: "old/2 v0.0.0", Path: "/new/2"},
}
return cfg
},
expectedReplacements: []modfile.Replace{
{
Old: module.Version{Path: "old/1", Version: "v0.0.0"},
New: module.Version{Path: "/new/1"},
},
{
Old: module.Version{Path: "old/2", Version: "v0.0.0"},
New: module.Version{Path: "/new/2"},
},
},
},
{
description: "receiver replacements generated",
cfgBuilder: func() Config {
cfg := newTestConfig()
cfg.Receivers = []Module{
{GoMod: "old/1 v0.0.0", Path: "/new/1"},
{GoMod: "old/2 v0.0.0", Path: "/new/2"},
}
return cfg
},
expectedReplacements: []modfile.Replace{
{
Old: module.Version{Path: "old/1", Version: "v0.0.0"},
New: module.Version{Path: "/new/1"},
},
{
Old: module.Version{Path: "old/2", Version: "v0.0.0"},
New: module.Version{Path: "/new/2"},
},
},
},
{
description: "exporter replacements generated",
cfgBuilder: func() Config {
cfg := newTestConfig()
cfg.Exporters = []Module{
{GoMod: "old/1 v0.0.0", Path: "/new/1"},
{GoMod: "old/2 v0.0.0", Path: "/new/2"},
}
return cfg
},
expectedReplacements: []modfile.Replace{
{
Old: module.Version{Path: "old/1", Version: "v0.0.0"},
New: module.Version{Path: "/new/1"},
},
{
Old: module.Version{Path: "old/2", Version: "v0.0.0"},
New: module.Version{Path: "/new/2"},
},
},
},
{
description: "processor replacements generated",
cfgBuilder: func() Config {
cfg := newTestConfig()
cfg.Processors = []Module{
{GoMod: "old/1 v0.0.0", Path: "/new/1"},
{GoMod: "old/2 v0.0.0", Path: "/new/2"},
}
return cfg
},
expectedReplacements: []modfile.Replace{
{
Old: module.Version{Path: "old/1", Version: "v0.0.0"},
New: module.Version{Path: "/new/1"},
},
{
Old: module.Version{Path: "old/2", Version: "v0.0.0"},
New: module.Version{Path: "/new/2"},
},
},
},
{
description: "generic replacements generated",
cfgBuilder: func() Config {
cfg := newTestConfig()
cfg.Replaces = []string{
"old/1 v1.2.3 => new/1 v4.5.6", // module => module
"old/2 v1.2.3 => /new/2", // module => path
}
return cfg
},
expectedReplacements: []modfile.Replace{
{
Old: module.Version{Path: "old/1", Version: "v1.2.3"},
New: module.Version{Path: "new/1", Version: "v4.5.6"},
},
{
Old: module.Version{Path: "old/2", Version: "v1.2.3"},
New: module.Version{Path: "/new/2"},
},
},
},
}

for _, tc := range cases {
t.Run(tc.description, func(t *testing.T) {
outputPath := t.TempDir()

cfg := tc.cfgBuilder()
cfg.Distribution.OutputPath = outputPath
require.NoError(t, cfg.ParseModules())
require.NoError(t, cfg.Validate())

require.NoError(t, Generate(cfg))

mod, err := parseGoMod(outputPath)
require.NoError(t, err)

require.Len(t, mod.Replace, len(tc.expectedReplacements))

for i := 0; i < len(tc.expectedReplacements); i++ {
actual := mod.Replace[i]
expected := tc.expectedReplacements[i]

require.Equal(t, expected.Old, actual.Old)
require.Equal(t, expected.New, actual.New)
}
})
}
}

func TestVersioning(t *testing.T) {
replaces := generateReplaces()
tests := []struct {
Expand Down Expand Up @@ -457,13 +646,18 @@ func TestReplaceStatementsAreComplete(t *testing.T) {
}
}

func verifyGoMod(t *testing.T, dir string, replaceMods map[string]bool) {
func parseGoMod(dir string) (*modfile.File, error) {
gomodpath := path.Join(dir, "go.mod")
// #nosec G304 We control this path and generate the file inside, so we can assume it is safe.
gomod, err := os.ReadFile(gomodpath)
require.NoError(t, err)
if err != nil {
return nil, err
}
return modfile.Parse(gomodpath, gomod, nil)
}

mod, err := modfile.Parse(gomodpath, gomod, nil)
func verifyGoMod(t *testing.T, dir string, replaceMods map[string]bool) {
mod, err := parseGoMod(dir)
require.NoError(t, err)

for _, req := range mod.Require {
Expand Down
3 changes: 3 additions & 0 deletions cmd/builder/internal/builder/templates/go.mod.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ require (
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
)

{{- range .Providers}}
{{if ne .Path ""}}replace {{.GoMod}} => {{.Path}}{{end}}
{{- end}}
{{- range .Connectors}}
{{if ne .Path ""}}replace {{.GoMod}} => {{.Path}}{{end}}
{{- end}}
Expand Down