13011: Fix 411 error when writing empty block to Azure storage.
[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         "log"
10         "os"
11         "strings"
12         "testing"
13         "time"
14
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
20         check "gopkg.in/check.v1"
21 )
22
23 var _ = check.Suite(&integrationSuite{})
24
25 type integrationSuite struct {
26         config     Config
27         keepClient *keepclient.KeepClient
28 }
29
30 func (s *integrationSuite) SetUpSuite(c *check.C) {
31         if testing.Short() {
32                 c.Skip("-short")
33         }
34         arvadostest.ResetEnv()
35         arvadostest.StartAPI()
36         arvadostest.StartKeep(4, true)
37
38         arv, err := arvadosclient.MakeArvadosClient()
39         arv.ApiToken = arvadostest.DataManagerToken
40         c.Assert(err, check.IsNil)
41
42         s.keepClient, err = keepclient.MakeKeepClient(arv)
43         c.Assert(err, check.IsNil)
44         s.putReplicas(c, "foo", 4)
45         s.putReplicas(c, "bar", 1)
46 }
47
48 func (s *integrationSuite) putReplicas(c *check.C, data string, replicas int) {
49         s.keepClient.Want_replicas = replicas
50         _, _, err := s.keepClient.PutB([]byte(data))
51         c.Assert(err, check.IsNil)
52 }
53
54 func (s *integrationSuite) TearDownSuite(c *check.C) {
55         if testing.Short() {
56                 c.Skip("-short")
57         }
58         arvadostest.StopKeep(4)
59         arvadostest.StopAPI()
60 }
61
62 func (s *integrationSuite) SetUpTest(c *check.C) {
63         s.config = Config{
64                 Client: arvados.Client{
65                         APIHost:   os.Getenv("ARVADOS_API_HOST"),
66                         AuthToken: arvadostest.DataManagerToken,
67                         Insecure:  true,
68                 },
69                 KeepServiceTypes: []string{"disk"},
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                 opts := RunOptions{
78                         CommitPulls: true,
79                         CommitTrash: true,
80                         Logger:      log.New(logBuf, "", log.LstdFlags),
81                 }
82                 nextOpts, err := (&Balancer{}).Run(s.config, opts)
83                 c.Check(err, check.IsNil)
84                 c.Check(nextOpts.SafeRendezvousState, check.Not(check.Equals), "")
85                 c.Check(nextOpts.CommitPulls, check.Equals, true)
86                 if iter == 0 {
87                         c.Check(logBuf.String(), check.Matches, `(?ms).*ChangeSet{Pulls:1.*`)
88                         c.Check(logBuf.String(), check.Not(check.Matches), `(?ms).*ChangeSet{.*Trashes:[^0]}*`)
89                 } else if strings.Contains(logBuf.String(), "ChangeSet{Pulls:0") {
90                         break
91                 }
92                 time.Sleep(200 * time.Millisecond)
93         }
94         c.Check(logBuf.String(), check.Not(check.Matches), `(?ms).*0 replicas (0 blocks, 0 bytes) underreplicated.*`)
95 }