-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCleanupAzureBlobStorageSnippetsCommandHandlerTests.cs
More file actions
186 lines (163 loc) · 7.34 KB
/
CleanupAzureBlobStorageSnippetsCommandHandlerTests.cs
File metadata and controls
186 lines (163 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using Azure;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using ByteSync.ServerCommon.Business.Settings;
using ByteSync.ServerCommon.Commands.Storage;
using ByteSync.ServerCommon.Interfaces.Services;
using FakeItEasy;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace ByteSync.ServerCommon.Tests.Commands.Storage;
[TestFixture]
public class CleanupAzureBlobStorageSnippetsCommandHandlerTests
{
private IBlobStorageContainerService _blobStorageContainerService = null!;
private ILogger<CleanupAzureBlobStorageSnippetsCommandHandler> _logger = null!;
private IOptions<AzureBlobStorageSettings> _options = null!;
private CleanupAzureBlobStorageSnippetsCommandHandler _handler = null!;
private AzureBlobStorageSettings _settings = null!;
[SetUp]
public void Setup()
{
_blobStorageContainerService = A.Fake<IBlobStorageContainerService>();
_logger = A.Fake<ILogger<CleanupAzureBlobStorageSnippetsCommandHandler>>();
_settings = new AzureBlobStorageSettings
{
RetentionDurationInDays = 3,
AccountName = "test",
AccountKey = "test",
Endpoint = "https://test.blob.core.windows.net/",
Container = "test"
};
_options = A.Fake<IOptions<AzureBlobStorageSettings>>();
A.CallTo(() => _options.Value).Returns(_settings);
_handler = new CleanupAzureBlobStorageSnippetsCommandHandler(_blobStorageContainerService, _options, _logger);
}
[Test]
public async Task Handle_DeletesBlobsOlderThanRetention()
{
// Arrange
var now = DateTimeOffset.UtcNow;
var blobs = new List<BlobItem>
{
BlobsModelFactory.BlobItem("old1", false, BuildBlobItemProperties(now.AddDays(-5))),
BlobsModelFactory.BlobItem("old2", false, BuildBlobItemProperties(now.AddDays(-4))),
BlobsModelFactory.BlobItem("recent", false, BuildBlobItemProperties(now.AddDays(-1))),
};
var container = A.Fake<BlobContainerClient>();
A.CallTo(() => container.ExistsAsync(A<CancellationToken>._))
.Returns(Task.FromResult(Response.FromValue(true, default)));
A.CallTo(() => container.GetBlobsAsync(A<BlobTraits>._, A<BlobStates>._, null, A<CancellationToken>._))
.Returns(new TestAsyncPageable<BlobItem>(blobs));
A.CallTo(() => _blobStorageContainerService.BuildBlobContainerClient())
.Returns(Task.FromResult(container));
// Act
var result = await _handler.Handle(new CleanupAzureBlobStorageSnippetsRequest(), CancellationToken.None);
// Assert
result.Should().Be(2);
A.CallTo(() => container.DeleteBlobAsync("old1", DeleteSnapshotsOption.IncludeSnapshots, null, A<CancellationToken>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => container.DeleteBlobAsync("old2", DeleteSnapshotsOption.IncludeSnapshots, null, A<CancellationToken>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => container.DeleteBlobAsync("recent", DeleteSnapshotsOption.IncludeSnapshots, null, A<CancellationToken>._)).MustNotHaveHappened();
}
[Test]
public async Task Handle_DoesNothingIfRetentionIsTooLow()
{
// Arrange
_settings.RetentionDurationInDays = 0;
// Act
var result = await _handler.Handle(new CleanupAzureBlobStorageSnippetsRequest(), CancellationToken.None);
// Assert
result.Should().Be(0);
A.CallTo(() => _blobStorageContainerService.BuildBlobContainerClient()).MustNotHaveHappened();
}
[Test]
public async Task Handle_DoesNothingIfContainerDoesNotExist()
{
// Arrange
var container = A.Fake<BlobContainerClient>();
A.CallTo(() => container.ExistsAsync(A<CancellationToken>._))
.Returns(Task.FromResult(Response.FromValue(false, default)));
A.CallTo(() => _blobStorageContainerService.BuildBlobContainerClient())
.Returns(Task.FromResult(container));
// Act
var result = await _handler.Handle(new CleanupAzureBlobStorageSnippetsRequest(), CancellationToken.None);
// Assert
result.Should().Be(0);
}
[Test]
public async Task Handle_ReturnsZeroIfNoBlobsToDelete()
{
// Arrange
var now = DateTimeOffset.UtcNow;
var blobs = new List<BlobItem>
{
BlobsModelFactory.BlobItem("recent1", false, BuildBlobItemProperties(now.AddDays(-1))),
BlobsModelFactory.BlobItem("recent2", false, BuildBlobItemProperties(now)),
};
var container = A.Fake<BlobContainerClient>();
A.CallTo(() => container.ExistsAsync(A<CancellationToken>._))
.Returns(Task.FromResult(Response.FromValue(true, default)));
A.CallTo(() => container.GetBlobsAsync(A<BlobTraits>._, A<BlobStates>._, null, A<CancellationToken>._))
.Returns(new TestAsyncPageable<BlobItem>(blobs));
A.CallTo(() => _blobStorageContainerService.BuildBlobContainerClient())
.Returns(Task.FromResult(container));
// Act
var result = await _handler.Handle(new CleanupAzureBlobStorageSnippetsRequest(), CancellationToken.None);
// Assert
result.Should().Be(0);
A.CallTo(() => container.DeleteBlobAsync(A<string>._, DeleteSnapshotsOption.IncludeSnapshots, null, A<CancellationToken>._)).MustNotHaveHappened();
}
// Helper to create BlobItemProperties with only CreatedOn set
private static BlobItemProperties BuildBlobItemProperties(DateTimeOffset createdOn)
{
// Adapter la signature à la version installée d'Azure.Storage.Blobs
return BlobsModelFactory.BlobItemProperties(
lastModified: null,
contentLength: null,
contentType: null,
contentEncoding: null,
contentLanguage: null,
contentHash: null,
contentDisposition: null,
cacheControl: null,
blobSequenceNumber: null,
blobType: null,
leaseStatus: null,
leaseState: null,
leaseDuration: null,
copyId: null,
copyStatus: null,
copySource: null,
copyProgress: null,
serverEncrypted: null,
incrementalCopy: null,
destinationSnapshot: null,
remainingRetentionDays: null,
accessTier: null,
accessTierInferred: false,
archiveStatus: null,
customerProvidedKeySha256: null,
encryptionScope: null,
tagCount: null,
expiresOn: null,
createdOn: createdOn
);
}
// Helper for AsyncPageable<BlobItem>
private class TestAsyncPageable<T> : AsyncPageable<T> where T : notnull
{
private readonly IEnumerable<T> _items;
public TestAsyncPageable(IEnumerable<T> items) => _items = items;
public override async IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
foreach (var item in _items)
yield return item;
await Task.CompletedTask;
}
public override IAsyncEnumerable<Page<T>> AsPages(string? continuationToken = null, int? pageSizeHint = null)
{
throw new ApplicationException(); // Not needed for these tests
}
}
}