15467: Added tests for KeepServices
[arvados.git] / services / crunch-dispatch-slurm / crunch-dispatch-slurm_test.go
index b76ece314d47806afcfb328ba12970b9171b58d5..63117128eb594e5c492fbe2e93e84c5f77672b1f 100644 (file)
@@ -24,7 +24,7 @@ import (
        "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
        "git.curoverse.com/arvados.git/sdk/go/arvadostest"
        "git.curoverse.com/arvados.git/sdk/go/dispatch"
-       "github.com/Sirupsen/logrus"
+       "github.com/sirupsen/logrus"
        . "gopkg.in/check.v1"
 )
 
@@ -45,6 +45,7 @@ func (s *IntegrationSuite) SetUpTest(c *C) {
        arvadostest.StartAPI()
        os.Setenv("ARVADOS_API_TOKEN", arvadostest.Dispatch1Token)
        s.disp = Dispatcher{}
+       s.disp.cluster = &arvados.Cluster{}
        s.disp.setup()
        s.slurm = slurmFake{}
 }
@@ -118,7 +119,7 @@ func (s *IntegrationSuite) integrationTest(c *C,
        c.Check(err, IsNil)
        c.Assert(len(containers.Items), Equals, 1)
 
-       s.disp.CrunchRunCommand = []string{"echo"}
+       s.disp.cluster.Containers.CrunchRunCommand = "echo"
 
        ctx, cancel := context.WithCancel(context.Background())
        doneRun := make(chan struct{})
@@ -202,6 +203,7 @@ func (s *IntegrationSuite) TestMissingFromSqueue(c *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),
@@ -217,7 +219,7 @@ func (s *IntegrationSuite) TestMissingFromSqueue(c *C) {
 func (s *IntegrationSuite) TestSbatchFail(c *C) {
        s.slurm = slurmFake{errBatch: errors.New("something terrible happened")}
        container := s.integrationTest(c,
-               [][]string{{"--job-name=zzzzz-dz642-queuedcontainer", "--nice=10000", "--mem=11445", "--cpus-per-task=4", "--tmp=45777"}},
+               [][]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)
@@ -242,6 +244,7 @@ type StubbedSuite struct {
 
 func (s *StubbedSuite) SetUpTest(c *C) {
        s.disp = Dispatcher{}
+       s.disp.cluster = &arvados.Cluster{}
        s.disp.setup()
 }
 
@@ -271,7 +274,7 @@ func (s *StubbedSuite) testWithServerStub(c *C, apiStubResponses map[string]arva
        logrus.SetOutput(io.MultiWriter(buf, os.Stderr))
        defer logrus.SetOutput(os.Stderr)
 
-       s.disp.CrunchRunCommand = []string{crunchCmd}
+       s.disp.cluster.Containers.CrunchRunCommand = "crunchCmd"
 
        ctx, cancel := context.WithCancel(context.Background())
        dispatcher := dispatch.Dispatcher{
@@ -301,51 +304,6 @@ func (s *StubbedSuite) testWithServerStub(c *C, apiStubResponses map[string]arva
        c.Check(buf.String(), Matches, `(?ms).*`+expected+`.*`)
 }
 
-func (s *StubbedSuite) TestNoSuchConfigFile(c *C) {
-       err := s.disp.readConfig("/nosuchdir89j7879/8hjwr7ojgyy7")
-       c.Assert(err, NotNil)
-}
-
-func (s *StubbedSuite) TestBadSbatchArgsConfig(c *C) {
-       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 = s.disp.readConfig(tmpfile.Name())
-       c.Assert(err, NotNil)
-}
-
-func (s *StubbedSuite) TestNoSuchArgInConfigIgnored(c *C) {
-       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 = s.disp.readConfig(tmpfile.Name())
-       c.Assert(err, IsNil)
-       c.Check(0, Equals, len(s.disp.SbatchArguments))
-}
-
-func (s *StubbedSuite) TestReadConfig(c *C) {
-       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 = s.disp.readConfig(tmpfile.Name())
-       c.Assert(err, IsNil)
-       c.Check(args, DeepEquals, s.disp.SbatchArguments)
-}
-
 func (s *StubbedSuite) TestSbatchArgs(c *C) {
        container := arvados.Container{
                UUID:               "123",
@@ -359,10 +317,10 @@ func (s *StubbedSuite) TestSbatchArgs(c *C) {
                {"--arg1=v1", "--arg2"},
        } {
                c.Logf("%#v", defaults)
-               s.disp.SbatchArguments = 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", "--mem=239", "--cpus-per-task=2", "--tmp=0"))
+               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)
        }
 }
@@ -408,7 +366,7 @@ func (s *StubbedSuite) TestSbatchInstanceTypeConstraint(c *C) {
                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"}, trial.sbatchArgs...))
+                       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))
                }
@@ -425,9 +383,66 @@ func (s *StubbedSuite) TestSbatchPartition(c *C) {
 
        args, err := s.disp.sbatchArgs(container)
        c.Check(args, DeepEquals, []string{
-               "--job-name=123", "--nice=10000",
+               "--job-name=123", "--nice=10000", "--no-requeue",
                "--mem=239", "--cpus-per-task=1", "--tmp=0",
                "--partition=blurb,b2",
        })
        c.Check(err, IsNil)
 }
+
+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)
+
+       }
+       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"})
+       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.KeepServices, DeepEquals, map[string]arvados.Service{
+               "00000-bi6l4-000000000000000": arvados.Service{
+                       InternalURLs: map[arvados.URL]arvados.ServiceInstance{
+                               arvados.URL{Scheme: "https", Path: "/keep1", Host: "example.com"}: struct{}{},
+                               arvados.URL{Scheme: "https", Path: "/keep2", Host: "example.com"}: struct{}{},
+                       },
+               },
+       })
+       ks := os.Getenv("ARVADOS_KEEP_SERVICES")
+       if ks != "https://example.com/keep1 https://example.com/keep2" && ks != "https://example.com/keep2 https://example.com/keep1" {
+               c.Assert(ks, Equals, "https://example.com/keep1 https://example.com/keep2")
+       }
+}