|
| 1 | +using FluentAssertions; |
| 2 | + |
| 3 | +// To interact with Amazon EBS |
| 4 | +using Amazon.EBS; |
| 5 | +using Amazon.EBS.Model; |
| 6 | + |
| 7 | +using System.Security.Cryptography; |
| 8 | + |
| 9 | + |
| 10 | +public class EbsSnashotTest |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public async Task TestSnapshotCreation() |
| 14 | + { |
| 15 | + |
| 16 | + AmazonEBSConfig config = new AmazonEBSConfig() |
| 17 | + { |
| 18 | + ServiceURL = "http://localhost:5000", |
| 19 | + }; |
| 20 | + |
| 21 | + AmazonEBSClient ebsClient = new AmazonEBSClient(config); |
| 22 | + |
| 23 | + // https://docs.aws.amazon.com/ebs/latest/APIReference/API_StartSnapshot.html |
| 24 | + var startSnapshotRequest = new StartSnapshotRequest { VolumeSize = 1 }; |
| 25 | + var startSnapshotResponse = await ebsClient.StartSnapshotAsync(startSnapshotRequest); |
| 26 | + startSnapshotResponse.Status.Should().Be("pending"); |
| 27 | + |
| 28 | + // https://docs.aws.amazon.com/ebs/latest/APIReference/API_PutSnapshotBlock.html |
| 29 | + var blockData = new byte[] { 0x01, 0x02, 0x03, 0x04 }; |
| 30 | + Stream blockDataStream = new MemoryStream(blockData); |
| 31 | + SHA256Managed sha256hasher = new SHA256Managed(); |
| 32 | + byte[] sha256Hash = sha256hasher.ComputeHash(blockDataStream); |
| 33 | + var sha256checksum = Convert.ToBase64String(sha256Hash); |
| 34 | + var putSnapshotBlockRequest = new PutSnapshotBlockRequest |
| 35 | + { |
| 36 | + BlockIndex = 0, |
| 37 | + Checksum = sha256checksum, |
| 38 | + ChecksumAlgorithm = "SHA256", |
| 39 | + DataLength = blockData.Length, |
| 40 | + SnapshotId = startSnapshotResponse.SnapshotId, |
| 41 | + BlockData = blockDataStream |
| 42 | + }; |
| 43 | + var putSnapshotBlockResponse = await ebsClient.PutSnapshotBlockAsync(putSnapshotBlockRequest); |
| 44 | + putSnapshotBlockResponse.Checksum.Should().Be(sha256checksum); |
| 45 | + putSnapshotBlockResponse.ChecksumAlgorithm.Should().Be("SHA256"); |
| 46 | + |
| 47 | + https://docs.aws.amazon.com/ebs/latest/APIReference/API_CompleteSnapshot.html |
| 48 | + var completeSnapshotRequest = new CompleteSnapshotRequest |
| 49 | + { |
| 50 | + ChangedBlocksCount = 1, |
| 51 | + SnapshotId = startSnapshotResponse.SnapshotId |
| 52 | + }; |
| 53 | + var completeSnapshotResponse = await ebsClient.CompleteSnapshotAsync(completeSnapshotRequest); |
| 54 | + completeSnapshotResponse.Status.Should().Be("completed"); |
| 55 | + } |
| 56 | +} |
0 commit comments