1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
22 "git.curoverse.com/arvados.git/sdk/go/arvados"
23 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
24 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
25 "git.curoverse.com/arvados.git/sdk/go/dispatch"
29 // Gocheck boilerplate
30 func Test(t *testing.T) {
34 var _ = Suite(&TestSuite{})
35 var _ = Suite(&MockArvadosServerSuite{})
37 type TestSuite struct{}
38 type MockArvadosServerSuite struct{}
40 var initialArgs []string
42 func (s *TestSuite) SetUpSuite(c *C) {
46 func (s *TestSuite) TearDownSuite(c *C) {
49 func (s *TestSuite) SetUpTest(c *C) {
50 args := []string{"crunch-dispatch-slurm"}
53 arvadostest.StartAPI()
54 os.Setenv("ARVADOS_API_TOKEN", arvadostest.Dispatch1Token)
57 func (s *TestSuite) TearDownTest(c *C) {
59 arvadostest.ResetEnv()
63 func (s *MockArvadosServerSuite) TearDownTest(c *C) {
64 arvadostest.ResetEnv()
67 func (s *TestSuite) integrationTest(c *C,
68 newSqueueCmd func() *exec.Cmd,
69 newScancelCmd func(arvados.Container) *exec.Cmd,
70 newSbatchCmd func(arvados.Container) *exec.Cmd,
71 newScontrolCmd func(arvados.Container) *exec.Cmd,
72 sbatchCmdComps []string,
73 runContainer func(*dispatch.Dispatcher, arvados.Container)) arvados.Container {
74 arvadostest.ResetEnv()
76 arv, err := arvadosclient.MakeArvadosClient()
79 var sbatchCmdLine []string
82 defer func(orig func(arvados.Container) *exec.Cmd) {
86 if newSbatchCmd != nil {
87 sbatchCmd = newSbatchCmd
89 sbatchCmd = func(container arvados.Container) *exec.Cmd {
90 sbatchCmdLine = sbatchFunc(container).Args
91 return exec.Command("sh")
96 defer func(orig func() *exec.Cmd) {
99 squeueCmd = newSqueueCmd
102 defer func(orig func(arvados.Container) *exec.Cmd) {
105 scancelCmd = newScancelCmd
108 defer func(orig func(arvados.Container) *exec.Cmd) {
111 scontrolCmd = newScontrolCmd
113 // There should be one queued container
114 params := arvadosclient.Dict{
115 "filters": [][]string{{"state", "=", "Queued"}},
117 var containers arvados.ContainerList
118 err = arv.List("containers", params, &containers)
120 c.Check(len(containers.Items), Equals, 1)
122 theConfig.CrunchRunCommand = []string{"echo"}
124 ctx, cancel := context.WithCancel(context.Background())
125 doneRun := make(chan struct{})
127 dispatcher := dispatch.Dispatcher{
129 PollPeriod: time.Duration(1) * time.Second,
130 RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) {
132 runContainer(disp, ctr)
133 doneRun <- struct{}{}
135 run(disp, ctr, status)
140 sqCheck = &SqueueChecker{Period: 500 * time.Millisecond}
142 err = dispatcher.Run(ctx)
144 c.Assert(err, Equals, context.Canceled)
148 c.Check(sbatchCmdLine, DeepEquals, sbatchCmdComps)
150 // There should be no queued containers now
151 err = arv.List("containers", params, &containers)
153 c.Check(len(containers.Items), Equals, 0)
155 // Previously "Queued" container should now be in "Complete" state
156 var container arvados.Container
157 err = arv.Get("containers", "zzzzz-dz642-queuedcontainer", nil, &container)
162 func (s *TestSuite) TestIntegrationNormal(c *C) {
164 container := s.integrationTest(c,
167 return exec.Command("true")
169 return exec.Command("echo", "zzzzz-dz642-queuedcontainer 9990 100")
176 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
177 dispatcher.UpdateState(container.UUID, dispatch.Running)
178 time.Sleep(3 * time.Second)
179 dispatcher.UpdateState(container.UUID, dispatch.Complete)
182 c.Check(container.State, Equals, arvados.ContainerStateComplete)
185 func (s *TestSuite) TestIntegrationCancel(c *C) {
187 var scancelCmdLine []string
190 container := s.integrationTest(c,
192 if cmd != nil && cmd.ProcessState != nil {
193 return exec.Command("true")
195 return exec.Command("echo", "zzzzz-dz642-queuedcontainer 9990 100")
198 func(container arvados.Container) *exec.Cmd {
199 if attempt++; attempt == 1 {
200 return exec.Command("false")
202 scancelCmdLine = scancelFunc(container).Args
203 cmd = exec.Command("echo")
210 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
211 dispatcher.UpdateState(container.UUID, dispatch.Running)
212 time.Sleep(1 * time.Second)
213 dispatcher.Arv.Update("containers", container.UUID,
215 "container": arvadosclient.Dict{"priority": 0}},
218 c.Check(container.State, Equals, arvados.ContainerStateCancelled)
219 c.Check(scancelCmdLine, DeepEquals, []string{"scancel", "--name=zzzzz-dz642-queuedcontainer"})
222 func (s *TestSuite) TestIntegrationMissingFromSqueue(c *C) {
223 container := s.integrationTest(c,
224 func() *exec.Cmd { return exec.Command("echo") },
229 fmt.Sprintf("--job-name=%s", "zzzzz-dz642-queuedcontainer"),
230 fmt.Sprintf("--mem=%d", 11445),
231 fmt.Sprintf("--cpus-per-task=%d", 4),
232 fmt.Sprintf("--tmp=%d", 45777),
233 fmt.Sprintf("--nice=%d", 9990)},
234 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
235 dispatcher.UpdateState(container.UUID, dispatch.Running)
236 time.Sleep(3 * time.Second)
237 dispatcher.UpdateState(container.UUID, dispatch.Complete)
239 c.Check(container.State, Equals, arvados.ContainerStateCancelled)
242 func (s *TestSuite) TestSbatchFail(c *C) {
243 container := s.integrationTest(c,
244 func() *exec.Cmd { return exec.Command("echo") },
246 func(container arvados.Container) *exec.Cmd {
247 return exec.Command("false")
251 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
252 dispatcher.UpdateState(container.UUID, dispatch.Running)
253 dispatcher.UpdateState(container.UUID, dispatch.Complete)
255 c.Check(container.State, Equals, arvados.ContainerStateComplete)
257 arv, err := arvadosclient.MakeArvadosClient()
260 var ll arvados.LogList
261 err = arv.List("logs", arvadosclient.Dict{"filters": [][]string{
262 {"object_uuid", "=", container.UUID},
263 {"event_type", "=", "dispatch"},
265 c.Assert(len(ll.Items), Equals, 1)
268 func (s *MockArvadosServerSuite) TestAPIErrorGettingContainers(c *C) {
269 apiStubResponses := make(map[string]arvadostest.StubResponse)
270 apiStubResponses["/arvados/v1/api_client_authorizations/current"] = arvadostest.StubResponse{200, `{"uuid":"` + arvadostest.Dispatch1AuthUUID + `"}`}
271 apiStubResponses["/arvados/v1/containers"] = arvadostest.StubResponse{500, string(`{}`)}
273 testWithServerStub(c, apiStubResponses, "echo", "Error getting list of containers")
276 func testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubResponse, crunchCmd string, expected string) {
277 apiStub := arvadostest.ServerStub{apiStubResponses}
279 api := httptest.NewServer(&apiStub)
282 arv := &arvadosclient.ArvadosClient{
284 ApiServer: api.URL[7:],
286 Client: &http.Client{Transport: &http.Transport{}},
290 buf := bytes.NewBuffer(nil)
291 log.SetOutput(io.MultiWriter(buf, os.Stderr))
292 defer log.SetOutput(os.Stderr)
294 theConfig.CrunchRunCommand = []string{crunchCmd}
296 ctx, cancel := context.WithCancel(context.Background())
297 dispatcher := dispatch.Dispatcher{
299 PollPeriod: time.Duration(1) * time.Second,
300 RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) {
302 time.Sleep(1 * time.Second)
303 disp.UpdateState(ctr.UUID, dispatch.Running)
304 disp.UpdateState(ctr.UUID, dispatch.Complete)
306 run(disp, ctr, status)
312 for i := 0; i < 80 && !strings.Contains(buf.String(), expected); i++ {
313 time.Sleep(100 * time.Millisecond)
318 err := dispatcher.Run(ctx)
319 c.Assert(err, Equals, context.Canceled)
321 c.Check(buf.String(), Matches, `(?ms).*`+expected+`.*`)
324 func (s *MockArvadosServerSuite) TestNoSuchConfigFile(c *C) {
326 err := readConfig(&config, "/nosuchdir89j7879/8hjwr7ojgyy7")
327 c.Assert(err, NotNil)
330 func (s *MockArvadosServerSuite) TestBadSbatchArgsConfig(c *C) {
333 tmpfile, err := ioutil.TempFile(os.TempDir(), "config")
335 defer os.Remove(tmpfile.Name())
337 _, err = tmpfile.Write([]byte(`{"SbatchArguments": "oops this is not a string array"}`))
340 err = readConfig(&config, tmpfile.Name())
341 c.Assert(err, NotNil)
344 func (s *MockArvadosServerSuite) TestNoSuchArgInConfigIgnored(c *C) {
347 tmpfile, err := ioutil.TempFile(os.TempDir(), "config")
349 defer os.Remove(tmpfile.Name())
351 _, err = tmpfile.Write([]byte(`{"NoSuchArg": "Nobody loves me, not one tiny hunk."}`))
354 err = readConfig(&config, tmpfile.Name())
356 c.Check(0, Equals, len(config.SbatchArguments))
359 func (s *MockArvadosServerSuite) TestReadConfig(c *C) {
362 tmpfile, err := ioutil.TempFile(os.TempDir(), "config")
364 defer os.Remove(tmpfile.Name())
366 args := []string{"--arg1=v1", "--arg2", "--arg3=v3"}
367 argsS := `{"SbatchArguments": ["--arg1=v1", "--arg2", "--arg3=v3"]}`
368 _, err = tmpfile.Write([]byte(argsS))
371 err = readConfig(&config, tmpfile.Name())
373 c.Check(3, Equals, len(config.SbatchArguments))
374 c.Check(args, DeepEquals, config.SbatchArguments)
377 func (s *MockArvadosServerSuite) TestSbatchFuncWithNoConfigArgs(c *C) {
378 testSbatchFuncWithArgs(c, nil)
381 func (s *MockArvadosServerSuite) TestSbatchFuncWithEmptyConfigArgs(c *C) {
382 testSbatchFuncWithArgs(c, []string{})
385 func (s *MockArvadosServerSuite) TestSbatchFuncWithConfigArgs(c *C) {
386 testSbatchFuncWithArgs(c, []string{"--arg1=v1", "--arg2"})
389 func testSbatchFuncWithArgs(c *C, args []string) {
390 theConfig.SbatchArguments = append(theConfig.SbatchArguments, args...)
392 container := arvados.Container{
394 RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 2},
396 sbatchCmd := sbatchFunc(container)
398 var expected []string
399 expected = append(expected, "sbatch")
400 expected = append(expected, theConfig.SbatchArguments...)
401 expected = append(expected, "--job-name=123", "--mem=239", "--cpus-per-task=2", "--tmp=0", "--nice=9990")
403 c.Check(sbatchCmd.Args, DeepEquals, expected)
406 func (s *MockArvadosServerSuite) TestSbatchPartition(c *C) {
407 theConfig.SbatchArguments = nil
408 container := arvados.Container{
410 RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 1},
411 SchedulingParameters: arvados.SchedulingParameters{Partitions: []string{"blurb", "b2"}},
413 sbatchCmd := sbatchFunc(container)
415 var expected []string
416 expected = append(expected, "sbatch")
417 expected = append(expected, "--job-name=123", "--mem=239", "--cpus-per-task=1", "--tmp=0", "--nice=9990", "--partition=blurb,b2")
419 c.Check(sbatchCmd.Args, DeepEquals, expected)
422 func (s *TestSuite) TestIntegrationChangePriority(c *C) {
423 var scontrolCmdLine []string
426 container := s.integrationTest(c,
429 return exec.Command("echo", "zzzzz-dz642-queuedcontainer 9990 100")
430 } else if step == 1 {
431 return exec.Command("echo", "zzzzz-dz642-queuedcontainer 4000 100")
433 return exec.Command("echo")
436 func(arvados.Container) *exec.Cmd { return exec.Command("true") },
438 func(container arvados.Container) *exec.Cmd {
439 scontrolCmdLine = scontrolFunc(container).Args
441 return exec.Command("true")
444 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
445 dispatcher.UpdateState(container.UUID, dispatch.Running)
446 time.Sleep(1 * time.Second)
447 dispatcher.Arv.Update("containers", container.UUID,
449 "container": arvadosclient.Dict{"priority": 600}},
451 time.Sleep(1 * time.Second)
453 dispatcher.UpdateState(container.UUID, dispatch.Complete)
455 c.Check(container.State, Equals, arvados.ContainerStateComplete)
456 c.Check(scontrolCmdLine, DeepEquals, []string{"scontrol", "update", "JobName=zzzzz-dz642-queuedcontainer", "Nice=4000"})