-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Simple artifacts collecting * Docs * Update Configuration.md * Update Configuration.md
- Loading branch information
1 parent
8819de4
commit a42f9f3
Showing
4 changed files
with
233 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...portPortal.Shared/Extensibility/Embedded/LaunchArtifacts/LaunchArtifactsEventsObserver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using ReportPortal.Client.Abstractions.Requests; | ||
using ReportPortal.Shared.Extensibility.ReportEvents; | ||
using ReportPortal.Shared.Internal.Logging; | ||
using ReportPortal.Shared.MimeTypes; | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace ReportPortal.Shared.Extensibility.Embedded.LaunchArtifacts | ||
{ | ||
public class LaunchArtifactsEventsObserver : IReportEventsObserver | ||
{ | ||
private static readonly ITraceLogger _logger = TraceLogManager.Instance.GetLogger(typeof(LaunchArtifactsEventsObserver)); | ||
|
||
public string BaseDirectory { get; set; } = Environment.CurrentDirectory; | ||
|
||
public void Initialize(IReportEventsSource reportEventsSource) | ||
{ | ||
reportEventsSource.OnBeforeLaunchFinishing += ReportEventsSource_OnBeforeLaunchFinishing; | ||
} | ||
|
||
private void ReportEventsSource_OnBeforeLaunchFinishing(Reporter.ILaunchReporter launchReporter, ReportEvents.EventArgs.BeforeLaunchFinishingEventArgs args) | ||
{ | ||
var artifactPaths = args.Configuration.GetValues<string>("Launch:Artifacts", null); | ||
|
||
if (artifactPaths != null) | ||
{ | ||
foreach (var filePattern in artifactPaths) | ||
{ | ||
var artifacts = Directory.GetFiles(BaseDirectory, filePattern); | ||
|
||
foreach (var artifact in artifacts) | ||
{ | ||
var createLogItemRequest = new CreateLogItemRequest | ||
{ | ||
LaunchUuid = launchReporter.Info.Uuid, | ||
Time = DateTime.UtcNow, | ||
Level = Client.Abstractions.Models.LogLevel.Trace, | ||
Text = Path.GetFileName(artifact), | ||
}; | ||
|
||
AttachFile(artifact, ref createLogItemRequest); | ||
|
||
Task.Run(async () => await args.ClientService.LogItem.CreateAsync(createLogItemRequest)).GetAwaiter().GetResult(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static void AttachFile(string filePath, ref CreateLogItemRequest request) | ||
{ | ||
try | ||
{ | ||
using (var fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | ||
{ | ||
using (var memoryStream = new MemoryStream()) | ||
{ | ||
fileStream.CopyTo(memoryStream); | ||
var bytes = memoryStream.ToArray(); | ||
|
||
request.Attach = new LogItemAttach | ||
{ | ||
Name = Path.GetFileName(filePath), | ||
MimeType = MimeTypeMap.GetMimeType(Path.GetExtension(filePath)), | ||
Data = bytes | ||
}; | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
request.Text = $"{request.Text}\n> Couldn't read content of `{filePath}` file. \n{ex}"; | ||
} | ||
} | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
test/ReportPortal.Shared.Tests/Extensibility/Embedded/LaunchArtifacts/LaunchArtifactsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
using FluentAssertions; | ||
using Moq; | ||
using ReportPortal.Client.Abstractions.Requests; | ||
using ReportPortal.Shared.Configuration; | ||
using ReportPortal.Shared.Extensibility; | ||
using ReportPortal.Shared.Extensibility.Embedded.LaunchArtifacts; | ||
using ReportPortal.Shared.Tests.Helpers; | ||
using System.IO; | ||
using System.Threading; | ||
using Xunit; | ||
|
||
namespace ReportPortal.Shared.Tests.Extensibility.Embedded.LaunchArtifacts | ||
{ | ||
public class LaunchArtifactsTest | ||
{ | ||
private readonly IExtensionManager _extensionManager; | ||
|
||
public LaunchArtifactsTest() | ||
{ | ||
_extensionManager = new Shared.Extensibility.ExtensionManager(); | ||
_extensionManager.ReportEventObservers.Add(new LaunchArtifactsEventsObserver()); | ||
} | ||
|
||
[Fact] | ||
public void ShouldNotAttachArtifacts() | ||
{ | ||
var client = new MockServiceBuilder().Build(); | ||
|
||
var launchReporter = new LaunchReporterBuilder(client.Object).With(_extensionManager).Build(1, 0, 0); | ||
launchReporter.Sync(); | ||
|
||
client.Verify(s => s.LogItem.CreateAsync(It.IsAny<CreateLogItemRequest[]>(), default), Times.Never()); | ||
} | ||
|
||
[Fact] | ||
public void ShouldAttachSingleArtifactByPath() | ||
{ | ||
File.WriteAllBytes("test_file_1.txt", new byte[] { 1, 2, 3 }); | ||
|
||
CreateLogItemRequest request = null; | ||
|
||
var client = new MockServiceBuilder().Build(); | ||
client.Setup(s => s.LogItem.CreateAsync(It.IsAny<CreateLogItemRequest>(), default)) | ||
.Callback<CreateLogItemRequest, CancellationToken>((a, b) => request = a); | ||
|
||
var config = new ConfigurationBuilder().Build(); | ||
config.Properties["launch:artifacts"] = "test_file_1.txt"; | ||
|
||
var launchReporter = new LaunchReporterBuilder(client.Object).With(_extensionManager).WithConfiguration(config).Build(1, 0, 0); | ||
launchReporter.Sync(); | ||
|
||
client.Verify(s => s.LogItem.CreateAsync(It.IsAny<CreateLogItemRequest>(), default), Times.Once); | ||
|
||
request.Should().NotBeNull(); | ||
request.Text.Should().Be("test_file_1.txt"); | ||
|
||
request.Attach.Should().NotBeNull(); | ||
request.Attach.Name.Should().Be("test_file_1.txt"); | ||
request.Attach.MimeType.Should().Be("text/plain"); | ||
request.Attach.Data.Should().BeEquivalentTo(new byte[] { 1, 2, 3 }); | ||
} | ||
|
||
[Fact] | ||
public void ShouldAttachSeveralArtifactsByPath() | ||
{ | ||
File.Create("test_file_1.txt").Close(); | ||
File.Create("test_file_2.txt").Close(); | ||
|
||
var client = new MockServiceBuilder().Build(); | ||
|
||
var config = new ConfigurationBuilder().Build(); | ||
config.Properties["launch:artifacts"] = "test_file_1.txt;test_file_2.txt"; | ||
|
||
var launchReporter = new LaunchReporterBuilder(client.Object).With(_extensionManager).WithConfiguration(config).Build(1, 0, 0); | ||
launchReporter.Sync(); | ||
|
||
client.Verify(s => s.LogItem.CreateAsync(It.IsAny<CreateLogItemRequest>(), default), Times.Exactly(2)); | ||
} | ||
|
||
[Fact] | ||
public void ShouldAttachSingleArtifactByPattern() | ||
{ | ||
File.WriteAllBytes("test_file_1.txt", new byte[] { 1, 2, 3 }); | ||
|
||
CreateLogItemRequest request = null; | ||
|
||
var client = new MockServiceBuilder().Build(); | ||
client.Setup(s => s.LogItem.CreateAsync(It.IsAny<CreateLogItemRequest>(), default)) | ||
.Callback<CreateLogItemRequest, CancellationToken>((a, b) => request = a); | ||
|
||
var config = new ConfigurationBuilder().Build(); | ||
config.Properties["launch:artifacts"] = "test_*_1.txt"; | ||
|
||
var launchReporter = new LaunchReporterBuilder(client.Object).With(_extensionManager).WithConfiguration(config).Build(1, 0, 0); | ||
launchReporter.Sync(); | ||
|
||
client.Verify(s => s.LogItem.CreateAsync(It.IsAny<CreateLogItemRequest>(), default), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public void ShouldAttachSingleArtifactByDir() | ||
{ | ||
Directory.CreateDirectory("a/b/c"); | ||
File.WriteAllBytes("a/b/c/abc.txt", new byte[] { 1, 2, 3 }); | ||
|
||
CreateLogItemRequest request = null; | ||
|
||
var client = new MockServiceBuilder().Build(); | ||
client.Setup(s => s.LogItem.CreateAsync(It.IsAny<CreateLogItemRequest>(), default)) | ||
.Callback<CreateLogItemRequest, CancellationToken>((a, b) => request = a); | ||
|
||
var config = new ConfigurationBuilder().Build(); | ||
config.Properties["launch:artifacts"] = "a/b/c/*.txt"; | ||
|
||
var launchReporter = new LaunchReporterBuilder(client.Object).With(_extensionManager).WithConfiguration(config).Build(1, 0, 0); | ||
launchReporter.Sync(); | ||
|
||
client.Verify(s => s.LogItem.CreateAsync(It.IsAny<CreateLogItemRequest>(), default), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public void ShouldAttachMessageWithException() | ||
{ | ||
File.Create("test_file_open.txt"); // leaves it open | ||
|
||
CreateLogItemRequest request = null; | ||
|
||
var client = new MockServiceBuilder().Build(); | ||
client.Setup(s => s.LogItem.CreateAsync(It.IsAny<CreateLogItemRequest>(), default)) | ||
.Callback<CreateLogItemRequest, CancellationToken>((a, b) => request = a); | ||
|
||
var config = new ConfigurationBuilder().Build(); | ||
config.Properties["launch:artifacts"] = "test_file_open.txt"; | ||
|
||
var launchReporter = new LaunchReporterBuilder(client.Object).With(_extensionManager).WithConfiguration(config).Build(1, 0, 0); | ||
launchReporter.Sync(); | ||
|
||
client.Verify(s => s.LogItem.CreateAsync(It.IsAny<CreateLogItemRequest>(), default), Times.Once); | ||
|
||
request.Text.Should().Contain("Couldn't read"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters