diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs b/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs index 1692400ee4..a2c028ab77 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs @@ -103,7 +103,7 @@ internal static async Task ProcessResponseAsync(HttpResponseMes /// machine id internal static string GetMachineId() { - if (VmMetadataApiHandler.azMetadata != null) + if (!String.IsNullOrWhiteSpace(VmMetadataApiHandler.azMetadata?.Compute?.VMId)) { return VmMetadataApiHandler.azMetadata.Compute.VMId; } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs index b47c9e4d03..1d169ab7b6 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs @@ -59,6 +59,30 @@ static Task sendFunc(HttpRequestMessage request, Cancellati Assert.AreEqual("vmId:d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd", VmMetadataApiHandler.GetMachineId()); } + [TestMethod] + public async Task GetVMMachineIdWithNullComputeTest() + { + static Task sendFunc(HttpRequestMessage request, CancellationToken cancellationToken) + { + object jsonObject = JsonConvert.DeserializeObject("{\"compute\":null}"); + string payload = JsonConvert.SerializeObject(jsonObject); + HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent(payload, Encoding.UTF8, "application/json") + }; + return Task.FromResult(response); + } + + HttpMessageHandler messageHandler = new MockMessageHandler(sendFunc); + CosmosHttpClient cosmoshttpClient = MockCosmosUtil.CreateCosmosHttpClient(() => new HttpClient(messageHandler)); + + VmMetadataApiHandler.TryInitialize(cosmoshttpClient); + + await Task.Delay(2000); + Assert.IsNull(VmMetadataApiHandler.GetMachineInfo()); + Assert.IsNotNull(VmMetadataApiHandler.GetMachineId()); + } + [TestMethod] public async Task GetHashedMachineNameAsMachineIdTest() {