17217: Remove crufty test util funcs.
[arvados.git] / services / crunch-dispatch-slurm / crunch-dispatch-slurm_test.go
index a823755379574155420e465b2ec2b72f234a3024..cf83257dad5a86d77826f0cd07154389bc40efc5 100644 (file)
@@ -7,10 +7,10 @@ package main
 import (
        "bytes"
        "context"
+       "errors"
        "fmt"
        "io"
        "io/ioutil"
-       "log"
        "net/http"
        "net/http/httptest"
        "os"
@@ -19,10 +19,12 @@ 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"
+       "git.arvados.org/arvados.git/lib/dispatchcloud"
+       "git.arvados.org/arvados.git/sdk/go/arvados"
+       "git.arvados.org/arvados.git/sdk/go/arvadosclient"
+       "git.arvados.org/arvados.git/sdk/go/arvadostest"
+       "git.arvados.org/arvados.git/sdk/go/dispatch"
+       "github.com/sirupsen/logrus"
        . "gopkg.in/check.v1"
 )
 
@@ -31,84 +33,83 @@ func Test(t *testing.T) {
        TestingT(t)
 }
 
-var _ = Suite(&TestSuite{})
-var _ = Suite(&MockArvadosServerSuite{})
+var _ = Suite(&IntegrationSuite{})
+var _ = Suite(&StubbedSuite{})
 
-type TestSuite struct{}
-type MockArvadosServerSuite struct{}
-
-var initialArgs []string
-
-func (s *TestSuite) SetUpSuite(c *C) {
-       initialArgs = os.Args
-}
-
-func (s *TestSuite) TearDownSuite(c *C) {
+type IntegrationSuite struct {
+       disp  Dispatcher
+       slurm slurmFake
 }
 
-func (s *TestSuite) SetUpTest(c *C) {
-       args := []string{"crunch-dispatch-slurm"}
-       os.Args = args
-
-       arvadostest.StartAPI()
+func (s *IntegrationSuite) SetUpTest(c *C) {
+       arvadostest.ResetEnv()
+       arvadostest.ResetDB(c)
        os.Setenv("ARVADOS_API_TOKEN", arvadostest.Dispatch1Token)
+       s.disp = Dispatcher{}
+       s.disp.cluster = &arvados.Cluster{}
+       s.disp.setup()
+       s.slurm = slurmFake{}
 }
 
-func (s *TestSuite) TearDownTest(c *C) {
-       os.Args = initialArgs
+func (s *IntegrationSuite) TearDownTest(c *C) {
        arvadostest.ResetEnv()
-       arvadostest.StopAPI()
+       arvadostest.ResetDB(c)
 }
 
-func (s *MockArvadosServerSuite) TearDownTest(c *C) {
-       arvadostest.ResetEnv()
+type slurmFake struct {
+       didBatch      [][]string
+       didCancel     []string
+       didRelease    []string
+       didRenice     [][]string
+       queue         string
+       rejectNice10K bool
+       // If non-nil, run this func during the 2nd+ call to Cancel()
+       onCancel func()
+       // Error returned by Batch()
+       errBatch error
 }
 
-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()
-
-       arv, err := arvadosclient.MakeArvadosClient()
-       c.Assert(err, IsNil)
+func (sf *slurmFake) Batch(script io.Reader, args []string) error {
+       sf.didBatch = append(sf.didBatch, args)
+       return sf.errBatch
+}
 
-       var sbatchCmdLine []string
+func (sf *slurmFake) QueueCommand(args []string) *exec.Cmd {
+       return exec.Command("echo", sf.queue)
+}
 
-       // Override sbatchCmd
-       defer func(orig func(arvados.Container) *exec.Cmd) {
-               sbatchCmd = orig
-       }(sbatchCmd)
+func (sf *slurmFake) Release(name string) error {
+       sf.didRelease = append(sf.didRelease, name)
+       return nil
+}
 
-       if newSbatchCmd != nil {
-               sbatchCmd = newSbatchCmd
-       } else {
-               sbatchCmd = func(container arvados.Container) *exec.Cmd {
-                       sbatchCmdLine = sbatchFunc(container).Args
-                       return exec.Command("sh")
-               }
+func (sf *slurmFake) Renice(name string, nice int64) error {
+       sf.didRenice = append(sf.didRenice, []string{name, fmt.Sprintf("%d", nice)})
+       if sf.rejectNice10K && nice > 10000 {
+               return errors.New("scontrol: error: Invalid nice value, must be between -10000 and 10000")
        }
+       return nil
+}
 
-       // Override squeueCmd
-       defer func(orig func() *exec.Cmd) {
-               squeueCmd = orig
-       }(squeueCmd)
-       squeueCmd = newSqueueCmd
+func (sf *slurmFake) Cancel(name string) error {
+       sf.didCancel = append(sf.didCancel, name)
+       if len(sf.didCancel) == 1 {
+               // simulate error on first attempt
+               return errors.New("something terrible happened")
+       }
+       if sf.onCancel != nil {
+               sf.onCancel()
+       }
+       return nil
+}
 
-       // Override scancel
-       defer func(orig func(arvados.Container) *exec.Cmd) {
-               scancelCmd = orig
-       }(scancelCmd)
-       scancelCmd = newScancelCmd
+func (s *IntegrationSuite) integrationTest(c *C,
+       expectBatch [][]string,
+       runContainer func(*dispatch.Dispatcher, arvados.Container)) (arvados.Container, error) {
+       arvadostest.ResetEnv()
 
-       // Override scontrol
-       defer func(orig func(arvados.Container) *exec.Cmd) {
-               scontrolCmd = orig
-       }(scontrolCmd)
-       scontrolCmd = newScontrolCmd
+       arv, err := arvadosclient.MakeArvadosClient()
+       c.Assert(err, IsNil)
 
        // There should be one queued container
        params := arvadosclient.Dict{
@@ -117,35 +118,45 @@ func (s *TestSuite) integrationTest(c *C,
        var containers arvados.ContainerList
        err = arv.List("containers", params, &containers)
        c.Check(err, IsNil)
-       c.Check(len(containers.Items), Equals, 1)
+       c.Assert(len(containers.Items), Equals, 1)
 
-       theConfig.CrunchRunCommand = []string{"echo"}
+       s.disp.cluster.Containers.CrunchRunCommand = "echo"
 
        ctx, cancel := context.WithCancel(context.Background())
        doneRun := make(chan struct{})
+       doneDispatch := make(chan error)
 
-       dispatcher := dispatch.Dispatcher{
+       s.disp.Dispatcher = &dispatch.Dispatcher{
                Arv:        arv,
-               PollPeriod: time.Duration(1) * time.Second,
-               RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) {
+               PollPeriod: time.Second,
+               RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) error {
                        go func() {
                                runContainer(disp, ctr)
+                               s.slurm.queue = ""
                                doneRun <- struct{}{}
                        }()
-                       run(disp, ctr, status)
+                       err := s.disp.runContainer(disp, ctr, status)
                        cancel()
+                       doneDispatch <- err
+                       return nil
                },
        }
 
-       sqCheck = &SqueueChecker{Period: 500 * time.Millisecond}
+       s.disp.slurm = &s.slurm
+       s.disp.sqCheck = &SqueueChecker{
+               Logger: logrus.StandardLogger(),
+               Period: 500 * time.Millisecond,
+               Slurm:  s.disp.slurm,
+       }
 
-       err = dispatcher.Run(ctx)
+       err = s.disp.Dispatcher.Run(ctx)
        <-doneRun
        c.Assert(err, Equals, context.Canceled)
+       errDispatch := <-doneDispatch
 
-       sqCheck.Stop()
+       s.disp.sqCheck.Stop()
 
-       c.Check(sbatchCmdLine, DeepEquals, sbatchCmdComps)
+       c.Check(s.slurm.didBatch, DeepEquals, expectBatch)
 
        // There should be no queued containers now
        err = arv.List("containers", params, &containers)
@@ -156,81 +167,52 @@ func (s *TestSuite) integrationTest(c *C,
        var container arvados.Container
        err = arv.Get("containers", "zzzzz-dz642-queuedcontainer", nil, &container)
        c.Check(err, IsNil)
-       return container
+       return container, errDispatch
 }
 
-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,
+func (s *IntegrationSuite) TestNormal(c *C) {
+       s.slurm = slurmFake{queue: "zzzzz-dz642-queuedcontainer 10000 100 PENDING Resources\n"}
+       container, _ := s.integrationTest(c,
                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,
+func (s *IntegrationSuite) TestCancel(c *C) {
+       s.slurm = slurmFake{queue: "zzzzz-dz642-queuedcontainer 10000 100 PENDING Resources\n"}
+       readyToCancel := make(chan bool)
+       s.slurm.onCancel = func() { <-readyToCancel }
+       container, _ := s.integrationTest(c,
                nil,
-               []string(nil),
                func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
                        dispatcher.UpdateState(container.UUID, dispatch.Running)
-                       time.Sleep(1 * time.Second)
+                       time.Sleep(time.Second)
                        dispatcher.Arv.Update("containers", container.UUID,
                                arvadosclient.Dict{
                                        "container": arvadosclient.Dict{"priority": 0}},
                                nil)
+                       readyToCancel <- true
+                       close(readyToCancel)
                })
        c.Check(container.State, Equals, arvados.ContainerStateCancelled)
-       c.Check(scancelCmdLine, DeepEquals, []string{"scancel", "--name=zzzzz-dz642-queuedcontainer"})
+       c.Check(len(s.slurm.didCancel) > 1, Equals, true)
+       c.Check(s.slurm.didCancel[:2], DeepEquals, []string{"zzzzz-dz642-queuedcontainer", "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",
+func (s *IntegrationSuite) TestMissingFromSqueue(c *C) {
+       container, _ := s.integrationTest(c,
+               [][]string{{
                        fmt.Sprintf("--job-name=%s", "zzzzz-dz642-queuedcontainer"),
+                       fmt.Sprintf("--nice=%d", 10000),
+                       "--no-requeue",
                        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)
@@ -239,41 +221,37 @@ func (s *TestSuite) TestIntegrationMissingFromSqueue(c *C) {
        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 (s *IntegrationSuite) TestSbatchFail(c *C) {
+       s.slurm = slurmFake{errBatch: errors.New("something terrible happened")}
+       container, err := s.integrationTest(c,
+               [][]string{{"--job-name=zzzzz-dz642-queuedcontainer", "--nice=10000", "--no-requeue", "--mem=11445", "--cpus-per-task=4", "--tmp=45777"}},
                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)
+       c.Check(err, ErrorMatches, `something terrible happened`)
+}
 
-       arv, err := arvadosclient.MakeArvadosClient()
-       c.Assert(err, IsNil)
+type StubbedSuite struct {
+       disp Dispatcher
+}
 
-       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 *StubbedSuite) SetUpTest(c *C) {
+       s.disp = Dispatcher{}
+       s.disp.cluster = &arvados.Cluster{}
+       s.disp.setup()
 }
 
-func (s *MockArvadosServerSuite) TestAPIErrorGettingContainers(c *C) {
+func (s *StubbedSuite) TestAPIErrorGettingContainers(c *C) {
        apiStubResponses := make(map[string]arvadostest.StubResponse)
        apiStubResponses["/arvados/v1/api_client_authorizations/current"] = arvadostest.StubResponse{200, `{"uuid":"` + arvadostest.Dispatch1AuthUUID + `"}`}
        apiStubResponses["/arvados/v1/containers"] = arvadostest.StubResponse{500, string(`{}`)}
 
-       testWithServerStub(c, apiStubResponses, "echo", "Error getting list of containers")
+       s.testWithServerStub(c, apiStubResponses, "echo", "error getting count of containers")
 }
 
-func testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubResponse, crunchCmd string, expected string) {
+func (s *StubbedSuite) testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubResponse, crunchCmd string, expected string) {
        apiStub := arvadostest.ServerStub{apiStubResponses}
 
        api := httptest.NewServer(&apiStub)
@@ -288,23 +266,24 @@ func testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubRespon
        }
 
        buf := bytes.NewBuffer(nil)
-       log.SetOutput(io.MultiWriter(buf, os.Stderr))
-       defer log.SetOutput(os.Stderr)
+       logrus.SetOutput(io.MultiWriter(buf, os.Stderr))
+       defer logrus.SetOutput(os.Stderr)
 
-       theConfig.CrunchRunCommand = []string{crunchCmd}
+       s.disp.cluster.Containers.CrunchRunCommand = "crunchCmd"
 
        ctx, cancel := context.WithCancel(context.Background())
        dispatcher := dispatch.Dispatcher{
                Arv:        arv,
-               PollPeriod: time.Duration(1) * time.Second,
-               RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) {
+               PollPeriod: time.Second,
+               RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) error {
                        go func() {
-                               time.Sleep(1 * time.Second)
+                               time.Sleep(time.Second)
                                disp.UpdateState(ctr.UUID, dispatch.Running)
                                disp.UpdateState(ctr.UUID, dispatch.Complete)
                        }()
-                       run(disp, ctr, status)
+                       s.disp.runContainer(disp, ctr, status)
                        cancel()
+                       return nil
                },
        }
 
@@ -321,137 +300,138 @@ func testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubRespon
        c.Check(buf.String(), Matches, `(?ms).*`+expected+`.*`)
 }
 
-func (s *MockArvadosServerSuite) TestNoSuchConfigFile(c *C) {
-       var config Config
-       err := readConfig(&config, "/nosuchdir89j7879/8hjwr7ojgyy7")
-       c.Assert(err, NotNil)
-}
-
-func (s *MockArvadosServerSuite) TestBadSbatchArgsConfig(c *C) {
-       var config Config
-
-       tmpfile, err := ioutil.TempFile(os.TempDir(), "config")
-       c.Check(err, IsNil)
-       defer os.Remove(tmpfile.Name())
-
-       _, err = tmpfile.Write([]byte(`{"SbatchArguments": "oops this is not a string array"}`))
-       c.Check(err, IsNil)
-
-       err = readConfig(&config, tmpfile.Name())
-       c.Assert(err, NotNil)
-}
-
-func (s *MockArvadosServerSuite) TestNoSuchArgInConfigIgnored(c *C) {
-       var config Config
-
-       tmpfile, err := ioutil.TempFile(os.TempDir(), "config")
-       c.Check(err, IsNil)
-       defer os.Remove(tmpfile.Name())
-
-       _, err = tmpfile.Write([]byte(`{"NoSuchArg": "Nobody loves me, not one tiny hunk."}`))
-       c.Check(err, IsNil)
-
-       err = readConfig(&config, tmpfile.Name())
-       c.Assert(err, IsNil)
-       c.Check(0, Equals, len(config.SbatchArguments))
-}
-
-func (s *MockArvadosServerSuite) TestReadConfig(c *C) {
-       var config Config
-
-       tmpfile, err := ioutil.TempFile(os.TempDir(), "config")
-       c.Check(err, IsNil)
-       defer os.Remove(tmpfile.Name())
-
-       args := []string{"--arg1=v1", "--arg2", "--arg3=v3"}
-       argsS := `{"SbatchArguments": ["--arg1=v1",  "--arg2", "--arg3=v3"]}`
-       _, err = tmpfile.Write([]byte(argsS))
-       c.Check(err, IsNil)
-
-       err = readConfig(&config, tmpfile.Name())
-       c.Assert(err, IsNil)
-       c.Check(3, Equals, len(config.SbatchArguments))
-       c.Check(args, DeepEquals, config.SbatchArguments)
-}
-
-func (s *MockArvadosServerSuite) TestSbatchFuncWithNoConfigArgs(c *C) {
-       testSbatchFuncWithArgs(c, nil)
-}
-
-func (s *MockArvadosServerSuite) TestSbatchFuncWithEmptyConfigArgs(c *C) {
-       testSbatchFuncWithArgs(c, []string{})
-}
+func (s *StubbedSuite) TestSbatchArgs(c *C) {
+       container := arvados.Container{
+               UUID:               "123",
+               RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 2},
+               Priority:           1,
+       }
 
-func (s *MockArvadosServerSuite) TestSbatchFuncWithConfigArgs(c *C) {
-       testSbatchFuncWithArgs(c, []string{"--arg1=v1", "--arg2"})
+       for _, defaults := range [][]string{
+               nil,
+               {},
+               {"--arg1=v1", "--arg2"},
+       } {
+               c.Logf("%#v", defaults)
+               s.disp.cluster.Containers.SLURM.SbatchArgumentsList = defaults
+
+               args, err := s.disp.sbatchArgs(container)
+               c.Check(args, DeepEquals, append(defaults, "--job-name=123", "--nice=10000", "--no-requeue", "--mem=239", "--cpus-per-task=2", "--tmp=0"))
+               c.Check(err, IsNil)
+       }
 }
 
-func testSbatchFuncWithArgs(c *C, args []string) {
-       theConfig.SbatchArguments = append(theConfig.SbatchArguments, args...)
-
+func (s *StubbedSuite) TestSbatchInstanceTypeConstraint(c *C) {
        container := arvados.Container{
                UUID:               "123",
                RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 2},
-               Priority:           1}
-       sbatchCmd := sbatchFunc(container)
-
-       var expected []string
-       expected = append(expected, "sbatch")
-       expected = append(expected, theConfig.SbatchArguments...)
-       expected = append(expected, "--job-name=123", "--mem=239", "--cpus-per-task=2", "--tmp=0", "--nice=9990")
+               Priority:           1,
+       }
 
-       c.Check(sbatchCmd.Args, DeepEquals, expected)
+       for _, trial := range []struct {
+               types      map[string]arvados.InstanceType
+               sbatchArgs []string
+               err        error
+       }{
+               // Choose node type => use --constraint arg
+               {
+                       types: map[string]arvados.InstanceType{
+                               "a1.tiny":   {Name: "a1.tiny", Price: 0.02, RAM: 128000000, VCPUs: 1},
+                               "a1.small":  {Name: "a1.small", Price: 0.04, RAM: 256000000, VCPUs: 2},
+                               "a1.medium": {Name: "a1.medium", Price: 0.08, RAM: 512000000, VCPUs: 4},
+                               "a1.large":  {Name: "a1.large", Price: 0.16, RAM: 1024000000, VCPUs: 8},
+                       },
+                       sbatchArgs: []string{"--constraint=instancetype=a1.medium"},
+               },
+               // No node types configured => no slurm constraint
+               {
+                       types:      nil,
+                       sbatchArgs: []string{"--mem=239", "--cpus-per-task=2", "--tmp=0"},
+               },
+               // No node type is big enough => error
+               {
+                       types: map[string]arvados.InstanceType{
+                               "a1.tiny": {Name: "a1.tiny", Price: 0.02, RAM: 128000000, VCPUs: 1},
+                       },
+                       err: dispatchcloud.ConstraintsNotSatisfiableError{},
+               },
+       } {
+               c.Logf("%#v", trial)
+               s.disp.cluster = &arvados.Cluster{InstanceTypes: trial.types}
+
+               args, err := s.disp.sbatchArgs(container)
+               c.Check(err == nil, Equals, trial.err == nil)
+               if trial.err == nil {
+                       c.Check(args, DeepEquals, append([]string{"--job-name=123", "--nice=10000", "--no-requeue"}, trial.sbatchArgs...))
+               } else {
+                       c.Check(len(err.(dispatchcloud.ConstraintsNotSatisfiableError).AvailableTypes), Equals, len(trial.types))
+               }
+       }
 }
 
-func (s *MockArvadosServerSuite) TestSbatchPartition(c *C) {
-       theConfig.SbatchArguments = nil
+func (s *StubbedSuite) TestSbatchPartition(c *C) {
        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")
+               Priority:             1,
+       }
 
-       c.Check(sbatchCmd.Args, DeepEquals, expected)
+       args, err := s.disp.sbatchArgs(container)
+       c.Check(args, DeepEquals, []string{
+               "--job-name=123", "--nice=10000", "--no-requeue",
+               "--mem=239", "--cpus-per-task=1", "--tmp=0",
+               "--partition=blurb,b2",
+       })
+       c.Check(err, IsNil)
 }
 
-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"})
+func (s *StubbedSuite) TestLoadLegacyConfig(c *C) {
+       content := []byte(`
+Client:
+  APIHost: example.com
+  AuthToken: abcdefg
+  KeepServiceURIs:
+    - https://example.com/keep1
+    - https://example.com/keep2
+SbatchArguments: ["--foo", "bar"]
+PollPeriod: 12s
+PrioritySpread: 42
+CrunchRunCommand: ["x-crunch-run", "--cgroup-parent-subsystem=memory"]
+ReserveExtraRAM: 12345
+MinRetryPeriod: 13s
+BatchSize: 99
+`)
+       tmpfile, err := ioutil.TempFile("", "example")
+       if err != nil {
+               c.Error(err)
+       }
+
+       defer os.Remove(tmpfile.Name()) // clean up
+
+       if _, err := tmpfile.Write(content); err != nil {
+               c.Error(err)
+       }
+       if err := tmpfile.Close(); err != nil {
+               c.Error(err)
+
+       }
+       os.Setenv("ARVADOS_KEEP_SERVICES", "")
+       err = s.disp.configure("crunch-dispatch-slurm", []string{"-config", tmpfile.Name()})
+       c.Check(err, IsNil)
+
+       c.Check(s.disp.cluster.Services.Controller.ExternalURL, Equals, arvados.URL{Scheme: "https", Host: "example.com", Path: "/"})
+       c.Check(s.disp.cluster.SystemRootToken, Equals, "abcdefg")
+       c.Check(s.disp.cluster.Containers.SLURM.SbatchArgumentsList, DeepEquals, []string{"--foo", "bar"})
+       c.Check(s.disp.cluster.Containers.CloudVMs.PollInterval, Equals, arvados.Duration(12*time.Second))
+       c.Check(s.disp.cluster.Containers.SLURM.PrioritySpread, Equals, int64(42))
+       c.Check(s.disp.cluster.Containers.CrunchRunCommand, Equals, "x-crunch-run")
+       c.Check(s.disp.cluster.Containers.CrunchRunArgumentsList, DeepEquals, []string{"--cgroup-parent-subsystem=memory"})
+       c.Check(s.disp.cluster.Containers.ReserveExtraRAM, Equals, arvados.ByteSize(12345))
+       c.Check(s.disp.cluster.Containers.MinRetryPeriod, Equals, arvados.Duration(13*time.Second))
+       c.Check(s.disp.cluster.API.MaxItemsPerResponse, Equals, 99)
+       c.Check(s.disp.cluster.Containers.SLURM.SbatchEnvironmentVariables, DeepEquals, map[string]string{
+               "ARVADOS_KEEP_SERVICES": "https://example.com/keep1 https://example.com/keep2",
+       })
+       c.Check(os.Getenv("ARVADOS_KEEP_SERVICES"), Equals, "https://example.com/keep1 https://example.com/keep2")
 }