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

chore: Update Testcontainers for .NET #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
55 changes: 33 additions & 22 deletions src/apps/cli/SandboxSkill.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
using System.Text;
using DotNet.Testcontainers.Builders;
using Microsoft.SemanticKernel.SkillDefinition;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;

public class SandboxSkill
{
public async Task<string> RunInAlpineAsync(string input)
public Task<string> RunInAlpineAsync(string input)
{
return await RunInContainer(input, "alpine");
return RunInContainer(input, "alpine:3.18");
}

public async Task<string> RunInDotnetAlpineAsync(string input)
public Task<string> RunInDotnetAlpineAsync(string input)
{
return await RunInContainer(input, "mcr.microsoft.com/dotnet/sdk:7.0");
return RunInContainer(input, "mcr.microsoft.com/dotnet/sdk:7.0-alpine");
}

private async Task<string> RunInContainer(string input, string image)
private static async Task<string> RunInContainer(string input, string image)
{
var tempScriptFile = $"{Guid.NewGuid().ToString()}.sh";
var tempScriptPath = $"./output/{tempScriptFile}";
await File.WriteAllTextAsync(tempScriptPath, input);
Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(),"output", "src"));
var tempScriptFile = Path.ChangeExtension(Guid.NewGuid().ToString(), "sh");

var srcDirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), "output", "src");
_ = Directory.CreateDirectory(srcDirectoryPath);

var dotnetContainer = new ContainerBuilder()
.WithName(Guid.NewGuid().ToString("D"))
.WithImage(image)
.WithBindMount(Path.Combine(Directory.GetCurrentDirectory(),"output", "src"), "/src")
.WithBindMount(Path.Combine(Directory.GetCurrentDirectory(), tempScriptPath), $"/src/{tempScriptFile}")
.WithWorkingDirectory("/src")
.WithCommand("sh", tempScriptFile)
.Build();
.WithImage(image)
.WithBindMount(srcDirectoryPath, "/src")
Copy link
Author

Choose a reason for hiding this comment

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

It is recommended to utilize the WithResourceMapping API here too. This adjustment will ensure that the implementation works correct with remote Docker hosts. I have not made the changes because I am unsure about the file sizes.

.WithResourceMapping(Encoding.Default.GetBytes(input), $"/src/{tempScriptFile}")
.WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new ScriptCompleted()))
Copy link
Author

Choose a reason for hiding this comment

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

The wait strategy I have implemented assumes that the container's startup process should wait until the shell script has completed.

.WithWorkingDirectory("/src")
.WithEntrypoint("/bin/sh")
.WithCommand(tempScriptFile)
.Build();

await dotnetContainer.StartAsync()
.ConfigureAwait(false);
// Cleanup
File.Delete(tempScriptPath);
File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "output", "src", tempScriptFile));
return "";
.ConfigureAwait(false);

File.Delete(Path.Combine(srcDirectoryPath, tempScriptFile));
return string.Empty;
}

private sealed class ScriptCompleted : IWaitUntil
{
public Task<bool> UntilAsync(IContainer container)
{
return Task.FromResult(TestcontainersStates.Exited.Equals(container.State));
}
}
}
2 changes: 1 addition & 1 deletion src/apps/cli/cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="Microsoft.SemanticKernel" Version="0.24.230918.1-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Qdrant" Version="0.24.230918.1-preview" />
<PackageReference Include="Testcontainers" Version="3.2.0" />
<PackageReference Include="Testcontainers" Version="3.5.0" />
<ProjectReference Include="..\..\libs\skills\skills.csproj" />
</ItemGroup>

Expand Down