Skip to content

Commit be22c69

Browse files
committed
test: add tests for k6 hosting integration
1 parent 5696e04 commit be22c69

File tree

7 files changed

+220
-0
lines changed

7 files changed

+220
-0
lines changed

.github/workflows/tests.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
Hosting.GoFeatureFlag.Tests,
3333
Hosting.Golang.Tests,
3434
Hosting.Java.Tests,
35+
Hosting.k6.Tests,
3536
Hosting.LavinMQ.Tests,
3637
Hosting.MailPit.Tests,
3738
Hosting.Meilisearch.Tests,

CommunityToolkit.Aspire.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hos
371371
EndProject
372372
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hosting.k6.ApiService", "examples\k6\CommunityToolkit.Aspire.Hosting.k6.ApiService\CommunityToolkit.Aspire.Hosting.k6.ApiService.csproj", "{9DB0C7B2-31D3-481C-9C0C-EEAEC9B2AA6A}"
373373
EndProject
374+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hosting.k6.Tests", "tests\CommunityToolkit.Aspire.Hosting.k6.Tests\CommunityToolkit.Aspire.Hosting.k6.Tests.csproj", "{CCFE3593-49A7-4F03-A329-687490CD0143}"
375+
EndProject
374376
Global
375377
GlobalSection(SolutionConfigurationPlatforms) = preSolution
376378
Debug|Any CPU = Debug|Any CPU
@@ -973,6 +975,10 @@ Global
973975
{9DB0C7B2-31D3-481C-9C0C-EEAEC9B2AA6A}.Debug|Any CPU.Build.0 = Debug|Any CPU
974976
{9DB0C7B2-31D3-481C-9C0C-EEAEC9B2AA6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
975977
{9DB0C7B2-31D3-481C-9C0C-EEAEC9B2AA6A}.Release|Any CPU.Build.0 = Release|Any CPU
978+
{CCFE3593-49A7-4F03-A329-687490CD0143}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
979+
{CCFE3593-49A7-4F03-A329-687490CD0143}.Debug|Any CPU.Build.0 = Debug|Any CPU
980+
{CCFE3593-49A7-4F03-A329-687490CD0143}.Release|Any CPU.ActiveCfg = Release|Any CPU
981+
{CCFE3593-49A7-4F03-A329-687490CD0143}.Release|Any CPU.Build.0 = Release|Any CPU
976982
EndGlobalSection
977983
GlobalSection(SolutionProperties) = preSolution
978984
HideSolutionNode = FALSE
@@ -1159,6 +1165,7 @@ Global
11591165
{744F01AD-B28E-4B25-B6BA-42821157CA72} = {612ECA40-80B7-4365-9A6A-C35A6BE30FED}
11601166
{15BBCE34-82A2-489C-A65B-5BAEA299F07E} = {612ECA40-80B7-4365-9A6A-C35A6BE30FED}
11611167
{9DB0C7B2-31D3-481C-9C0C-EEAEC9B2AA6A} = {612ECA40-80B7-4365-9A6A-C35A6BE30FED}
1168+
{CCFE3593-49A7-4F03-A329-687490CD0143} = {899F0713-7FC6-4750-BAFC-AC650B35B453}
11621169
EndGlobalSection
11631170
GlobalSection(ExtensibilityGlobals) = postSolution
11641171
SolutionGuid = {08B1D4B8-D2C5-4A64-BB8B-E1A2B29525F0}

src/CommunityToolkit.Aspire.Hosting.k6/K6BuilderExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public static IResourceBuilder<K6Resource> WithScript(
9292
string duration = "30s")
9393
{
9494
ArgumentNullException.ThrowIfNull(builder);
95+
ArgumentNullException.ThrowIfNull(scriptPath);
96+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(virtualUsers);
97+
ArgumentNullException.ThrowIfNull(duration);
9598

9699
return builder.WithArgs(
97100
"run",
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 System.Net.Sockets;
5+
using Aspire.Hosting;
6+
7+
namespace CommunityToolkit.Aspire.Hosting.k6.Tests;
8+
9+
public class AddK6Tests
10+
{
11+
[Fact]
12+
public void AddK6ContainerWithDefaultsAddsAnnotationMetadata()
13+
{
14+
var appBuilder = DistributedApplication.CreateBuilder();
15+
16+
var k6 = appBuilder.AddK6("k6");
17+
18+
using var app = appBuilder.Build();
19+
20+
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
21+
22+
var containerResource = Assert.Single(appModel.Resources.OfType<K6Resource>());
23+
Assert.Equal("k6", containerResource.Name);
24+
25+
var endpoints = containerResource.Annotations.OfType<EndpointAnnotation>();
26+
Assert.Single(endpoints);
27+
28+
var primaryEndpoint = Assert.Single(endpoints, e => e.Name == "http");
29+
Assert.Equal(6565, primaryEndpoint.TargetPort);
30+
Assert.False(primaryEndpoint.IsExternal);
31+
Assert.Equal("http", primaryEndpoint.Name);
32+
Assert.Null(primaryEndpoint.Port);
33+
Assert.Equal(ProtocolType.Tcp, primaryEndpoint.Protocol);
34+
Assert.Equal("http", primaryEndpoint.Transport);
35+
Assert.Equal("http", primaryEndpoint.UriScheme);
36+
37+
var containerAnnotation = Assert.Single(containerResource.Annotations.OfType<ContainerImageAnnotation>());
38+
Assert.Equal(K6ContainerImageTags.Tag, containerAnnotation.Tag);
39+
Assert.Equal(K6ContainerImageTags.Image, containerAnnotation.Image);
40+
Assert.Equal(K6ContainerImageTags.Registry, containerAnnotation.Registry);
41+
}
42+
43+
[Fact]
44+
public void AddK6ContainerAddsAnnotationMetadata()
45+
{
46+
var appBuilder = DistributedApplication.CreateBuilder();
47+
var k6 = appBuilder.AddK6("k6");
48+
49+
using var app = appBuilder.Build();
50+
51+
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
52+
53+
var containerResource = Assert.Single(appModel.Resources.OfType<K6Resource>());
54+
Assert.Equal("k6", containerResource.Name);
55+
56+
var endpoints = containerResource.Annotations.OfType<EndpointAnnotation>();
57+
Assert.Single(endpoints);
58+
59+
var primaryEndpoint = Assert.Single(endpoints, e => e.Name == "http");
60+
Assert.Equal(6565, primaryEndpoint.TargetPort);
61+
Assert.False(primaryEndpoint.IsExternal);
62+
Assert.Equal("http", primaryEndpoint.Name);
63+
Assert.Null(primaryEndpoint.Port);
64+
Assert.Equal(ProtocolType.Tcp, primaryEndpoint.Protocol);
65+
Assert.Equal("http", primaryEndpoint.Transport);
66+
Assert.Equal("http", primaryEndpoint.UriScheme);
67+
68+
var containerAnnotation = Assert.Single(containerResource.Annotations.OfType<ContainerImageAnnotation>());
69+
Assert.Equal(K6ContainerImageTags.Tag, containerAnnotation.Tag);
70+
Assert.Equal(K6ContainerImageTags.Image, containerAnnotation.Image);
71+
Assert.Equal(K6ContainerImageTags.Registry, containerAnnotation.Registry);
72+
}
73+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using CommunityToolkit.Aspire.Testing;
2+
using Aspire.Components.Common.Tests;
3+
4+
namespace CommunityToolkit.Aspire.Hosting.k6.Tests;
5+
6+
[RequiresDocker]
7+
public class AppHostTests(AspireIntegrationTestFixture<Projects.CommunityToolkit_Aspire_Hosting_k6_AppHost> fixture) : IClassFixture<AspireIntegrationTestFixture<Projects.CommunityToolkit_Aspire_Hosting_k6_AppHost>>
8+
{
9+
[Fact]
10+
public async Task ResourceStartsAndRespondsOk()
11+
{
12+
var resourceName = "k6";
13+
await fixture.ResourceNotificationService.WaitForResourceHealthyAsync(resourceName).WaitAsync(TimeSpan.FromMinutes(5));
14+
var httpClient = fixture.CreateHttpClient(resourceName);
15+
16+
var response = await httpClient.GetAsync("/v1/status");
17+
18+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<Compile Include="$(RepoRoot)src\CommunityToolkit.Aspire.Hosting.k6\K6ContainerImageTags.cs" />
5+
</ItemGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\..\examples\k6\CommunityToolkit.Aspire.Hosting.k6.AppHost\CommunityToolkit.Aspire.Hosting.k6.AppHost.csproj" />
9+
<ProjectReference Include="..\..\src\CommunityToolkit.Aspire.Hosting.k6\CommunityToolkit.Aspire.Hosting.k6.csproj" />
10+
<ProjectReference Include="..\CommunityToolkit.Aspire.Testing\CommunityToolkit.Aspire.Testing.csproj" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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;
5+
6+
namespace CommunityToolkit.Aspire.Hosting.k6.Tests;
7+
8+
public class K6PublicApiTests
9+
{
10+
[Fact]
11+
public void AddK6ContainerShouldThrowWhenBuilderIsNull()
12+
{
13+
IDistributedApplicationBuilder builder = null!;
14+
const string name = "k6";
15+
16+
var action = () => builder.AddK6(name);
17+
18+
var exception = Assert.Throws<ArgumentNullException>(action);
19+
Assert.Equal(nameof(builder), exception.ParamName);
20+
}
21+
22+
[Fact]
23+
public void AddK6ContainerShouldThrowWhenNameIsNull()
24+
{
25+
IDistributedApplicationBuilder builder = new DistributedApplicationBuilder([]);
26+
string name = null!;
27+
28+
var action = () => builder.AddK6(name);
29+
30+
var exception = Assert.Throws<ArgumentNullException>(action);
31+
Assert.Equal(nameof(name), exception.ParamName);
32+
}
33+
34+
[Fact]
35+
public void WithScriptShouldThrowWhenBuilderIsNull()
36+
{
37+
IResourceBuilder<K6Resource> builder = null!;
38+
39+
const string scriptPath = "/scripts/main.js";
40+
41+
var action = () => builder.WithScript(scriptPath);
42+
43+
var exception = Assert.Throws<ArgumentNullException>(action);
44+
Assert.Equal(nameof(builder), exception.ParamName);
45+
}
46+
47+
[Fact]
48+
public void WithScriptShouldThrowWhenScriptPathIsNull()
49+
{
50+
var builder = new DistributedApplicationBuilder([]);
51+
var resourceBuilder = builder.AddK6("k6");
52+
53+
string scriptPath = null!;
54+
55+
var action = () => resourceBuilder.WithScript(scriptPath);
56+
57+
var exception = Assert.Throws<ArgumentNullException>(action);
58+
Assert.Equal(nameof(scriptPath), exception.ParamName);
59+
}
60+
61+
[Theory]
62+
[InlineData(-5)]
63+
[InlineData(0)]
64+
public void WithScriptShouldThrowWhenVirtualUsersIsNegativeOrZero(int virtualUsers)
65+
{
66+
var builder = new DistributedApplicationBuilder([]);
67+
var resourceBuilder = builder.AddK6("k6");
68+
69+
const string scriptPath = "scripts/main.js";
70+
71+
var action = () => resourceBuilder.WithScript(scriptPath, virtualUsers);
72+
73+
var exception = Assert.Throws<ArgumentOutOfRangeException>(action);
74+
Assert.Equal(nameof(virtualUsers), exception.ParamName);
75+
}
76+
77+
[Fact]
78+
public void WithScriptShouldThrowWhenDurationIsNull()
79+
{
80+
var builder = new DistributedApplicationBuilder([]);
81+
var resourceBuilder = builder.AddK6("k6");
82+
83+
const string scriptPath = "scripts/main.js";
84+
int vus = 10;
85+
string duration = null!;
86+
87+
var action = () => resourceBuilder.WithScript(scriptPath, vus, duration);
88+
89+
var exception = Assert.Throws<ArgumentNullException>(action);
90+
Assert.Equal(nameof(duration), exception.ParamName);
91+
}
92+
93+
[Fact]
94+
public void CtorK6ResourceShouldThrowWhenNameIsNull()
95+
{
96+
const string name = null!;
97+
98+
var action = () => new K6Resource(name!);
99+
100+
var exception = Assert.Throws<ArgumentNullException>(action);
101+
Assert.Equal(nameof(name), exception.ParamName);
102+
}
103+
}

0 commit comments

Comments
 (0)