1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.curoverse.com/arvados.git/sdk/go/arvados"
15 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
16 "github.com/sirupsen/logrus"
17 check "gopkg.in/check.v1"
20 // Gocheck boilerplate
21 func Test(t *testing.T) {
25 var _ = check.Suite(&IntegrationSuite{})
27 func logger() logrus.FieldLogger {
28 logger := logrus.StandardLogger()
29 if os.Getenv("ARVADOS_DEBUG") != "" {
30 logger.SetLevel(logrus.DebugLevel)
35 type IntegrationSuite struct{}
37 func (suite *IntegrationSuite) TearDownTest(c *check.C) {
38 err := arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil)
39 c.Check(err, check.IsNil)
42 func (suite *IntegrationSuite) TestGetLockUnlockCancel(c *check.C) {
43 typeChooser := func(ctr *arvados.Container) (arvados.InstanceType, error) {
44 return arvados.InstanceType{Name: "testType"}, nil
47 client := arvados.NewClientFromEnv()
48 cq := NewQueue(logger(), nil, typeChooser, client)
51 c.Check(err, check.IsNil)
53 ents, threshold := cq.Entries()
54 c.Check(len(ents), check.Not(check.Equals), 0)
55 c.Check(time.Since(threshold) < time.Minute, check.Equals, true)
56 c.Check(time.Since(threshold) > 0, check.Equals, true)
58 _, ok := ents[arvadostest.QueuedContainerUUID]
59 c.Check(ok, check.Equals, true)
62 for uuid, ent := range ents {
63 c.Check(ent.Container.UUID, check.Equals, uuid)
64 c.Check(ent.InstanceType.Name, check.Equals, "testType")
65 c.Check(ent.Container.State, check.Equals, arvados.ContainerStateQueued)
66 c.Check(ent.Container.Priority > 0, check.Equals, true)
68 ctr, ok := cq.Get(uuid)
69 c.Check(ok, check.Equals, true)
70 c.Check(ctr.UUID, check.Equals, uuid)
75 err := cq.Unlock(uuid)
76 c.Check(err, check.NotNil)
78 c.Check(err, check.IsNil)
79 ctr, ok := cq.Get(uuid)
80 c.Check(ok, check.Equals, true)
81 c.Check(ctr.State, check.Equals, arvados.ContainerStateLocked)
83 c.Check(err, check.NotNil)
85 c.Check(err, check.IsNil)
86 ctr, ok = cq.Get(uuid)
87 c.Check(ok, check.Equals, true)
88 c.Check(ctr.State, check.Equals, arvados.ContainerStateQueued)
90 c.Check(err, check.NotNil)
95 err = cq.Cancel(arvadostest.CompletedContainerUUID)
96 c.Check(err, check.ErrorMatches, `.*State cannot change from Complete to Cancelled.*`)
99 func (suite *IntegrationSuite) TestCancelIfNoInstanceType(c *check.C) {
100 errorTypeChooser := func(ctr *arvados.Container) (arvados.InstanceType, error) {
101 return arvados.InstanceType{}, errors.New("no suitable instance type")
104 client := arvados.NewClientFromEnv()
105 cq := NewQueue(logger(), nil, errorTypeChooser, client)
107 var ctr arvados.Container
108 err := client.RequestAndDecode(&ctr, "GET", "arvados/v1/containers/"+arvadostest.QueuedContainerUUID, nil, nil)
109 c.Check(err, check.IsNil)
110 c.Check(ctr.State, check.Equals, arvados.ContainerStateQueued)
114 // Wait for the cancel operation to take effect. Container
115 // will have state=Cancelled or just disappear from the queue.
116 suite.waitfor(c, time.Second, func() bool {
117 err := client.RequestAndDecode(&ctr, "GET", "arvados/v1/containers/"+arvadostest.QueuedContainerUUID, nil, nil)
118 return err == nil && ctr.State == arvados.ContainerStateCancelled
120 c.Check(ctr.RuntimeStatus["error"], check.Equals, `no suitable instance type`)
123 func (suite *IntegrationSuite) waitfor(c *check.C, timeout time.Duration, fn func() bool) {
125 c.Check(fn(), check.Equals, true)
127 deadline := time.Now().Add(timeout)
128 for !fn() && time.Now().Before(deadline) {
129 time.Sleep(timeout / 1000)