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