From 2d77f4d7c8fd8de75f5d408562e7e510fe2cddd9 Mon Sep 17 00:00:00 2001 From: Matthew Hensley Date: Fri, 29 Mar 2024 11:26:09 -0400 Subject: [PATCH] restructure for linux file path testing --- .../HostDetector.cs | 16 ++++++-- .../HostDetectorTests.cs | 40 +++++++++++++++---- ...emetry.ResourceDetectors.Host.Tests.csproj | 9 +++++ .../Samples/etc_machineid | 1 + .../Samples/etc_var_dbus_machineid | 1 + 5 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 test/OpenTelemetry.ResourceDetectors.Host.Tests/Samples/etc_machineid create mode 100644 test/OpenTelemetry.ResourceDetectors.Host.Tests/Samples/etc_var_dbus_machineid diff --git a/src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs b/src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs index e574b16008..d571e8fc60 100644 --- a/src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs +++ b/src/OpenTelemetry.ResourceDetectors.Host/HostDetector.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.Runtime.CompilerServices; using System.Text; using Microsoft.Win32; using OpenTelemetry.Resources; @@ -17,7 +16,10 @@ namespace OpenTelemetry.ResourceDetectors.Host; /// public sealed class HostDetector : IResourceDetector { + private const string ETC_MACHINEID = "/etc/machine-id"; + private const string ETC_VAR_DBUS_MACHINEID = "/var/lib/dbus/machine-id"; private readonly PlatformID platformId; + private readonly Func> getFilePaths; private readonly Func getMacOsMachineId; private readonly Func getWindowsMachineId; @@ -27,6 +29,7 @@ public sealed class HostDetector : IResourceDetector public HostDetector() : this( Environment.OSVersion.Platform, + GetFilePaths, GetMachineIdMacOs, GetMachineIdWindows) { @@ -38,9 +41,10 @@ public HostDetector() /// Target platform ID. /// Function to get MacOS machine ID. /// Function to get Windows machine ID. - internal HostDetector(PlatformID platformId, Func getMacOsMachineId, Func getWindowsMachineId) + internal HostDetector(PlatformID platformId, Func> getFilePaths, Func getMacOsMachineId, Func getWindowsMachineId) { this.platformId = platformId; + this.getFilePaths = getFilePaths ?? throw new ArgumentNullException(nameof(getFilePaths)); this.getMacOsMachineId = getMacOsMachineId ?? throw new ArgumentNullException(nameof(getMacOsMachineId)); this.getWindowsMachineId = getWindowsMachineId ?? throw new ArgumentNullException(nameof(getWindowsMachineId)); } @@ -68,6 +72,12 @@ public Resource Detect() return Resource.Empty; } + private static IEnumerable GetFilePaths() + { + yield return ETC_MACHINEID; + yield return ETC_VAR_DBUS_MACHINEID; + } + private static string GetMachineIdMacOs() { var startInfo = new ProcessStartInfo @@ -108,7 +118,7 @@ private string GetMachineId() private string GetMachineIdLinux() { - var paths = new[] { "/etc/machine-id", "/var/lib/dbus/machine-id" }; + var paths = this.getFilePaths(); foreach (var path in paths) { diff --git a/test/OpenTelemetry.ResourceDetectors.Host.Tests/HostDetectorTests.cs b/test/OpenTelemetry.ResourceDetectors.Host.Tests/HostDetectorTests.cs index fca946402c..15834f1291 100644 --- a/test/OpenTelemetry.ResourceDetectors.Host.Tests/HostDetectorTests.cs +++ b/test/OpenTelemetry.ResourceDetectors.Host.Tests/HostDetectorTests.cs @@ -2,7 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 using System; -using System.IO; +using System.Collections; +using System.Collections.Generic; using System.Linq; using System.Text; using OpenTelemetry.Resources; @@ -12,6 +13,9 @@ namespace OpenTelemetry.ResourceDetectors.Host.Tests; public class HostDetectorTests { + private static readonly IEnumerable ETC_MACHINEID = new[] { "Samples/etc_machineid" }; + private static readonly IEnumerable ETC_VAR_DBUS_MACHINEID = new[] { "Samples/etc_var_dbus_machineid" }; + [Fact] public void TestHostAttributes() { @@ -26,15 +30,35 @@ public void TestHostAttributes() } [Fact] - public void TestHostMachineId() + public void TestHostMachineIdLinux() { - var etcMachineIdStream = (string path) => + var combos = new[] { - return path == "/etc/machine-id" - ? new MemoryStream(Encoding.UTF8.GetBytes("etc-machine-id")) - : null; + (Enumerable.Empty(), string.Empty), + (ETC_MACHINEID, "etc_machineid"), + (ETC_VAR_DBUS_MACHINEID, "etc_var_dbus_machineid"), + (Enumerable.Concat(ETC_MACHINEID, ETC_VAR_DBUS_MACHINEID), "etc_machineid"), }; - var varLibDbusMachineIdStream = new MemoryStream(Encoding.UTF8.GetBytes("var-lib-dbus-machine-id")); + + foreach (var (path, expected) in combos) + { + var detector = new HostDetector( + PlatformID.Unix, + () => path, + () => throw new Exception("should not be called"), + () => throw new Exception("should not be called")); + var resource = ResourceBuilder.CreateEmpty().AddDetector(detector).Build(); + var resourceAttributes = resource.Attributes.ToDictionary(x => x.Key, x => (string)x.Value); + + if (string.IsNullOrEmpty(expected)) + { + Assert.Empty(resourceAttributes[HostSemanticConventions.AttributeHostId]); + } else + { + Assert.NotEmpty(resourceAttributes[HostSemanticConventions.AttributeHostId]); + Assert.Equal(expected, resourceAttributes[HostSemanticConventions.AttributeHostId]); + } + } } [Fact] @@ -42,6 +66,7 @@ public void TestHostMachineIdMacOs() { var detector = new HostDetector( PlatformID.MacOSX, + () => Enumerable.Empty(), () => "macos-machine-id", () => throw new Exception("should not be called")); var resource = ResourceBuilder.CreateEmpty().AddDetector(detector).Build(); @@ -55,6 +80,7 @@ public void TestHostMachineIdWindows() { var detector = new HostDetector( PlatformID.Win32NT, + () => Enumerable.Empty(), () => throw new Exception("should not be called"), () => "windows-machine-id"); var resource = ResourceBuilder.CreateEmpty().AddDetector(detector).Build(); diff --git a/test/OpenTelemetry.ResourceDetectors.Host.Tests/OpenTelemetry.ResourceDetectors.Host.Tests.csproj b/test/OpenTelemetry.ResourceDetectors.Host.Tests/OpenTelemetry.ResourceDetectors.Host.Tests.csproj index 23db48b06d..eefd21a835 100644 --- a/test/OpenTelemetry.ResourceDetectors.Host.Tests/OpenTelemetry.ResourceDetectors.Host.Tests.csproj +++ b/test/OpenTelemetry.ResourceDetectors.Host.Tests/OpenTelemetry.ResourceDetectors.Host.Tests.csproj @@ -11,4 +11,13 @@ + + + PreserveNewest + + + PreserveNewest + + + diff --git a/test/OpenTelemetry.ResourceDetectors.Host.Tests/Samples/etc_machineid b/test/OpenTelemetry.ResourceDetectors.Host.Tests/Samples/etc_machineid new file mode 100644 index 0000000000..033e610b97 --- /dev/null +++ b/test/OpenTelemetry.ResourceDetectors.Host.Tests/Samples/etc_machineid @@ -0,0 +1 @@ +etc_machineid diff --git a/test/OpenTelemetry.ResourceDetectors.Host.Tests/Samples/etc_var_dbus_machineid b/test/OpenTelemetry.ResourceDetectors.Host.Tests/Samples/etc_var_dbus_machineid new file mode 100644 index 0000000000..713c4b8b2a --- /dev/null +++ b/test/OpenTelemetry.ResourceDetectors.Host.Tests/Samples/etc_var_dbus_machineid @@ -0,0 +1 @@ +etc_var_dbus_machineid