17857: Fix bsub args so containers can use multiple CPU cores.
[arvados.git] / services / keep-balance / integration_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "bytes"
9         "io"
10         "os"
11         "strings"
12         "testing"
13         "time"
14
15         "git.arvados.org/arvados.git/lib/config"
16         "git.arvados.org/arvados.git/sdk/go/arvados"
17         "git.arvados.org/arvados.git/sdk/go/arvadosclient"
18         "git.arvados.org/arvados.git/sdk/go/arvadostest"
19         "git.arvados.org/arvados.git/sdk/go/ctxlog"
20         "git.arvados.org/arvados.git/sdk/go/keepclient"
21         "github.com/jmoiron/sqlx"
22         "github.com/prometheus/client_golang/prometheus"
23         "github.com/sirupsen/logrus"
24         check "gopkg.in/check.v1"
25 )
26
27 var _ = check.Suite(&integrationSuite{})
28
29 type integrationSuite struct {
30         config     *arvados.Cluster
31         db         *sqlx.DB
32         client     *arvados.Client
33         keepClient *keepclient.KeepClient
34 }
35
36 func (s *integrationSuite) SetUpSuite(c *check.C) {
37         if testing.Short() {
38                 c.Skip("-short")
39         }
40         arvadostest.ResetEnv()
41         arvadostest.StartKeep(4, true)
42
43         arv, err := arvadosclient.MakeArvadosClient()
44         arv.ApiToken = arvadostest.SystemRootToken
45         c.Assert(err, check.IsNil)
46
47         s.keepClient, err = keepclient.MakeKeepClient(arv)
48         c.Assert(err, check.IsNil)
49         s.putReplicas(c, "foo", 4)
50         s.putReplicas(c, "bar", 1)
51 }
52
53 func (s *integrationSuite) putReplicas(c *check.C, data string, replicas int) {
54         s.keepClient.Want_replicas = replicas
55         _, _, err := s.keepClient.PutB([]byte(data))
56         c.Assert(err, check.IsNil)
57 }
58
59 func (s *integrationSuite) TearDownSuite(c *check.C) {
60         if testing.Short() {
61                 c.Skip("-short")
62         }
63         arvadostest.StopKeep(4)
64 }
65
66 func (s *integrationSuite) SetUpTest(c *check.C) {
67         cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
68         c.Assert(err, check.Equals, nil)
69         s.config, err = cfg.GetCluster("")
70         c.Assert(err, check.Equals, nil)
71         s.db, err = sqlx.Open("postgres", s.config.PostgreSQL.Connection.String())
72         c.Assert(err, check.IsNil)
73         s.config.Collections.BalancePeriod = arvados.Duration(time.Second)
74
75         s.client = &arvados.Client{
76                 APIHost:   os.Getenv("ARVADOS_API_HOST"),
77                 AuthToken: arvadostest.SystemRootToken,
78                 Insecure:  true,
79         }
80 }
81
82 func (s *integrationSuite) TestBalanceAPIFixtures(c *check.C) {
83         var logBuf bytes.Buffer
84         for iter := 0; iter < 20; iter++ {
85                 logBuf.Reset()
86                 logger := logrus.New()
87                 logger.Out = io.MultiWriter(&logBuf, os.Stderr)
88                 opts := RunOptions{
89                         CommitPulls:           true,
90                         CommitTrash:           true,
91                         CommitConfirmedFields: true,
92                         Logger:                logger,
93                 }
94
95                 bal := &Balancer{
96                         DB:      s.db,
97                         Logger:  logger,
98                         Metrics: newMetrics(prometheus.NewRegistry()),
99                 }
100                 nextOpts, err := bal.Run(s.client, s.config, opts)
101                 c.Check(err, check.IsNil)
102                 c.Check(nextOpts.SafeRendezvousState, check.Not(check.Equals), "")
103                 c.Check(nextOpts.CommitPulls, check.Equals, true)
104                 if iter == 0 {
105                         c.Check(logBuf.String(), check.Matches, `(?ms).*ChangeSet{Pulls:1.*`)
106                         c.Check(logBuf.String(), check.Not(check.Matches), `(?ms).*ChangeSet{.*Trashes:[^0]}*`)
107                 } else if strings.Contains(logBuf.String(), "ChangeSet{Pulls:0") {
108                         break
109                 }
110                 time.Sleep(200 * time.Millisecond)
111         }
112         c.Check(logBuf.String(), check.Not(check.Matches), `(?ms).*0 replicas (0 blocks, 0 bytes) underreplicated.*`)
113
114         for _, trial := range []struct {
115                 uuid    string
116                 repl    int
117                 classes []string
118         }{
119                 {arvadostest.EmptyCollectionUUID, 0, []string{}},
120                 {arvadostest.FooCollection, 2, []string{"default"}},                                // "foo" blk
121                 {arvadostest.StorageClassesDesiredDefaultConfirmedDefault, 2, []string{"default"}}, // "bar" blk
122                 {arvadostest.StorageClassesDesiredArchiveConfirmedDefault, 0, []string{}},          // "bar" blk
123         } {
124                 c.Logf("%#v", trial)
125                 var coll arvados.Collection
126                 s.client.RequestAndDecode(&coll, "GET", "arvados/v1/collections/"+trial.uuid, nil, nil)
127                 if c.Check(coll.ReplicationConfirmed, check.NotNil) {
128                         c.Check(*coll.ReplicationConfirmed, check.Equals, trial.repl)
129                 }
130                 c.Check(coll.StorageClassesConfirmed, check.DeepEquals, trial.classes)
131         }
132 }