Skip to content

Commit 63f6f54

Browse files
authored
Adding openssl version to metadata dependencies (#510)
* adding openssl version to metadata dependencies * trim version value rxd from stdout * adding version to logMetrics * fix Logging and add toolversion in metadata scenario * set env variable before invoking openssl version command * make getversion as async and add unit test * remove printing to console from unittest * adding AddForScenario as it was * fix failing unit test due to StandardOutput overwrite
1 parent 591b1d2 commit 63f6f54

2 files changed

Lines changed: 72 additions & 7 deletions

File tree

src/VirtualClient/VirtualClient.Actions.UnitTests/OpenSSL/OpenSslExecutorTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class OpenSslExecutorTests
2323
{
2424
private DependencyFixture fixture;
2525
private DependencyPath mockPackage;
26+
private string mockOpensslVersion = "OpenSSL 3.5.0 8 Apr 2025 (Library: OpenSSL 3.5.0 8 Apr 2025)\n";
2627

2728
[Test]
2829
[TestCase(PlatformID.Unix, Architecture.X64, "linux-x64/bin/openssl")]
@@ -287,6 +288,24 @@ await executor.ExecuteAsync(CancellationToken.None)
287288
}
288289
}
289290

291+
[Test]
292+
public async Task OpenSslExecutorExecutesGetOpensslVersion()
293+
{
294+
this.SetupDefaultBehaviors();
295+
this.SetupOpensslVersionBehavior();
296+
297+
using (TestOpenSslExecutor executor = new TestOpenSslExecutor(this.fixture))
298+
{
299+
300+
await executor.ExecuteAsync(CancellationToken.None)
301+
.ConfigureAwait(false);
302+
303+
var messages = this.fixture.Logger.MessagesLogged($"{nameof(OpenSslExecutor)}.GetOpenSslVersion");
304+
Assert.IsNotEmpty(messages);
305+
Assert.IsTrue(messages.All(msg => (msg.Item3 as EventContext).Properties["opensslVersion"].ToString() == "OpenSSL 3.5.0 8 Apr 2025 (Library: OpenSSL 3.5.0 8 Apr 2025)"));
306+
}
307+
}
308+
290309
private void SetupDefaultBehaviors(PlatformID platform = PlatformID.Unix, Architecture architecture = Architecture.X64)
291310
{
292311
// Setup the default behaviors given all expected dependencies are in place such that the
@@ -329,8 +348,20 @@ private void SetupDefaultBehaviors(PlatformID platform = PlatformID.Unix, Archit
329348
process.StandardOutput.Append(TestResources.Results_OpenSSL_speed);
330349
}
331350
};
351+
332352
}
333353

354+
private void SetupOpensslVersionBehavior()
355+
{
356+
this.fixture.ProcessManager.OnProcessCreated = (process) =>
357+
{
358+
// mock openssl version command result
359+
if (process.IsMatch("openssl(.exe)* version"))
360+
{
361+
process.StandardOutput.Append(this.mockOpensslVersion);
362+
}
363+
};
364+
}
334365
private class TestOpenSslExecutor : OpenSslExecutor
335366
{
336367
public TestOpenSslExecutor(DependencyFixture mockFixture)

src/VirtualClient/VirtualClient.Actions/OpenSSL/OpenSslExecutor.cs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace VirtualClient.Actions
1818
using VirtualClient.Common.Telemetry;
1919
using VirtualClient.Contracts;
2020
using VirtualClient.Contracts.Metadata;
21+
using YamlDotNet.Serialization;
2122

2223
/// <summary>
2324
/// Executes the OpenSSL workload.
@@ -90,17 +91,50 @@ await this.InitializeWorkloadToolsetsAsync(cancellationToken)
9091
.ConfigureAwait(false);
9192
}
9293

93-
private void CaptureMetrics(IProcessProxy workloadProcess, string commandArguments, EventContext telemetryContext)
94+
/// <summary>
95+
/// Gets openssl version by running openssl version command.
96+
/// </summary>
97+
private async Task GetOpenSslVersion(CancellationToken cancellationToken, string toolCommand)
9498
{
95-
if (workloadProcess.ExitCode == 0)
99+
// The OpenSSL version is not available in the workload output. We need to run a separate command to get the version.
100+
// The command 'openssl version' will return the version of OpenSSL installed on the system.
101+
string opensslVersion = "Unknown";
102+
if (!cancellationToken.IsCancellationRequested)
96103
{
97-
try
104+
this.Logger.LogTraceMessage($"Executing process 'openssl version' at directory '{this.ExecutablePath}'.");
105+
using (IProcessProxy process = this.systemManagement.ProcessManager.CreateProcess(this.ExecutablePath, "version"))
98106
{
107+
this.SetEnvironmentVariables(process);
108+
await process.StartAndWaitAsync(cancellationToken).ConfigureAwait(false);
109+
process.ThrowIfWorkloadFailed();
110+
111+
opensslVersion = process.StandardOutput?.ToString().Trim() ?? "Unknown";
112+
if (string.IsNullOrWhiteSpace(opensslVersion))
113+
{
114+
opensslVersion = "Unknown";
115+
}
116+
117+
this.MetadataContract.Add("OpenSSLVersion", opensslVersion, MetadataContractCategory.Dependencies);
118+
this.Logger.LogMessage($"{nameof(OpenSslExecutor)}.GetOpenSslVersion", LogLevel.Information, EventContext.Persisted().AddContext("opensslVersion", opensslVersion));
119+
99120
this.MetadataContract.AddForScenario(
100121
"OpenSSL Speed",
101-
workloadProcess.FullCommand(),
102-
toolVersion: null);
122+
toolCommand,
123+
toolVersion: opensslVersion);
124+
}
125+
}
126+
127+
}
103128

129+
private void CaptureMetrics(IProcessProxy workloadProcess, string commandArguments, EventContext telemetryContext, CancellationToken cancellationToken)
130+
{
131+
if (workloadProcess.ExitCode == 0)
132+
{
133+
try
134+
{
135+
// Retrieve OpenSSL version
136+
this.GetOpenSslVersion(cancellationToken, workloadProcess.FullCommand());
137+
104138
this.MetadataContract.Apply(telemetryContext);
105139

106140
OpenSslMetricsParser resultsParser = new OpenSslMetricsParser(workloadProcess.StandardOutput.ToString(), commandArguments);
@@ -133,7 +167,7 @@ private Task ExecuteWorkloadAsync(EventContext telemetryContext, CancellationTok
133167

134168
EventContext relatedContext = telemetryContext.Clone()
135169
.AddContext("executable", this.ExecutablePath)
136-
.AddContext("commandArguments", commandArguments);
170+
.AddContext("commandArguments", commandArguments);
137171

138172
return this.Logger.LogMessageAsync($"{nameof(OpenSslExecutor)}.ExecuteWorkload", relatedContext, async () =>
139173
{
@@ -153,7 +187,7 @@ private Task ExecuteWorkloadAsync(EventContext telemetryContext, CancellationTok
153187
await this.LogProcessDetailsAsync(process, telemetryContext, "OpenSSL", logToFile: true);
154188

155189
process.ThrowIfWorkloadFailed();
156-
this.CaptureMetrics(process, commandArguments, telemetryContext);
190+
this.CaptureMetrics(process, commandArguments, telemetryContext, cancellationToken);
157191
}
158192
}
159193
finally

0 commit comments

Comments
 (0)