cli: force periodic job if its id equals search prefix

This commit is contained in:
Vladimir Sokolov 2022-08-26 17:54:37 +03:00 committed by GitHub
parent b87689d2d1
commit b646810401
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 1 deletions

3
.changelog/14333.txt Normal file
View File

@ -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
```

View File

@ -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
}

View File

@ -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\"")
}