13996: Explicitly get string representation of URLs
[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         "os"
10         "strings"
11         "testing"
12         "time"
13
14         "git.curoverse.com/arvados.git/sdk/go/arvados"
15         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
16         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
17         "git.curoverse.com/arvados.git/sdk/go/keepclient"
18         "github.com/sirupsen/logrus"
19         check "gopkg.in/check.v1"
20 )
21
22 var _ = check.Suite(&integrationSuite{})
23
24 type integrationSuite struct {
25         config     Config
26         keepClient *keepclient.KeepClient
27 }
28
29 func (s *integrationSuite) SetUpSuite(c *check.C) {
30         if testing.Short() {
31                 c.Skip("-short")
32         }
33         arvadostest.ResetEnv()
34         arvadostest.StartAPI()
35         arvadostest.StartKeep(4, true)
36
37         arv, err := arvadosclient.MakeArvadosClient()
38         arv.ApiToken = arvadostest.DataManagerToken
39         c.Assert(err, check.IsNil)
40
41         s.keepClient, err = keepclient.MakeKeepClient(arv)
42         c.Assert(err, check.IsNil)
43         s.putReplicas(c, "foo", 4)
44         s.putReplicas(c, "bar", 1)
45 }
46
47 func (s *integrationSuite) putReplicas(c *check.C, data string, replicas int) {
48         s.keepClient.Want_replicas = replicas
49         _, _, err := s.keepClient.PutB([]byte(data))
50         c.Assert(err, check.IsNil)
51 }
52
53 func (s *integrationSuite) TearDownSuite(c *check.C) {
54         if testing.Short() {
55                 c.Skip("-short")
56         }
57         arvadostest.StopKeep(4)
58         arvadostest.StopAPI()
59 }
60
61 func (s *integrationSuite) SetUpTest(c *check.C) {
62         s.config = Config{
63                 Client: arvados.Client{
64                         APIHost:   os.Getenv("ARVADOS_API_HOST"),
65                         AuthToken: arvadostest.DataManagerToken,
66                         Insecure:  true,
67                 },
68                 KeepServiceTypes: []string{"disk"},
69                 RunPeriod:        arvados.Duration(time.Second),
70         }
71 }
72
73 func (s *integrationSuite) TestBalanceAPIFixtures(c *check.C) {
74         var logBuf *bytes.Buffer
75         for iter := 0; iter < 20; iter++ {
76                 logBuf := &bytes.Buffer{}
77                 logger := logrus.New()
78                 logger.Out = logBuf
79                 opts := RunOptions{
80                         CommitPulls: true,
81                         CommitTrash: true,
82                         Logger:      logger,
83                 }
84
85                 bal := &Balancer{
86                         Logger:  logger,
87                         Metrics: newMetrics(),
88                 }
89                 nextOpts, err := bal.Run(s.config, opts)
90                 c.Check(err, check.IsNil)
91                 c.Check(nextOpts.SafeRendezvousState, check.Not(check.Equals), "")
92                 c.Check(nextOpts.CommitPulls, check.Equals, true)
93                 if iter == 0 {
94                         c.Check(logBuf.String(), check.Matches, `(?ms).*ChangeSet{Pulls:1.*`)
95                         c.Check(logBuf.String(), check.Not(check.Matches), `(?ms).*ChangeSet{.*Trashes:[^0]}*`)
96                 } else if strings.Contains(logBuf.String(), "ChangeSet{Pulls:0") {
97                         break
98                 }
99                 time.Sleep(200 * time.Millisecond)
100         }
101         c.Check(logBuf.String(), check.Not(check.Matches), `(?ms).*0 replicas (0 blocks, 0 bytes) underreplicated.*`)
102 }