Skip to content

Commit

Permalink
detect container and test accordingly (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
cklutz authored Oct 17, 2024
1 parent c9f0d0c commit 89b3662
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
22 changes: 18 additions & 4 deletions test/LockCheck.Tests/Linux/ProcFileSystemTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using LockCheck.Linux;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -14,10 +15,23 @@ public void GetProcessExecutablePathFromCmdLine_ShouldReturnExecutableName_WhenN
{
using var init = Process.GetProcessById(1);

// Assume unit tests are not run as root.
Assert.IsNull(init.MainModule);

Assert.AreEqual("/sbin/init", ProcFileSystem.GetProcessExecutablePathFromCmdLine(1));
// This check will only work with "official" Microsoft images.
if (Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true")
{
// Running inside a container, PID 1 will not be /sbin/init, but the process that was
// initially started in the container.
Assert.IsNotNull(init.MainModule);
// MainModule.FileName might just be the file name, not the full path, as documented.
StringAssert.Contains(init.MainModule!.FileName, ProcFileSystem.GetProcessExecutablePathFromCmdLine(1));
}
else
{
// Not running in a container, PID 1 is the init process.
// MainModule should be null, because we expect the unit tests to not run as root and thus
// don't have permissions to access details of the process.
Assert.IsNull(init.MainModule);
Assert.AreEqual("/sbin/init", ProcFileSystem.GetProcessExecutablePathFromCmdLine(1));
}
}

[TestMethod]
Expand Down
14 changes: 8 additions & 6 deletions test/test-docker-linux.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# -e VSTEST_HOST_DEBUG=1 `
docker run `
--rm --name LockCheck.Tests `
-v ${PWD}\..:/mnt/lc `
-w /mnt/lc `
mcr.microsoft.com/dotnet/sdk:8.0 `
bash -c '/usr/share/dotnet/dotnet test -c Release -f net8.0 test/LockCheck.Tests/LockCheck.Tests.csproj && /usr/share/dotnet/dotnet test -c Debug -f net8.0 test/LockCheck.Tests/LockCheck.Tests.csproj'
$mydir = $PSScriptRoot
$project = './test/LockCheck.Tests/LockCheck.Tests.csproj'
$dotnet = '/usr/share/dotnet/dotnet'

$script = "$dotnet test -c Release -f net8.0 && $dotnet test -c Debug -f net8.0"
$script = $script.Replace("`r", "")

docker run --rm --name LockCheck.Tests -v ${mydir}/..:/mnt/lc -w /mnt/lc mcr.microsoft.com/dotnet/sdk:8.0 bash -c $script

0 comments on commit 89b3662

Please sign in to comment.