From a014af45ebad68f1c3ae77bb6988b4ee2ee203b5 Mon Sep 17 00:00:00 2001 From: dlamarmorgan Date: Thu, 19 Oct 2023 17:01:27 -0700 Subject: [PATCH] satellite/metabase: add tests CommitObjectWithSegments.Versioned Change-Id: Ied838e6bee4e2d8254708d21c90edb68bdf05916 --- satellite/metabase/commit_object_test.go | 195 ++++++++++++++++++++++- 1 file changed, 193 insertions(+), 2 deletions(-) diff --git a/satellite/metabase/commit_object_test.go b/satellite/metabase/commit_object_test.go index 57ed306df..ec7bc70f1 100644 --- a/satellite/metabase/commit_object_test.go +++ b/satellite/metabase/commit_object_test.go @@ -171,9 +171,132 @@ func TestCommitObjectWithSegments(t *testing.T) { }.Check(ctx, t, db) }) - // TODO(ver): add tests for DisallowDelete + t.Run("disallow delete but nothing to delete", func(t *testing.T) { + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - t.Run("delete previous unversioned object", func(t *testing.T) { + metabasetest.BeginObjectExactVersion{ + Opts: metabase.BeginObjectExactVersion{ + ObjectStream: metabase.ObjectStream{ + ProjectID: obj.ProjectID, + BucketName: obj.BucketName, + ObjectKey: obj.ObjectKey, + Version: 5, + StreamID: obj.StreamID, + }, + Encryption: metabasetest.DefaultEncryption, + }, + }.Check(ctx, t, db) + now := time.Now() + + encryptedMetadata := testrand.Bytes(1024) + encryptedMetadataNonce := testrand.Nonce() + encryptedMetadataKey := testrand.Bytes(265) + + metabasetest.CommitObjectWithSegments{ + Opts: metabase.CommitObjectWithSegments{ + ObjectStream: metabase.ObjectStream{ + ProjectID: obj.ProjectID, + BucketName: obj.BucketName, + ObjectKey: obj.ObjectKey, + Version: 5, + StreamID: obj.StreamID, + }, + EncryptedMetadataNonce: encryptedMetadataNonce[:], + EncryptedMetadata: encryptedMetadata, + EncryptedMetadataEncryptedKey: encryptedMetadataKey, + DisallowDelete: true, + }, + }.Check(ctx, t, db) + + metabasetest.Verify{ + Objects: []metabase.RawObject{ + { + ObjectStream: metabase.ObjectStream{ + ProjectID: obj.ProjectID, + BucketName: obj.BucketName, + ObjectKey: obj.ObjectKey, + Version: 5, + StreamID: obj.StreamID, + }, + CreatedAt: now, + Status: metabase.CommittedUnversioned, + + EncryptedMetadataNonce: encryptedMetadataNonce[:], + EncryptedMetadata: encryptedMetadata, + EncryptedMetadataEncryptedKey: encryptedMetadataKey, + + Encryption: metabasetest.DefaultEncryption, + }, + }, + }.Check(ctx, t, db) + }) + + t.Run("disallow delete when committing unversioned", func(t *testing.T) { + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + + unversionedStream := obj + unversionedStream.Version = 3 + unversionedObject := metabasetest.CreateObject(ctx, t, db, unversionedStream, 0) + + metabasetest.BeginObjectExactVersion{ + Opts: metabase.BeginObjectExactVersion{ + ObjectStream: metabase.ObjectStream{ + ProjectID: obj.ProjectID, + BucketName: obj.BucketName, + ObjectKey: obj.ObjectKey, + Version: 5, + StreamID: obj.StreamID, + }, + Encryption: metabasetest.DefaultEncryption, + }, + }.Check(ctx, t, db) + now := time.Now() + zombieDeadline := now.Add(24 * time.Hour) + + encryptedMetadata := testrand.Bytes(1024) + encryptedMetadataNonce := testrand.Nonce() + encryptedMetadataKey := testrand.Bytes(265) + + metabasetest.CommitObjectWithSegments{ + Opts: metabase.CommitObjectWithSegments{ + ObjectStream: metabase.ObjectStream{ + ProjectID: obj.ProjectID, + BucketName: obj.BucketName, + ObjectKey: obj.ObjectKey, + Version: 5, + StreamID: obj.StreamID, + }, + EncryptedMetadataNonce: encryptedMetadataNonce[:], + EncryptedMetadata: encryptedMetadata, + EncryptedMetadataEncryptedKey: encryptedMetadataKey, + DisallowDelete: true, + }, + ErrClass: &metabase.ErrPermissionDenied, + ErrText: "no permissions to delete existing object", + }.Check(ctx, t, db) + + metabasetest.Verify{ + Objects: []metabase.RawObject{ + metabase.RawObject(unversionedObject), + { + ObjectStream: metabase.ObjectStream{ + ProjectID: obj.ProjectID, + BucketName: obj.BucketName, + ObjectKey: obj.ObjectKey, + Version: 5, + StreamID: obj.StreamID, + }, + CreatedAt: now, + Status: metabase.Pending, + + Encryption: metabasetest.DefaultEncryption, + ZombieDeletionDeadline: &zombieDeadline, + }, + }, + }.Check(ctx, t, db) + }) + + t.Run("overwrite previous object when committing unversioned", func(t *testing.T) { defer metabasetest.DeleteAll{}.Check(ctx, t, db) metabasetest.CreateObject(ctx, t, db, obj, 1) @@ -304,6 +427,74 @@ func TestCommitObjectWithSegments(t *testing.T) { }.Check(ctx, t, db) }) + t.Run("keep versioned objects when committing unversioned", func(t *testing.T) { + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + + versionedStream1 := obj + versionedStream1.Version = 3 + versionedObject1 := metabasetest.CreateObjectVersioned(ctx, t, db, versionedStream1, 0) + versionedStream2 := obj + versionedStream2.Version = 4 + versionedObject2 := metabasetest.CreateObjectVersioned(ctx, t, db, versionedStream2, 0) + + metabasetest.BeginObjectExactVersion{ + Opts: metabase.BeginObjectExactVersion{ + ObjectStream: metabase.ObjectStream{ + ProjectID: obj.ProjectID, + BucketName: obj.BucketName, + ObjectKey: obj.ObjectKey, + Version: 5, + StreamID: obj.StreamID, + }, + Encryption: metabasetest.DefaultEncryption, + }, + }.Check(ctx, t, db) + now := time.Now() + + encryptedMetadata := testrand.Bytes(1024) + encryptedMetadataNonce := testrand.Nonce() + encryptedMetadataKey := testrand.Bytes(265) + + metabasetest.CommitObjectWithSegments{ + Opts: metabase.CommitObjectWithSegments{ + ObjectStream: metabase.ObjectStream{ + ProjectID: obj.ProjectID, + BucketName: obj.BucketName, + ObjectKey: obj.ObjectKey, + Version: 5, + StreamID: obj.StreamID, + }, + EncryptedMetadataNonce: encryptedMetadataNonce[:], + EncryptedMetadata: encryptedMetadata, + EncryptedMetadataEncryptedKey: encryptedMetadataKey, + }, + }.Check(ctx, t, db) + + metabasetest.Verify{ + Objects: []metabase.RawObject{ + metabase.RawObject(versionedObject1), + metabase.RawObject(versionedObject2), + { + ObjectStream: metabase.ObjectStream{ + ProjectID: obj.ProjectID, + BucketName: obj.BucketName, + ObjectKey: obj.ObjectKey, + Version: 5, + StreamID: obj.StreamID, + }, + CreatedAt: now, + Status: metabase.CommittedUnversioned, + + EncryptedMetadataNonce: encryptedMetadataNonce[:], + EncryptedMetadata: encryptedMetadata, + EncryptedMetadataEncryptedKey: encryptedMetadataKey, + + Encryption: metabasetest.DefaultEncryption, + }, + }, + }.Check(ctx, t, db) + }) + t.Run("segments missing in database", func(t *testing.T) { defer metabasetest.DeleteAll{}.Check(ctx, t, db)