From 3779208d49e33caeb25324ffacc3a166d031bed6 Mon Sep 17 00:00:00 2001 From: Marwan Sulaiman Date: Fri, 24 May 2019 09:47:24 -0400 Subject: [PATCH] storage/s3: fix checker prefix (#1239) --- pkg/storage/compliance/tests.go | 19 +++++++++++++++++++ pkg/storage/s3/checker.go | 15 +++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/pkg/storage/compliance/tests.go b/pkg/storage/compliance/tests.go index afee4db2..48c6fb45 100644 --- a/pkg/storage/compliance/tests.go +++ b/pkg/storage/compliance/tests.go @@ -25,6 +25,7 @@ func RunTests(t *testing.T, b storage.Backend, clearBackend func() error) { testDelete(t, b) testGet(t, b) testExists(t, b) + testShouldNotExist(t, b) testCatalog(t, b) } @@ -166,6 +167,24 @@ func testExists(t *testing.T, b storage.Backend) { require.Equal(t, true, exists) } +func testShouldNotExist(t *testing.T, b storage.Backend) { + ctx := context.Background() + mod := "shouldNotExist" + ver := "v1.2.3-pre.1" + mock := getMockModule() + zipBts, _ := ioutil.ReadAll(mock.Zip) + err := b.Save(ctx, mod, ver, mock.Mod, bytes.NewReader(zipBts), mock.Info) + require.NoError(t, err, "should successfully safe a mock module") + defer b.Delete(ctx, mod, ver) + + prefixVer := "v1.2.3-pre" + exists, err := b.Exists(ctx, mod, prefixVer) + require.NoError(t, err) + if exists { + t.Fatal("a non existing version that has the same prefix of an existing version should not exist") + } +} + // testDelete tests that a module can be deleted from a // storage Backend and the the Exists method returns false // afterwards. diff --git a/pkg/storage/s3/checker.go b/pkg/storage/s3/checker.go index 623187cf..684cc23f 100644 --- a/pkg/storage/s3/checker.go +++ b/pkg/storage/s3/checker.go @@ -30,6 +30,17 @@ func (s *Storage) Exists(ctx context.Context, module, version string) (bool, err if err != nil { return false, errors.E(op, err, errors.M(module), errors.V(version)) } - - return len(loo.Contents) == 3, nil + var count int + for _, o := range loo.Contents { + // sane assumption: no duplicate keys. + switch *o.Key { + 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 }