From b646810401ab2004496cf38091d8c1bfb1a8a1a5 Mon Sep 17 00:00:00 2001 From: Vladimir Sokolov Date: Fri, 26 Aug 2022 17:54:37 +0300 Subject: [PATCH] cli: force periodic job if its id equals search prefix --- .changelog/14333.txt | 3 ++ command/job_periodic_force.go | 4 ++- command/job_periodic_force_test.go | 50 ++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 .changelog/14333.txt diff --git a/.changelog/14333.txt b/.changelog/14333.txt new file mode 100644 index 000000000..7d9d69f69 --- /dev/null +++ b/.changelog/14333.txt @@ -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 +``` diff --git a/command/job_periodic_force.go b/command/job_periodic_force.go index 2e5a14e4d..2e074c0f9 100644 --- a/command/job_periodic_force.go +++ b/command/job_periodic_force.go @@ -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 } diff --git a/command/job_periodic_force_test.go b/command/job_periodic_force_test.go index bbae868b2..efc40c8f6 100644 --- a/command/job_periodic_force_test.go +++ b/command/job_periodic_force_test.go @@ -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\"") +}