Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ public void Prime95ExecutorThrowsWhenTheWorkloadDoesNotProduceValidResults(Platf
}

[Test]
[TestCase(PlatformID.Win32NT, @"\win-x64\results.txt")]
[TestCase(PlatformID.Unix, @"/linux-x64/results.txt")]
public void Prime95ExecutorThrowsWhenWorkloadResultsFileNotFound(PlatformID platform, string resultsPath)
[TestCase(PlatformID.Win32NT)]
[TestCase(PlatformID.Unix)]
public void Prime95ExecutorThrowsWhenWorkloadResultsFileNotFound(PlatformID platform)
{
this.SetupTest(platform);
this.mockFixture.File.Setup(fe => fe.Exists(this.mockPackage.Path + resultsPath))
this.mockFixture.File.Setup(fe => fe.Exists(It.Is<string>(file => file.EndsWith("results.txt"))))
.Returns(false);

using (TestPrime95Executor executor = new TestPrime95Executor(this.mockFixture))
Expand Down
19 changes: 14 additions & 5 deletions src/VirtualClient/VirtualClient.Actions/Prime95/Prime95Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace VirtualClient.Actions
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.Extensions.DependencyInjection;
using VirtualClient.Common;
using VirtualClient.Common.Extensions;
Expand Down Expand Up @@ -182,6 +183,11 @@ protected override async Task CleanupAsync(EventContext telemetryContext, Cancel
processProxy.SafeKill();
}
}

if (this.fileSystem.File.Exists(this.ResultsFilePath))
{
await this.fileSystem.File.DeleteAsync(this.ResultsFilePath, RetryPolicies.FileDelete);
}
}

/// <summary>
Expand Down Expand Up @@ -221,8 +227,8 @@ protected override async Task InitializeAsync(EventContext telemetryContext, Can
ErrorReason.DependencyNotFound);
}

this.SettingsFilePath = this.Combine(this.Prime95Package.Path, "prime.txt");
this.ResultsFilePath = this.Combine(this.Prime95Package.Path, "results.txt");
this.SettingsFilePath = this.Combine(this.GetTempPath(), "prime.txt");
this.ResultsFilePath = this.Combine(this.GetTempPath(), FileContext.GetFileName("results.txt", DateTime.UtcNow));
}

/// <summary>
Expand Down Expand Up @@ -298,10 +304,13 @@ await this.Logger.LogMessageAsync($"{this.TypeName}.ExecuteWorkload", telemetryC

try
{
if (this.fileSystem.File.Exists(this.ResultsFilePath))
await RetryPolicies.FileOperations.ExecuteAsync(async () =>
{
results = await this.fileSystem.File.ReadAllTextAsync(this.ResultsFilePath);
}
if (this.fileSystem.File.Exists(this.ResultsFilePath))
{
results = await this.fileSystem.File.ReadAllTextAsync(this.ResultsFilePath);
}
});

if (string.IsNullOrWhiteSpace(results))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ namespace VirtualClient.Actions
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ public class SysbenchExecutor : VirtualClientComponent
protected const string PythonCommand = "python3";

private readonly IStateManager stateManager;
private static readonly string[] SelectWorkloads =
{
"select_random_points",
"select_random_ranges"
};

/// <summary>
/// Constructor for <see cref="SysbenchExecutor"/>
Expand All @@ -65,7 +60,7 @@ public SysbenchExecutor(IServiceCollection dependencies, IDictionary<string, ICo
}

/// <summary>
/// The database name option passed to Sysbench.
/// The benchmark (e.g. OLTP, TPCC).
/// </summary>
public string Benchmark
{
Expand Down Expand Up @@ -111,7 +106,7 @@ public int? RecordCount
}

/// <summary>
/// The workload option passed to Sysbench.
/// The number of tables to create in the database.
/// </summary>
public int? TableCount
{
Expand All @@ -135,7 +130,7 @@ public int? Threads
}

/// <summary>
/// Number of records per table.
/// The database system (e.g. MySQL, PostgreSQL).
/// </summary>
public string DatabaseSystem
{
Expand All @@ -158,7 +153,7 @@ public string SuperUserPassword
}

/// <summary>
/// Number of records per table.
/// Number of warehouses.
/// </summary>
public int? WarehouseCount
{
Expand All @@ -170,7 +165,7 @@ public int? WarehouseCount
}

/// <summary>
/// The workload option passed to Sysbench.
/// The workload option passed to Sysbench (e.g. oltp_update_index, oltp_update_non_index).
/// </summary>
public string Workload
{
Expand Down Expand Up @@ -252,7 +247,6 @@ public static int GetRecordCount(ISystemManagement systemManagement, string data
recordCountExponent = Math.Max(3, recordCountExponent);

int recordEstimate = (int)Math.Pow(10, recordCountExponent);

int recordCount = records.GetValueOrDefault(recordEstimate);

// record count specified in profile if it is the configurable scenario
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,62 @@ public void GetPackagePathReturnsTheExpectedPathOnUnixSystems()
Assert.AreEqual("/home/anyuser/virtualclient/packages/any.package/1.0.0", platformSpecifics.GetPackagePath("any.package", "1.0.0"));
}

[Test]
public void GetLoggedInUserReturnsTheExpectedUserOnWindowsSystems()
{
PlatformSpecifics platformSpecifics = new TestPlatformSpecifics(PlatformID.Win32NT, Architecture.X64);
string user = platformSpecifics.GetLoggedInUser();
Assert.AreEqual(Environment.UserName, user);

platformSpecifics.SetEnvironmentVariable(EnvironmentVariable.SUDO_USER, "User01");
}

[Test]
public void GetLoggedInUserReturnsTheExpectedUserOnWindowsSystems_2()
{
// Environment variables do not matter on Windows and should not affect
// the return user.
PlatformSpecifics platformSpecifics = new TestPlatformSpecifics(PlatformID.Win32NT, Architecture.X64);

platformSpecifics.SetEnvironmentVariable(EnvironmentVariable.SUDO_USER, "User01");
string user = platformSpecifics.GetLoggedInUser();
Assert.AreEqual(Environment.UserName, user);

platformSpecifics.SetEnvironmentVariable(EnvironmentVariable.VC_SUDO_USER, "User02");
user = platformSpecifics.GetLoggedInUser();
Assert.AreEqual(Environment.UserName, user);
}

[Test]
public void GetLoggedInUserReturnsTheExpectedUserOnUnixSystems()
{
PlatformSpecifics platformSpecifics = new TestPlatformSpecifics(PlatformID.Unix, Architecture.X64);
string user = platformSpecifics.GetLoggedInUser();
Assert.AreEqual(Environment.UserName, user);
}

[Test]
public void GetLoggedInUserReturnsTheExpectedUserOnUnixSystemsWhenSudoIsUsed()
{
PlatformSpecifics platformSpecifics = new TestPlatformSpecifics(PlatformID.Unix, Architecture.X64);

string sudoUser = "User01";
platformSpecifics.SetEnvironmentVariable(EnvironmentVariable.SUDO_USER, sudoUser);
string user = platformSpecifics.GetLoggedInUser();
Assert.AreEqual(sudoUser, user);
}

[Test]
public void GetLoggedInUserReturnsTheExpectedUserOnUnixSystemsWhenCustomSudoAlternativesAreUsed()
{
PlatformSpecifics platformSpecifics = new TestPlatformSpecifics(PlatformID.Unix, Architecture.X64);

string sudoUser = "User01";
platformSpecifics.SetEnvironmentVariable(EnvironmentVariable.VC_SUDO_USER, sudoUser);
string user = platformSpecifics.GetLoggedInUser();
Assert.AreEqual(sudoUser, user);
}

[Test]
public void GetPackagePathReturnsTheExpectedPathOnWindowsSystems()
{
Expand Down
15 changes: 15 additions & 0 deletions src/VirtualClient/VirtualClient.Contracts/FileContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace VirtualClient.Contracts
/// </summary>
public class FileContext
{
private const string FileTimestampFormat = "yyyy-MM-ddTHH-mm-ss-fffffK";
private static readonly Regex PathReservedCharacterExpression = new Regex(@"[""<>:|?*\\/]+", RegexOptions.Compiled);
private static readonly Regex TemplatePlaceholderExpression = new Regex(@"\{(.*?)\}", RegexOptions.Compiled | RegexOptions.IgnoreCase);

/// <summary>
Expand Down Expand Up @@ -94,6 +96,19 @@ public FileContext(IFileInfo file, string contentType, string contentEncoding, s
/// </summary>
public string ToolName { get; }

/// <summary>
/// Returns a file name containing a timestamp as part of the name having removed any
/// characters not allowed in file paths (e.g. 2023-02-01T12-23-30241Z-randomwrite_4k_blocksize.log).
/// </summary>
/// <param name="fileName">The name of the file (e.g. randomwrite_4k_blocksize.log)</param>
/// <param name="timestamp">The timestamp to add to the file name.</param>
public static string GetFileName(string fileName, DateTime timestamp)
{
return PathReservedCharacterExpression.Replace(
$"{timestamp.ToString(FileTimestampFormat)}-{fileName.RemoveWhitespace()}",
string.Empty);
}

/// <summary>
/// Resolves placeholders in the path template provided.
/// </summary>
Expand Down
16 changes: 0 additions & 16 deletions src/VirtualClient/VirtualClient.Contracts/FileUploadDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public class FileUploadDescriptor
/// The default extension for the file uploads.
/// </summary>
public const string UploadDescriptorFileExtension = "upload.json";

private const string FileTimestampFormat = "yyyy-MM-ddTHH-mm-ss-fffffK";
private static readonly Regex PathReservedCharacterExpression = new Regex(@"[""<>:|?*\\/]+", RegexOptions.Compiled);
private static readonly char[] PathDelimiters = new char[] { '/', '\\' };

/// <summary>
Expand Down Expand Up @@ -173,19 +170,6 @@ public static IDictionary<string, IConvertible> CreateManifest(FileContext fileC
return fileManifest.ObscureSecrets();
}

/// <summary>
/// Returns a file name containing a timestamp as part of the name having removed any
/// characters not allowed in file paths (e.g. 2023-02-01T12-23-30241Z-randomwrite_4k_blocksize.log).
/// </summary>
/// <param name="fileName">The name of the file (e.g. randomwrite_4k_blocksize.log)</param>
/// <param name="timestamp">The timestamp to add to the file name.</param>
public static string GetFileName(string fileName, DateTime timestamp)
{
return PathReservedCharacterExpression.Replace(
$"{timestamp.ToString(FileTimestampFormat)}-{fileName.RemoveWhitespace()}",
string.Empty);
}

/// <summary>
/// Returns a <see cref="BlobDescriptor"/> for the current instance.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static FileUploadDescriptor CreateDescriptor(FileContext fileContext, IDi

if (timestamped)
{
blobName = FileUploadDescriptor.GetFileName(blobName, fileContext.File.CreationTimeUtc);
blobName = FileContext.GetFileName(blobName, fileContext.File.CreationTimeUtc);
}

// The caller of this factory method makes the determination on the runtime parameters that are
Expand Down
21 changes: 11 additions & 10 deletions src/VirtualClient/VirtualClient.Contracts/PlatformSpecifics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,18 +453,19 @@ public virtual string GetEnvironmentVariable(string variableName, EnvironmentVar
public string GetLoggedInUser()
{
string loggedInUserName = Environment.UserName;
if (string.Equals(loggedInUserName, "root"))
if (this.Platform == PlatformID.Unix)
{
loggedInUserName = this.GetEnvironmentVariable(EnvironmentVariable.SUDO_USER);
if (string.IsNullOrWhiteSpace(loggedInUserName))
// Note that when the user is "root" and running a command with "sudo", there will be
// no "SUDO_USER" environment variable.
string sudoUser = this.GetEnvironmentVariable(EnvironmentVariable.SUDO_USER);
if (string.IsNullOrWhiteSpace(sudoUser))
{
loggedInUserName = this.GetEnvironmentVariable(EnvironmentVariable.VC_SUDO_USER);
if (string.IsNullOrEmpty(loggedInUserName))
{
// When the user is "root" and running a command with "sudo", there will be
// no "SUDO_USER" environment variable.
return "root";
}
sudoUser = this.GetEnvironmentVariable(EnvironmentVariable.VC_SUDO_USER);
}

if (!string.IsNullOrEmpty(sudoUser))
{
return sudoUser;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public static async Task RequestFileUploadAsync(this VirtualClientComponent comp
fileSystem.Directory.CreateDirectory(targetDirectory);
}

string fileName = FileUploadDescriptor.GetFileName(FileUploadDescriptor.UploadDescriptorFileExtension, DateTime.UtcNow);
string fileName = FileContext.GetFileName(FileUploadDescriptor.UploadDescriptorFileExtension, DateTime.UtcNow);
string filePath = component.Combine(targetDirectory, fileName.ToLowerInvariant());

await fileSystem.File.WriteAllTextAsync(filePath, descriptor.ToJson());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ConsoleLogger(string categoryName, LogLevel minimumLogLevel = LogLevel.In
/// <summary>
/// The default console logger.
/// </summary>
public static ConsoleLogger Default { get; set; } = new ConsoleLogger("VirtualClient", LogLevel.Debug);
public static ConsoleLogger Default { get; set; } = new ConsoleLogger("VirtualClient", LogLevel.Trace);

/// <summary>
/// True to include log severity levels in output.
Expand Down
Loading
Loading