diff --git a/test/LockCheck.Tests/Linux/ProcFileSystemTests.cs b/test/LockCheck.Tests/Linux/ProcFileSystemTests.cs index b63d12f..91e055d 100644 --- a/test/LockCheck.Tests/Linux/ProcFileSystemTests.cs +++ b/test/LockCheck.Tests/Linux/ProcFileSystemTests.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using System.IO; using System.Text; using LockCheck.Linux; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -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] diff --git a/test/test-docker-linux.ps1 b/test/test-docker-linux.ps1 index 8e49189..5082cb6 100644 --- a/test/test-docker-linux.ps1 +++ b/test/test-docker-linux.ps1 @@ -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' \ No newline at end of file +$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