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) TestIntegrationNormal(c *C) {
69 container := s.integrationTest(c,
72 return exec.Command("true")
74 return exec.Command("echo", "zzzzz-dz642-queuedcontainer")
80 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
81 dispatcher.UpdateState(container.UUID, dispatch.Running)
82 time.Sleep(3 * time.Second)
83 dispatcher.UpdateState(container.UUID, dispatch.Complete)
86 c.Check(container.State, Equals, arvados.ContainerStateComplete)
89 func (s *TestSuite) TestIntegrationCancel(c *C) {
91 var scancelCmdLine []string
94 container := s.integrationTest(c,
96 if cmd != nil && cmd.ProcessState != nil {
97 return exec.Command("true")
99 return exec.Command("echo", "zzzzz-dz642-queuedcontainer")
102 func(container arvados.Container) *exec.Cmd {
103 if attempt++; attempt == 1 {
104 return exec.Command("false")
106 scancelCmdLine = scancelFunc(container).Args
107 cmd = exec.Command("echo")
113 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
114 dispatcher.UpdateState(container.UUID, dispatch.Running)
115 time.Sleep(1 * time.Second)
116 dispatcher.Arv.Update("containers", container.UUID,
118 "container": arvadosclient.Dict{"priority": 0}},
121 c.Check(container.State, Equals, arvados.ContainerStateCancelled)
122 c.Check(scancelCmdLine, DeepEquals, []string{"scancel", "--name=zzzzz-dz642-queuedcontainer"})
125 func (s *TestSuite) TestIntegrationMissingFromSqueue(c *C) {
126 container := s.integrationTest(c,
127 func() *exec.Cmd { return exec.Command("echo") },
131 fmt.Sprintf("--job-name=%s", "zzzzz-dz642-queuedcontainer"),
132 fmt.Sprintf("--mem=%d", 11445),
133 fmt.Sprintf("--cpus-per-task=%d", 4),
134 fmt.Sprintf("--tmp=%d", 45777)},
135 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
136 dispatcher.UpdateState(container.UUID, dispatch.Running)
137 time.Sleep(3 * time.Second)
138 dispatcher.UpdateState(container.UUID, dispatch.Complete)
140 c.Check(container.State, Equals, arvados.ContainerStateCancelled)
143 func (s *TestSuite) TestSbatchFail(c *C) {
144 container := s.integrationTest(c,
145 func() *exec.Cmd { return exec.Command("echo") },
147 func(container arvados.Container) *exec.Cmd {
148 return exec.Command("false")
151 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
152 dispatcher.UpdateState(container.UUID, dispatch.Running)
153 dispatcher.UpdateState(container.UUID, dispatch.Complete)
155 c.Check(container.State, Equals, arvados.ContainerStateComplete)
157 arv, err := arvadosclient.MakeArvadosClient()
160 var ll arvados.LogList
161 err = arv.List("logs", arvadosclient.Dict{"filters": [][]string{
162 []string{"object_uuid", "=", container.UUID},
163 []string{"event_type", "=", "dispatch"},
165 c.Assert(len(ll.Items), Equals, 1)
168 func (s *TestSuite) integrationTest(c *C,
169 newSqueueCmd func() *exec.Cmd,
170 newScancelCmd func(arvados.Container) *exec.Cmd,
171 newSbatchCmd func(arvados.Container) *exec.Cmd,
172 sbatchCmdComps []string,
173 runContainer func(*dispatch.Dispatcher, arvados.Container)) arvados.Container {
174 arvadostest.ResetEnv()
176 arv, err := arvadosclient.MakeArvadosClient()
179 var sbatchCmdLine []string
181 // Override sbatchCmd
182 defer func(orig func(arvados.Container) *exec.Cmd) {
186 if newSbatchCmd != nil {
187 sbatchCmd = newSbatchCmd
189 sbatchCmd = func(container arvados.Container) *exec.Cmd {
190 sbatchCmdLine = sbatchFunc(container).Args
191 return exec.Command("sh")
195 // Override squeueCmd
196 defer func(orig func() *exec.Cmd) {
199 squeueCmd = newSqueueCmd
202 defer func(orig func(arvados.Container) *exec.Cmd) {
205 scancelCmd = newScancelCmd
207 // There should be one queued container
208 params := arvadosclient.Dict{
209 "filters": [][]string{{"state", "=", "Queued"}},
211 var containers arvados.ContainerList
212 err = arv.List("containers", params, &containers)
214 c.Check(len(containers.Items), Equals, 1)
216 theConfig.CrunchRunCommand = []string{"echo"}
218 ctx, cancel := context.WithCancel(context.Background())
219 doneRun := make(chan struct{})
221 dispatcher := dispatch.Dispatcher{
223 PollPeriod: time.Duration(1) * time.Second,
224 RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) {
226 runContainer(disp, ctr)
227 doneRun <- struct{}{}
229 run(disp, ctr, status)
234 sqCheck = &SqueueChecker{Period: 500 * time.Millisecond}
236 err = dispatcher.Run(ctx)
238 c.Assert(err, Equals, context.Canceled)
242 c.Check(sbatchCmdLine, DeepEquals, sbatchCmdComps)
244 // There should be no queued containers now
245 err = arv.List("containers", params, &containers)
247 c.Check(len(containers.Items), Equals, 0)
249 // Previously "Queued" container should now be in "Complete" state
250 var container arvados.Container
251 err = arv.Get("containers", "zzzzz-dz642-queuedcontainer", nil, &container)
256 func (s *MockArvadosServerSuite) TestAPIErrorGettingContainers(c *C) {
257 apiStubResponses := make(map[string]arvadostest.StubResponse)
258 apiStubResponses["/arvados/v1/api_client_authorizations/current"] = arvadostest.StubResponse{200, `{"uuid":"` + arvadostest.Dispatch1AuthUUID + `"}`}
259 apiStubResponses["/arvados/v1/containers"] = arvadostest.StubResponse{500, string(`{}`)}
261 testWithServerStub(c, apiStubResponses, "echo", "Error getting list of containers")
264 func testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubResponse, crunchCmd string, expected string) {
265 apiStub := arvadostest.ServerStub{apiStubResponses}
267 api := httptest.NewServer(&apiStub)
270 arv := &arvadosclient.ArvadosClient{
272 ApiServer: api.URL[7:],
274 Client: &http.Client{Transport: &http.Transport{}},
278 buf := bytes.NewBuffer(nil)
279 log.SetOutput(io.MultiWriter(buf, os.Stderr))
280 defer log.SetOutput(os.Stderr)
282 theConfig.CrunchRunCommand = []string{crunchCmd}
284 ctx, cancel := context.WithCancel(context.Background())
285 dispatcher := dispatch.Dispatcher{
287 PollPeriod: time.Duration(1) * time.Second,
288 RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) {
290 time.Sleep(1 * time.Second)
291 disp.UpdateState(ctr.UUID, dispatch.Running)
292 disp.UpdateState(ctr.UUID, dispatch.Complete)
294 run(disp, ctr, status)
300 for i := 0; i < 80 && !strings.Contains(buf.String(), expected); i++ {
301 time.Sleep(100 * time.Millisecond)
306 err := dispatcher.Run(ctx)
307 c.Assert(err, Equals, context.Canceled)
309 c.Check(buf.String(), Matches, `(?ms).*`+expected+`.*`)
312 func (s *MockArvadosServerSuite) TestNoSuchConfigFile(c *C) {
314 err := readConfig(&config, "/nosuchdir89j7879/8hjwr7ojgyy7")
315 c.Assert(err, NotNil)
318 func (s *MockArvadosServerSuite) TestBadSbatchArgsConfig(c *C) {
321 tmpfile, err := ioutil.TempFile(os.TempDir(), "config")
323 defer os.Remove(tmpfile.Name())
325 _, err = tmpfile.Write([]byte(`{"SbatchArguments": "oops this is not a string array"}`))
328 err = readConfig(&config, tmpfile.Name())
329 c.Assert(err, NotNil)
332 func (s *MockArvadosServerSuite) TestNoSuchArgInConfigIgnored(c *C) {
335 tmpfile, err := ioutil.TempFile(os.TempDir(), "config")
337 defer os.Remove(tmpfile.Name())
339 _, err = tmpfile.Write([]byte(`{"NoSuchArg": "Nobody loves me, not one tiny hunk."}`))
342 err = readConfig(&config, tmpfile.Name())
344 c.Check(0, Equals, len(config.SbatchArguments))
347 func (s *MockArvadosServerSuite) TestReadConfig(c *C) {
350 tmpfile, err := ioutil.TempFile(os.TempDir(), "config")
352 defer os.Remove(tmpfile.Name())
354 args := []string{"--arg1=v1", "--arg2", "--arg3=v3"}
355 argsS := `{"SbatchArguments": ["--arg1=v1", "--arg2", "--arg3=v3"]}`
356 _, err = tmpfile.Write([]byte(argsS))
359 err = readConfig(&config, tmpfile.Name())
361 c.Check(3, Equals, len(config.SbatchArguments))
362 c.Check(args, DeepEquals, config.SbatchArguments)
365 func (s *MockArvadosServerSuite) TestSbatchFuncWithNoConfigArgs(c *C) {
366 testSbatchFuncWithArgs(c, nil)
369 func (s *MockArvadosServerSuite) TestSbatchFuncWithEmptyConfigArgs(c *C) {
370 testSbatchFuncWithArgs(c, []string{})
373 func (s *MockArvadosServerSuite) TestSbatchFuncWithConfigArgs(c *C) {
374 testSbatchFuncWithArgs(c, []string{"--arg1=v1", "--arg2"})
377 func testSbatchFuncWithArgs(c *C, args []string) {
378 theConfig.SbatchArguments = append(theConfig.SbatchArguments, args...)
380 container := arvados.Container{UUID: "123", RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 2}}
381 sbatchCmd := sbatchFunc(container)
383 var expected []string
384 expected = append(expected, "sbatch")
385 expected = append(expected, theConfig.SbatchArguments...)
386 expected = append(expected, "--job-name=123", "--mem=239", "--cpus-per-task=2", "--tmp=0")
388 c.Check(sbatchCmd.Args, DeepEquals, expected)
391 func (s *MockArvadosServerSuite) TestSbatchPartition(c *C) {
392 theConfig.SbatchArguments = nil
393 container := arvados.Container{UUID: "123", RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 1}, SchedulingParameters: arvados.SchedulingParameters{Partitions: []string{"blurb", "b2"}}}
394 sbatchCmd := sbatchFunc(container)
396 var expected []string
397 expected = append(expected, "sbatch")
398 expected = append(expected, "--job-name=123", "--mem=239", "--cpus-per-task=1", "--tmp=0", "--partition=blurb,b2")
400 c.Check(sbatchCmd.Args, DeepEquals, expected)