X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/7314917d65573b0e9d55f7b6522463c470356fba..47e59a35d5ed9b2cdb052894d741972324058505:/services/crunch-dispatch-slurm/crunch-dispatch-slurm_test.go diff --git a/services/crunch-dispatch-slurm/crunch-dispatch-slurm_test.go b/services/crunch-dispatch-slurm/crunch-dispatch-slurm_test.go index e7ccf25fc9..a823755379 100644 --- a/services/crunch-dispatch-slurm/crunch-dispatch-slurm_test.go +++ b/services/crunch-dispatch-slurm/crunch-dispatch-slurm_test.go @@ -1,12 +1,13 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + package main import ( "bytes" + "context" "fmt" - "git.curoverse.com/arvados.git/sdk/go/arvados" - "git.curoverse.com/arvados.git/sdk/go/arvadosclient" - "git.curoverse.com/arvados.git/sdk/go/arvadostest" - "git.curoverse.com/arvados.git/sdk/go/dispatch" "io" "io/ioutil" "log" @@ -18,6 +19,10 @@ import ( "testing" "time" + "git.curoverse.com/arvados.git/sdk/go/arvados" + "git.curoverse.com/arvados.git/sdk/go/arvadosclient" + "git.curoverse.com/arvados.git/sdk/go/arvadostest" + "git.curoverse.com/arvados.git/sdk/go/dispatch" . "gopkg.in/check.v1" ) @@ -51,6 +56,7 @@ func (s *TestSuite) SetUpTest(c *C) { func (s *TestSuite) TearDownTest(c *C) { os.Args = initialArgs + arvadostest.ResetEnv() arvadostest.StopAPI() } @@ -58,58 +64,11 @@ func (s *MockArvadosServerSuite) TearDownTest(c *C) { arvadostest.ResetEnv() } -func (s *TestSuite) TestIntegrationNormal(c *C) { - container := s.integrationTest(c, func() *exec.Cmd { return exec.Command("echo", "zzzzz-dz642-queuedcontainer") }, - []string(nil), - func(dispatcher *dispatch.Dispatcher, container arvados.Container) { - dispatcher.UpdateState(container.UUID, dispatch.Running) - time.Sleep(3 * time.Second) - dispatcher.UpdateState(container.UUID, dispatch.Complete) - }) - c.Check(container.State, Equals, arvados.ContainerStateComplete) -} - -func (s *TestSuite) TestIntegrationCancel(c *C) { - - // Override sbatchCmd - var scancelCmdLine []string - defer func(orig func(arvados.Container) *exec.Cmd) { - scancelCmd = orig - }(scancelCmd) - scancelCmd = func(container arvados.Container) *exec.Cmd { - scancelCmdLine = scancelFunc(container).Args - return exec.Command("echo") - } - - container := s.integrationTest(c, func() *exec.Cmd { return exec.Command("echo", "zzzzz-dz642-queuedcontainer") }, - []string(nil), - func(dispatcher *dispatch.Dispatcher, container arvados.Container) { - dispatcher.UpdateState(container.UUID, dispatch.Running) - time.Sleep(1 * time.Second) - dispatcher.Arv.Update("containers", container.UUID, - arvadosclient.Dict{ - "container": arvadosclient.Dict{"priority": 0}}, - nil) - }) - c.Check(container.State, Equals, arvados.ContainerStateCancelled) - c.Check(scancelCmdLine, DeepEquals, []string{"scancel", "--name=zzzzz-dz642-queuedcontainer"}) -} - -func (s *TestSuite) TestIntegrationMissingFromSqueue(c *C) { - container := s.integrationTest(c, func() *exec.Cmd { return exec.Command("echo") }, []string{"sbatch", "--share", - fmt.Sprintf("--job-name=%s", "zzzzz-dz642-queuedcontainer"), - fmt.Sprintf("--mem-per-cpu=%d", 2862), - fmt.Sprintf("--cpus-per-task=%d", 4)}, - func(dispatcher *dispatch.Dispatcher, container arvados.Container) { - dispatcher.UpdateState(container.UUID, dispatch.Running) - time.Sleep(3 * time.Second) - dispatcher.UpdateState(container.UUID, dispatch.Complete) - }) - c.Check(container.State, Equals, arvados.ContainerStateCancelled) -} - func (s *TestSuite) integrationTest(c *C, newSqueueCmd func() *exec.Cmd, + newScancelCmd func(arvados.Container) *exec.Cmd, + newSbatchCmd func(arvados.Container) *exec.Cmd, + newScontrolCmd func(arvados.Container) *exec.Cmd, sbatchCmdComps []string, runContainer func(*dispatch.Dispatcher, arvados.Container)) arvados.Container { arvadostest.ResetEnv() @@ -123,9 +82,14 @@ func (s *TestSuite) integrationTest(c *C, defer func(orig func(arvados.Container) *exec.Cmd) { sbatchCmd = orig }(sbatchCmd) - sbatchCmd = func(container arvados.Container) *exec.Cmd { - sbatchCmdLine = sbatchFunc(container).Args - return exec.Command("sh") + + if newSbatchCmd != nil { + sbatchCmd = newSbatchCmd + } else { + sbatchCmd = func(container arvados.Container) *exec.Cmd { + sbatchCmdLine = sbatchFunc(container).Args + return exec.Command("sh") + } } // Override squeueCmd @@ -134,7 +98,19 @@ func (s *TestSuite) integrationTest(c *C, }(squeueCmd) squeueCmd = newSqueueCmd - // There should be no queued containers now + // Override scancel + defer func(orig func(arvados.Container) *exec.Cmd) { + scancelCmd = orig + }(scancelCmd) + scancelCmd = newScancelCmd + + // Override scontrol + defer func(orig func(arvados.Container) *exec.Cmd) { + scontrolCmd = orig + }(scontrolCmd) + scontrolCmd = newScontrolCmd + + // There should be one queued container params := arvadosclient.Dict{ "filters": [][]string{{"state", "=", "Queued"}}, } @@ -145,25 +121,29 @@ func (s *TestSuite) integrationTest(c *C, theConfig.CrunchRunCommand = []string{"echo"} - doneProcessing := make(chan struct{}) + ctx, cancel := context.WithCancel(context.Background()) + doneRun := make(chan struct{}) + dispatcher := dispatch.Dispatcher{ - Arv: arv, - PollInterval: time.Duration(1) * time.Second, - RunContainer: func(dispatcher *dispatch.Dispatcher, - container arvados.Container, - status chan arvados.Container) { - go runContainer(dispatcher, container) - run(dispatcher, container, status) - doneProcessing <- struct{}{} + Arv: arv, + PollPeriod: time.Duration(1) * time.Second, + RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) { + go func() { + runContainer(disp, ctr) + doneRun <- struct{}{} + }() + run(disp, ctr, status) + cancel() }, - DoneProcessing: doneProcessing} + } - squeueUpdater.StartMonitor(time.Duration(500) * time.Millisecond) + sqCheck = &SqueueChecker{Period: 500 * time.Millisecond} - err = dispatcher.RunDispatcher() - c.Assert(err, IsNil) + err = dispatcher.Run(ctx) + <-doneRun + c.Assert(err, Equals, context.Canceled) - squeueUpdater.Done() + sqCheck.Stop() c.Check(sbatchCmdLine, DeepEquals, sbatchCmdComps) @@ -179,6 +159,112 @@ func (s *TestSuite) integrationTest(c *C, return container } +func (s *TestSuite) TestIntegrationNormal(c *C) { + done := false + container := s.integrationTest(c, + func() *exec.Cmd { + if done { + return exec.Command("true") + } else { + return exec.Command("echo", "zzzzz-dz642-queuedcontainer 9990 100") + } + }, + nil, + nil, + nil, + []string(nil), + func(dispatcher *dispatch.Dispatcher, container arvados.Container) { + dispatcher.UpdateState(container.UUID, dispatch.Running) + time.Sleep(3 * time.Second) + dispatcher.UpdateState(container.UUID, dispatch.Complete) + done = true + }) + c.Check(container.State, Equals, arvados.ContainerStateComplete) +} + +func (s *TestSuite) TestIntegrationCancel(c *C) { + var cmd *exec.Cmd + var scancelCmdLine []string + attempt := 0 + + container := s.integrationTest(c, + func() *exec.Cmd { + if cmd != nil && cmd.ProcessState != nil { + return exec.Command("true") + } else { + return exec.Command("echo", "zzzzz-dz642-queuedcontainer 9990 100") + } + }, + func(container arvados.Container) *exec.Cmd { + if attempt++; attempt == 1 { + return exec.Command("false") + } else { + scancelCmdLine = scancelFunc(container).Args + cmd = exec.Command("echo") + return cmd + } + }, + nil, + nil, + []string(nil), + func(dispatcher *dispatch.Dispatcher, container arvados.Container) { + dispatcher.UpdateState(container.UUID, dispatch.Running) + time.Sleep(1 * time.Second) + dispatcher.Arv.Update("containers", container.UUID, + arvadosclient.Dict{ + "container": arvadosclient.Dict{"priority": 0}}, + nil) + }) + c.Check(container.State, Equals, arvados.ContainerStateCancelled) + c.Check(scancelCmdLine, DeepEquals, []string{"scancel", "--name=zzzzz-dz642-queuedcontainer"}) +} + +func (s *TestSuite) TestIntegrationMissingFromSqueue(c *C) { + container := s.integrationTest(c, + func() *exec.Cmd { return exec.Command("echo") }, + nil, + nil, + nil, + []string{"sbatch", + fmt.Sprintf("--job-name=%s", "zzzzz-dz642-queuedcontainer"), + fmt.Sprintf("--mem=%d", 11445), + fmt.Sprintf("--cpus-per-task=%d", 4), + fmt.Sprintf("--tmp=%d", 45777), + fmt.Sprintf("--nice=%d", 9990)}, + func(dispatcher *dispatch.Dispatcher, container arvados.Container) { + dispatcher.UpdateState(container.UUID, dispatch.Running) + time.Sleep(3 * time.Second) + dispatcher.UpdateState(container.UUID, dispatch.Complete) + }) + c.Check(container.State, Equals, arvados.ContainerStateCancelled) +} + +func (s *TestSuite) TestSbatchFail(c *C) { + container := s.integrationTest(c, + func() *exec.Cmd { return exec.Command("echo") }, + nil, + func(container arvados.Container) *exec.Cmd { + return exec.Command("false") + }, + nil, + []string(nil), + func(dispatcher *dispatch.Dispatcher, container arvados.Container) { + dispatcher.UpdateState(container.UUID, dispatch.Running) + dispatcher.UpdateState(container.UUID, dispatch.Complete) + }) + c.Check(container.State, Equals, arvados.ContainerStateComplete) + + arv, err := arvadosclient.MakeArvadosClient() + c.Assert(err, IsNil) + + var ll arvados.LogList + err = arv.List("logs", arvadosclient.Dict{"filters": [][]string{ + {"object_uuid", "=", container.UUID}, + {"event_type", "=", "dispatch"}, + }}, &ll) + c.Assert(len(ll.Items), Equals, 1) +} + func (s *MockArvadosServerSuite) TestAPIErrorGettingContainers(c *C) { apiStubResponses := make(map[string]arvadostest.StubResponse) apiStubResponses["/arvados/v1/api_client_authorizations/current"] = arvadostest.StubResponse{200, `{"uuid":"` + arvadostest.Dispatch1AuthUUID + `"}`} @@ -207,32 +293,30 @@ func testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubRespon theConfig.CrunchRunCommand = []string{crunchCmd} - doneProcessing := make(chan struct{}) + ctx, cancel := context.WithCancel(context.Background()) dispatcher := dispatch.Dispatcher{ - Arv: arv, - PollInterval: time.Duration(1) * time.Second, - RunContainer: func(dispatcher *dispatch.Dispatcher, - container arvados.Container, - status chan arvados.Container) { + Arv: arv, + PollPeriod: time.Duration(1) * time.Second, + RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) { go func() { time.Sleep(1 * time.Second) - dispatcher.UpdateState(container.UUID, dispatch.Running) - dispatcher.UpdateState(container.UUID, dispatch.Complete) + disp.UpdateState(ctr.UUID, dispatch.Running) + disp.UpdateState(ctr.UUID, dispatch.Complete) }() - run(dispatcher, container, status) - doneProcessing <- struct{}{} + run(disp, ctr, status) + cancel() }, - DoneProcessing: doneProcessing} + } go func() { for i := 0; i < 80 && !strings.Contains(buf.String(), expected); i++ { time.Sleep(100 * time.Millisecond) } - dispatcher.DoneProcessing <- struct{}{} + cancel() }() - err := dispatcher.RunDispatcher() - c.Assert(err, IsNil) + err := dispatcher.Run(ctx) + c.Assert(err, Equals, context.Canceled) c.Check(buf.String(), Matches, `(?ms).*`+expected+`.*`) } @@ -305,13 +389,69 @@ func (s *MockArvadosServerSuite) TestSbatchFuncWithConfigArgs(c *C) { func testSbatchFuncWithArgs(c *C, args []string) { theConfig.SbatchArguments = append(theConfig.SbatchArguments, args...) - container := arvados.Container{UUID: "123", RuntimeConstraints: arvados.RuntimeConstraints{RAM: 1000000, VCPUs: 2}} + container := arvados.Container{ + UUID: "123", + RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 2}, + Priority: 1} sbatchCmd := sbatchFunc(container) var expected []string - expected = append(expected, "sbatch", "--share") + expected = append(expected, "sbatch") expected = append(expected, theConfig.SbatchArguments...) - expected = append(expected, "--job-name=123", "--mem-per-cpu=1", "--cpus-per-task=2") + expected = append(expected, "--job-name=123", "--mem=239", "--cpus-per-task=2", "--tmp=0", "--nice=9990") + + c.Check(sbatchCmd.Args, DeepEquals, expected) +} + +func (s *MockArvadosServerSuite) TestSbatchPartition(c *C) { + theConfig.SbatchArguments = nil + container := arvados.Container{ + UUID: "123", + RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 1}, + SchedulingParameters: arvados.SchedulingParameters{Partitions: []string{"blurb", "b2"}}, + Priority: 1} + sbatchCmd := sbatchFunc(container) + + var expected []string + expected = append(expected, "sbatch") + expected = append(expected, "--job-name=123", "--mem=239", "--cpus-per-task=1", "--tmp=0", "--nice=9990", "--partition=blurb,b2") c.Check(sbatchCmd.Args, DeepEquals, expected) } + +func (s *TestSuite) TestIntegrationChangePriority(c *C) { + var scontrolCmdLine []string + step := 0 + + container := s.integrationTest(c, + func() *exec.Cmd { + if step == 0 { + return exec.Command("echo", "zzzzz-dz642-queuedcontainer 9990 100") + } else if step == 1 { + return exec.Command("echo", "zzzzz-dz642-queuedcontainer 4000 100") + } else { + return exec.Command("echo") + } + }, + func(arvados.Container) *exec.Cmd { return exec.Command("true") }, + nil, + func(container arvados.Container) *exec.Cmd { + scontrolCmdLine = scontrolFunc(container).Args + step = 1 + return exec.Command("true") + }, + []string(nil), + func(dispatcher *dispatch.Dispatcher, container arvados.Container) { + dispatcher.UpdateState(container.UUID, dispatch.Running) + time.Sleep(1 * time.Second) + dispatcher.Arv.Update("containers", container.UUID, + arvadosclient.Dict{ + "container": arvadosclient.Dict{"priority": 600}}, + nil) + time.Sleep(1 * time.Second) + step = 2 + dispatcher.UpdateState(container.UUID, dispatch.Complete) + }) + c.Check(container.State, Equals, arvados.ContainerStateComplete) + c.Check(scontrolCmdLine, DeepEquals, []string{"scontrol", "update", "JobName=zzzzz-dz642-queuedcontainer", "Nice=4000"}) +}