Skip to content

Commit

Permalink
cli: force periodic job if its id equals search prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
vsvastey committed Aug 26, 2022
1 parent 6d99ca1 commit b665da6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/14333.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
cli: Fixed a bug where forcing a periodic job would fail if the job ID prefix-matched other periodic jobs
```
4 changes: 3 additions & 1 deletion command/job_periodic_force.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ func (c *JobPeriodicForceCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf("No periodic job(s) with prefix or id %q found", jobID))
return 1
}
if len(periodicJobs) > 1 {
// preriodicJobs is sorted by job ID
// so if there is a job whose ID is equal to jobID then it must be the first item
if len(periodicJobs) > 1 && periodicJobs[0].ID != jobID {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple periodic jobs\n\n%s", createStatusListOutput(periodicJobs, c.allNamespaces())))
return 1
}
Expand Down
50 changes: 50 additions & 0 deletions command/job_periodic_force_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,53 @@ func TestJobPeriodicForceCommand_SuccessfulPeriodicForce(t *testing.T) {
require.Contains(t, out, "Monitoring evaluation")
require.Contains(t, out, "finished with status \"complete\"")
}

func TestJobPeriodicForceCommand_SuccessfulIfJobIDEqualsPrefix(t *testing.T) {
ci.Parallel(t)
srv, client, url := testServer(t, true, nil)
defer srv.Shutdown()
testutil.WaitForResult(func() (bool, error) {
nodes, _, err := client.Nodes().List(nil)
if err != nil {
return false, err
}
if len(nodes) == 0 {
return false, fmt.Errorf("missing node")
}
if _, ok := nodes[0].Drivers["mock_driver"]; !ok {
return false, fmt.Errorf("mock_driver not ready")
}
return true, nil
}, func(err error) {
require.NoError(t, err)
})

j1 := testJob("periodic-prefix")
j1.Periodic = &api.PeriodicConfig{
SpecType: pointer.Of(api.PeriodicSpecCron),
Spec: pointer.Of("*/15 * * * * *"),
ProhibitOverlap: pointer.Of(true),
TimeZone: pointer.Of("Europe/Minsk"),
}
j2 := testJob("periodic-prefix-another-job")
j2.Periodic = &api.PeriodicConfig{
SpecType: pointer.Of(api.PeriodicSpecCron),
Spec: pointer.Of("*/15 * * * * *"),
ProhibitOverlap: pointer.Of(true),
TimeZone: pointer.Of("Europe/Minsk"),
}

ui := cli.NewMockUi()
cmd := &JobPeriodicForceCommand{Meta: Meta{Ui: ui, flagAddress: url}}

_, _, err := client.Jobs().Register(j1, nil)
require.NoError(t, err)
_, _, err = client.Jobs().Register(j2, nil)
require.NoError(t, err)

code := cmd.Run([]string{"-address=" + url, "periodic-prefix"})
require.Equal(t, 0, code, "expected no error code")
out := ui.OutputWriter.String()
require.Contains(t, out, "Monitoring evaluation")
require.Contains(t, out, "finished with status \"complete\"")
}

0 comments on commit b665da6

Please sign in to comment.