Skip to content
Merged
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
4 changes: 1 addition & 3 deletions command/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ func TestController_CopyFailsFrom(t *testing.T) {
tc.from.ServiceStatus.Fails.Command.Set(k, *v)
}
}
for i, v := range tc.fromNextRunnable {
tc.from.nextRunnable[i] = v
}
copy(tc.from.nextRunnable, tc.fromNextRunnable)
}
if tc.to != nil && tc.to.Command != nil {
tc.to.Init(
Expand Down
6 changes: 4 additions & 2 deletions service/latest_version/filter/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import (
)

// ExecCommand will run Command.
func (r *Require) ExecCommand(logFrom logutil.LogFrom) error {
func (r *Require) ExecCommand(version string, logFrom logutil.LogFrom) error {
if r == nil || len(r.Command) == 0 {
return nil
}

// Apply the template vars to the command.
cmd := r.Command.ApplyTemplate(r.Status.GetServiceInfo())
serviceInfo := r.Status.GetServiceInfo()
serviceInfo.LatestVersion = version
cmd := r.Command.ApplyTemplate(serviceInfo)

// Execute the command.
if err := cmd.Exec(logFrom); err != nil {
Expand Down
37 changes: 32 additions & 5 deletions service/latest_version/filter/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,49 @@ import (
"github.com/release-argus/Argus/command"
"github.com/release-argus/Argus/service/dashboard"
"github.com/release-argus/Argus/service/status"
serviceinfo "github.com/release-argus/Argus/service/status/info"
"github.com/release-argus/Argus/test"
"github.com/release-argus/Argus/util"
logutil "github.com/release-argus/Argus/util/log"
)

func TestRequire_ExecCommand(t *testing.T) {
// GIVEN a Require with a Command.
tests := map[string]struct {
cmd command.Command
errRegex string
cmd command.Command
sInfo *serviceinfo.ServiceInfo
version string
stdoutRegex string
errRegex string
}{
"no command": {
errRegex: `^$`},
"valid command": {
cmd: []string{"true"},
errRegex: `^$`},
"valid multi-arg command": {
cmd: []string{"ls", "-lah"},
errRegex: `^$`},
cmd: []string{"ls", "-lah"},
stdoutRegex: `\.go`,
errRegex: `^$`},
"invalid command": {
cmd: []string{"false"},
errRegex: `exit status 1`},
"new version overrides previous latest_version": {
cmd: []string{"echo", "approved_version='{{ approved_version}}', deployed_version='{{ deployed_version }}', version='{{ version }}', latest_version='{{ latest_version }}',"},
sInfo: &serviceinfo.ServiceInfo{
ApprovedVersion: "v1.0.0-approved",
DeployedVersion: "v2.0.0-deployed",
LatestVersion: "v3.0.0-latest",
},
version: "v4.0.0",
stdoutRegex: `approved_version='v1.0.0-approved', deployed_version='v2.0.0-deployed', version='v4.0.0', latest_version='v4.0.0'`,
errRegex: `^$`},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// t.Parallel() - Cannot run in parallel since we're using stdout and sharing log resultChannel.
releaseStdout := test.CaptureLog(logutil.Log)

svcDashboard := &dashboard.Options{
WebURL: "https://example.com"}
Expand All @@ -56,16 +74,25 @@ func TestRequire_ExecCommand(t *testing.T) {
0, 1, 0,
name, "", "",
svcDashboard)
if tc.sInfo != nil {
require.Status.ServiceInfo = *tc.sInfo
}

// WHEN ApplyTemplate is called on the Command.
err := require.ExecCommand(logutil.LogFrom{})
err := require.ExecCommand(tc.version, logutil.LogFrom{})

// THEN the err is expected.
e := util.ErrorToString(err)
if !util.RegexCheck(tc.errRegex, e) {
t.Errorf("%s\nerror mismatch\nwant: %q\ngot: %q",
packageName, tc.errRegex, e)
}
// AND the stdout is expected.
stdout := releaseStdout()
if !util.RegexCheck(tc.stdoutRegex, stdout) {
t.Fatalf("%s\nstdout mismatch\nwant: %q\ngot: %q",
packageName, tc.stdoutRegex, stdout)
}
})
}
}
2 changes: 1 addition & 1 deletion service/latest_version/types/github/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func (l *Lookup) releaseMeetsRequirements(release github_types.Release, logFrom
}

// If the Command didn't return successfully.
if err := l.Require.ExecCommand(logFrom); err != nil {
if err := l.Require.ExecCommand(version, logFrom); err != nil {
return "", "", err //nolint:wrapcheck
}

Expand Down
2 changes: 1 addition & 1 deletion service/latest_version/types/web/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (l *Lookup) versionMeetsRequirements(version, body string, logFrom logutil.
}

// If the Command didn't return successfully.
if err := l.Require.ExecCommand(logFrom); err != nil {
if err := l.Require.ExecCommand(version, logFrom); err != nil {
return err //nolint:wrapcheck
}

Expand Down
Loading