Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(playwrighttesting): Using dependency injection instead of BlobClient in TestProcessor #46807

Merged
merged 11 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,47 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Text;
using Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Interface;
using Azure.Storage.Blobs;
using System.Threading.Tasks;

namespace Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Implementation
{
internal class BlobService:IBlobService
{
private readonly ILogger _logger;

public BlobService(ILogger logger)
{
_logger = logger;
}

public async Task UploadBufferAsync(string uri, string buffer, string fileRelativePath)
{
try
{
string cloudFilePath = GetCloudFilePath(uri, fileRelativePath);
BlobClient blobClient = new(new Uri(cloudFilePath));
byte[] bufferBytes = Encoding.UTF8.GetBytes(buffer);
await blobClient.UploadAsync(new BinaryData(bufferBytes), overwrite: true).ConfigureAwait(false);
_logger.Info($"Uploaded buffer to {fileRelativePath}");
}
catch (Exception ex)
{
_logger.Error($"Failed to upload buffer: {ex}");
}
}

private static string GetCloudFilePath(string uri, string fileRelativePath)
{
string[] parts = uri.Split(new string[] { ReporterConstants.s_sASUriSeparator }, StringSplitOptions.None);
string containerUri = parts[0];
string sasToken = parts.Length > 1 ? parts[1] : string.Empty;

return $"{containerUri}/{fileRelativePath}?{sasToken}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Threading.Tasks;
namespace Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Interface
{
internal interface IBlobService
{
/// <summary>
///
/// </summary>
/// <param name="uri"></param>
/// <param name="buffer"></param>
/// <param name="fileRelativePath"></param>
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
Task UploadBufferAsync(string uri, string buffer, string fileRelativePath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Implementation;
using Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Interface;
using Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Model;
Expand All @@ -28,6 +29,7 @@ internal class TestProcessor : ITestProcessor
private readonly IConsoleWriter _consoleWriter;
private readonly CIInfo _cIInfo;
private readonly CloudRunMetadata _cloudRunMetadata;
private readonly IBlobService _blobService;

// Test Metadata
internal int TotalTestCount { get; set; } = 0;
Expand All @@ -39,7 +41,7 @@ internal class TestProcessor : ITestProcessor
internal bool FatalTestExecution { get; set; } = false;
internal TestRunShardDto? _testRunShard;

public TestProcessor(CloudRunMetadata cloudRunMetadata, CIInfo cIInfo, ILogger? logger = null, IDataProcessor? dataProcessor = null, ICloudRunErrorParser? cloudRunErrorParser = null, IServiceClient? serviceClient = null, IConsoleWriter? consoleWriter = null)
public TestProcessor(CloudRunMetadata cloudRunMetadata, CIInfo cIInfo, ILogger? logger = null, IDataProcessor? dataProcessor = null, ICloudRunErrorParser? cloudRunErrorParser = null, IServiceClient? serviceClient = null, IConsoleWriter? consoleWriter = null, IBlobService? blobService = null)
{
_cloudRunMetadata = cloudRunMetadata;
_cIInfo = cIInfo;
Expand All @@ -48,6 +50,7 @@ public TestProcessor(CloudRunMetadata cloudRunMetadata, CIInfo cIInfo, ILogger?
_cloudRunErrorParser = cloudRunErrorParser ?? new CloudRunErrorParser(_logger);
_serviceClient = serviceClient ?? new ServiceClient(_cloudRunMetadata, _cloudRunErrorParser);
_consoleWriter = consoleWriter ?? new ConsoleWriter();
_blobService = blobService ?? new BlobService(_logger);
}

public void TestRunStartHandler(object? sender, TestRunStartEventArgs e)
Expand Down Expand Up @@ -166,7 +169,7 @@ public void TestRunCompleteHandler(object? sender, TestRunCompleteEventArgs e)
// Upload rawResult to blob storage using sasUri
var rawTestResultJson = JsonSerializer.Serialize(rawResult);
var filePath = $"{testResult.TestExecutionId}/rawTestResult.json";
UploadBuffer(sasUri!.Uri!, rawTestResultJson, filePath);
UploadBuffer(sasUri!.Uri!, rawTestResultJson, filePath);
}
else
{
Expand Down Expand Up @@ -211,22 +214,11 @@ private void EndTestRun(TestRunCompleteEventArgs e)
}
_cloudRunErrorParser.DisplayMessages();
}
private static string GetCloudFilePath(string uri, string fileRelativePath)
private void UploadBuffer(string uri, string buffer, string fileRelativePath)
{
string[] parts = uri.Split(new string[] { ReporterConstants.s_sASUriSeparator }, StringSplitOptions.None);
string containerUri = parts[0];
string sasToken = parts.Length > 1 ? parts[1] : string.Empty;

return $"{containerUri}/{fileRelativePath}?{sasToken}";
}
private void UploadBuffer(string uri, string buffer, string fileRelativePath)
{
string cloudFilePath = GetCloudFilePath(uri, fileRelativePath);
BlobClient blobClient = new(new Uri(cloudFilePath));
byte[] bufferBytes = Encoding.UTF8.GetBytes(buffer);
blobClient.Upload(new BinaryData(bufferBytes), overwrite: true);
_logger.Info($"Uploaded buffer to {fileRelativePath}");
_blobService.UploadBufferAsync(uri, buffer, fileRelativePath);
}

private TestRunShardDto GetTestRunEndShard(TestRunCompleteEventArgs e)
{
DateTime testRunEndedOn = DateTime.UtcNow;
Expand Down