Skip to content

Commit db4edc9

Browse files
committed
tests
1 parent e06d485 commit db4edc9

22 files changed

+472
-124
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using DotNet.Testcontainers.Containers;
5+
using Microsoft.Extensions.Logging;
6+
using Testcontainers.Azurite;
7+
8+
namespace ManagedCode.Storage.Tests;
9+
10+
public sealed class EmptyContainer : DockerContainer
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="AzuriteContainer" /> class.
14+
/// </summary>
15+
/// <param name="configuration">The container configuration.</param>
16+
/// <param name="logger">The logger.</param>
17+
public EmptyContainer(AzuriteConfiguration configuration, ILogger logger)
18+
: base(configuration, logger)
19+
{
20+
}
21+
22+
public EmptyContainer() : base(null, null)
23+
{
24+
}
25+
26+
/// <summary>
27+
/// Gets the Azurite connection string.
28+
/// </summary>
29+
/// <returns>The Azurite connection string.</returns>
30+
public string GetConnectionString()
31+
{
32+
return string.Empty;
33+
}
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Testcontainers.LocalStack;
3+
4+
namespace ManagedCode.Storage.Tests.GCP;
5+
6+
public class AWSBlobTests : BlobTests<LocalStackContainer>
7+
{
8+
protected override LocalStackContainer Build()
9+
{
10+
return new LocalStackBuilder().Build();
11+
}
12+
13+
protected override ServiceProvider ConfigureServices()
14+
{
15+
return AWSConfigurator.ConfigureServices(Container.GetConnectionString());
16+
}
17+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.ComponentModel;
2+
using Amazon.S3;
3+
using Google.Cloud.Storage.V1;
4+
using ManagedCode.Storage.Aws.Extensions;
5+
using ManagedCode.Storage.Aws.Options;
6+
using ManagedCode.Storage.Google.Extensions;
7+
using ManagedCode.Storage.Google.Options;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Testcontainers.GCS;
10+
11+
// ReSharper disable MethodHasAsyncOverload
12+
13+
namespace ManagedCode.Storage.Tests.GCP;
14+
15+
public class AWSConfigurator
16+
{
17+
public static ServiceProvider ConfigureServices(string connectionString)
18+
{
19+
20+
var services = new ServiceCollection();
21+
22+
var config = new AmazonS3Config();
23+
config.ServiceURL = connectionString;
24+
25+
services.AddAWSStorageAsDefault(opt =>
26+
{
27+
opt.PublicKey = "localkey";
28+
opt.SecretKey = "localsecret";
29+
opt.Bucket = "managed-code-bucket";
30+
opt.OriginalOptions = config;
31+
});
32+
33+
services.AddAWSStorage(new AWSStorageOptions
34+
{
35+
PublicKey = "localkey",
36+
SecretKey = "localsecret",
37+
Bucket = "managed-code-bucket",
38+
OriginalOptions = config
39+
});
40+
return services.BuildServiceProvider();
41+
}
42+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Testcontainers.LocalStack;
3+
4+
namespace ManagedCode.Storage.Tests.GCP;
5+
6+
public class AWSContainerTests : ContainerTests<LocalStackContainer>
7+
{
8+
protected override LocalStackContainer Build()
9+
{
10+
return new LocalStackBuilder().Build();
11+
}
12+
13+
protected override ServiceProvider ConfigureServices()
14+
{
15+
return AWSConfigurator.ConfigureServices(Container.GetConnectionString());
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Testcontainers.LocalStack;
3+
4+
namespace ManagedCode.Storage.Tests.GCP;
5+
6+
public class AWSDownloadTests : DownloadTests<LocalStackContainer>
7+
{
8+
protected override LocalStackContainer Build()
9+
{
10+
return new LocalStackBuilder().Build();
11+
}
12+
13+
protected override ServiceProvider ConfigureServices()
14+
{
15+
return AWSConfigurator.ConfigureServices(Container.GetConnectionString());
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Testcontainers.LocalStack;
3+
4+
namespace ManagedCode.Storage.Tests.GCP;
5+
6+
public class AWSUploadTests : UploadTests<LocalStackContainer>
7+
{
8+
protected override LocalStackContainer Build()
9+
{
10+
return new LocalStackBuilder().Build();
11+
}
12+
13+
protected override ServiceProvider ConfigureServices()
14+
{
15+
return AWSConfigurator.ConfigureServices(Container.GetConnectionString());
16+
}
17+
}

ManagedCode.Storage.Tests/Storages/AWS/AmazonStorageTests.cs renamed to ManagedCode.Storage.Tests/Storages/AWS/AwsConfigTests.cs

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,17 @@
77
using ManagedCode.Storage.Aws.Options;
88
using ManagedCode.Storage.Core;
99
using ManagedCode.Storage.Core.Exceptions;
10+
using ManagedCode.Storage.Tests.GCP;
1011
using Microsoft.Extensions.DependencyInjection;
1112
using Testcontainers.LocalStack;
1213
using Xunit;
1314

1415
namespace ManagedCode.Storage.Tests.AWS;
1516

1617

17-
public class AmazonStorageTests : StorageBaseTests<LocalStackContainer>
18+
public class AwsConfigTests
1819
{
19-
protected override LocalStackContainer Build()
20-
{
21-
return new LocalStackBuilder().Build();
22-
}
23-
24-
protected override ServiceProvider ConfigureServices()
25-
{
26-
var services = new ServiceCollection();
27-
28-
var config = new AmazonS3Config();
29-
config.ServiceURL = Container.GetConnectionString();
30-
31-
services.AddAWSStorageAsDefault(opt =>
32-
{
33-
opt.PublicKey = "localkey";
34-
opt.SecretKey = "localsecret";
35-
opt.Bucket = "managed-code-bucket";
36-
opt.OriginalOptions = config;
37-
});
3820

39-
services.AddAWSStorage(new AWSStorageOptions
40-
{
41-
PublicKey = "localkey",
42-
SecretKey = "localsecret",
43-
Bucket = "managed-code-bucket",
44-
OriginalOptions = config
45-
});
46-
return services.BuildServiceProvider();
47-
}
4821

4922
[Fact]
5023
public void BadConfigurationForStorage_WithoutPublicKey_ThrowException()
@@ -91,8 +64,8 @@ public void BadConfigurationForStorage_WithoutBucket_ThrowException()
9164
[Fact]
9265
public void StorageAsDefaultTest()
9366
{
94-
var storage = ServiceProvider.GetService<IAWSStorage>();
95-
var defaultStorage = ServiceProvider.GetService<IStorage>();
67+
var storage = AWSConfigurator.ConfigureServices("test").GetService<IAWSStorage>();
68+
var defaultStorage = AWSConfigurator.ConfigureServices("test").GetService<IStorage>();
9669
storage?.GetType().FullName.Should().Be(defaultStorage?.GetType().FullName);
9770
}
9871

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Testcontainers.Azurite;
3+
4+
namespace ManagedCode.Storage.Tests.GCP;
5+
6+
public class AzureBlobTests : BlobTests<AzuriteContainer>
7+
{
8+
protected override AzuriteContainer Build()
9+
{
10+
return new AzuriteBuilder().Build();
11+
}
12+
13+
protected override ServiceProvider ConfigureServices()
14+
{
15+
return AzureConfigurator.ConfigureServices(Container.GetConnectionString());
16+
}
17+
}

ManagedCode.Storage.Tests/Storages/Azure/AzureStorageTests.cs renamed to ManagedCode.Storage.Tests/Storages/Azure/AzureConfigTests.cs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,17 @@
55
using ManagedCode.Storage.Azure.Options;
66
using ManagedCode.Storage.Core;
77
using ManagedCode.Storage.Core.Exceptions;
8+
using ManagedCode.Storage.Tests.GCP;
89
using Microsoft.Extensions.DependencyInjection;
910
using Testcontainers.Azurite;
1011
using Xunit;
1112

1213
namespace ManagedCode.Storage.Tests.Azure;
1314

1415

15-
public class AzureStorageTests : StorageBaseTests<AzuriteContainer>
16+
public class AzureConfigTests
1617
{
17-
protected override AzuriteContainer Build()
18-
{
19-
return new AzuriteBuilder().Build();
20-
}
21-
22-
protected override ServiceProvider ConfigureServices()
23-
{
24-
var services = new ServiceCollection();
25-
26-
services.AddAzureStorageAsDefault(opt =>
27-
{
28-
opt.Container = "managed-code-bucket";
29-
opt.ConnectionString = Container.GetConnectionString();
30-
});
31-
32-
services.AddAzureStorage(new AzureStorageOptions
33-
{
34-
Container = "managed-code-bucket",
35-
ConnectionString = Container.GetConnectionString()
36-
});
37-
38-
return services.BuildServiceProvider();
39-
}
18+
4019

4120
[Fact]
4221
public void BadConfigurationForStorage_WithoutContainer_ThrowException()
@@ -45,8 +24,7 @@ public void BadConfigurationForStorage_WithoutContainer_ThrowException()
4524

4625
Action action = () => services.AddAzureStorage(opt =>
4726
{
48-
opt.ConnectionString =
49-
"DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://localhost:10000/devstoreaccount1;QueueEndpoint=http://localhost:10001/devstoreaccount1;TableEndpoint=http://localhost:10002/devstoreaccount1;";
27+
opt.ConnectionString = "test";
5028
});
5129

5230
action.Should().Throw<BadConfigurationException>();
@@ -70,8 +48,8 @@ public void BadConfigurationForStorage_WithoutConnectionString_ThrowException()
7048
[Fact]
7149
public void StorageAsDefaultTest()
7250
{
73-
var storage = ServiceProvider.GetService<IAzureStorage>();
74-
var defaultStorage = ServiceProvider.GetService<IStorage>();
51+
var storage = AzureConfigurator.ConfigureServices("test").GetService<IAzureStorage>();
52+
var defaultStorage = AzureConfigurator.ConfigureServices("test").GetService<IStorage>();
7553
storage?.GetType().FullName.Should().Be(defaultStorage?.GetType().FullName);
7654
}
7755
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Google.Cloud.Storage.V1;
2+
using ManagedCode.Storage.Google.Extensions;
3+
using ManagedCode.Storage.Google.Options;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Testcontainers.GCS;
6+
7+
// ReSharper disable MethodHasAsyncOverload
8+
9+
namespace ManagedCode.Storage.Tests.GCP;
10+
11+
public class AzureConfigurator
12+
{
13+
public static ServiceProvider ConfigureServices(string connectionString)
14+
{
15+
16+
var services = new ServiceCollection();
17+
18+
services.AddGCPStorageAsDefault(opt =>
19+
{
20+
opt.BucketOptions = new BucketOptions
21+
{
22+
ProjectId = "api-project-0000000000000",
23+
Bucket = "managed-code-bucket"
24+
};
25+
opt.StorageClientBuilder = new StorageClientBuilder
26+
{
27+
UnauthenticatedAccess = true,
28+
BaseUri = connectionString
29+
};
30+
});
31+
32+
services.AddGCPStorage(new GCPStorageOptions
33+
{
34+
BucketOptions = new BucketOptions
35+
{
36+
ProjectId = "api-project-0000000000000",
37+
Bucket = "managed-code-bucket"
38+
},
39+
StorageClientBuilder = new StorageClientBuilder
40+
{
41+
UnauthenticatedAccess = true,
42+
BaseUri = connectionString
43+
}
44+
});
45+
return services.BuildServiceProvider();
46+
}
47+
}

0 commit comments

Comments
 (0)