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