1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
13 "git.curoverse.com/arvados.git/sdk/go/arvados"
14 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
15 check "gopkg.in/check.v1"
18 // Gocheck boilerplate
19 func Test(t *testing.T) {
23 var _ = check.Suite(&IntegrationSuite{})
25 type IntegrationSuite struct{}
27 func (suite *IntegrationSuite) TearDownTest(c *check.C) {
28 err := arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil)
29 c.Check(err, check.IsNil)
32 func (suite *IntegrationSuite) TestGetLockUnlockCancel(c *check.C) {
33 typeChooser := func(ctr *arvados.Container) (arvados.InstanceType, error) {
34 return arvados.InstanceType{Name: "testType"}, nil
37 client := arvados.NewClientFromEnv()
38 cq := NewQueue(test.Logger(), nil, typeChooser, client)
41 c.Check(err, check.IsNil)
43 ents, threshold := cq.Entries()
44 c.Check(len(ents), check.Not(check.Equals), 0)
45 c.Check(time.Since(threshold) < time.Minute, check.Equals, true)
46 c.Check(time.Since(threshold) > 0, check.Equals, true)
48 _, ok := ents[arvadostest.QueuedContainerUUID]
49 c.Check(ok, check.Equals, true)
52 for uuid, ent := range ents {
53 c.Check(ent.Container.UUID, check.Equals, uuid)
54 c.Check(ent.InstanceType.Name, check.Equals, "testType")
55 c.Check(ent.Container.State, check.Equals, arvados.ContainerStateQueued)
56 c.Check(ent.Container.Priority > 0, check.Equals, true)
58 ctr, ok := cq.Get(uuid)
59 c.Check(ok, check.Equals, true)
60 c.Check(ctr.UUID, check.Equals, uuid)
65 err := cq.Unlock(uuid)
66 c.Check(err, check.NotNil)
68 c.Check(err, check.IsNil)
69 ctr, ok := cq.Get(uuid)
70 c.Check(ok, check.Equals, true)
71 c.Check(ctr.State, check.Equals, arvados.ContainerStateLocked)
73 c.Check(err, check.NotNil)
75 c.Check(err, check.IsNil)
76 ctr, ok = cq.Get(uuid)
77 c.Check(ok, check.Equals, true)
78 c.Check(ctr.State, check.Equals, arvados.ContainerStateQueued)
80 c.Check(err, check.NotNil)
85 err = cq.Cancel(arvadostest.CompletedContainerUUID)
86 c.Check(err, check.ErrorMatches, `.*State cannot change from Complete to Cancelled.*`)
89 func (suite *IntegrationSuite) TestCancelIfNoInstanceType(c *check.C) {
90 errorTypeChooser := func(ctr *arvados.Container) (arvados.InstanceType, error) {
91 return arvados.InstanceType{}, errors.New("no suitable instance type")
94 client := arvados.NewClientFromEnv()
95 cq := NewQueue(test.Logger(), nil, errorTypeChooser, client)
97 var ctr arvados.Container
98 err := client.RequestAndDecode(&ctr, "GET", "arvados/v1/containers/"+arvadostest.QueuedContainerUUID, nil, nil)
99 c.Check(err, check.IsNil)
100 c.Check(ctr.State, check.Equals, arvados.ContainerStateQueued)
104 // Wait for the cancel operation to take effect. Container
105 // will have state=Cancelled or just disappear from the queue.
106 suite.waitfor(c, time.Second, func() bool {
107 err := client.RequestAndDecode(&ctr, "GET", "arvados/v1/containers/"+arvadostest.QueuedContainerUUID, nil, nil)
108 return err == nil && ctr.State == arvados.ContainerStateCancelled
110 c.Check(ctr.RuntimeStatus["error"], check.Equals, `no suitable instance type`)
113 func (suite *IntegrationSuite) waitfor(c *check.C, timeout time.Duration, fn func() bool) {
115 c.Check(fn(), check.Equals, true)
117 deadline := time.Now().Add(timeout)
118 for !fn() && time.Now().Before(deadline) {
119 time.Sleep(timeout / 1000)