1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/lib/config"
16 "git.arvados.org/arvados.git/sdk/go/arvados"
17 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
18 "git.arvados.org/arvados.git/sdk/go/arvadostest"
19 "git.arvados.org/arvados.git/sdk/go/ctxlog"
20 "git.arvados.org/arvados.git/sdk/go/keepclient"
21 "github.com/jmoiron/sqlx"
22 "github.com/prometheus/client_golang/prometheus"
23 "github.com/sirupsen/logrus"
24 check "gopkg.in/check.v1"
27 var _ = check.Suite(&integrationSuite{})
29 type integrationSuite struct {
30 config *arvados.Cluster
32 client *arvados.Client
33 keepClient *keepclient.KeepClient
36 func (s *integrationSuite) SetUpSuite(c *check.C) {
40 arvadostest.ResetEnv()
41 arvadostest.StartKeep(4, true)
43 arv, err := arvadosclient.MakeArvadosClient()
44 arv.ApiToken = arvadostest.SystemRootToken
45 c.Assert(err, check.IsNil)
47 s.keepClient, err = keepclient.MakeKeepClient(arv)
48 c.Assert(err, check.IsNil)
49 s.putReplicas(c, "foo", 4)
50 s.putReplicas(c, "bar", 1)
53 func (s *integrationSuite) putReplicas(c *check.C, data string, replicas int) {
54 s.keepClient.Want_replicas = replicas
55 _, _, err := s.keepClient.PutB([]byte(data))
56 c.Assert(err, check.IsNil)
59 func (s *integrationSuite) TearDownSuite(c *check.C) {
63 arvadostest.StopKeep(4)
66 func (s *integrationSuite) SetUpTest(c *check.C) {
67 cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
68 c.Assert(err, check.Equals, nil)
69 s.config, err = cfg.GetCluster("")
70 c.Assert(err, check.Equals, nil)
71 s.db, err = sqlx.Open("postgres", s.config.PostgreSQL.Connection.String())
72 c.Assert(err, check.IsNil)
73 s.config.Collections.BalancePeriod = arvados.Duration(time.Second)
75 s.client = &arvados.Client{
76 APIHost: os.Getenv("ARVADOS_API_HOST"),
77 AuthToken: arvadostest.SystemRootToken,
82 func (s *integrationSuite) TestBalanceAPIFixtures(c *check.C) {
83 var logBuf bytes.Buffer
84 for iter := 0; iter < 20; iter++ {
86 logger := logrus.New()
87 logger.Out = io.MultiWriter(&logBuf, os.Stderr)
91 CommitConfirmedFields: true,
98 Metrics: newMetrics(prometheus.NewRegistry()),
100 nextOpts, err := bal.Run(s.client, s.config, opts)
101 c.Check(err, check.IsNil)
102 c.Check(nextOpts.SafeRendezvousState, check.Not(check.Equals), "")
103 c.Check(nextOpts.CommitPulls, check.Equals, true)
105 c.Check(logBuf.String(), check.Matches, `(?ms).*ChangeSet{Pulls:1.*`)
106 c.Check(logBuf.String(), check.Not(check.Matches), `(?ms).*ChangeSet{.*Trashes:[^0]}*`)
107 } else if strings.Contains(logBuf.String(), "ChangeSet{Pulls:0") {
110 time.Sleep(200 * time.Millisecond)
112 c.Check(logBuf.String(), check.Not(check.Matches), `(?ms).*0 replicas (0 blocks, 0 bytes) underreplicated.*`)
114 for _, trial := range []struct {
119 {arvadostest.EmptyCollectionUUID, 0, []string{}},
120 {arvadostest.FooCollection, 2, []string{"default"}}, // "foo" blk
121 {arvadostest.StorageClassesDesiredDefaultConfirmedDefault, 2, []string{"default"}}, // "bar" blk
122 {arvadostest.StorageClassesDesiredArchiveConfirmedDefault, 0, []string{}}, // "bar" blk
125 var coll arvados.Collection
126 s.client.RequestAndDecode(&coll, "GET", "arvados/v1/collections/"+trial.uuid, nil, nil)
127 if c.Check(coll.ReplicationConfirmed, check.NotNil) {
128 c.Check(*coll.ReplicationConfirmed, check.Equals, trial.repl)
130 c.Check(coll.StorageClassesConfirmed, check.DeepEquals, trial.classes)