Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Inok committed Dec 9, 2023
1 parent 81c7202 commit 4dde70d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
50 changes: 50 additions & 0 deletions tests/Promote.NuGet.Feeds.Tests/LocalNugetFeed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Net;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;

namespace Promote.NuGet.Feeds.Tests;

public sealed class LocalNugetFeed : IAsyncDisposable
{
private readonly IContainer _container;

public string FeedUrl { get; }

public string ApiKey { get; }

private LocalNugetFeed(IContainer container, string feedUrl, string apiKey)
{
_container = container;
FeedUrl = feedUrl;
ApiKey = apiKey;
}

public static async Task<LocalNugetFeed> Create()
{
var buffer = new byte[64];
Random.Shared.NextBytes(buffer);
var apiKey = Convert.ToBase64String(buffer);

const ushort bagetterPort = 8080;

var container = new ContainerBuilder()
.WithImage("bagetter/bagetter:1.0.0")
.WithEnvironment("ApiKey", apiKey)
.WithPortBinding(bagetterPort, true)
.WithWaitStrategy(Wait.ForUnixContainer()
.UntilHttpRequestIsSucceeded(
r => r.ForPort(bagetterPort).ForPath("/").ForStatusCode(HttpStatusCode.OK)))
.Build();

await container.StartAsync();

var feedUrl = new UriBuilder("http", container.Hostname, container.GetMappedPublicPort(bagetterPort), "/v3/index.json").Uri.ToString();

return new LocalNugetFeed(container, feedUrl, apiKey);
}

public ValueTask DisposeAsync()
{
return _container.DisposeAsync();
}
}
18 changes: 2 additions & 16 deletions tests/Promote.NuGet.Feeds.Tests/NuGetRepositoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.IO;
using System.Net;
using DotNet.Testcontainers.Builders;
using NuGet.Packaging.Core;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
Expand All @@ -13,22 +11,10 @@ public class NuGetRepositoryTests
[Test]
public async Task Downloads_a_package_from_source_feed_and_pushes_it_to_destination_feed()
{
var apiKey = TestContext.CurrentContext.Random.GetString(20);

await using var container = new ContainerBuilder()
.WithImage("bagetter/bagetter:1.0.0")
.WithEnvironment("ApiKey", apiKey)
.WithPortBinding(8080, true)
.WithWaitStrategy(Wait.ForUnixContainer()
.UntilHttpRequestIsSucceeded(r => r.ForPort(8080).ForPath("/").ForStatusCode(HttpStatusCode.OK)))
.Build();

await container.StartAsync();
await using var feed = await LocalNugetFeed.Create();

var nugetOrgDescriptor = new NuGetRepositoryDescriptor("https://api.nuget.org/v3/index.json", apiKey: null);

var destinationUri = new UriBuilder("http", container.Hostname, container.GetMappedPublicPort(8080), "/v3/index.json").Uri.ToString();
var destinationFeedDescriptor = new NuGetRepositoryDescriptor(destinationUri, apiKey);
var destinationFeedDescriptor = new NuGetRepositoryDescriptor(feed.FeedUrl, feed.ApiKey);

var sourceRepo = new NuGetRepository(nugetOrgDescriptor, new NullSourceCacheContext(), TestConsoleLogger.Instance);
var destinationRepo = new NuGetRepository(destinationFeedDescriptor, new NullSourceCacheContext(), TestConsoleLogger.Instance);
Expand Down
2 changes: 1 addition & 1 deletion tests/Promote.NuGet.Feeds.Tests/TestConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Promote.NuGet.Feeds.Tests;

public class TestConsoleLogger : LoggerBase
public sealed class TestConsoleLogger : LoggerBase
{
public static TestConsoleLogger Instance { get; } = new();

Expand Down

0 comments on commit 4dde70d

Please sign in to comment.