Skip to content

Commit 4fc0a77

Browse files
authored
Adding public API test coverage for Aspire.Hosting.Garnet (#5160)
1 parent 4a71736 commit 4fc0a77

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed

src/Aspire.Hosting.Garnet/GarnetBuilderExtensions.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public static class GarnetBuilderExtensions
5151
public static IResourceBuilder<GarnetResource> AddGarnet(this IDistributedApplicationBuilder builder, string name,
5252
int? port = null)
5353
{
54+
ArgumentNullException.ThrowIfNull(builder);
55+
ArgumentNullException.ThrowIfNull(name);
56+
5457
var garnet = new GarnetResource(name);
5558
return builder.AddResource(garnet)
5659
.WithEndpoint(port: port, targetPort: 6379, name: GarnetResource.PrimaryEndpointName)
@@ -79,6 +82,8 @@ public static IResourceBuilder<GarnetResource> AddGarnet(this IDistributedApplic
7982
public static IResourceBuilder<GarnetResource> WithDataVolume(this IResourceBuilder<GarnetResource> builder,
8083
string? name = null, bool isReadOnly = false)
8184
{
85+
ArgumentNullException.ThrowIfNull(builder);
86+
8287
builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), GarnetContainerDataDirectory,
8388
isReadOnly);
8489
if (!isReadOnly)
@@ -110,6 +115,9 @@ public static IResourceBuilder<GarnetResource> WithDataVolume(this IResourceBuil
110115
public static IResourceBuilder<GarnetResource> WithDataBindMount(this IResourceBuilder<GarnetResource> builder,
111116
string source, bool isReadOnly = false)
112117
{
118+
ArgumentNullException.ThrowIfNull(builder);
119+
ArgumentNullException.ThrowIfNull(source);
120+
113121
builder.WithBindMount(source, GarnetContainerDataDirectory, isReadOnly);
114122
if (!isReadOnly)
115123
{
@@ -137,11 +145,16 @@ public static IResourceBuilder<GarnetResource> WithDataBindMount(this IResourceB
137145
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
138146
public static IResourceBuilder<GarnetResource> WithPersistence(this IResourceBuilder<GarnetResource> builder,
139147
TimeSpan? interval = null, long keysChangedThreshold = 1)
140-
=> builder.WithAnnotation(new CommandLineArgsCallbackAnnotation(context =>
148+
{
149+
ArgumentNullException.ThrowIfNull(builder);
150+
151+
return builder.WithAnnotation(new CommandLineArgsCallbackAnnotation(context =>
141152
{
142153
context.Args.Add("--save");
143-
context.Args.Add((interval ?? TimeSpan.FromSeconds(60)).TotalSeconds.ToString(CultureInfo.InvariantCulture));
154+
context.Args.Add(
155+
(interval ?? TimeSpan.FromSeconds(60)).TotalSeconds.ToString(CultureInfo.InvariantCulture));
144156
context.Args.Add(keysChangedThreshold.ToString(CultureInfo.InvariantCulture));
145157
return Task.CompletedTask;
146158
}), ResourceAnnotationMutationBehavior.Replace);
159+
}
147160
}

src/Aspire.Hosting.Garnet/GarnetResource.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
using System.Runtime.CompilerServices;
6+
47
namespace Aspire.Hosting.ApplicationModel;
58

69
/// <summary>
710
/// A resource that represents a Garnet resource independent of the hosting model.
811
/// </summary>
912
/// <param name="name">The name of the resource.</param>
10-
public class GarnetResource(string name) : ContainerResource(name), IResourceWithConnectionString
13+
public class GarnetResource(string name) : ContainerResource(ThrowIfNull(name)), IResourceWithConnectionString
1114
{
1215
internal const string PrimaryEndpointName = "tcp";
1316

@@ -24,4 +27,7 @@ public class GarnetResource(string name) : ContainerResource(name), IResourceWit
2427
public ReferenceExpression ConnectionStringExpression =>
2528
ReferenceExpression.Create(
2629
$"{PrimaryEndpoint.Property(EndpointProperty.Host)}:{PrimaryEndpoint.Property(EndpointProperty.Port)}");
30+
31+
private static string ThrowIfNull([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
32+
=> argument ?? throw new ArgumentNullException(paramName);
2733
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Aspire.Hosting.ApplicationModel;
5+
using Aspire.Hosting.Utils;
6+
using Xunit;
7+
8+
namespace Aspire.Hosting.Garnet.Tests;
9+
10+
public class GarnetPublicApiTests
11+
{
12+
[Fact]
13+
public void AddGarnetContainerShouldThrowWhenBuilderIsNull()
14+
{
15+
IDistributedApplicationBuilder builder = null!;
16+
const string name = "Garnet";
17+
18+
var action = () => builder.AddGarnet(name);
19+
20+
var exception = Assert.Throws<ArgumentNullException>(action);
21+
Assert.Equal(nameof(builder), exception.ParamName);
22+
}
23+
24+
[Fact]
25+
public void AddGarnetContainerShouldThrowWhenNameIsNull()
26+
{
27+
var builder = TestDistributedApplicationBuilder.Create();
28+
string name = null!;
29+
30+
var action = () => builder.AddGarnet(name);
31+
32+
var exception = Assert.Throws<ArgumentNullException>(action);
33+
Assert.Equal(nameof(name), exception.ParamName);
34+
}
35+
36+
[Fact]
37+
public void CtorGarnetResourceShouldThrowWhenNameIsNull()
38+
{
39+
string name = null!;
40+
41+
var action = () => new GarnetResource(name);
42+
43+
var exception = Assert.Throws<ArgumentNullException>(action);
44+
Assert.Equal(nameof(name), exception.ParamName);
45+
}
46+
47+
[Fact]
48+
public void WithDataBindMountShouldThrowWhenBuilderIsNull()
49+
{
50+
IResourceBuilder<GarnetResource> builder = null!;
51+
const string source = "/data";
52+
53+
var action = () => builder.WithDataBindMount(source);
54+
55+
var exception = Assert.Throws<ArgumentNullException>(action);
56+
Assert.Equal(nameof(builder), exception.ParamName);
57+
}
58+
59+
[Fact]
60+
public void WithDataBindMountShouldThrowWhenSourceIsNull()
61+
{
62+
var builder = TestDistributedApplicationBuilder.Create();
63+
var garnet = builder.AddGarnet("Garnet");
64+
string source = null!;
65+
66+
var action = () => garnet.WithDataBindMount(source);
67+
68+
var exception = Assert.Throws<ArgumentNullException>(action);
69+
Assert.Equal(nameof(source), exception.ParamName);
70+
}
71+
72+
[Fact]
73+
public void WithDataVolumeShouldThrowWhenBuilderIsNull()
74+
{
75+
IResourceBuilder<GarnetResource> builder = null!;
76+
77+
var action = () => builder.WithDataVolume();
78+
79+
var exception = Assert.Throws<ArgumentNullException>(action);
80+
Assert.Equal(nameof(builder), exception.ParamName);
81+
}
82+
83+
[Fact]
84+
public void WithPersistenceShouldThrowWhenBuilderIsNull()
85+
{
86+
IResourceBuilder<GarnetResource> builder = null!;
87+
88+
var action = () => builder.WithPersistence();
89+
90+
var exception = Assert.Throws<ArgumentNullException>(action);
91+
Assert.Equal(nameof(builder), exception.ParamName);
92+
}
93+
}

0 commit comments

Comments
 (0)