Skip to content

Commit

Permalink
Users/nmalkapuram/user name detection logic (#252)
Browse files Browse the repository at this point in the history
* Adding username detection logic

* create package directory if it doesn't exists for wgetInstallationDependency

* updating unit tests and resolving comments

* removing unused library reference

* fixinf scenario name in redis profile

* username changes

* Adding username detection logic

* updating unit tests and resolving comments

* removing unused library reference

* removing comments

* resolving comments

* resolving comments

---------

Co-authored-by: Yang Pan <31965446+yangpanMS@users.noreply.github.com>
  • Loading branch information
nmalkapuram and yangpanMS authored Mar 21, 2024
1 parent 74df00b commit e1e1d96
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace VirtualClient.Actions
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using VirtualClient.Actions.Memtier;
using VirtualClient.Common;
Expand Down Expand Up @@ -62,9 +63,12 @@ await executor.ExecuteAsync(ProfileTiming.OneIteration(), CancellationToken.None
[TestCase("PERF-MEMCACHED.json")]
public async Task MemcachedMemtierWorkloadProfileExecutesTheWorkloadAsExpectedOfServerOnUnixPlatformMultiVM(string profile)
{
this.mockFixture.SystemManagement.Setup(mgr => mgr.GetLoggedInUserName())
.Returns("mockuser");

IEnumerable<string> expectedCommands = new List<string>
{
$"sudo -u {Environment.UserName} bash -c \"numactl -C {string.Join(",", Enumerable.Range(0, Environment.ProcessorCount))} /.+/memcached -p 6379 -t 4 -m 30720 -c 16384\""
$"sudo -u mockuser bash -c \"numactl -C {string.Join(",", Enumerable.Range(0, Environment.ProcessorCount))} /.+/memcached -p 6379 -t 4 -m 30720 -c 16384\""
};

// Setup the expectations for the workload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public string Username
string username = this.Parameters.GetValue<string>(nameof(MemcachedExecutor.Username), string.Empty);
if (string.IsNullOrWhiteSpace(username))
{
username = Environment.UserName;
username = this.SystemManagement.GetLoggedInUserName();
}

return username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ private void CaptureMetrics(EventContext telemetryContext, CancellationToken can
{
this.Logger.LogMetrics(
$"Memtier-{this.Benchmark}",
this.Scenario,
this.MetricScenario ?? this.Scenario,
processInfo.StartTime,
processInfo.EndTime,
metric.Name,
Expand All @@ -409,7 +409,7 @@ private void CaptureMetrics(EventContext telemetryContext, CancellationToken can
{
this.Logger.LogMetrics(
$"Memtier-{this.Benchmark}",
this.Scenario,
this.MetricScenario ?? this.Scenario,
processReference.StartTime,
processReference.EndTime,
metric.Name,
Expand Down
6 changes: 6 additions & 0 deletions src/VirtualClient/VirtualClient.Contracts/ISystemInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public interface ISystemInfo
/// <returns>True/False is an IP is defined on current system.</returns>
bool IsLocalIPAddress(string ipAddress);

/// <summary>
/// Get the logged In Username i.e, username of the user who invoked a command with elevated privileges using the "sudo" command in Unix operating system.
/// </summary>
/// <returns>Username of the logged In user</returns>
string GetLoggedInUserName();

/// <summary>
/// Returns information about the CPU on the system.
/// </summary>
Expand Down
24 changes: 24 additions & 0 deletions src/VirtualClient/VirtualClient.Core/SystemManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,30 @@ public async Task<CpuInfo> GetCpuInfoAsync(CancellationToken cancellationToken)
return info;
}

/// <summary>
/// Get the logged In Username i.e, username of the user who invoked a command with elevated privileges using the "sudo" command in Unix operating system.
/// </summary>
public string GetLoggedInUserName()
{
string loggedInUserName = Environment.UserName;
if (string.Equals(loggedInUserName, "root"))
{
loggedInUserName = Environment.GetEnvironmentVariable("SUDO_USER");
if (string.Equals(loggedInUserName, "root") || string.IsNullOrEmpty(loggedInUserName))
{
loggedInUserName = Environment.GetEnvironmentVariable("VC_SUDO_USER");
if (string.IsNullOrEmpty(loggedInUserName))
{
throw new EnvironmentSetupException($"'USER' Environment variable is set to root and 'SUDO_USER' Environment variable is either root or null." +
"The required environment variable 'VC_SUDO_USER' is expected to be set to a valid non-empty value." +
"Please ensure that the necessary environment variables are configured properly for the execution environment.", ErrorReason.EnvironmentIsInsufficent);
}
}
}

return loggedInUserName;
}

/// <summary>
/// Returns information about the specific Linux distribution (e.g. Ubuntu, CentOS).
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ protected override async Task ExecuteAsync(EventContext telemetryContext, Cancel
string installationPath = downloadedPackagePath;
string packagesDirectory = this.GetPackagePath();

// Create packages directory if not present.
if (!this.fileSystem.Directory.Exists(packagesDirectory))
{
this.fileSystem.Directory.CreateDirectory(packagesDirectory);
}

// Cleanup the file if it exists.
if (this.fileSystem.File.Exists(downloadedPackagePath))
{
Expand Down

0 comments on commit e1e1d96

Please sign in to comment.