1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
22 "git.arvados.org/arvados.git/lib/dispatchcloud"
23 "git.arvados.org/arvados.git/sdk/go/arvados"
24 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
25 "git.arvados.org/arvados.git/sdk/go/arvadostest"
26 "git.arvados.org/arvados.git/sdk/go/dispatch"
27 "github.com/sirupsen/logrus"
31 // Gocheck boilerplate
32 func Test(t *testing.T) {
36 var _ = Suite(&IntegrationSuite{})
37 var _ = Suite(&StubbedSuite{})
39 type IntegrationSuite struct {
44 func (s *IntegrationSuite) SetUpTest(c *C) {
45 arvadostest.ResetEnv()
46 arvadostest.ResetDB(c)
47 os.Setenv("ARVADOS_API_TOKEN", arvadostest.Dispatch1Token)
49 s.disp.cluster = &arvados.Cluster{}
54 func (s *IntegrationSuite) TearDownTest(c *C) {
55 arvadostest.ResetEnv()
56 arvadostest.ResetDB(c)
59 type slurmFake struct {
66 // If non-nil, run this func during the 2nd+ call to Cancel()
68 // Error returned by Batch()
72 func (sf *slurmFake) Batch(script io.Reader, args []string) error {
73 sf.didBatch = append(sf.didBatch, args)
77 func (sf *slurmFake) QueueCommand(args []string) *exec.Cmd {
78 return exec.Command("echo", sf.queue)
81 func (sf *slurmFake) Release(name string) error {
82 sf.didRelease = append(sf.didRelease, name)
86 func (sf *slurmFake) Renice(name string, nice int64) error {
87 sf.didRenice = append(sf.didRenice, []string{name, fmt.Sprintf("%d", nice)})
88 if sf.rejectNice10K && nice > 10000 {
89 return errors.New("scontrol: error: Invalid nice value, must be between -10000 and 10000")
94 func (sf *slurmFake) Cancel(name string) error {
95 sf.didCancel = append(sf.didCancel, name)
96 if len(sf.didCancel) == 1 {
97 // simulate error on first attempt
98 return errors.New("something terrible happened")
100 if sf.onCancel != nil {
106 func (s *IntegrationSuite) integrationTest(c *C,
107 expectBatch [][]string,
108 runContainer func(*dispatch.Dispatcher, arvados.Container)) (arvados.Container, error) {
109 arvadostest.ResetEnv()
111 arv, err := arvadosclient.MakeArvadosClient()
114 // There should be one queued container
115 params := arvadosclient.Dict{
116 "filters": [][]string{{"state", "=", "Queued"}},
118 var containers arvados.ContainerList
119 err = arv.List("containers", params, &containers)
121 c.Assert(len(containers.Items), Equals, 1)
123 s.disp.cluster.Containers.CrunchRunCommand = "echo"
125 ctx, cancel := context.WithCancel(context.Background())
126 doneRun := make(chan struct{})
127 doneDispatch := make(chan error)
129 s.disp.Dispatcher = &dispatch.Dispatcher{
131 PollPeriod: time.Second,
132 RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) error {
134 runContainer(disp, ctr)
136 doneRun <- struct{}{}
138 err := s.disp.runContainer(disp, ctr, status)
145 s.disp.slurm = &s.slurm
146 s.disp.sqCheck = &SqueueChecker{
147 Logger: logrus.StandardLogger(),
148 Period: 500 * time.Millisecond,
152 err = s.disp.Dispatcher.Run(ctx)
154 c.Assert(err, Equals, context.Canceled)
155 errDispatch := <-doneDispatch
157 s.disp.sqCheck.Stop()
159 c.Check(s.slurm.didBatch, DeepEquals, expectBatch)
161 // There should be no queued containers now
162 err = arv.List("containers", params, &containers)
164 c.Check(len(containers.Items), Equals, 0)
166 // Previously "Queued" container should now be in "Complete" state
167 var container arvados.Container
168 err = arv.Get("containers", "zzzzz-dz642-queuedcontainer", nil, &container)
170 return container, errDispatch
173 func (s *IntegrationSuite) TestNormal(c *C) {
174 s.slurm = slurmFake{queue: "zzzzz-dz642-queuedcontainer 10000 100 PENDING Resources\n"}
175 container, _ := s.integrationTest(c,
177 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
178 dispatcher.UpdateState(container.UUID, dispatch.Running)
179 time.Sleep(3 * time.Second)
180 dispatcher.UpdateState(container.UUID, dispatch.Complete)
182 c.Check(container.State, Equals, arvados.ContainerStateComplete)
185 func (s *IntegrationSuite) TestCancel(c *C) {
186 s.slurm = slurmFake{queue: "zzzzz-dz642-queuedcontainer 10000 100 PENDING Resources\n"}
187 readyToCancel := make(chan bool)
188 s.slurm.onCancel = func() { <-readyToCancel }
189 container, _ := s.integrationTest(c,
191 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
192 dispatcher.UpdateState(container.UUID, dispatch.Running)
193 time.Sleep(time.Second)
194 dispatcher.Arv.Update("containers", container.UUID,
196 "container": arvadosclient.Dict{"priority": 0}},
198 readyToCancel <- true
201 c.Check(container.State, Equals, arvados.ContainerStateCancelled)
202 c.Check(len(s.slurm.didCancel) > 1, Equals, true)
203 c.Check(s.slurm.didCancel[:2], DeepEquals, []string{"zzzzz-dz642-queuedcontainer", "zzzzz-dz642-queuedcontainer"})
206 func (s *IntegrationSuite) TestMissingFromSqueue(c *C) {
207 container, _ := s.integrationTest(c,
209 fmt.Sprintf("--job-name=%s", "zzzzz-dz642-queuedcontainer"),
210 fmt.Sprintf("--nice=%d", 10000),
212 fmt.Sprintf("--mem=%d", 11445),
213 fmt.Sprintf("--cpus-per-task=%d", 4),
214 fmt.Sprintf("--tmp=%d", 45777),
216 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
217 dispatcher.UpdateState(container.UUID, dispatch.Running)
218 time.Sleep(3 * time.Second)
219 dispatcher.UpdateState(container.UUID, dispatch.Complete)
221 c.Check(container.State, Equals, arvados.ContainerStateCancelled)
224 func (s *IntegrationSuite) TestSbatchFail(c *C) {
225 s.slurm = slurmFake{errBatch: errors.New("something terrible happened")}
226 container, err := s.integrationTest(c,
227 [][]string{{"--job-name=zzzzz-dz642-queuedcontainer", "--nice=10000", "--no-requeue", "--mem=11445", "--cpus-per-task=4", "--tmp=45777"}},
228 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
229 dispatcher.UpdateState(container.UUID, dispatch.Running)
230 dispatcher.UpdateState(container.UUID, dispatch.Complete)
232 c.Check(container.State, Equals, arvados.ContainerStateComplete)
233 c.Check(err, ErrorMatches, `something terrible happened`)
236 type StubbedSuite struct {
240 func (s *StubbedSuite) SetUpTest(c *C) {
241 s.disp = Dispatcher{}
242 s.disp.cluster = &arvados.Cluster{}
246 func (s *StubbedSuite) TestAPIErrorGettingContainers(c *C) {
247 apiStubResponses := make(map[string]arvadostest.StubResponse)
248 apiStubResponses["/arvados/v1/api_client_authorizations/current"] = arvadostest.StubResponse{200, `{"uuid":"` + arvadostest.Dispatch1AuthUUID + `"}`}
249 apiStubResponses["/arvados/v1/containers"] = arvadostest.StubResponse{500, string(`{}`)}
251 s.testWithServerStub(c, apiStubResponses, "echo", "error getting count of containers")
254 func (s *StubbedSuite) testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubResponse, crunchCmd string, expected string) {
255 apiStub := arvadostest.ServerStub{apiStubResponses}
257 api := httptest.NewServer(&apiStub)
260 arv := &arvadosclient.ArvadosClient{
262 ApiServer: api.URL[7:],
264 Client: &http.Client{Transport: &http.Transport{}},
268 buf := bytes.NewBuffer(nil)
269 logrus.SetOutput(io.MultiWriter(buf, os.Stderr))
270 defer logrus.SetOutput(os.Stderr)
272 s.disp.cluster.Containers.CrunchRunCommand = "crunchCmd"
274 ctx, cancel := context.WithCancel(context.Background())
275 dispatcher := dispatch.Dispatcher{
277 PollPeriod: time.Second,
278 RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) error {
280 time.Sleep(time.Second)
281 disp.UpdateState(ctr.UUID, dispatch.Running)
282 disp.UpdateState(ctr.UUID, dispatch.Complete)
284 s.disp.runContainer(disp, ctr, status)
291 for i := 0; i < 80 && !strings.Contains(buf.String(), expected); i++ {
292 time.Sleep(100 * time.Millisecond)
297 err := dispatcher.Run(ctx)
298 c.Assert(err, Equals, context.Canceled)
300 c.Check(buf.String(), Matches, `(?ms).*`+expected+`.*`)
303 func (s *StubbedSuite) TestSbatchArgs(c *C) {
304 container := arvados.Container{
306 RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 2},
310 for _, defaults := range [][]string{
313 {"--arg1=v1", "--arg2"},
315 c.Logf("%#v", defaults)
316 s.disp.cluster.Containers.SLURM.SbatchArgumentsList = defaults
318 args, err := s.disp.sbatchArgs(container)
319 c.Check(args, DeepEquals, append(defaults, "--job-name=123", "--nice=10000", "--no-requeue", "--mem=239", "--cpus-per-task=2", "--tmp=0"))
324 func (s *StubbedSuite) TestSbatchInstanceTypeConstraint(c *C) {
325 container := arvados.Container{
327 RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 2},
331 for _, trial := range []struct {
332 types map[string]arvados.InstanceType
336 // Choose node type => use --constraint arg
338 types: map[string]arvados.InstanceType{
339 "a1.tiny": {Name: "a1.tiny", Price: 0.02, RAM: 128000000, VCPUs: 1},
340 "a1.small": {Name: "a1.small", Price: 0.04, RAM: 256000000, VCPUs: 2},
341 "a1.medium": {Name: "a1.medium", Price: 0.08, RAM: 512000000, VCPUs: 4},
342 "a1.large": {Name: "a1.large", Price: 0.16, RAM: 1024000000, VCPUs: 8},
344 sbatchArgs: []string{"--constraint=instancetype=a1.medium"},
346 // No node types configured => no slurm constraint
349 sbatchArgs: []string{"--mem=239", "--cpus-per-task=2", "--tmp=0"},
351 // No node type is big enough => error
353 types: map[string]arvados.InstanceType{
354 "a1.tiny": {Name: "a1.tiny", Price: 0.02, RAM: 128000000, VCPUs: 1},
356 err: dispatchcloud.ConstraintsNotSatisfiableError{},
360 s.disp.cluster = &arvados.Cluster{InstanceTypes: trial.types}
362 args, err := s.disp.sbatchArgs(container)
363 c.Check(err == nil, Equals, trial.err == nil)
364 if trial.err == nil {
365 c.Check(args, DeepEquals, append([]string{"--job-name=123", "--nice=10000", "--no-requeue"}, trial.sbatchArgs...))
367 c.Check(len(err.(dispatchcloud.ConstraintsNotSatisfiableError).AvailableTypes), Equals, len(trial.types))
372 func (s *StubbedSuite) TestSbatchPartition(c *C) {
373 container := arvados.Container{
375 RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 1},
376 SchedulingParameters: arvados.SchedulingParameters{Partitions: []string{"blurb", "b2"}},
380 args, err := s.disp.sbatchArgs(container)
381 c.Check(args, DeepEquals, []string{
382 "--job-name=123", "--nice=10000", "--no-requeue",
383 "--mem=239", "--cpus-per-task=1", "--tmp=0",
384 "--partition=blurb,b2",
389 func (s *StubbedSuite) TestLoadLegacyConfig(c *C) {
395 - https://example.com/keep1
396 - https://example.com/keep2
397 SbatchArguments: ["--foo", "bar"]
400 CrunchRunCommand: ["x-crunch-run", "--cgroup-parent-subsystem=memory"]
401 ReserveExtraRAM: 12345
405 tmpfile, err := ioutil.TempFile("", "example")
410 defer os.Remove(tmpfile.Name()) // clean up
412 if _, err := tmpfile.Write(content); err != nil {
415 if err := tmpfile.Close(); err != nil {
419 os.Setenv("ARVADOS_KEEP_SERVICES", "")
420 err = s.disp.configure("crunch-dispatch-slurm", []string{"-config", tmpfile.Name()})
423 c.Check(s.disp.cluster.Services.Controller.ExternalURL, Equals, arvados.URL{Scheme: "https", Host: "example.com", Path: "/"})
424 c.Check(s.disp.cluster.SystemRootToken, Equals, "abcdefg")
425 c.Check(s.disp.cluster.Containers.SLURM.SbatchArgumentsList, DeepEquals, []string{"--foo", "bar"})
426 c.Check(s.disp.cluster.Containers.CloudVMs.PollInterval, Equals, arvados.Duration(12*time.Second))
427 c.Check(s.disp.cluster.Containers.SLURM.PrioritySpread, Equals, int64(42))
428 c.Check(s.disp.cluster.Containers.CrunchRunCommand, Equals, "x-crunch-run")
429 c.Check(s.disp.cluster.Containers.CrunchRunArgumentsList, DeepEquals, []string{"--cgroup-parent-subsystem=memory"})
430 c.Check(s.disp.cluster.Containers.ReserveExtraRAM, Equals, arvados.ByteSize(12345))
431 c.Check(s.disp.cluster.Containers.MinRetryPeriod, Equals, arvados.Duration(13*time.Second))
432 c.Check(s.disp.cluster.API.MaxItemsPerResponse, Equals, 99)
433 c.Check(s.disp.cluster.Containers.SLURM.SbatchEnvironmentVariables, DeepEquals, map[string]string{
434 "ARVADOS_KEEP_SERVICES": "https://example.com/keep1 https://example.com/keep2",
436 c.Check(os.Getenv("ARVADOS_KEEP_SERVICES"), Equals, "https://example.com/keep1 https://example.com/keep2")