forked from dotnet/aspire
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Aspire.Hosting.NodeJs.Tests project
Extract the main NodeJs tests from Aspire.Hosting.Tests and the end-to-end TestProject. Aspire.Hosting.Tests still uses NodeJs in a few places as an example "Executable" resource. Since NodeJs is just being used as an example - and not testing NodeJs itself, I decided to leave Aspire.Hosting.Tests using it. Also fix an issue with the RequiresTools attribute when the executable isn't an ".exe" extension - like npm is on Windows - it is `npm.cmd`. Contributes to dotnet#4294
- Loading branch information
Showing
16 changed files
with
262 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// 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.Utils; | ||
using Xunit; | ||
|
||
namespace Aspire.Hosting.NodeJs.Tests; | ||
|
||
public class AddNodeAppTests | ||
{ | ||
[Fact] | ||
public async Task NodeAppIsExecutableResource() | ||
{ | ||
using var builder = TestDistributedApplicationBuilder.Create(); | ||
|
||
var nodeApp = builder.AddNodeApp("nodeapp", "..\\foo\\app.js") | ||
.WithHttpEndpoint(port: 5031, env: "PORT"); | ||
var manifest = await ManifestUtils.GetManifest(nodeApp.Resource); | ||
|
||
var expectedManifest = $$""" | ||
{ | ||
"type": "executable.v0", | ||
"workingDirectory": "../../../../../tests/foo", | ||
"command": "node", | ||
"args": [ | ||
"..\\foo\\app.js" | ||
], | ||
"env": { | ||
"NODE_ENV": "development", | ||
"PORT": "{nodeapp.bindings.http.targetPort}" | ||
}, | ||
"bindings": { | ||
"http": { | ||
"scheme": "http", | ||
"protocol": "tcp", | ||
"transport": "http", | ||
"port": 5031, | ||
"targetPort": 8000 | ||
} | ||
} | ||
} | ||
"""; | ||
Assert.Equal(expectedManifest, manifest.ToString()); | ||
|
||
var npmApp = builder.AddNpmApp("npmapp", "..\\foo") | ||
.WithHttpEndpoint(port: 5032, env: "PORT"); | ||
manifest = await ManifestUtils.GetManifest(npmApp.Resource); | ||
|
||
expectedManifest = $$""" | ||
{ | ||
"type": "executable.v0", | ||
"workingDirectory": "../../../../../tests/foo", | ||
"command": "npm", | ||
"args": [ | ||
"run", | ||
"start" | ||
], | ||
"env": { | ||
"NODE_ENV": "development", | ||
"PORT": "{npmapp.bindings.http.targetPort}" | ||
}, | ||
"bindings": { | ||
"http": { | ||
"scheme": "http", | ||
"protocol": "tcp", | ||
"transport": "http", | ||
"port": 5032, | ||
"targetPort": 8000 | ||
} | ||
} | ||
} | ||
"""; | ||
Assert.Equal(expectedManifest, manifest.ToString()); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/Aspire.Hosting.NodeJs.Tests/Aspire.Hosting.NodeJs.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>$(NetCurrent)</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Aspire.Hosting.AppHost\Aspire.Hosting.AppHost.csproj" /> | ||
<ProjectReference Include="..\..\src\Aspire.Hosting.NodeJs\Aspire.Hosting.NodeJs.csproj" /> | ||
<ProjectReference Include="..\Aspire.Hosting.Tests\Aspire.Hosting.Tests.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 System.Globalization; | ||
using Aspire.Hosting.ApplicationModel; | ||
using Aspire.Hosting.Testing; | ||
using Aspire.Hosting.Utils; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
namespace Aspire.Hosting.NodeJs.Tests; | ||
|
||
/// <summary> | ||
/// TestProgram with node and npm apps. | ||
/// </summary> | ||
public class NodeAppFixture(IMessageSink diagnosticMessageSink) : IAsyncLifetime | ||
{ | ||
private DistributedApplication? _app; | ||
private string? _nodeAppPath; | ||
|
||
public DistributedApplication App => _app ?? throw new InvalidOperationException("DistributedApplication is not initialized."); | ||
|
||
public IResourceBuilder<NodeAppResource>? NodeAppBuilder { get; private set; } | ||
public IResourceBuilder<NodeAppResource>? NpmAppBuilder { get; private set; } | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
var builder = TestDistributedApplicationBuilder.Create(); | ||
builder.Services.AddXunitLogging(new TestOutputWrapper(diagnosticMessageSink)); | ||
|
||
_nodeAppPath = CreateNodeApp(); | ||
var scriptPath = Path.Combine(_nodeAppPath, "app.js"); | ||
|
||
NodeAppBuilder = builder.AddNodeApp("nodeapp", scriptPath) | ||
.WithHttpEndpoint(port: 5031, env: "PORT"); | ||
|
||
NpmAppBuilder = builder.AddNpmApp("npmapp", _nodeAppPath) | ||
.WithHttpEndpoint(port: 5032, env: "PORT"); | ||
|
||
_app = builder.Build(); | ||
|
||
using var cts = new CancellationTokenSource(TimeSpan.FromMinutes(5)); | ||
|
||
await _app.StartAsync(cts.Token); | ||
|
||
await WaitReadyStateAsync(cts.Token); | ||
} | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
if (_app is not null) | ||
{ | ||
await _app.StopAsync(); | ||
await _app.DisposeAsync(); | ||
} | ||
|
||
if (_nodeAppPath is not null) | ||
{ | ||
Directory.Delete(_nodeAppPath, recursive: true); | ||
} | ||
} | ||
|
||
private static string CreateNodeApp() | ||
{ | ||
var tempDir = Directory.CreateTempSubdirectory("aspire-nodejs-tests").FullName; | ||
|
||
File.WriteAllText(Path.Combine(tempDir, "app.js"), | ||
""" | ||
const http = require('http'); | ||
const port = process.env.PORT ?? 3000; | ||
const server = http.createServer((req, res) => { | ||
res.statusCode = 200; | ||
res.setHeader('Content-Type', 'text/plain'); | ||
if (process.env.npm_lifecycle_event === undefined) { | ||
res.end('Hello from node!'); | ||
} else { | ||
res.end('Hello from npm!'); | ||
} | ||
}); | ||
server.listen(port, () => { | ||
console.log('Web server running on on %s', port); | ||
}); | ||
"""); | ||
|
||
File.WriteAllText(Path.Combine(tempDir, "package.json"), | ||
""" | ||
{ | ||
"scripts": { | ||
"start": "node app.js" | ||
} | ||
} | ||
"""); | ||
|
||
return tempDir; | ||
} | ||
|
||
private async Task WaitReadyStateAsync(CancellationToken cancellationToken = default) | ||
{ | ||
using var client = App.CreateHttpClient(NodeAppBuilder!.Resource.Name, endpointName: "http"); | ||
await client.GetStringAsync("/", cancellationToken); | ||
} | ||
|
||
private sealed class TestOutputWrapper(IMessageSink messageSink) : ITestOutputHelper | ||
{ | ||
public void WriteLine(string message) | ||
{ | ||
messageSink.OnMessage(new DiagnosticMessage(message)); | ||
} | ||
|
||
public void WriteLine(string format, params object[] args) | ||
{ | ||
messageSink.OnMessage(new DiagnosticMessage(string.Format(CultureInfo.CurrentCulture, format, args))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.