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 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
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(builder);
ArgumentNullException.ThrowIfNull(name);

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 Elasticsearch 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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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 AddElasticsearchContainerShouldThrowWhenBuilderIsNull()
{
IDistributedApplicationBuilder builder = null!;
const string name = "Elasticsearch";

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

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

[Fact]
public void AddElasticsearchContainerShouldThrowWhenNameIsNull()
{
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);

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

[Theory]
[InlineData(false)]
[InlineData(true)]
public void WithDataShouldThrowWhenBuilderIsNull(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);
}

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

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

string source = null!;

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

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

[Fact]
public void CtorElasticsearchResourceShouldThrowWhenNameIsNull()
{
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);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
}
[Fact]
public void CtorElasticsearchResourceShouldThrowWhenPasswordIsNull()
{
const string name = "Elasticsearch";
ParameterResource password = null!;

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

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