11002: Merge branch 'master' into 11002-arvput-crash-fix
[arvados.git] / services / keep-balance / integration_test.go
1 package main
2
3 import (
4         "bytes"
5         "log"
6         "net/http"
7         "os"
8         "strings"
9         "testing"
10         "time"
11
12         "git.curoverse.com/arvados.git/sdk/go/arvados"
13         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
14         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
15         "git.curoverse.com/arvados.git/sdk/go/keepclient"
16
17         check "gopkg.in/check.v1"
18 )
19
20 var _ = check.Suite(&integrationSuite{})
21
22 type integrationSuite struct {
23         config     Config
24         keepClient *keepclient.KeepClient
25 }
26
27 func (s *integrationSuite) SetUpSuite(c *check.C) {
28         if testing.Short() {
29                 c.Skip("-short")
30         }
31         arvadostest.ResetEnv()
32         arvadostest.StartAPI()
33         arvadostest.StartKeep(4, true)
34
35         arv, err := arvadosclient.MakeArvadosClient()
36         arv.ApiToken = arvadostest.DataManagerToken
37         c.Assert(err, check.IsNil)
38         s.keepClient = &keepclient.KeepClient{
39                 Arvados: arv,
40                 Client:  &http.Client{},
41         }
42         c.Assert(s.keepClient.DiscoverKeepServers(), 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         }
70 }
71
72 func (s *integrationSuite) TestBalanceAPIFixtures(c *check.C) {
73         var logBuf *bytes.Buffer
74         for iter := 0; iter < 20; iter++ {
75                 logBuf := &bytes.Buffer{}
76                 opts := RunOptions{
77                         CommitPulls: true,
78                         CommitTrash: true,
79                         Logger:      log.New(logBuf, "", log.LstdFlags),
80                 }
81                 nextOpts, err := (&Balancer{}).Run(s.config, opts)
82                 c.Check(err, check.IsNil)
83                 c.Check(nextOpts.SafeRendezvousState, check.Not(check.Equals), "")
84                 c.Check(nextOpts.CommitPulls, check.Equals, true)
85                 if iter == 0 {
86                         c.Check(logBuf.String(), check.Matches, `(?ms).*ChangeSet{Pulls:1.*`)
87                         c.Check(logBuf.String(), check.Not(check.Matches), `(?ms).*ChangeSet{.*Trashes:[^0]}*`)
88                 } else if strings.Contains(logBuf.String(), "ChangeSet{Pulls:0") {
89                         break
90                 }
91                 time.Sleep(200 * time.Millisecond)
92         }
93         c.Check(logBuf.String(), check.Not(check.Matches), `(?ms).*0 replicas (0 blocks, 0 bytes) underreplicated.*`)
94 }