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.Elasticsearch #5119

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 13 additions & 2 deletions src/Aspire.Hosting.Elasticsearch/ElasticsearchBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public static IResourceBuilder<ElasticsearchResource> AddElasticsearch(
IResourceBuilder<ParameterResource>? password = null,
int? port = null)
{
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(builder);

Alirexaa marked this conversation as resolved.
Show resolved Hide resolved
var passwordParameter = password?.Resource ?? ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder, $"{name}-password");

var elasticsearch = new ElasticsearchResource(name, passwordParameter);
Expand Down Expand Up @@ -82,7 +85,11 @@ public static IResourceBuilder<ElasticsearchResource> AddElasticsearch(
/// </code>
/// </example>
public static IResourceBuilder<ElasticsearchResource> WithDataVolume(this IResourceBuilder<ElasticsearchResource> builder, string? name = null)
=> builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/usr/share/elasticsearch/data");
{
ArgumentNullException.ThrowIfNull(builder);

return builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/usr/share/elasticsearch/data");
}

/// <summary>
/// Adds a bind mount for the data folder to a Elasticseach container resource.
Expand All @@ -105,6 +112,10 @@ public static IResourceBuilder<ElasticsearchResource> WithDataVolume(this IResou
/// </code>
/// </example>
public static IResourceBuilder<ElasticsearchResource> WithDataBindMount(this IResourceBuilder<ElasticsearchResource> builder, string source)
=> builder.WithBindMount(source, "/usr/share/elasticsearch/data");
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(source);

return builder.WithBindMount(source, "/usr/share/elasticsearch/data");
}
}
1 change: 1 addition & 0 deletions src/Aspire.Hosting.Elasticsearch/ElasticsearchResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class ElasticsearchResource : ContainerResource, IResourceWithConnectionS
/// <param name="password">A parameter that contains the Elasticsearch superuser password.</param>
public ElasticsearchResource(string name, ParameterResource password) : base(name)
{
ArgumentNullException.ThrowIfNull(name);
Alirexaa marked this conversation as resolved.
Show resolved Hide resolved
ArgumentNullException.ThrowIfNull(password);
PasswordParameter = password;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// 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 Xunit;

namespace Aspire.Hosting.Elasticsearch.Tests;

public class ElasticsearchPublicApiTests
{
[Fact]
public void AddElasticsearchContainerShouldThrowsWhenBuilderIsNull()
Alirexaa marked this conversation as resolved.
Show resolved Hide resolved
{
IDistributedApplicationBuilder builder = null!;
const string name = "Elasticsearch";

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

Assert.Multiple(() =>
Alirexaa marked this conversation as resolved.
Show resolved Hide resolved
{
var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
});
}

[Fact]
public void AddElasticsearchContainerShouldThrowsWhenNameIsNull()
{
IDistributedApplicationBuilder builder = new DistributedApplicationBuilder([]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I found out. Better to use TestDistributedApplicationBuilder.Create();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFIK TestDistributedApplicationBuilder is suitable when starting the app. Here, it seems unnecessary.

Copy link
Contributor

@Zombach Zombach Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFIK TestDistributedApplicationBuilder is suitable when starting the app. Here, it seems unnecessary.

I wanted this more from the point of view that there would be a single code base for checking the public 'null'.
Although you may be right. View this at your own discretion. Well, or ask the guys what they think about this

string name = null!;

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

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

[Theory]
[InlineData(false)]
[InlineData(true)]
public void WithDataShouldThrowsWhenBuilderIsNull(bool useVolume)
{
IResourceBuilder<ElasticsearchResource> builder = null!;

Func<IResourceBuilder<ElasticsearchResource>>? action = null;

if (useVolume)
{
action = () => builder.WithDataVolume();
}
else
{
const string source = "/data";

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

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

[Fact]
public void WithDataBindMountShouldThrowsWhenSourceIsNull()
{
var builder = new DistributedApplicationBuilder([]);
var resourceBuilder = builder.AddElasticsearch("Elasticsearch");

string source = null!;

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

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

[Fact]
public void CtorElasticsearchResourceShouldThrowsWhenNameIsNull()
{
var builder = new DistributedApplicationBuilder([]);
builder.Configuration["Parameters:Password"] = "p@ssw0rd";
var password = builder.AddParameter("Password");
const string name = null!;

var action = () => new ElasticsearchResource(name, password.Resource);

Assert.Multiple(() =>
{
var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
});
}
[Fact]
public void CtorElasticsearchResourceShouldThrowsWhenPasswordIsNull()
{
var builder = new DistributedApplicationBuilder([]);
Alirexaa marked this conversation as resolved.
Show resolved Hide resolved
const string name = "Elasticsearch";
ParameterResource password = null!;

var action = () => new ElasticsearchResource(name, password);

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