Skip to content

Commit 880320f

Browse files
Log Process Details File Upload Fixing FolderName and bumping version (#635)
* Log Process Details File Upload Fixing FolderName * Fixing Log foldername instead of directory * Adding FileName only * Adding unit tests * Adding GetLogDirectoryName * bumping version --------- Co-authored-by: Deepanshu Vaid <devaid@microsoft.com>
1 parent fb94a14 commit 880320f

3 files changed

Lines changed: 58 additions & 6 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.54
1+
2.1.55

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,46 @@ public void SetupTest()
4848
this.mockFixture.Dependencies.RemoveAll<IEnumerable<IBlobManager>>();
4949
}
5050

51+
52+
[Test]
53+
[TestCase("<Any Toolset?>", "ComponentlogfolderName", "anytoolset")]
54+
[TestCase(null, "ComponentlogfolderName", "componentlogfoldername")]
55+
[TestCase(null, null, "testexecutor")]
56+
public void GetLogDirectoryNameReturnsSafeNameAndPrefersToolNameOverComponentValues(string toolName, string logFolderName, string expected)
57+
{
58+
TestExecutor component = new TestExecutor(this.mockFixture);
59+
60+
component.Parameters[nameof(component.LogFolderName)] = logFolderName;
61+
string actual = component.GetLogDirectoryName(toolName);
62+
63+
Assert.AreEqual(expected, actual, "ToolName should be preferred and sanitized into a safe folder name.");
64+
}
65+
66+
[Test]
67+
[TestCase("<Any Toolset?>", PlatformID.Unix, "/home/user/tools/VirtualClient/logs/anytoolset")]
68+
[TestCase(null, PlatformID.Unix, "/home/user/tools/VirtualClient/logs/testexecutor")]
69+
[TestCase("<Any Toolset?>", PlatformID.Win32NT, "C:\\users\\any\\tools\\VirtualClient\\logs\\anytoolset")]
70+
[TestCase(null, PlatformID.Win32NT, "C:\\users\\any\\tools\\VirtualClient\\logs\\testexecutor")]
71+
public void GetLogDirectoryReturnsSafeNameAndPrefersToolNameOverComponentValues(string toolName, PlatformID platformID, string expected)
72+
{
73+
this.mockFixture = new MockFixture();
74+
this.mockFixture.Setup(platformID);
75+
this.mockLogger = new Mock<ILogger>();
76+
this.mockEventContext = new EventContext(Guid.NewGuid());
77+
78+
this.mockFixture.Parameters[nameof(TestExecutor.LogToFile)] = true;
79+
80+
// When there is a content manager, the application will also write a file
81+
// upload notification file (e.g. upload.json). We validate this separately.
82+
this.mockFixture.Dependencies.RemoveAll<IEnumerable<IBlobManager>>();
83+
TestExecutor component = new TestExecutor(this.mockFixture);
84+
85+
string actual = component.GetLogDirectory(toolName);
86+
87+
Assert.AreEqual(expected, actual, "ToolName should be preferred and sanitized into a safe folder name.");
88+
}
89+
90+
5191
[Test]
5292
[TestCase(0, null, null, null, null, null)]
5393
[TestCase(0, "", "", "", "", "")]

src/VirtualClient/VirtualClient.Core/VirtualClientLoggingExtensions.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public static class VirtualClientLoggingExtensions
2828
private static readonly Semaphore FileAccessLock = new Semaphore(1, 1);
2929

3030
/// <summary>
31-
/// Returns the target log directory for the component and tool name.
31+
/// Returns the target log directory name for the component.
3232
/// </summary>
3333
/// <param name="component">The component requesting the logging.</param>
3434
/// <param name="toolName">The name of the toolset running in the process.</param>
35-
public static string GetLogDirectory(this VirtualClientComponent component, string toolName = null)
35+
public static string GetLogDirectoryName(this VirtualClientComponent component, string toolName = null)
3636
{
3737
string[] possibleLogFolderNames = new string[]
3838
{
@@ -41,8 +41,20 @@ public static string GetLogDirectory(this VirtualClientComponent component, stri
4141
component.TypeName
4242
};
4343

44-
string logFolderName = VirtualClientLoggingExtensions.GetSafeFileName(possibleLogFolderNames.First(name => !string.IsNullOrWhiteSpace(name)), false);
45-
string logDirectory = component.PlatformSpecifics.GetLogsPath(logFolderName.ToLowerInvariant().RemoveWhitespace());
44+
string logDirectoryName = VirtualClientLoggingExtensions.GetSafeFileName(possibleLogFolderNames.First(name => !string.IsNullOrWhiteSpace(name)), false);
45+
46+
return logDirectoryName;
47+
}
48+
49+
/// <summary>
50+
/// Returns the target log directory for the component.
51+
/// </summary>
52+
/// <param name="component">The component requesting the logging.</param>
53+
/// <param name="toolName">The name of the toolset running in the process.</param>
54+
public static string GetLogDirectory(this VirtualClientComponent component, string toolName = null)
55+
{
56+
string logDirectoryName = component.GetLogDirectoryName(toolName);
57+
string logDirectory = component.PlatformSpecifics.GetLogsPath(logDirectoryName.ToLowerInvariant().RemoveWhitespace());
4658

4759
return logDirectory;
4860
}
@@ -397,7 +409,7 @@ await RetryPolicies.FileOperations.ExecuteAsync(async () =>
397409

398410
if (upload && component.TryGetContentStoreManager(out IBlobManager blobManager))
399411
{
400-
string effectiveToolName = fileSystem.Path.GetDirectoryName(logDirectory);
412+
string effectiveToolName = component.GetLogDirectoryName(processDetails.ToolName);
401413

402414
FileContext fileContext = new FileContext(
403415
fileSystem.FileInfo.New(logFilePath),

0 commit comments

Comments
 (0)