18 "git.curoverse.com/arvados.git/sdk/go/arvados"
19 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
20 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
21 "git.curoverse.com/arvados.git/sdk/go/dispatch"
25 // Gocheck boilerplate
26 func Test(t *testing.T) {
30 var _ = Suite(&TestSuite{})
31 var _ = Suite(&MockArvadosServerSuite{})
33 type TestSuite struct{}
34 type MockArvadosServerSuite struct{}
36 var initialArgs []string
38 func (s *TestSuite) SetUpSuite(c *C) {
42 func (s *TestSuite) TearDownSuite(c *C) {
45 func (s *TestSuite) SetUpTest(c *C) {
46 args := []string{"crunch-dispatch-slurm"}
49 arvadostest.StartAPI()
50 os.Setenv("ARVADOS_API_TOKEN", arvadostest.Dispatch1Token)
53 func (s *TestSuite) TearDownTest(c *C) {
55 arvadostest.ResetEnv()
59 func (s *MockArvadosServerSuite) TearDownTest(c *C) {
60 arvadostest.ResetEnv()
63 func (s *TestSuite) TestIntegrationNormal(c *C) {
65 container := s.integrationTest(c,
68 return exec.Command("true")
70 return exec.Command("echo", "zzzzz-dz642-queuedcontainer")
76 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
77 dispatcher.UpdateState(container.UUID, dispatch.Running)
78 time.Sleep(3 * time.Second)
79 dispatcher.UpdateState(container.UUID, dispatch.Complete)
82 c.Check(container.State, Equals, arvados.ContainerStateComplete)
85 func (s *TestSuite) TestIntegrationCancel(c *C) {
87 var scancelCmdLine []string
90 container := s.integrationTest(c,
92 if cmd != nil && cmd.ProcessState != nil {
93 return exec.Command("true")
95 return exec.Command("echo", "zzzzz-dz642-queuedcontainer")
98 func(container arvados.Container) *exec.Cmd {
99 if attempt++; attempt == 1 {
100 return exec.Command("false")
102 scancelCmdLine = scancelFunc(container).Args
103 cmd = exec.Command("echo")
109 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
110 dispatcher.UpdateState(container.UUID, dispatch.Running)
111 time.Sleep(1 * time.Second)
112 dispatcher.Arv.Update("containers", container.UUID,
114 "container": arvadosclient.Dict{"priority": 0}},
117 c.Check(container.State, Equals, arvados.ContainerStateCancelled)
118 c.Check(scancelCmdLine, DeepEquals, []string{"scancel", "--name=zzzzz-dz642-queuedcontainer"})
121 func (s *TestSuite) TestIntegrationMissingFromSqueue(c *C) {
122 container := s.integrationTest(c,
123 func() *exec.Cmd { return exec.Command("echo") },
127 fmt.Sprintf("--job-name=%s", "zzzzz-dz642-queuedcontainer"),
128 fmt.Sprintf("--mem=%d", 11445),
129 fmt.Sprintf("--cpus-per-task=%d", 4),
130 fmt.Sprintf("--tmp=%d", 45777)},
131 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
132 dispatcher.UpdateState(container.UUID, dispatch.Running)
133 time.Sleep(3 * time.Second)
134 dispatcher.UpdateState(container.UUID, dispatch.Complete)
136 c.Check(container.State, Equals, arvados.ContainerStateCancelled)
139 func (s *TestSuite) TestSbatchFail(c *C) {
140 container := s.integrationTest(c,
141 func() *exec.Cmd { return exec.Command("echo") },
143 func(container arvados.Container) *exec.Cmd {
144 return exec.Command("false")
147 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
148 dispatcher.UpdateState(container.UUID, dispatch.Running)
149 dispatcher.UpdateState(container.UUID, dispatch.Complete)
151 c.Check(container.State, Equals, arvados.ContainerStateComplete)
153 arv, err := arvadosclient.MakeArvadosClient()
156 var ll arvados.LogList
157 err = arv.List("logs", arvadosclient.Dict{"filters": [][]string{
158 []string{"object_uuid", "=", container.UUID},
159 []string{"event_type", "=", "dispatch"},
161 c.Assert(len(ll.Items), Equals, 1)
164 func (s *TestSuite) integrationTest(c *C,
165 newSqueueCmd func() *exec.Cmd,
166 newScancelCmd func(arvados.Container) *exec.Cmd,
167 newSbatchCmd func(arvados.Container) *exec.Cmd,
168 sbatchCmdComps []string,
169 runContainer func(*dispatch.Dispatcher, arvados.Container)) arvados.Container {
170 arvadostest.ResetEnv()
172 arv, err := arvadosclient.MakeArvadosClient()
175 var sbatchCmdLine []string
177 // Override sbatchCmd
178 defer func(orig func(arvados.Container) *exec.Cmd) {
182 if newSbatchCmd != nil {
183 sbatchCmd = newSbatchCmd
185 sbatchCmd = func(container arvados.Container) *exec.Cmd {
186 sbatchCmdLine = sbatchFunc(container).Args
187 return exec.Command("sh")
191 // Override squeueCmd
192 defer func(orig func() *exec.Cmd) {
195 squeueCmd = newSqueueCmd
198 defer func(orig func(arvados.Container) *exec.Cmd) {
201 scancelCmd = newScancelCmd
203 // There should be one queued container
204 params := arvadosclient.Dict{
205 "filters": [][]string{{"state", "=", "Queued"}},
207 var containers arvados.ContainerList
208 err = arv.List("containers", params, &containers)
210 c.Check(len(containers.Items), Equals, 1)
212 theConfig.CrunchRunCommand = []string{"echo"}
214 ctx, cancel := context.WithCancel(context.Background())
215 doneRun := make(chan struct{})
217 dispatcher := dispatch.Dispatcher{
219 PollPeriod: time.Duration(1) * time.Second,
220 RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) {
222 runContainer(disp, ctr)
223 doneRun <- struct{}{}
225 run(disp, ctr, status)
230 sqCheck = &SqueueChecker{Period: 500 * time.Millisecond}
232 err = dispatcher.Run(ctx)
234 c.Assert(err, Equals, context.Canceled)
238 c.Check(sbatchCmdLine, DeepEquals, sbatchCmdComps)
240 // There should be no queued containers now
241 err = arv.List("containers", params, &containers)
243 c.Check(len(containers.Items), Equals, 0)
245 // Previously "Queued" container should now be in "Complete" state
246 var container arvados.Container
247 err = arv.Get("containers", "zzzzz-dz642-queuedcontainer", nil, &container)
252 func (s *MockArvadosServerSuite) TestAPIErrorGettingContainers(c *C) {
253 apiStubResponses := make(map[string]arvadostest.StubResponse)
254 apiStubResponses["/arvados/v1/api_client_authorizations/current"] = arvadostest.StubResponse{200, `{"uuid":"` + arvadostest.Dispatch1AuthUUID + `"}`}
255 apiStubResponses["/arvados/v1/containers"] = arvadostest.StubResponse{500, string(`{}`)}
257 testWithServerStub(c, apiStubResponses, "echo", "Error getting list of containers")
260 func testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubResponse, crunchCmd string, expected string) {
261 apiStub := arvadostest.ServerStub{apiStubResponses}
263 api := httptest.NewServer(&apiStub)
266 arv := &arvadosclient.ArvadosClient{
268 ApiServer: api.URL[7:],
270 Client: &http.Client{Transport: &http.Transport{}},
274 buf := bytes.NewBuffer(nil)
275 log.SetOutput(io.MultiWriter(buf, os.Stderr))
276 defer log.SetOutput(os.Stderr)
278 theConfig.CrunchRunCommand = []string{crunchCmd}
280 ctx, cancel := context.WithCancel(context.Background())
281 dispatcher := dispatch.Dispatcher{
283 PollPeriod: time.Duration(1) * time.Second,
284 RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) {
286 time.Sleep(1 * time.Second)
287 disp.UpdateState(ctr.UUID, dispatch.Running)
288 disp.UpdateState(ctr.UUID, dispatch.Complete)
290 run(disp, ctr, status)
296 for i := 0; i < 80 && !strings.Contains(buf.String(), expected); i++ {
297 time.Sleep(100 * time.Millisecond)
302 err := dispatcher.Run(ctx)
303 c.Assert(err, Equals, context.Canceled)
305 c.Check(buf.String(), Matches, `(?ms).*`+expected+`.*`)
308 func (s *MockArvadosServerSuite) TestNoSuchConfigFile(c *C) {
310 err := readConfig(&config, "/nosuchdir89j7879/8hjwr7ojgyy7")
311 c.Assert(err, NotNil)
314 func (s *MockArvadosServerSuite) TestBadSbatchArgsConfig(c *C) {
317 tmpfile, err := ioutil.TempFile(os.TempDir(), "config")
319 defer os.Remove(tmpfile.Name())
321 _, err = tmpfile.Write([]byte(`{"SbatchArguments": "oops this is not a string array"}`))
324 err = readConfig(&config, tmpfile.Name())
325 c.Assert(err, NotNil)
328 func (s *MockArvadosServerSuite) TestNoSuchArgInConfigIgnored(c *C) {
331 tmpfile, err := ioutil.TempFile(os.TempDir(), "config")
333 defer os.Remove(tmpfile.Name())
335 _, err = tmpfile.Write([]byte(`{"NoSuchArg": "Nobody loves me, not one tiny hunk."}`))
338 err = readConfig(&config, tmpfile.Name())
340 c.Check(0, Equals, len(config.SbatchArguments))
343 func (s *MockArvadosServerSuite) TestReadConfig(c *C) {
346 tmpfile, err := ioutil.TempFile(os.TempDir(), "config")
348 defer os.Remove(tmpfile.Name())
350 args := []string{"--arg1=v1", "--arg2", "--arg3=v3"}
351 argsS := `{"SbatchArguments": ["--arg1=v1", "--arg2", "--arg3=v3"]}`
352 _, err = tmpfile.Write([]byte(argsS))
355 err = readConfig(&config, tmpfile.Name())
357 c.Check(3, Equals, len(config.SbatchArguments))
358 c.Check(args, DeepEquals, config.SbatchArguments)
361 func (s *MockArvadosServerSuite) TestSbatchFuncWithNoConfigArgs(c *C) {
362 testSbatchFuncWithArgs(c, nil)
365 func (s *MockArvadosServerSuite) TestSbatchFuncWithEmptyConfigArgs(c *C) {
366 testSbatchFuncWithArgs(c, []string{})
369 func (s *MockArvadosServerSuite) TestSbatchFuncWithConfigArgs(c *C) {
370 testSbatchFuncWithArgs(c, []string{"--arg1=v1", "--arg2"})
373 func testSbatchFuncWithArgs(c *C, args []string) {
374 theConfig.SbatchArguments = append(theConfig.SbatchArguments, args...)
376 container := arvados.Container{UUID: "123", RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 2}}
377 sbatchCmd := sbatchFunc(container)
379 var expected []string
380 expected = append(expected, "sbatch")
381 expected = append(expected, theConfig.SbatchArguments...)
382 expected = append(expected, "--job-name=123", "--mem=239", "--cpus-per-task=2", "--tmp=0")
384 c.Check(sbatchCmd.Args, DeepEquals, expected)
387 func (s *MockArvadosServerSuite) TestSbatchPartition(c *C) {
388 theConfig.SbatchArguments = nil
389 container := arvados.Container{UUID: "123", RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 1}, SchedulingParameters: arvados.SchedulingParameters{Partitions: []string{"blurb", "b2"}}}
390 sbatchCmd := sbatchFunc(container)
392 var expected []string
393 expected = append(expected, "sbatch")
394 expected = append(expected, "--job-name=123", "--mem=239", "--cpus-per-task=1", "--tmp=0", "--partition=blurb,b2")
396 c.Check(sbatchCmd.Args, DeepEquals, expected)