14325: Clean up test suite logging.
[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         "sync"
10         "testing"
11         "time"
12
13         "git.curoverse.com/arvados.git/sdk/go/arvados"
14         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
15         check "gopkg.in/check.v1"
16 )
17
18 // Gocheck boilerplate
19 func Test(t *testing.T) {
20         check.TestingT(t)
21 }
22
23 var _ = check.Suite(&IntegrationSuite{})
24
25 type IntegrationSuite struct{}
26
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)
30 }
31
32 func (suite *IntegrationSuite) TestGetLockUnlockCancel(c *check.C) {
33         typeChooser := func(ctr *arvados.Container) (arvados.InstanceType, error) {
34                 return arvados.InstanceType{Name: "testType"}, nil
35         }
36
37         client := arvados.NewClientFromEnv()
38         cq := NewQueue(test.Logger(), nil, typeChooser, client)
39
40         err := cq.Update()
41         c.Check(err, check.IsNil)
42
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)
47
48         _, ok := ents[arvadostest.QueuedContainerUUID]
49         c.Check(ok, check.Equals, true)
50
51         var wg sync.WaitGroup
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)
57
58                 ctr, ok := cq.Get(uuid)
59                 c.Check(ok, check.Equals, true)
60                 c.Check(ctr.UUID, check.Equals, uuid)
61
62                 wg.Add(1)
63                 go func() {
64                         defer wg.Done()
65                         err := cq.Unlock(uuid)
66                         c.Check(err, check.NotNil)
67                         err = cq.Lock(uuid)
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)
72                         err = cq.Lock(uuid)
73                         c.Check(err, check.NotNil)
74                         err = cq.Unlock(uuid)
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)
79                         err = cq.Unlock(uuid)
80                         c.Check(err, check.NotNil)
81                 }()
82         }
83         wg.Wait()
84
85         err = cq.Cancel(arvadostest.CompletedContainerUUID)
86         c.Check(err, check.ErrorMatches, `.*State cannot change from Complete to Cancelled.*`)
87 }
88
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")
92         }
93
94         client := arvados.NewClientFromEnv()
95         cq := NewQueue(test.Logger(), nil, errorTypeChooser, client)
96
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)
101
102         cq.Update()
103
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
109         })
110         c.Check(ctr.RuntimeStatus["error"], check.Equals, `no suitable instance type`)
111 }
112
113 func (suite *IntegrationSuite) waitfor(c *check.C, timeout time.Duration, fn func() bool) {
114         defer func() {
115                 c.Check(fn(), check.Equals, true)
116         }()
117         deadline := time.Now().Add(timeout)
118         for !fn() && time.Now().Before(deadline) {
119                 time.Sleep(timeout / 1000)
120         }
121 }