Skip to content

Commit 07e7dfe

Browse files
committed
Revert "delete file that was renamed"
This reverts commit 5c29eb7.
1 parent b016cd0 commit 07e7dfe

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Reflection;
5+
using System.Text.Json;
6+
using Xunit.Sdk;
7+
8+
namespace Microsoft.NET.Build.Containers.IntegrationTests;
9+
10+
public class DockerSupportsArchInlineData : DataAttribute
11+
{
12+
private readonly string _arch;
13+
private readonly object[] _data;
14+
15+
public DockerSupportsArchInlineData(string arch, params object[] data)
16+
{
17+
_arch = arch;
18+
_data = data;
19+
}
20+
21+
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
22+
{
23+
if (DockerSupportsArchHelper.DaemonSupportsArch(_arch))
24+
{
25+
return new object[][] { _data.Prepend(_arch).ToArray() };
26+
}
27+
else
28+
{
29+
base.Skip = $"Skipping test because Docker daemon does not support {_arch}.";
30+
}
31+
return Array.Empty<object[]>();
32+
}
33+
}
34+
35+
internal static class DockerSupportsArchHelper
36+
{
37+
internal static bool DaemonIsAvailable => ContainerCli.IsAvailable;
38+
39+
internal static bool IsContainerdStoreEnabledForDocker => ContainerCli.IsContainerdStoreEnabledForDocker;
40+
41+
internal static bool DaemonSupportsArch(string arch)
42+
{
43+
// an optimization - this doesn't change over time so we can compute it once
44+
string[] LinuxPlatforms = GetSupportedLinuxPlatforms();
45+
46+
if (LinuxPlatforms.Contains(arch))
47+
{
48+
return true;
49+
}
50+
else
51+
{
52+
// another optimization - daemons don't switch types easily or quickly, so this is as good as static
53+
bool IsWindowsDockerDaemon = GetIsWindowsDockerDaemon();
54+
55+
if (IsWindowsDockerDaemon && arch.StartsWith("windows", StringComparison.OrdinalIgnoreCase))
56+
{
57+
return true;
58+
}
59+
return false;
60+
}
61+
}
62+
63+
private static string[] GetSupportedLinuxPlatforms()
64+
{
65+
if (ContainerCli.IsPodman)
66+
{
67+
var inspectResult = new RunExeCommand(NullLogger.Instance, "podman", "info").Execute();
68+
inspectResult.Should().Pass();
69+
var platformsLine = inspectResult.StdOut!.Split(Environment.NewLine).First(x => x.Contains("OsArch:", StringComparison.OrdinalIgnoreCase));
70+
return new[] { platformsLine.Trim().Substring("OsArch: ".Length) };
71+
}
72+
else
73+
{
74+
var inspectResult = new RunExeCommand(NullLogger.Instance, "docker", "buildx", "inspect", "default").Execute();
75+
inspectResult.Should().Pass();
76+
var platformsLine = inspectResult.StdOut!.Split(Environment.NewLine).First(x => x.StartsWith("Platforms:", StringComparison.OrdinalIgnoreCase));
77+
return platformsLine.Substring("Platforms: ".Length).Split(",", StringSplitOptions.TrimEntries);
78+
}
79+
}
80+
81+
private static bool GetIsWindowsDockerDaemon()
82+
{
83+
if (ContainerCli.IsPodman)
84+
{
85+
return false;
86+
}
87+
// the config json has an OSType property that is either "linux" or "windows" -
88+
// we can't use this for linux arch detection because that isn't enough information.
89+
var config = DockerCli.GetDockerConfig();
90+
if (config.RootElement.TryGetProperty("OSType", out JsonElement osTypeProperty))
91+
{
92+
return osTypeProperty.GetString() == "windows";
93+
}
94+
else
95+
{
96+
return false;
97+
}
98+
}
99+
100+
private class NullLogger : ITestOutputHelper
101+
{
102+
private NullLogger() { }
103+
104+
public static NullLogger Instance { get; } = new NullLogger();
105+
106+
public void WriteLine(string message)
107+
{
108+
//do nothing
109+
}
110+
public void WriteLine(string format, params object[] args)
111+
{
112+
//do nothing
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)