From ec1d1e9061caf50b4e485ab36a6042346914668d Mon Sep 17 00:00:00 2001 From: Eddie Wassef Date: Wed, 28 Jan 2026 15:36:04 -0600 Subject: [PATCH] Skip Docker tests when Docker isn't available Make FallbackDockerEngine tests safe to run on machines without Docker by checking connectivity in the test constructor and skipping tests when Docker is unavailable. Replaced [Fact] with [SkippableFact], added Skip.IfNot(_dockerAvailable, ...) to each test, and only pull the test image when Docker can be reached. Added a using Xunit and added the Xunit.SkippableFact package to the test project so tests can be conditionally skipped. --- .../Vdk.Tests/FallbackDockerEngineTests.cs | 23 +++++++++++++++---- cli/tests/Vdk.Tests/Vdk.Tests.csproj | 1 + 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/cli/tests/Vdk.Tests/FallbackDockerEngineTests.cs b/cli/tests/Vdk.Tests/FallbackDockerEngineTests.cs index 0655d8c..c626ff6 100644 --- a/cli/tests/Vdk.Tests/FallbackDockerEngineTests.cs +++ b/cli/tests/Vdk.Tests/FallbackDockerEngineTests.cs @@ -1,5 +1,6 @@ using Vdk.Models; using Vdk.Services; +using Xunit; namespace Vdk.Tests { @@ -8,16 +9,24 @@ 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"); @@ -25,9 +34,11 @@ public void Run_And_Exists_And_Delete_Works() 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 @@ -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" }); diff --git a/cli/tests/Vdk.Tests/Vdk.Tests.csproj b/cli/tests/Vdk.Tests/Vdk.Tests.csproj index d64b460..5a5ac29 100644 --- a/cli/tests/Vdk.Tests/Vdk.Tests.csproj +++ b/cli/tests/Vdk.Tests/Vdk.Tests.csproj @@ -24,6 +24,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive all