Skip to content
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
23 changes: 18 additions & 5 deletions cli/tests/Vdk.Tests/FallbackDockerEngineTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Vdk.Models;
using Vdk.Services;
using Xunit;

namespace Vdk.Tests
{
Expand All @@ -8,26 +9,36 @@ public class FallbackDockerEngineTests : IDisposable
private readonly FallbackDockerEngine _engine = new();
private readonly string _containerName = $"vdk_test_{Guid.NewGuid().ToString().Substring(0, 8)}";
private readonly string _image = "alpine";
private readonly bool _dockerAvailable;

public FallbackDockerEngineTests()
{
// Pull image to avoid network flakiness in tests
FallbackDockerEngine.RunProcess("docker", $"pull {_image}", out _, out _);
// Check if Docker is available before running tests
_dockerAvailable = _engine.CanConnect();
if (_dockerAvailable)
{
// Pull image to avoid network flakiness in tests
FallbackDockerEngine.RunProcess("docker", $"pull {_image}", out _, out _);
}
}

[Fact]
[SkippableFact]
public void Run_And_Exists_And_Delete_Works()
{
Skip.IfNot(_dockerAvailable, "Docker is not available on this machine");

var result = _engine.Run(_image, _containerName, null, null, null, new[] { "sleep", "60" });
Assert.True(result, "Container should start");
Assert.True(_engine.Exists(_containerName), "Container should exist and be running");
Assert.True(_engine.Delete(_containerName), "Container should be deleted");
Assert.False(_engine.Exists(_containerName, false), "Container should not exist after delete");
}

[Fact]
[SkippableFact]
public void Stop_And_Exec_Works()
{
Skip.IfNot(_dockerAvailable, "Docker is not available on this machine");

var started = _engine.Run(_image, _containerName, null, null, null, new[] { "sleep", "60" });
Assert.True(started, "Container should start");
// Exec a command
Expand All @@ -40,9 +51,11 @@ public void Stop_And_Exec_Works()
Assert.True(_engine.Delete(_containerName), "Container should be deleted");
}

[Fact]
[SkippableFact]
public void Run_With_Ports_And_Volumes_Works()
{
Skip.IfNot(_dockerAvailable, "Docker is not available on this machine");

var ports = new[] { new PortMapping { HostPort = 12345, ContainerPort = 80 } };
var volumes = new[] { new FileMapping { Source = "/tmp", Destination = "/mnt" } };
var result = _engine.Run(_image, _containerName, ports, null, volumes, new[] { "sleep", "60" });
Expand Down
1 change: 1 addition & 0 deletions cli/tests/Vdk.Tests/Vdk.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="Xunit.SkippableFact" Version="1.5.61" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Loading