Skip to content

Commit ff7a485

Browse files
[builder] make retries configurable for faster tests (#10035)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description When running tests, waiting for `downloadModules()` to fail 3 times when that's expected adds time to the test run. This updates tests to only attempt downloading once. Note: if there's a network failure that could cause `downloadModules()` to fail when it should normally succeed. Also the wording here is `retries` when in actuality it's the number of attempts. I didn't change this to keep the log wording the same, but I can change the wording if that's preferable. <!-- Issue number if applicable --> #### Link to tracking issue this will help for adding tests for #9252 and #9896 <!--Describe what testing was performed and which tests were added.--> #### Testing Tests ran --------- Co-authored-by: Pablo Baeyens <[email protected]>
1 parent 174f003 commit ff7a485

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

cmd/builder/internal/builder/config.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os/exec"
1111
"path/filepath"
1212
"strings"
13+
"time"
1314

1415
"github.com/hashicorp/go-version"
1516
"go.uber.org/multierr"
@@ -41,6 +42,8 @@ type Config struct {
4142
Providers *[]Module `mapstructure:"providers"`
4243
Replaces []string `mapstructure:"replaces"`
4344
Excludes []string `mapstructure:"excludes"`
45+
46+
downloadModules retry `mapstructure:"-"`
4447
}
4548

4649
// Distribution holds the parameters for the final binary
@@ -66,6 +69,11 @@ type Module struct {
6669
Path string `mapstructure:"path"` // an optional path to the local version of this module
6770
}
6871

72+
type retry struct {
73+
numRetries int
74+
wait time.Duration
75+
}
76+
6977
// NewDefaultConfig creates a new config, with default values
7078
func NewDefaultConfig() Config {
7179
log, err := zap.NewDevelopment()
@@ -85,6 +93,12 @@ func NewDefaultConfig() Config {
8593
OtelColVersion: defaultOtelColVersion,
8694
Module: "go.opentelemetry.io/collector/cmd/builder",
8795
},
96+
// basic retry if error from go mod command (in case of transient network error).
97+
// retry 3 times with 5 second spacing interval
98+
downloadModules: retry{
99+
numRetries: 3,
100+
wait: 5 * time.Second,
101+
},
88102
}
89103
}
90104

cmd/builder/internal/builder/main.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,12 @@ func GetModules(cfg Config) error {
189189

190190
func downloadModules(cfg Config) error {
191191
cfg.Logger.Info("Getting go modules")
192-
// basic retry if error from go mod command (in case of transient network error). This could be improved
193-
// retry 3 times with 5 second spacing interval
194-
retries := 3
195192
failReason := "unknown"
196-
for i := 1; i <= retries; i++ {
193+
for i := 1; i <= cfg.downloadModules.numRetries; i++ {
197194
if _, err := runGoCommand(cfg, "mod", "download"); err != nil {
198195
failReason = err.Error()
199-
cfg.Logger.Info("Failed modules download", zap.String("retry", fmt.Sprintf("%d/%d", i, retries)))
200-
time.Sleep(5 * time.Second)
196+
cfg.Logger.Info("Failed modules download", zap.String("retry", fmt.Sprintf("%d/%d", i, cfg.downloadModules.numRetries)))
197+
time.Sleep(cfg.downloadModules.wait)
201198
continue
202199
}
203200
return nil

cmd/builder/internal/builder/main_test.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,15 @@ require (
3333
)`)
3434
)
3535

36-
func newInitializedConfig(t *testing.T) Config {
36+
func newTestConfig() Config {
3737
cfg := NewDefaultConfig()
38+
cfg.downloadModules.wait = 0
39+
cfg.downloadModules.numRetries = 1
40+
return cfg
41+
}
42+
43+
func newInitializedConfig(t *testing.T) Config {
44+
cfg := newTestConfig()
3845
// Validate and ParseModules will be called before the config is
3946
// given to Generate.
4047
assert.NoError(t, cfg.Validate())
@@ -66,7 +73,7 @@ func TestVersioning(t *testing.T) {
6673
{
6774
description: "defaults",
6875
cfgBuilder: func() Config {
69-
cfg := NewDefaultConfig()
76+
cfg := newTestConfig()
7077
cfg.Distribution.Go = "go"
7178
cfg.Replaces = append(cfg.Replaces, replaces...)
7279
return cfg
@@ -76,7 +83,7 @@ func TestVersioning(t *testing.T) {
7683
{
7784
description: "require otelcol",
7885
cfgBuilder: func() Config {
79-
cfg := NewDefaultConfig()
86+
cfg := newTestConfig()
8087
cfg.Distribution.Go = "go"
8188
cfg.Distribution.RequireOtelColModule = true
8289
cfg.Replaces = append(cfg.Replaces, replaces...)
@@ -87,7 +94,7 @@ func TestVersioning(t *testing.T) {
8794
{
8895
description: "only gomod file, skip generate",
8996
cfgBuilder: func() Config {
90-
cfg := NewDefaultConfig()
97+
cfg := newTestConfig()
9198
tempDir := t.TempDir()
9299
err := makeModule(tempDir, goModTestFile)
93100
require.NoError(t, err)
@@ -101,7 +108,7 @@ func TestVersioning(t *testing.T) {
101108
{
102109
description: "old otel version",
103110
cfgBuilder: func() Config {
104-
cfg := NewDefaultConfig()
111+
cfg := newTestConfig()
105112
cfg.Verbose = true
106113
cfg.Distribution.Go = "go"
107114
cfg.Distribution.OtelColVersion = "0.97.0"
@@ -133,7 +140,7 @@ func TestVersioning(t *testing.T) {
133140
{
134141
description: "old component version",
135142
cfgBuilder: func() Config {
136-
cfg := NewDefaultConfig()
143+
cfg := newTestConfig()
137144
cfg.Distribution.Go = "go"
138145
cfg.Exporters = []Module{
139146
{
@@ -149,7 +156,7 @@ func TestVersioning(t *testing.T) {
149156
{
150157
description: "old component version without strict mode",
151158
cfgBuilder: func() Config {
152-
cfg := NewDefaultConfig()
159+
cfg := newTestConfig()
153160
cfg.Distribution.Go = "go"
154161
cfg.SkipStrictVersioning = true
155162
cfg.Exporters = []Module{
@@ -200,7 +207,7 @@ func TestGenerateAndCompile(t *testing.T) {
200207
{
201208
testCase: "Default Configuration Compilation",
202209
cfgBuilder: func(t *testing.T) Config {
203-
cfg := NewDefaultConfig()
210+
cfg := newTestConfig()
204211
cfg.Distribution.OutputPath = t.TempDir()
205212
cfg.Replaces = append(cfg.Replaces, replaces...)
206213
return cfg
@@ -209,7 +216,7 @@ func TestGenerateAndCompile(t *testing.T) {
209216
{
210217
testCase: "LDFlags Compilation",
211218
cfgBuilder: func(t *testing.T) Config {
212-
cfg := NewDefaultConfig()
219+
cfg := newTestConfig()
213220
cfg.Distribution.OutputPath = t.TempDir()
214221
cfg.Replaces = append(cfg.Replaces, replaces...)
215222
cfg.LDFlags = `-X "test.gitVersion=0743dc6c6411272b98494a9b32a63378e84c34da" -X "test.gitTag=local-testing" -X "test.goVersion=go version go1.20.7 darwin/amd64"`
@@ -219,7 +226,7 @@ func TestGenerateAndCompile(t *testing.T) {
219226
{
220227
testCase: "Debug Compilation",
221228
cfgBuilder: func(t *testing.T) Config {
222-
cfg := NewDefaultConfig()
229+
cfg := newTestConfig()
223230
cfg.Distribution.OutputPath = t.TempDir()
224231
cfg.Replaces = append(cfg.Replaces, replaces...)
225232
cfg.Logger = zap.NewNop()
@@ -230,7 +237,7 @@ func TestGenerateAndCompile(t *testing.T) {
230237
{
231238
testCase: "No providers",
232239
cfgBuilder: func(t *testing.T) Config {
233-
cfg := NewDefaultConfig()
240+
cfg := newTestConfig()
234241
cfg.Distribution.OutputPath = t.TempDir()
235242
cfg.Replaces = append(cfg.Replaces, replaces...)
236243
cfg.Providers = &[]Module{}
@@ -240,7 +247,7 @@ func TestGenerateAndCompile(t *testing.T) {
240247
{
241248
testCase: "Pre-confmap factories",
242249
cfgBuilder: func(t *testing.T) Config {
243-
cfg := NewDefaultConfig()
250+
cfg := newTestConfig()
244251
cfg.Distribution.OutputPath = t.TempDir()
245252
cfg.Replaces = append(cfg.Replaces, replaces...)
246253
cfg.Distribution.OtelColVersion = "0.98.0"
@@ -251,7 +258,7 @@ func TestGenerateAndCompile(t *testing.T) {
251258
{
252259
testCase: "With confmap factories",
253260
cfgBuilder: func(t *testing.T) Config {
254-
cfg := NewDefaultConfig()
261+
cfg := newTestConfig()
255262
cfg.Distribution.OutputPath = t.TempDir()
256263
cfg.Replaces = append(cfg.Replaces, replaces...)
257264
cfg.Distribution.OtelColVersion = "0.99.0"

0 commit comments

Comments
 (0)