Skip to content

Commit ff2624a

Browse files
committed
Enable profile components to define additional logging instructions (e.g. timestamping).
1 parent 6cb2ddb commit ff2624a

File tree

10 files changed

+88
-13
lines changed

10 files changed

+88
-13
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.34
1+
2.1.35

src/VirtualClient/VirtualClient.Contracts.UnitTests/ComponentFactoryTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,51 @@ public void ComponentFactoryCreatesExpectedComponentsFromAnExecutionProfile(stri
6969
}
7070
}
7171

72+
[Test]
73+
[TestCase("TEST-PROFILE-1.json")]
74+
public void ComponentFactorySetsComponentPropertiesToExpectedDefaults(string profileName)
75+
{
76+
ExecutionProfile profile = File.ReadAllText(Path.Combine(MockFixture.TestAssemblyDirectory, "Resources", profileName))
77+
.FromJson<ExecutionProfile>();
78+
79+
foreach (ExecutionProfileElement action in profile.Actions)
80+
{
81+
VirtualClientComponent component = ComponentFactory.CreateComponent(action, this.mockFixture.Dependencies);
82+
Assert.IsNotNull(component);
83+
Assert.IsNotNull(component.Parameters);
84+
Assert.IsFalse(component.LogToFile);
85+
Assert.IsTrue(component.LogTimestamped);
86+
Assert.IsNull(component.ContentPathTemplate);
87+
Assert.IsNull(component.LogFileName);
88+
Assert.IsNull(component.LogFolderName);
89+
}
90+
}
91+
92+
[Test]
93+
[TestCase("TEST-PROFILE-5.json")]
94+
public void ComponentFactoryDoesNotInadvertentlyOverwriteComponentLevelProperties(string profileName)
95+
{
96+
ExecutionProfile profile = File.ReadAllText(Path.Combine(MockFixture.TestAssemblyDirectory, "Resources", profileName))
97+
.FromJson<ExecutionProfile>();
98+
99+
ComponentSettings settings = new ComponentSettings
100+
{
101+
ContentPathTemplate = "content/path/template/A",
102+
LogToFile = false
103+
};
104+
105+
foreach (ExecutionProfileElement action in profile.Actions)
106+
{
107+
VirtualClientComponent component = ComponentFactory.CreateComponent(action, this.mockFixture.Dependencies, settings);
108+
Assert.IsNotNull(component);
109+
Assert.IsNotNull(component.Parameters);
110+
Assert.IsTrue(component.LogToFile);
111+
Assert.IsFalse(component.LogTimestamped);
112+
Assert.AreEqual("test.log", component.LogFileName);
113+
Assert.AreEqual("test", component.LogFolderName);
114+
}
115+
}
116+
72117
[Test]
73118
[TestCase("TEST-PROFILE-3-PARALLEL.json")]
74119
public void ComponentFactoryCreatesExpectedParallelExecutionComponentsFromAnExecutionProfile(string profileName)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"Description": "Test Profile Executor (Metadata + Extensions)",
3+
"Actions": [
4+
{
5+
"Type": "TestExecutor",
6+
"Parameters": {
7+
"CommandLine": "--name=testName --engine=V8",
8+
"LogToFile": true,
9+
"LogFileName": "test.log",
10+
"LogFolderName": "test",
11+
"LogTimestamped": false,
12+
"Tags": "Test,VC"
13+
}
14+
}
15+
]
16+
}

src/VirtualClient/VirtualClient.Contracts.UnitTests/VirtualClientLoggingExtensionsTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public void SetupTest()
3838
this.mockLogger = new Mock<ILogger>();
3939
this.mockEventContext = new EventContext(Guid.NewGuid());
4040

41+
this.mockFixture.Parameters[nameof(TestExecutor.LogToFile)] = true;
42+
4143
// When there is a content manager, the application will also write a file
4244
// upload notification file (e.g. upload.json). We validate this separately.
4345
this.mockFixture.Dependencies.RemoveAll<IEnumerable<IBlobManager>>();

src/VirtualClient/VirtualClient.Contracts/ComponentFactory.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,28 +105,24 @@ private static VirtualClientComponent CreateComponent(
105105
IEnumerable<string> includeScenarios = null,
106106
IEnumerable<string> excludeScenarios = null)
107107
{
108-
if (!dependencies.TryGetService<ComponentSettings>(out ComponentSettings settings))
109-
{
110-
settings = new ComponentSettings();
111-
}
112-
113108
IDictionary<string, IConvertible> effectiveParameters = new SortedDictionary<string, IConvertible>(StringComparer.OrdinalIgnoreCase);
114109
if (componentDescription.Parameters?.Any() == true)
115110
{
116111
effectiveParameters.AddRange(componentDescription.Parameters);
117112
}
118113

119-
if (componentSettings.FailFast != null)
114+
// We do not override profile component-level/defined settings.
115+
if (componentSettings.FailFast != null && !effectiveParameters.ContainsKey(nameof(VirtualClientComponent.FailFast)))
120116
{
121117
effectiveParameters[nameof(VirtualClientComponent.FailFast)] = componentSettings.FailFast.Value;
122118
}
123119

124-
if (componentSettings.LogToFile != null)
120+
if (componentSettings.LogToFile != null && !effectiveParameters.ContainsKey(nameof(VirtualClientComponent.LogToFile)))
125121
{
126122
effectiveParameters[nameof(VirtualClientComponent.LogToFile)] = componentSettings.LogToFile.Value;
127123
}
128124

129-
if (componentSettings.Seed != null)
125+
if (componentSettings.Seed != null && !effectiveParameters.ContainsKey(nameof(VirtualClientComponent.Seed)))
130126
{
131127
effectiveParameters[nameof(VirtualClientComponent.Seed)] = componentSettings.Seed.Value;
132128
}

src/VirtualClient/VirtualClient.Contracts/VirtualClientComponent.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,23 @@ protected set
295295
}
296296
}
297297

298+
/// <summary>
299+
/// True/false whether log files written will have timestamps appended to the
300+
/// file names (e.g. 2023-02-01-100530635478-geekbench.log). Default = true.
301+
/// </summary>
302+
public bool LogTimestamped
303+
{
304+
get
305+
{
306+
return this.Parameters.GetValue<bool>(nameof(this.LogTimestamped), true);
307+
}
308+
309+
protected set
310+
{
311+
this.Parameters[nameof(this.LogTimestamped)] = value;
312+
}
313+
}
314+
298315
/// <summary>
299316
/// Metadata provided to the application on the command line.
300317
/// </summary>

src/VirtualClient/VirtualClient.Core.UnitTests/VirtualClientLoggingExtensionsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class VirtualClientLoggingExtensionsTests : MockFixture
2525
public void Setup()
2626
{
2727
this.Setup(PlatformID.Unix);
28+
this.Parameters[nameof(TestExecutor.LogToFile)] = true;
2829
}
2930

3031
[Test]

src/VirtualClient/VirtualClient.Dependencies/ExecuteCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ protected override async Task ExecuteAsync(EventContext telemetryContext, Cancel
146146

147147
if (!cancellationToken.IsCancellationRequested)
148148
{
149-
await this.LogProcessDetailsAsync(process, telemetryContext, toolName: this.LogFolderName, logFileName: this.LogFileName);
149+
await this.LogProcessDetailsAsync(process, telemetryContext, toolName: this.LogFolderName, logFileName: this.LogFileName, timestamped: this.LogTimestamped);
150150
process.ThrowIfComponentOperationFailed(this.ComponentType);
151151
}
152152
}

src/VirtualClient/VirtualClient.Monitors/ExecuteCommandMonitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ protected async Task ExecuteCommandAsync(EventContext telemetryContext, Cancella
252252

253253
if (!cancellationToken.IsCancellationRequested)
254254
{
255-
await this.LogProcessDetailsAsync(process, telemetryContext, toolName: this.LogFolderName, logFileName: this.LogFileName);
255+
await this.LogProcessDetailsAsync(process, telemetryContext, toolName: this.LogFolderName, logFileName: this.LogFileName, timestamped: this.LogTimestamped);
256256
process.ThrowIfMonitorFailed();
257257
this.CaptureEventInformation(process, telemetryContext);
258258
}

src/VirtualClient/VirtualClient.TestFramework/TestExecutor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class TestExecutor : VirtualClientComponent
2222
public TestExecutor(MockFixture fixture)
2323
: base(fixture?.Dependencies, fixture?.Parameters)
2424
{
25-
this.LogToFile = true;
2625
}
2726

2827
/// <summary>
@@ -31,7 +30,6 @@ public TestExecutor(MockFixture fixture)
3130
public TestExecutor(IServiceCollection dependencies, IDictionary<string, IConvertible> parameters)
3231
: base(dependencies, parameters)
3332
{
34-
this.LogToFile = true;
3533
}
3634

3735
/// <summary>

0 commit comments

Comments
 (0)