Fix stale cached container state after successful Cancel.
[arvados.git] / lib / dispatchcloud / container / queue_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package container
6
7 import (
8         "errors"
9         "os"
10         "sync"
11         "testing"
12         "time"
13
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"
18 )
19
20 // Gocheck boilerplate
21 func Test(t *testing.T) {
22         check.TestingT(t)
23 }
24
25 var _ = check.Suite(&IntegrationSuite{})
26
27 func logger() logrus.FieldLogger {
28         logger := logrus.StandardLogger()
29         if os.Getenv("ARVADOS_DEBUG") != "" {
30                 logger.SetLevel(logrus.DebugLevel)
31         }
32         return logger
33 }
34
35 type IntegrationSuite struct{}
36
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)
40 }
41
42 func (suite *IntegrationSuite) TestGetLockUnlockCancel(c *check.C) {
43         typeChooser := func(ctr *arvados.Container) (arvados.InstanceType, error) {
44                 return arvados.InstanceType{Name: "testType"}, nil
45         }
46
47         client := arvados.NewClientFromEnv()
48         cq := NewQueue(logger(), nil, typeChooser, client)
49
50         err := cq.Update()
51         c.Check(err, check.IsNil)
52
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)
57
58         _, ok := ents[arvadostest.QueuedContainerUUID]
59         c.Check(ok, check.Equals, true)
60
61         var wg sync.WaitGroup
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)
67
68                 ctr, ok := cq.Get(uuid)
69                 c.Check(ok, check.Equals, true)
70                 c.Check(ctr.UUID, check.Equals, uuid)
71
72                 wg.Add(1)
73                 go func() {
74                         defer wg.Done()
75                         err := cq.Unlock(uuid)
76                         c.Check(err, check.NotNil)
77
78                         err = cq.Lock(uuid)
79                         c.Check(err, check.IsNil)
80                         ctr, ok := cq.Get(uuid)
81                         c.Check(ok, check.Equals, true)
82                         c.Check(ctr.State, check.Equals, arvados.ContainerStateLocked)
83                         err = cq.Lock(uuid)
84                         c.Check(err, check.NotNil)
85
86                         err = cq.Unlock(uuid)
87                         c.Check(err, check.IsNil)
88                         ctr, ok = cq.Get(uuid)
89                         c.Check(ok, check.Equals, true)
90                         c.Check(ctr.State, check.Equals, arvados.ContainerStateQueued)
91                         err = cq.Unlock(uuid)
92                         c.Check(err, check.NotNil)
93
94                         err = cq.Cancel(uuid)
95                         c.Check(err, check.IsNil)
96                         ctr, ok = cq.Get(uuid)
97                         c.Check(ok, check.Equals, true)
98                         c.Check(ctr.State, check.Equals, arvados.ContainerStateCancelled)
99                         err = cq.Lock(uuid)
100                         c.Check(err, check.NotNil)
101                 }()
102         }
103         wg.Wait()
104
105         err = cq.Cancel(arvadostest.CompletedContainerUUID)
106         c.Check(err, check.ErrorMatches, `.*State cannot change from Complete to Cancelled.*`)
107 }
108
109 func (suite *IntegrationSuite) TestCancelIfNoInstanceType(c *check.C) {
110         errorTypeChooser := func(ctr *arvados.Container) (arvados.InstanceType, error) {
111                 return arvados.InstanceType{}, errors.New("no suitable instance type")
112         }
113
114         client := arvados.NewClientFromEnv()
115         cq := NewQueue(logger(), nil, errorTypeChooser, client)
116
117         var ctr arvados.Container
118         err := client.RequestAndDecode(&ctr, "GET", "arvados/v1/containers/"+arvadostest.QueuedContainerUUID, nil, nil)
119         c.Check(err, check.IsNil)
120         c.Check(ctr.State, check.Equals, arvados.ContainerStateQueued)
121
122         cq.Update()
123
124         // Wait for the cancel operation to take effect. Container
125         // will have state=Cancelled or just disappear from the queue.
126         suite.waitfor(c, time.Second, func() bool {
127                 err := client.RequestAndDecode(&ctr, "GET", "arvados/v1/containers/"+arvadostest.QueuedContainerUUID, nil, nil)
128                 return err == nil && ctr.State == arvados.ContainerStateCancelled
129         })
130         c.Check(ctr.RuntimeStatus["error"], check.Equals, `no suitable instance type`)
131 }
132
133 func (suite *IntegrationSuite) waitfor(c *check.C, timeout time.Duration, fn func() bool) {
134         defer func() {
135                 c.Check(fn(), check.Equals, true)
136         }()
137         deadline := time.Now().Add(timeout)
138         for !fn() && time.Now().Before(deadline) {
139                 time.Sleep(timeout / 1000)
140         }
141 }