1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
23 "git.curoverse.com/arvados.git/sdk/go/arvados"
24 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
25 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
26 "git.curoverse.com/arvados.git/sdk/go/dispatch"
27 "git.curoverse.com/arvados.git/services/dispatchcloud"
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.StartAPI()
46 os.Setenv("ARVADOS_API_TOKEN", arvadostest.Dispatch1Token)
52 func (s *IntegrationSuite) TearDownTest(c *C) {
53 arvadostest.ResetEnv()
57 type slurmFake struct {
62 // If non-nil, run this func during the 2nd+ call to Cancel()
64 // Error returned by Batch()
68 func (sf *slurmFake) Batch(script io.Reader, args []string) error {
69 sf.didBatch = append(sf.didBatch, args)
73 func (sf *slurmFake) QueueCommand(args []string) *exec.Cmd {
74 return exec.Command("echo", sf.queue)
77 func (sf *slurmFake) Renice(name string, nice int) error {
78 sf.didRenice = append(sf.didRenice, []string{name, fmt.Sprintf("%d", nice)})
82 func (sf *slurmFake) Cancel(name string) error {
83 sf.didCancel = append(sf.didCancel, name)
84 if len(sf.didCancel) == 1 {
85 // simulate error on first attempt
86 return errors.New("something terrible happened")
88 if sf.onCancel != nil {
94 func (s *IntegrationSuite) integrationTest(c *C,
95 expectBatch [][]string,
96 runContainer func(*dispatch.Dispatcher, arvados.Container)) arvados.Container {
97 arvadostest.ResetEnv()
99 arv, err := arvadosclient.MakeArvadosClient()
102 // There should be one queued container
103 params := arvadosclient.Dict{
104 "filters": [][]string{{"state", "=", "Queued"}},
106 var containers arvados.ContainerList
107 err = arv.List("containers", params, &containers)
109 c.Check(len(containers.Items), Equals, 1)
111 s.disp.CrunchRunCommand = []string{"echo"}
113 ctx, cancel := context.WithCancel(context.Background())
114 doneRun := make(chan struct{})
116 s.disp.Dispatcher = &dispatch.Dispatcher{
118 PollPeriod: time.Duration(1) * time.Second,
119 RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) {
121 runContainer(disp, ctr)
123 doneRun <- struct{}{}
125 s.disp.runContainer(disp, ctr, status)
130 s.disp.slurm = &s.slurm
131 s.disp.sqCheck = &SqueueChecker{Period: 500 * time.Millisecond, Slurm: s.disp.slurm}
133 err = s.disp.Dispatcher.Run(ctx)
135 c.Assert(err, Equals, context.Canceled)
137 s.disp.sqCheck.Stop()
139 c.Check(s.slurm.didBatch, DeepEquals, expectBatch)
141 // There should be no queued containers now
142 err = arv.List("containers", params, &containers)
144 c.Check(len(containers.Items), Equals, 0)
146 // Previously "Queued" container should now be in "Complete" state
147 var container arvados.Container
148 err = arv.Get("containers", "zzzzz-dz642-queuedcontainer", nil, &container)
153 func (s *IntegrationSuite) TestNormal(c *C) {
154 s.slurm = slurmFake{queue: "zzzzz-dz642-queuedcontainer 9990 100\n"}
155 container := s.integrationTest(c,
157 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
158 dispatcher.UpdateState(container.UUID, dispatch.Running)
159 time.Sleep(3 * time.Second)
160 dispatcher.UpdateState(container.UUID, dispatch.Complete)
162 c.Check(container.State, Equals, arvados.ContainerStateComplete)
165 func (s *IntegrationSuite) TestCancel(c *C) {
166 s.slurm = slurmFake{queue: "zzzzz-dz642-queuedcontainer 9990 100\n"}
167 readyToCancel := make(chan bool)
168 s.slurm.onCancel = func() { <-readyToCancel }
169 container := s.integrationTest(c,
171 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
172 dispatcher.UpdateState(container.UUID, dispatch.Running)
173 time.Sleep(time.Second)
174 dispatcher.Arv.Update("containers", container.UUID,
176 "container": arvadosclient.Dict{"priority": 0}},
178 readyToCancel <- true
181 c.Check(container.State, Equals, arvados.ContainerStateCancelled)
182 c.Check(len(s.slurm.didCancel) > 1, Equals, true)
183 c.Check(s.slurm.didCancel[:2], DeepEquals, []string{"zzzzz-dz642-queuedcontainer", "zzzzz-dz642-queuedcontainer"})
186 func (s *IntegrationSuite) TestMissingFromSqueue(c *C) {
187 container := s.integrationTest(c,
189 fmt.Sprintf("--job-name=%s", "zzzzz-dz642-queuedcontainer"),
190 fmt.Sprintf("--mem=%d", 11445),
191 fmt.Sprintf("--cpus-per-task=%d", 4),
192 fmt.Sprintf("--tmp=%d", 45777),
193 fmt.Sprintf("--nice=%d", 9990)}},
194 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
195 dispatcher.UpdateState(container.UUID, dispatch.Running)
196 time.Sleep(3 * time.Second)
197 dispatcher.UpdateState(container.UUID, dispatch.Complete)
199 c.Check(container.State, Equals, arvados.ContainerStateCancelled)
202 func (s *IntegrationSuite) TestSbatchFail(c *C) {
203 s.slurm = slurmFake{errBatch: errors.New("something terrible happened")}
204 container := s.integrationTest(c,
205 [][]string{{"--job-name=zzzzz-dz642-queuedcontainer", "--mem=11445", "--cpus-per-task=4", "--tmp=45777", "--nice=9990"}},
206 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
207 dispatcher.UpdateState(container.UUID, dispatch.Running)
208 dispatcher.UpdateState(container.UUID, dispatch.Complete)
210 c.Check(container.State, Equals, arvados.ContainerStateComplete)
212 arv, err := arvadosclient.MakeArvadosClient()
215 var ll arvados.LogList
216 err = arv.List("logs", arvadosclient.Dict{"filters": [][]string{
217 {"object_uuid", "=", container.UUID},
218 {"event_type", "=", "dispatch"},
220 c.Assert(len(ll.Items), Equals, 1)
223 func (s *IntegrationSuite) TestChangePriority(c *C) {
224 s.slurm = slurmFake{queue: "zzzzz-dz642-queuedcontainer 9990 100\n"}
225 container := s.integrationTest(c, nil,
226 func(dispatcher *dispatch.Dispatcher, container arvados.Container) {
227 dispatcher.UpdateState(container.UUID, dispatch.Running)
228 time.Sleep(time.Second)
229 dispatcher.Arv.Update("containers", container.UUID,
231 "container": arvadosclient.Dict{"priority": 600}},
233 time.Sleep(time.Second)
234 dispatcher.UpdateState(container.UUID, dispatch.Complete)
236 c.Check(container.State, Equals, arvados.ContainerStateComplete)
237 c.Assert(len(s.slurm.didRenice), Not(Equals), 0)
238 c.Check(s.slurm.didRenice[len(s.slurm.didRenice)-1], DeepEquals, []string{"zzzzz-dz642-queuedcontainer", "4000"})
241 type StubbedSuite struct {
245 func (s *StubbedSuite) SetUpTest(c *C) {
246 s.disp = Dispatcher{}
250 func (s *StubbedSuite) TestAPIErrorGettingContainers(c *C) {
251 apiStubResponses := make(map[string]arvadostest.StubResponse)
252 apiStubResponses["/arvados/v1/api_client_authorizations/current"] = arvadostest.StubResponse{200, `{"uuid":"` + arvadostest.Dispatch1AuthUUID + `"}`}
253 apiStubResponses["/arvados/v1/containers"] = arvadostest.StubResponse{500, string(`{}`)}
255 s.testWithServerStub(c, apiStubResponses, "echo", "Error getting list of containers")
258 func (s *StubbedSuite) testWithServerStub(c *C, apiStubResponses map[string]arvadostest.StubResponse, crunchCmd string, expected string) {
259 apiStub := arvadostest.ServerStub{apiStubResponses}
261 api := httptest.NewServer(&apiStub)
264 arv := &arvadosclient.ArvadosClient{
266 ApiServer: api.URL[7:],
268 Client: &http.Client{Transport: &http.Transport{}},
272 buf := bytes.NewBuffer(nil)
273 log.SetOutput(io.MultiWriter(buf, os.Stderr))
274 defer log.SetOutput(os.Stderr)
276 s.disp.CrunchRunCommand = []string{crunchCmd}
278 ctx, cancel := context.WithCancel(context.Background())
279 dispatcher := dispatch.Dispatcher{
281 PollPeriod: time.Duration(1) * time.Second,
282 RunContainer: func(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) {
284 time.Sleep(1 * time.Second)
285 disp.UpdateState(ctr.UUID, dispatch.Running)
286 disp.UpdateState(ctr.UUID, dispatch.Complete)
288 s.disp.runContainer(disp, ctr, status)
294 for i := 0; i < 80 && !strings.Contains(buf.String(), expected); i++ {
295 time.Sleep(100 * time.Millisecond)
300 err := dispatcher.Run(ctx)
301 c.Assert(err, Equals, context.Canceled)
303 c.Check(buf.String(), Matches, `(?ms).*`+expected+`.*`)
306 func (s *StubbedSuite) TestNoSuchConfigFile(c *C) {
307 err := s.disp.readConfig("/nosuchdir89j7879/8hjwr7ojgyy7")
308 c.Assert(err, NotNil)
311 func (s *StubbedSuite) TestBadSbatchArgsConfig(c *C) {
312 tmpfile, err := ioutil.TempFile(os.TempDir(), "config")
314 defer os.Remove(tmpfile.Name())
316 _, err = tmpfile.Write([]byte(`{"SbatchArguments": "oops this is not a string array"}`))
319 err = s.disp.readConfig(tmpfile.Name())
320 c.Assert(err, NotNil)
323 func (s *StubbedSuite) TestNoSuchArgInConfigIgnored(c *C) {
324 tmpfile, err := ioutil.TempFile(os.TempDir(), "config")
326 defer os.Remove(tmpfile.Name())
328 _, err = tmpfile.Write([]byte(`{"NoSuchArg": "Nobody loves me, not one tiny hunk."}`))
331 err = s.disp.readConfig(tmpfile.Name())
333 c.Check(0, Equals, len(s.disp.SbatchArguments))
336 func (s *StubbedSuite) TestReadConfig(c *C) {
337 tmpfile, err := ioutil.TempFile(os.TempDir(), "config")
339 defer os.Remove(tmpfile.Name())
341 args := []string{"--arg1=v1", "--arg2", "--arg3=v3"}
342 argsS := `{"SbatchArguments": ["--arg1=v1", "--arg2", "--arg3=v3"]}`
343 _, err = tmpfile.Write([]byte(argsS))
346 err = s.disp.readConfig(tmpfile.Name())
348 c.Check(args, DeepEquals, s.disp.SbatchArguments)
351 func (s *StubbedSuite) TestSbatchArgs(c *C) {
352 container := arvados.Container{
354 RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 2},
358 for _, defaults := range [][]string{
361 {"--arg1=v1", "--arg2"},
363 c.Logf("%#v", defaults)
364 s.disp.SbatchArguments = defaults
366 args, err := s.disp.sbatchArgs(container)
367 c.Check(args, DeepEquals, append(defaults, "--job-name=123", "--mem=239", "--cpus-per-task=2", "--tmp=0", "--nice=9990"))
372 func (s *StubbedSuite) TestSbatchInstanceTypeConstraint(c *C) {
373 container := arvados.Container{
375 RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 2},
379 for _, trial := range []struct {
380 types []arvados.InstanceType
384 // Choose node type => use --constraint arg
386 types: []arvados.InstanceType{
387 {Name: "a1.tiny", Price: 0.02, RAM: 128000000, VCPUs: 1},
388 {Name: "a1.small", Price: 0.04, RAM: 256000000, VCPUs: 2},
389 {Name: "a1.medium", Price: 0.08, RAM: 512000000, VCPUs: 4},
391 sbatchArgs: []string{"--constraint=instancetype=a1.small"},
393 // No node types configured => no slurm constraint
398 // No node type is big enough => error
400 types: []arvados.InstanceType{
401 {Name: "a1.tiny", Price: 0.02, RAM: 128000000, VCPUs: 1},
403 err: dispatchcloud.ErrConstraintsNotSatisfiable,
407 s.disp.cluster = &arvados.Cluster{InstanceTypes: trial.types}
409 args, err := s.disp.sbatchArgs(container)
410 c.Check(err, Equals, trial.err)
411 if trial.err == nil {
412 c.Check(args, DeepEquals, append([]string{"--job-name=123", "--mem=239", "--cpus-per-task=2", "--tmp=0", "--nice=9990"}, trial.sbatchArgs...))
417 func (s *StubbedSuite) TestSbatchPartition(c *C) {
418 container := arvados.Container{
420 RuntimeConstraints: arvados.RuntimeConstraints{RAM: 250000000, VCPUs: 1},
421 SchedulingParameters: arvados.SchedulingParameters{Partitions: []string{"blurb", "b2"}},
425 args, err := s.disp.sbatchArgs(container)
426 c.Check(args, DeepEquals, []string{
427 "--job-name=123", "--mem=239", "--cpus-per-task=1", "--tmp=0", "--nice=9990",
428 "--partition=blurb,b2",