|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using ManagedCode.Storage.Azure.Extensions; |
| 7 | +using ManagedCode.Storage.Core; |
| 8 | +using ManagedCode.Storage.Core.Primitives; |
| 9 | +using ManagedCode.Storage.Tests.Common; |
| 10 | +using ManagedCode.Storage.VirtualFileSystem.Core; |
| 11 | +using ManagedCode.Storage.VirtualFileSystem.Extensions; |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
| 13 | +using Shouldly; |
| 14 | +using Testcontainers.Azurite; |
| 15 | +using Xunit; |
| 16 | + |
| 17 | +namespace ManagedCode.Storage.Tests.VirtualFileSystem; |
| 18 | + |
| 19 | +public sealed class VfsImmutableMetadataTests : IAsyncLifetime |
| 20 | +{ |
| 21 | + private readonly AzuriteContainer _container = new AzuriteBuilder(ContainerImages.Azurite) |
| 22 | + .WithCommand("--skipApiVersionCheck").Build(); |
| 23 | + |
| 24 | + public Task InitializeAsync() => _container.StartAsync(); |
| 25 | + public Task DisposeAsync() => _container.DisposeAsync().AsTask(); |
| 26 | + |
| 27 | + [Theory] |
| 28 | + [InlineData(false, "material.txt")] |
| 29 | + [InlineData(true, "material.txt")] |
| 30 | + [InlineData(false, "матеріал.txt")] |
| 31 | + [InlineData(true, "матеріал.txt")] |
| 32 | + public async Task ImmutableWrite_PreservesMetadataThroughFreshAndPreviouslyEmptyHandles(bool cache, string name) |
| 33 | + { |
| 34 | + await using var provider = CreateProvider(cache); |
| 35 | + await using var scope = provider.CreateAsyncScope(); |
| 36 | + var vfs = scope.ServiceProvider.GetRequiredService<IVirtualFileSystem>(); |
| 37 | + var path = new VfsPath("/learning/material/content"); |
| 38 | + var previous = await vfs.GetFileAsync(path); |
| 39 | + (await previous.GetMetadataAsync()).ShouldBeEmpty(); |
| 40 | + (await vfs.FileExistsAsync(path)).ShouldBeFalse(); |
| 41 | + var bytes = Encoding.UTF8.GetBytes("exact immutable author bytes"); |
| 42 | + var metadata = new Dictionary<string, string> |
| 43 | + { |
| 44 | + ["fileName"] = name, |
| 45 | + ["ownerUserId"] = "exact-owner", |
| 46 | + ["contentType"] = "text/plain" |
| 47 | + }; |
| 48 | + var options = new StorageWriteOptions { ContentType = "text/plain", Metadata = metadata }; |
| 49 | + var written = await vfs.WriteBytesIfAbsentOrSameAsync(path, bytes, options); |
| 50 | + written.ReusedExisting.ShouldBeFalse(); |
| 51 | + AssertMetadata(await previous.GetMetadataAsync(), metadata); |
| 52 | + (await vfs.FileExistsAsync(path)).ShouldBeTrue(); |
| 53 | + var fresh = await vfs.GetFileAsync(path); |
| 54 | + await fresh.RefreshAsync(); |
| 55 | + fresh.Size.ShouldBe(bytes.LongLength); |
| 56 | + AssertMetadata(await fresh.GetMetadataAsync(), metadata); |
| 57 | + await using var stream = await fresh.OpenReadAsync(); |
| 58 | + using var content = new MemoryStream(); |
| 59 | + await stream.CopyToAsync(content); |
| 60 | + content.ToArray().ShouldBe(bytes); |
| 61 | + var replay = await vfs.WriteBytesIfAbsentOrSameAsync(path, bytes, options); |
| 62 | + replay.ReusedExisting.ShouldBeTrue(); |
| 63 | + replay.Info.ETag.ShouldBe(written.Info.ETag); |
| 64 | + AssertMetadata(await fresh.GetMetadataAsync(), metadata); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public async Task MetadataRead_PreservesLegacyCustomFieldsAndFiltersOnlyReservedVfsFields() |
| 69 | + { |
| 70 | + await using var provider = CreateProvider(false); |
| 71 | + await using var scope = provider.CreateAsyncScope(); |
| 72 | + var storage = scope.ServiceProvider.GetRequiredService<IStorage>(); |
| 73 | + var vfs = scope.ServiceProvider.GetRequiredService<IVirtualFileSystem>(); |
| 74 | + var metadata = new Dictionary<string, string> |
| 75 | + { |
| 76 | + ["ownerUserId"] = "exact-owner", |
| 77 | + ["x-vfs-reviewer"] = "legacy-custom", |
| 78 | + ["x-vfs-review-vfs-created"] = "user-field", |
| 79 | + ["x-vfs-vfs-version"] = "1", |
| 80 | + ["x-vfs-vfs-created"] = DateTimeOffset.UtcNow.ToString("O"), |
| 81 | + ["x-vfs-vfs-modified"] = DateTimeOffset.UtcNow.ToString("O"), |
| 82 | + ["x-vfs-vfs-attributes"] = "0", |
| 83 | + ["x-vfs-vfs-internal"] = "private-vfs-state" |
| 84 | + }; |
| 85 | + await storage.RequireObjectStorage().CreatePrivateContainerAsync(); |
| 86 | + using var bytes = new MemoryStream(Encoding.UTF8.GetBytes("legacy bytes")); |
| 87 | + await storage.RequireObjectStorage().WriteObjectAsync("legacy/content", bytes, |
| 88 | + new StorageWriteOptions { Metadata = metadata }); |
| 89 | + var file = await vfs.GetFileAsync("/legacy/content"); |
| 90 | + AssertMetadata(await file.GetMetadataAsync(), new Dictionary<string, string> |
| 91 | + { |
| 92 | + ["ownerUserId"] = "exact-owner", |
| 93 | + ["reviewer"] = "legacy-custom", |
| 94 | + ["review-vfs-created"] = "user-field" |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + private ServiceProvider CreateProvider(bool cache) |
| 99 | + { |
| 100 | + var services = new ServiceCollection(); |
| 101 | + services.AddLogging(); |
| 102 | + var containerName = $"vfs-metadata-{Guid.NewGuid():N}"; |
| 103 | + services.AddAzureStorageAsDefault(options => |
| 104 | + { |
| 105 | + options.ConnectionString = _container.GetConnectionString(); |
| 106 | + options.Container = containerName; |
| 107 | + options.CreateContainerIfNotExists = true; |
| 108 | + }); |
| 109 | + services.AddVirtualFileSystem(options => |
| 110 | + { |
| 111 | + options.DefaultContainer = containerName; |
| 112 | + options.EnableCache = cache; |
| 113 | + }); |
| 114 | + return services.BuildServiceProvider(new ServiceProviderOptions { ValidateScopes = true }); |
| 115 | + } |
| 116 | + |
| 117 | + private static void AssertMetadata(IReadOnlyDictionary<string, string> actual, Dictionary<string, string> expected) |
| 118 | + { |
| 119 | + actual.Count.ShouldBe(expected.Count); |
| 120 | + foreach (var item in expected) |
| 121 | + { |
| 122 | + actual[item.Key].ShouldBe(item.Value); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments