Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding public API test coverage for Aspire.Hosting.RabbitMQ #5153

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/Aspire.Hosting.RabbitMQ/RabbitMQBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public static IResourceBuilder<RabbitMQServerResource> AddRabbitMQ(this IDistrib
IResourceBuilder<ParameterResource>? password = null,
int? port = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);

// don't use special characters in the password, since it goes into a URI
var passwordParameter = password?.Resource ?? ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder, $"{name}-password", special: false);

Expand All @@ -55,9 +58,12 @@ public static IResourceBuilder<RabbitMQServerResource> AddRabbitMQ(this IDistrib
/// <param name="isReadOnly">A flag that indicates if this is a read-only volume.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<RabbitMQServerResource> WithDataVolume(this IResourceBuilder<RabbitMQServerResource> builder, string? name = null, bool isReadOnly = false)
=> builder
.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/var/lib/rabbitmq", isReadOnly)
.RunWithStableNodeName();
{
ArgumentNullException.ThrowIfNull(builder);

return builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/var/lib/rabbitmq", isReadOnly)
.RunWithStableNodeName();
}

/// <summary>
/// Adds a bind mount for the data folder to a RabbitMQ container resource.
Expand All @@ -67,8 +73,13 @@ public static IResourceBuilder<RabbitMQServerResource> WithDataVolume(this IReso
/// <param name="isReadOnly">A flag that indicates if this is a read-only mount.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<RabbitMQServerResource> WithDataBindMount(this IResourceBuilder<RabbitMQServerResource> builder, string source, bool isReadOnly = false)
=> builder.WithBindMount(source, "/var/lib/rabbitmq", isReadOnly)
.RunWithStableNodeName();
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(source);

return builder.WithBindMount(source, "/var/lib/rabbitmq", isReadOnly)
.RunWithStableNodeName();
}

/// <summary>
/// Configures the RabbitMQ container resource to enable the RabbitMQ management plugin.
Expand All @@ -81,7 +92,11 @@ public static IResourceBuilder<RabbitMQServerResource> WithDataBindMount(this IR
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
/// <exception cref="DistributedApplicationException">Thrown when the current container image and tag do not match the defaults for <see cref="RabbitMQServerResource"/>.</exception>
public static IResourceBuilder<RabbitMQServerResource> WithManagementPlugin(this IResourceBuilder<RabbitMQServerResource> builder)
=> builder.WithManagementPlugin(port: null);
{
ArgumentNullException.ThrowIfNull(builder);

return builder.WithManagementPlugin(port: null);
}

/// <inheritdoc cref="WithManagementPlugin(IResourceBuilder{RabbitMQServerResource})" />
/// <param name="builder">The resource builder.</param>
Expand All @@ -96,6 +111,8 @@ public static IResourceBuilder<RabbitMQServerResource> WithManagementPlugin(this
/// </example>
public static IResourceBuilder<RabbitMQServerResource> WithManagementPlugin(this IResourceBuilder<RabbitMQServerResource> builder, int? port)
{
ArgumentNullException.ThrowIfNull(builder);

var handled = false;
var containerAnnotations = builder.Resource.Annotations.OfType<ContainerImageAnnotation>().ToList();

Expand Down
119 changes: 119 additions & 0 deletions tests/Aspire.Hosting.RabbitMQ.Tests/RabbitMQPublicApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Utils;
using Xunit;

namespace Aspire.Hosting.RabbitMQ.Tests;

public class RabbitMQPublicApiTests
{
[Fact]
public void AddRabbitMQContainerShouldThrowWhenBuilderIsNull()
{
IDistributedApplicationBuilder builder = null!;
const string name = "rabbitMQ";

var action = () => builder.AddRabbitMQ(name);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void AddRabbitMQContainerShouldThrowWhenNameIsNull()
{
var builder = DistributedApplication.CreateBuilder([]);
string name = null!;

var action = () => builder.AddRabbitMQ(name);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
}

[Fact]
public void WithDataVolumeShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<RabbitMQServerResource> builder = null!;

var action = () => builder.WithDataVolume();

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void WithDataBindMountShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<RabbitMQServerResource> builder = null!;
const string source = "/rabbitMQ/data";

var action = () => builder.WithDataBindMount(source);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void WithDataBindMountShouldThrowWhenSourceIsNull()
{
var builderResource = TestDistributedApplicationBuilder.Create();
var rabbitMQ = builderResource.AddRabbitMQ("rabbitMQ");
string source = null!;

var action = () => rabbitMQ.WithDataBindMount(source);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(source), exception.ParamName);
}

[Fact]
public void WithManagementPluginShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<RabbitMQServerResource> builder = null!;

var action = () => builder.WithManagementPlugin();

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void WithManagementPluginAndPortShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<RabbitMQServerResource> builder = null!;

var action = () => builder.WithManagementPlugin(15672);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void CtorRabbitMQServerResourceShouldThrowWhenNameIsNull()
{
var distributedApplicationBuilder = DistributedApplication.CreateBuilder([]);
string name = null!;
var password = ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(distributedApplicationBuilder, "password", special: false);

var action = () => new RabbitMQServerResource(name: name, userName: null, password: password);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
}

[Fact]
public void CtorRabbitMQServerResourceShouldThrowWhenPasswordIsNull()
{
var distributedApplicationBuilder = DistributedApplication.CreateBuilder([]);
string name = "rabbitMQ";
ParameterResource password = null!;

var action = () => new RabbitMQServerResource(name: name, userName: null, password: password);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(password), exception.ParamName);
}
}