Azureblob tests + checker fix (#1251)

* add azureblob tests

* get account name from env

* benchmarks

* skip test if account missing
This commit is contained in:
marpio
2019-06-08 00:21:09 +02:00
committed by Aaron Schlesinger
parent f0f046ae09
commit 3bda516c19
4 changed files with 112 additions and 5 deletions
+1 -1
View File
@@ -46,7 +46,7 @@ func newBlobStoreClient(accountURL *url.URL, accountName, accountKey, containerN
// Storage implements (github.com/gomods/athens/pkg/storage).Saver and
// also provides a function to fetch the location of a module
type Storage struct {
client client
client *azureBlobStoreClient
timeout time.Duration
}
+95
View File
@@ -0,0 +1,95 @@
package azureblob
import (
"context"
"fmt"
"os"
"testing"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/gomods/athens/pkg/config"
"github.com/gomods/athens/pkg/storage/compliance"
"github.com/technosophos/moniker"
)
func TestBackend(t *testing.T) {
backend := getStorage(t)
defer backend.client.containerURL.Delete(context.Background(), azblob.ContainerAccessConditions{})
compliance.RunTests(t, backend, backend.clear)
}
func BenchmarkBackend(b *testing.B) {
backend := getStorage(b)
defer backend.client.containerURL.Delete(context.Background(), azblob.ContainerAccessConditions{})
compliance.RunBenchmarks(b, backend, backend.clear)
}
func (s *Storage) clear() error {
ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
defer cancel()
for marker := (azblob.Marker{}); marker.NotDone(); {
listBlob, err := s.client.containerURL.ListBlobsFlatSegment(ctx, marker, azblob.ListBlobsSegmentOptions{})
if err != nil {
return err
}
marker = listBlob.NextMarker
for _, blob := range listBlob.Segment.BlobItems {
blobURL := s.client.containerURL.NewBlockBlobURL(blob.Name)
_, err := blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone, azblob.BlobAccessConditions{})
if err != nil {
return err
}
}
}
return nil
}
func getStorage(t testing.TB) *Storage {
t.Helper()
containerName := randomContainerName(os.Getenv("DRONE_PULL_REQUEST"))
cfg := getTestConfig(containerName)
if cfg == nil {
t.SkipNow()
}
s, err := New(cfg, config.GetTimeoutDuration(30))
if err != nil {
t.Fatal(err)
}
_, err = s.client.containerURL.Create(context.Background(), azblob.Metadata{}, azblob.PublicAccessNone)
if err != nil {
t.Fatal(err)
}
return s
}
func getTestConfig(containerName string) *config.AzureBlobConfig {
key := os.Getenv("ATHENS_AZURE_ACCOUNT_KEY")
if key == "" {
return nil
}
name := os.Getenv("ATHENS_AZURE_ACCOUNT_NAME")
if name == "" {
return nil
}
return &config.AzureBlobConfig{
AccountName: name,
AccountKey: key,
ContainerName: containerName,
}
}
func randomContainerName(prefix string) string {
// moniker is a cool library to produce mostly unique, human-readable names
// see https://github.com/technosophos/moniker for more details
namer := moniker.New()
if prefix != "" {
return fmt.Sprintf("%s_%s", prefix, namer.NameSep(""))
}
return namer.NameSep("")
}
+14 -3
View File
@@ -16,10 +16,21 @@ func (s *Storage) Exists(ctx context.Context, module string, version string) (bo
defer span.End()
px := config.PackageVersionedName(module, version, "")
blobs, err := s.client.ListBlobs(ctx, px)
paths, err := s.client.ListBlobs(ctx, px)
if err != nil {
return false, errors.E(op, err, errors.M(module), errors.V(version))
}
return len(blobs) == 3, nil
var count int
for _, p := range paths {
// sane assumption: no duplicate keys.
switch p {
case config.PackageVersionedName(module, version, "info"):
count++
case config.PackageVersionedName(module, version, "mod"):
count++
case config.PackageVersionedName(module, version, "zip"):
count++
}
}
return count == 3, nil
}
+2 -1
View File
@@ -15,7 +15,8 @@ func (s *Storage) List(ctx context.Context, module string) ([]string, error) {
ctx, span := observ.StartSpan(ctx, op.String())
defer span.End()
blobnames, err := s.client.ListBlobs(ctx, module)
modulePrefix := strings.TrimSuffix(module, "/") + "/@v"
blobnames, err := s.client.ListBlobs(ctx, modulePrefix)
if err != nil {
return nil, errors.E(op, err, errors.M(module))
}