Skip to content

Commit ad7c19f

Browse files
authored
[Heartbeat] Fix broken invocation of synth package (#26228)
Fixes invocation of synthetics broken in #26188 Additionally, for dev purposes, this lets accepts a synthetics version of file:/// as valid to help with debugging development versions of the synthetics agent. Never released, so no changelog needed Still just sticking with unit tests here since testing more deeply with synthetics (esp. master where this is a problem) is a larger problem than we're equipped to handle ATM.
1 parent 30b9c8f commit ad7c19f

4 files changed

Lines changed: 70 additions & 3 deletions

File tree

x-pack/heartbeat/monitors/browser/source/validatepackage.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func parseVersion(version string) string {
4242
}
4343

4444
func validateVersion(expected string, current string) error {
45+
if strings.HasPrefix(current, "file://") {
46+
return nil
47+
}
48+
4549
expectedRange, err := semver.NewConstraint(expected)
4650
if err != nil {
4751
return err
@@ -75,6 +79,7 @@ func validatePackageJSON(path string) error {
7579
if synthVersion == "" {
7680
synthVersion = pkgJson.DevDependencies.SynthVersion
7781
}
82+
7883
err = validateVersion(ExpectedSynthVersion, synthVersion)
7984
if err != nil {
8085
return fmt.Errorf("could not validate @elastic/synthetics version: '%s' %w", synthVersion, err)

x-pack/heartbeat/monitors/browser/source/validatepackage_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ func TestValidateVersion(t *testing.T) {
7070
current: "0.0.1-alpha.11",
7171
shouldErr: false,
7272
},
73+
{
74+
expected: "",
75+
current: "file://blahblahblah",
76+
shouldErr: false,
77+
},
7378
}
7479

7580
for _, tt := range tests {

x-pack/heartbeat/monitors/browser/synthexec/synthexec.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ const debugSelector = "synthexec"
3030
func SuiteJob(ctx context.Context, suitePath string, params common.MapStr, extraArgs ...string) (jobs.Job, error) {
3131
// Run the command in the given suitePath, use '.' as the first arg since the command runs
3232
// in the correct dir
33-
newCmd, err := suiteCommandFactory(suitePath, append(extraArgs, ".")...)
33+
cmdFactory, err := suiteCommandFactory(suitePath, extraArgs...)
3434
if err != nil {
3535
return nil, err
3636
}
3737

38-
return startCmdJob(ctx, newCmd, nil, params), nil
38+
return startCmdJob(ctx, cmdFactory, nil, params), nil
3939
}
4040

4141
func suiteCommandFactory(suitePath string, args ...string) (func() *exec.Cmd, error) {
@@ -46,7 +46,11 @@ func suiteCommandFactory(suitePath string, args ...string) (func() *exec.Cmd, er
4646

4747
newCmd := func() *exec.Cmd {
4848
bin := filepath.Join(npmRoot, "node_modules/.bin/elastic-synthetics")
49-
cmd := exec.Command(bin, args...)
49+
// Always put the suite path first to prevent conflation with variadic args!
50+
// See https://github.com/tj/commander.js/blob/master/docs/options-taking-varying-arguments.md
51+
// Note, we don't use the -- approach because it's cleaner to always know we can add new options
52+
// to the end.
53+
cmd := exec.Command(bin, append([]string{suitePath}, args...)...)
5054
cmd.Dir = npmRoot
5155
return cmd
5256
}

x-pack/heartbeat/monitors/browser/synthexec/synthexec_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,56 @@ Loop:
157157
})
158158
}
159159
}
160+
161+
func TestSuiteCommandFactory(t *testing.T) {
162+
_, filename, _, _ := runtime.Caller(0)
163+
origPath := path.Join(filepath.Dir(filename), "../source/fixtures/todos")
164+
suitePath, err := filepath.Abs(origPath)
165+
require.NoError(t, err)
166+
binPath := path.Join(suitePath, "node_modules/.bin/elastic-synthetics")
167+
168+
tests := []struct {
169+
name string
170+
suitePath string
171+
extraArgs []string
172+
want []string
173+
wantErr bool
174+
}{
175+
{
176+
"no args",
177+
suitePath,
178+
nil,
179+
[]string{binPath, suitePath},
180+
false,
181+
},
182+
{
183+
"with args",
184+
suitePath,
185+
[]string{"--capability", "foo", "bar", "--rich-events"},
186+
[]string{binPath, suitePath, "--capability", "foo", "bar", "--rich-events"},
187+
false,
188+
},
189+
{
190+
"no npm root",
191+
"/not/a/path/for/sure",
192+
nil,
193+
nil,
194+
true,
195+
},
196+
}
197+
for _, tt := range tests {
198+
t.Run(tt.name, func(t *testing.T) {
199+
factory, err := suiteCommandFactory(tt.suitePath, tt.extraArgs...)
200+
201+
if tt.wantErr {
202+
require.Error(t, err)
203+
return
204+
}
205+
require.NoError(t, err)
206+
207+
cmd := factory()
208+
got := cmd.Args
209+
require.Equal(t, tt.want, got)
210+
})
211+
}
212+
}

0 commit comments

Comments
 (0)