X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/5fd9b5a5fb88f40b999ab6c9fa9435ad01f595ff..3a3d67ccee068a85aa3b79c5abd40170223071e3:/services/keepstore/s3_volume_test.go diff --git a/services/keepstore/s3_volume_test.go b/services/keepstore/s3_volume_test.go index 76dcbc9f9e..e88efffe41 100644 --- a/services/keepstore/s3_volume_test.go +++ b/services/keepstore/s3_volume_test.go @@ -1,17 +1,26 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + package main import ( "bytes" + "context" "crypto/md5" + "encoding/json" "fmt" "io/ioutil" - "log" + "net/http" + "net/http/httptest" "os" "time" "git.curoverse.com/arvados.git/sdk/go/arvados" "github.com/AdRoll/goamz/s3" "github.com/AdRoll/goamz/s3/s3test" + "github.com/ghodss/yaml" + "github.com/prometheus/client_golang/prometheus" check "gopkg.in/check.v1" ) @@ -81,6 +90,125 @@ func (s *StubbedS3Suite) TestIndex(c *check.C) { } } +func (s *StubbedS3Suite) TestStats(c *check.C) { + v := s.newTestableVolume(c, 5*time.Minute, false, 2) + stats := func() string { + buf, err := json.Marshal(v.InternalStats()) + c.Check(err, check.IsNil) + return string(buf) + } + + c.Check(stats(), check.Matches, `.*"Ops":0,.*`) + + loc := "acbd18db4cc2f85cedef654fccc4a4d8" + _, err := v.Get(context.Background(), loc, make([]byte, 3)) + c.Check(err, check.NotNil) + c.Check(stats(), check.Matches, `.*"Ops":[^0],.*`) + c.Check(stats(), check.Matches, `.*"\*s3.Error 404 [^"]*":[^0].*`) + c.Check(stats(), check.Matches, `.*"InBytes":0,.*`) + + err = v.Put(context.Background(), loc, []byte("foo")) + c.Check(err, check.IsNil) + c.Check(stats(), check.Matches, `.*"OutBytes":3,.*`) + c.Check(stats(), check.Matches, `.*"PutOps":2,.*`) + + _, err = v.Get(context.Background(), loc, make([]byte, 3)) + c.Check(err, check.IsNil) + _, err = v.Get(context.Background(), loc, make([]byte, 3)) + c.Check(err, check.IsNil) + c.Check(stats(), check.Matches, `.*"InBytes":6,.*`) +} + +type blockingHandler struct { + requested chan *http.Request + unblock chan struct{} +} + +func (h *blockingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if h.requested != nil { + h.requested <- r + } + if h.unblock != nil { + <-h.unblock + } + http.Error(w, "nothing here", http.StatusNotFound) +} + +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() + + v := s.newTestableVolume(c, 5*time.Minute, false, 2) + vol := *v.S3Volume + vol.Endpoint = srv.URL + v = &TestableS3Volume{S3Volume: &vol} + 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()) + + handler.requested = make(chan *http.Request) + handler.unblock = make(chan struct{}) + defer close(handler.unblock) + + doneFunc := make(chan struct{}) + go func() { + err := testFunc(ctx, v) + c.Check(err, check.Equals, context.Canceled) + close(doneFunc) + }() + + timeout := time.After(10 * time.Second) + + // Wait for the stub server to receive a request, meaning + // Get() is waiting for an s3 operation. + select { + case <-timeout: + 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: + } + + cancel() + + select { + case <-timeout: + c.Fatal("timed out") + case <-doneFunc: + } +} + func (s *StubbedS3Suite) TestBackendStates(c *check.C) { defer func(tl, bs arvados.Duration) { theConfig.TrashLifetime = tl @@ -223,24 +351,24 @@ func (s *StubbedS3Suite) TestBackendStates(c *check.C) { // Check canGet loc, blk := setupScenario() buf := make([]byte, len(blk)) - _, err := v.Get(loc, buf) + _, err := v.Get(context.Background(), loc, buf) c.Check(err == nil, check.Equals, scenario.canGet) if err != nil { c.Check(os.IsNotExist(err), check.Equals, true) } // 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(loc, buf) + _, err = v.Get(context.Background(), loc, buf) c.Check(err == nil, check.Equals, scenario.canGetAfterTrash) if err != nil { c.Check(os.IsNotExist(err), check.Equals, true) } // 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 { @@ -248,13 +376,13 @@ func (s *StubbedS3Suite) TestBackendStates(c *check.C) { // should be able to Get after Untrash -- // regardless of timestamps, errors, race // conditions, etc. - _, err = v.Get(loc, buf) + _, err = v.Get(context.Background(), loc, buf) c.Check(err, check.IsNil) } // 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) @@ -269,7 +397,7 @@ func (s *StubbedS3Suite) TestBackendStates(c *check.C) { // Check for current Mtime after Put (applies to all // scenarios) loc, blk = setupScenario() - err = v.Put(loc, blk) + err = v.Put(context.Background(), loc, blk) c.Check(err, check.IsNil) t, err := v.Mtime(loc) c.Check(err, check.IsNil) @@ -289,18 +417,9 @@ func (s *StubbedS3Suite) newTestableVolume(c *check.C, raceWindow time.Duration, srv, err := s3test.NewServer(&s3test.Config{Clock: clock}) c.Assert(err, check.IsNil) - tmp, err := ioutil.TempFile("", "keepstore") - c.Assert(err, check.IsNil) - defer os.Remove(tmp.Name()) - _, err = tmp.Write([]byte("xxx\n")) - c.Assert(err, check.IsNil) - c.Assert(tmp.Close(), check.IsNil) - v := &TestableS3Volume{ S3Volume: &S3Volume{ Bucket: TestBucketName, - AccessKeyFile: tmp.Name(), - SecretKeyFile: tmp.Name(), Endpoint: srv.URL(), Region: "test-region-1", LocationConstraint: true, @@ -310,20 +429,54 @@ func (s *StubbedS3Suite) newTestableVolume(c *check.C, raceWindow time.Duration, ReadOnly: readonly, IndexPageSize: 1000, }, + c: c, server: srv, serverClock: clock, } - c.Assert(v.Start(), check.IsNil) + 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 (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()) + _, err = tmp.Write([]byte("xxx\n")) + v.c.Assert(err, check.IsNil) + v.c.Assert(tmp.Close(), check.IsNil) + + v.S3Volume.AccessKeyFile = tmp.Name() + v.S3Volume.SecretKeyFile = tmp.Name() + + v.c.Assert(v.S3Volume.Start(m), check.IsNil) + return nil +} + // PutRaw skips the ContentMD5 test 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) } }