Skip to content

Commit f6d662f

Browse files
authored
Merge pull request #153 from homiedopie/bugfix/DOTNET-178
(DOTNET-178) - Initial logic fix for GetDeviceName
2 parents 652ea9e + 9f55710 commit f6d662f

File tree

5 files changed

+85
-23
lines changed

5 files changed

+85
-23
lines changed

Src/StackifyLib/Models/EnvironmentDetail.cs

+17-17
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public static EnvironmentDetail Get()
3030
return _CachedCopy ?? (_CachedCopy = new EnvironmentDetail());
3131
}
3232
#if NETSTANDARD
33-
private static System.Net.Http.HttpClient Client => new System.Net.Http.HttpClient();
33+
private System.Net.Http.HttpClient Client => new System.Net.Http.HttpClient();
3434
#endif
35-
private static bool registryAccessFailure = false;
35+
private bool registryAccessFailure = false;
3636

3737
/// <summary>
3838
/// Figures out if the server is in azure and if so gets the azure instance name
@@ -110,23 +110,23 @@ private void GetAzureInfo()
110110
#endif
111111
}
112112

113-
private static bool? _isIMDSv1;
113+
private bool? _isIMDSv1;
114114
// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html#d0e30002
115115
private const string EC2InstanceIdUrl = "http://169.254.169.254/latest/meta-data/instance-id";
116116
private const string IMDS_BASE_URL = "http://169.254.169.254/latest";
117117
private const string IMDS_TOKEN_PATH = "/api/token";
118118
private const string IMDS_INSTANCE_ID_PATH = "/meta-data/instance-id";
119119
private const string IMDSV1_BASE_URL = "http://169.254.169.254/latest/meta-data/";
120-
public static readonly object ec2InstanceLock = new object();
121-
private static DateTimeOffset? ec2InstanceIdLastUpdate = null;
122-
private static string ec2InstanceId = null;
120+
public static readonly object ec2InstanceLock = new object();
121+
private DateTimeOffset? ec2InstanceIdLastUpdate = null;
122+
private string ec2InstanceId = string.Empty;
123123

124124
/// <summary>
125125
/// Get the EC2 Instance name if it exists else null
126126
/// </summary>
127127
#if NETFULL
128128

129-
public static string GetDeviceName()
129+
public string GetDeviceName()
130130
{
131131
var deviceName = Environment.GetEnvironmentVariable("STACKIFY_DEVICE_NAME");
132132
if (!String.IsNullOrEmpty(deviceName))
@@ -139,7 +139,7 @@ public static string GetDeviceName()
139139

140140
var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName);
141141

142-
if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2)
142+
if ((Config.IsEc2.HasValue && Config.IsEc2 == true) || isDefaultDeviceNameEc2)
143143
{
144144
var instanceID_task = GetEC2InstanceId();
145145
if (string.IsNullOrWhiteSpace(instanceID_task) == false)
@@ -151,7 +151,7 @@ public static string GetDeviceName()
151151
return deviceName.Substring(0, deviceName.Length > 60 ? 60 : deviceName.Length);
152152
}
153153

154-
public static bool IsIMDSv1()
154+
public bool IsIMDSv1()
155155
{
156156

157157
if (_isIMDSv1.HasValue)
@@ -174,7 +174,7 @@ public static bool IsIMDSv1()
174174
}
175175
}
176176

177-
public static string GetAccessToken()
177+
public string GetAccessToken()
178178
{
179179
var url = IMDS_BASE_URL + IMDS_TOKEN_PATH;
180180
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
@@ -190,7 +190,7 @@ public static string GetAccessToken()
190190
}
191191

192192

193-
public static string GetEC2InstanceId()
193+
public string GetEC2InstanceId()
194194
{
195195
string r = null;
196196

@@ -258,7 +258,7 @@ public static string GetEC2InstanceId()
258258
return r;
259259
}
260260
#else
261-
public static string GetDeviceName()
261+
public string GetDeviceName()
262262
{
263263
var deviceName = Environment.GetEnvironmentVariable("STACKIFY_DEVICE_NAME");
264264
if (!String.IsNullOrEmpty(deviceName))
@@ -271,7 +271,7 @@ public static string GetDeviceName()
271271

272272
var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName);
273273

274-
if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2)
274+
if ((Config.IsEc2.HasValue && Config.IsEc2 == true) || isDefaultDeviceNameEc2)
275275
{
276276
var instanceID_task = GetEC2InstanceId();
277277
instanceID_task.Wait();
@@ -284,7 +284,7 @@ public static string GetDeviceName()
284284
return deviceName.Substring(0, deviceName.Length > 60 ? 60 : deviceName.Length);
285285
}
286286

287-
public static async Task<string> GetAccessTokenAsync()
287+
public async Task<string> GetAccessTokenAsync()
288288
{
289289
var url = IMDS_BASE_URL + IMDS_TOKEN_PATH;
290290
var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Put, url);
@@ -294,7 +294,7 @@ public static async Task<string> GetAccessTokenAsync()
294294
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
295295
}
296296

297-
public static async Task<bool> IsIMDSv1()
297+
public async Task<bool> IsIMDSv1()
298298
{
299299
if (_isIMDSv1.HasValue)
300300
{
@@ -314,7 +314,7 @@ public static async Task<bool> IsIMDSv1()
314314
}
315315
}
316316

317-
public static async Task<string> GetEC2InstanceId()
317+
public async Task<string> GetEC2InstanceId()
318318
{
319319
string r = null;
320320
try
@@ -345,7 +345,7 @@ public static async Task<string> GetEC2InstanceId()
345345
}
346346

347347
#endif
348-
private static bool IsEc2MachineName(string machineName)
348+
private bool IsEc2MachineName(string machineName)
349349
{
350350
if (string.IsNullOrWhiteSpace(machineName))
351351
{

test/StackifyLib.UnitTests/ConfigJSON_Tests.cs

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2024 BMC Software, Inc.
2+
// Copyright (c) 2021-2024 Netreo
3+
// Copyright (c) 2019 Stackify
4+
#if NETCORE
15
using System;
26
using System.Collections.Generic;
37
using System.Diagnostics;
@@ -125,3 +129,4 @@ private void ResetConfig()
125129
}
126130

127131
}
132+
#endif

test/StackifyLib.UnitTests/JsonSerialization_Tests.cs

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2024 BMC Software, Inc.
2+
// Copyright (c) 2021-2024 Netreo
3+
// Copyright (c) 2019 Stackify
4+
#if NETCORE
15
using System;
26
using System.Collections.Generic;
37
using System.Diagnostics;
@@ -134,3 +138,4 @@ private static object GetTestObject()
134138
}
135139
}
136140
}
141+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2024 BMC Software, Inc.
2+
// Copyright (c) 2021-2024 Netreo
3+
// Copyright (c) 2019 Stackify
4+
using StackifyLib.Models;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Reflection;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
using Xunit;
12+
using Moq;
13+
14+
namespace StackifyLib.UnitTests.Models
15+
{
16+
public class EnvironmentDetail_Tests
17+
{
18+
#if NETCORE
19+
[Fact]
20+
public async Task TestIfConfigEc2IsNull()
21+
{
22+
var envService = new EnvironmentDetail();
23+
Assert.Equal(Environment.MachineName, envService.GetDeviceName());
24+
25+
var type = envService.GetType();
26+
var property = type.GetField("ec2InstanceId", BindingFlags.NonPublic | BindingFlags.Instance);
27+
Assert.Equal(property.GetValue(envService) as string, string.Empty);
28+
}
29+
#endif
30+
#if NETFULL
31+
[Fact]
32+
public void TestIfConfigEc2IsNull()
33+
{
34+
var envService = new EnvironmentDetail();
35+
Assert.Equal(Environment.MachineName, envService.GetDeviceName());
36+
37+
var type = envService.GetType();
38+
var property = type.GetField("ec2InstanceId", BindingFlags.NonPublic | BindingFlags.Instance);
39+
Assert.Equal(property.GetValue(envService) as string, string.Empty);
40+
}
41+
#endif
42+
}
43+
}

test/StackifyLib.UnitTests/StackifyLib.UnitTests.csproj

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.1</TargetFramework>
4+
<TargetFrameworks>net6.0;net462</TargetFrameworks>
5+
<IsPackable>false</IsPackable>
6+
<IsTestProject>true</IsTestProject>
57
</PropertyGroup>
8+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
9+
<DefineConstants>NETCORE</DefineConstants>
10+
</PropertyGroup>
11+
12+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net462' ">
13+
<DefineConstants>NETFULL</DefineConstants>
14+
</PropertyGroup>
15+
616

717
<ItemGroup>
818
<PackageReference Include="FluentAssertions" Version="5.5.3" />
9-
<PackageReference Include="xunit" Version="2.4.1" />
10-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
11-
<PrivateAssets>all</PrivateAssets>
12-
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
13-
</PackageReference>
19+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
20+
<PackageReference Include="Moq" Version="4.20.70" />
21+
<PackageReference Include="xunit" Version="2.8.1" />
22+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1" />
1423
</ItemGroup>
1524

1625
<ItemGroup>

0 commit comments

Comments
 (0)