13937: Refactors approach to pass volume metrics as curried vecs (WIP)
[arvados.git] / services / keepstore / s3_volume_test.go
index 702e5539dfb0f060360120bc4312ce391b523b0c..e88efffe41aa52d72ddd19553ea0b378d8da3e83 100644 (file)
@@ -1,3 +1,7 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
 package main
 
 import (
@@ -15,7 +19,8 @@ import (
        "git.curoverse.com/arvados.git/sdk/go/arvados"
        "github.com/AdRoll/goamz/s3"
        "github.com/AdRoll/goamz/s3/s3test"
-       log "github.com/Sirupsen/logrus"
+       "github.com/ghodss/yaml"
+       "github.com/prometheus/client_golang/prometheus"
        check "gopkg.in/check.v1"
 )
 
@@ -129,10 +134,35 @@ func (h *blockingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        http.Error(w, "nothing here", http.StatusNotFound)
 }
 
-func (s *StubbedS3Suite) TestClientDisconnect(c *check.C) {
+func (s *StubbedS3Suite) TestGetContextCancel(c *check.C) {
        loc := "acbd18db4cc2f85cedef654fccc4a4d8"
        buf := make([]byte, 3)
 
+       s.testContextCancel(c, func(ctx context.Context, v *TestableS3Volume) error {
+               _, err := v.Get(ctx, loc, buf)
+               return err
+       })
+}
+
+func (s *StubbedS3Suite) TestCompareContextCancel(c *check.C) {
+       loc := "acbd18db4cc2f85cedef654fccc4a4d8"
+       buf := []byte("bar")
+
+       s.testContextCancel(c, func(ctx context.Context, v *TestableS3Volume) error {
+               return v.Compare(ctx, loc, buf)
+       })
+}
+
+func (s *StubbedS3Suite) TestPutContextCancel(c *check.C) {
+       loc := "acbd18db4cc2f85cedef654fccc4a4d8"
+       buf := []byte("foo")
+
+       s.testContextCancel(c, func(ctx context.Context, v *TestableS3Volume) error {
+               return v.Put(ctx, loc, buf)
+       })
+}
+
+func (s *StubbedS3Suite) testContextCancel(c *check.C, testFunc func(context.Context, *TestableS3Volume) error) {
        handler := &blockingHandler{}
        srv := httptest.NewServer(handler)
        defer srv.Close()
@@ -141,7 +171,9 @@ func (s *StubbedS3Suite) TestClientDisconnect(c *check.C) {
        vol := *v.S3Volume
        vol.Endpoint = srv.URL
        v = &TestableS3Volume{S3Volume: &vol}
-       v.Start()
+       metrics := newVolumeMetricsVecs(prometheus.NewRegistry()).curryWith(
+               v.String(), v.Status().MountPoint, fmt.Sprintf("%d", v.Status().DeviceNum))
+       v.Start(metrics)
 
        ctx, cancel := context.WithCancel(context.Background())
 
@@ -149,12 +181,11 @@ func (s *StubbedS3Suite) TestClientDisconnect(c *check.C) {
        handler.unblock = make(chan struct{})
        defer close(handler.unblock)
 
-       var n int
-       var err error
-       doneGet := make(chan struct{})
+       doneFunc := make(chan struct{})
        go func() {
-               n, err = v.Get(ctx, loc, buf)
-               close(doneGet)
+               err := testFunc(ctx, v)
+               c.Check(err, check.Equals, context.Canceled)
+               close(doneFunc)
        }()
 
        timeout := time.After(10 * time.Second)
@@ -163,9 +194,9 @@ func (s *StubbedS3Suite) TestClientDisconnect(c *check.C) {
        // Get() is waiting for an s3 operation.
        select {
        case <-timeout:
-               c.Fatal("timed out waiting for Get to call our handler")
-       case <-doneGet:
-               c.Fatal("Get finished without calling our handler!")
+               c.Fatal("timed out waiting for test func to call our handler")
+       case <-doneFunc:
+               c.Fatal("test func finished without even calling our handler!")
        case <-handler.requested:
        }
 
@@ -174,10 +205,7 @@ func (s *StubbedS3Suite) TestClientDisconnect(c *check.C) {
        select {
        case <-timeout:
                c.Fatal("timed out")
-       case <-doneGet:
-               c.Check(n, check.Equals, 0)
-               c.Check(err, check.NotNil)
-               c.Check(err, check.Equals, context.Canceled)
+       case <-doneFunc:
        }
 }
 
@@ -330,7 +358,7 @@ func (s *StubbedS3Suite) TestBackendStates(c *check.C) {
                }
 
                // Call Trash, then check canTrash and canGetAfterTrash
-               loc, blk = setupScenario()
+               loc, _ = setupScenario()
                err = v.Trash(loc)
                c.Check(err == nil, check.Equals, scenario.canTrash)
                _, err = v.Get(context.Background(), loc, buf)
@@ -340,7 +368,7 @@ func (s *StubbedS3Suite) TestBackendStates(c *check.C) {
                }
 
                // Call Untrash, then check canUntrash
-               loc, blk = setupScenario()
+               loc, _ = setupScenario()
                err = v.Untrash(loc)
                c.Check(err == nil, check.Equals, scenario.canUntrash)
                if scenario.dataT != none || scenario.trashT != none {
@@ -354,7 +382,7 @@ func (s *StubbedS3Suite) TestBackendStates(c *check.C) {
 
                // Call EmptyTrash, then check haveTrashAfterEmpty and
                // freshAfterEmpty
-               loc, blk = setupScenario()
+               loc, _ = setupScenario()
                v.EmptyTrash()
                _, err = v.bucket.Head("trash/"+loc, nil)
                c.Check(err == nil, check.Equals, scenario.haveTrashAfterEmpty)
@@ -405,13 +433,27 @@ func (s *StubbedS3Suite) newTestableVolume(c *check.C, raceWindow time.Duration,
                server:      srv,
                serverClock: clock,
        }
-       v.Start()
+       metrics := newVolumeMetricsVecs(prometheus.NewRegistry()).curryWith(
+               v.String(), v.Status().MountPoint, fmt.Sprintf("%d", v.Status().DeviceNum))
+       v.Start(metrics)
        err = v.bucket.PutBucket(s3.ACL("private"))
        c.Assert(err, check.IsNil)
        return v
 }
 
-func (v *TestableS3Volume) Start() error {
+func (s *StubbedS3Suite) TestConfig(c *check.C) {
+       var cfg Config
+       err := yaml.Unmarshal([]byte(`
+Volumes:
+  - Type: S3
+    StorageClasses: ["class_a", "class_b"]
+`), &cfg)
+
+       c.Check(err, check.IsNil)
+       c.Check(cfg.Volumes[0].GetStorageClasses(), check.DeepEquals, []string{"class_a", "class_b"})
+}
+
+func (v *TestableS3Volume) Start(m *volumeMetrics) error {
        tmp, err := ioutil.TempFile("", "keepstore")
        v.c.Assert(err, check.IsNil)
        defer os.Remove(tmp.Name())
@@ -422,7 +464,7 @@ func (v *TestableS3Volume) Start() error {
        v.S3Volume.AccessKeyFile = tmp.Name()
        v.S3Volume.SecretKeyFile = tmp.Name()
 
-       v.c.Assert(v.S3Volume.Start(), check.IsNil)
+       v.c.Assert(v.S3Volume.Start(m), check.IsNil)
        return nil
 }
 
@@ -430,7 +472,11 @@ func (v *TestableS3Volume) Start() error {
 func (v *TestableS3Volume) PutRaw(loc string, block []byte) {
        err := v.bucket.Put(loc, block, "application/octet-stream", s3ACL, s3.Options{})
        if err != nil {
-               log.Printf("PutRaw: %+v", err)
+               log.Printf("PutRaw: %s: %+v", loc, err)
+       }
+       err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{})
+       if err != nil {
+               log.Printf("PutRaw: recent/%s: %+v", loc, err)
        }
 }