DockerSupportsArchInlineData.cs 3.4 KB

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