-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(playwrighttesting): Using dependency injection instead of BlobCli…
…ent in TestProcessor (#46807) * fix(playwrighttesting): Using dependency injection instead of BlobClient in TestProcessor * update * update * removed previous commit * added test for bobService * update * update test * added test for sas uri. * updated UploadBlobFile * update --------- Co-authored-by: guptakashish <guptakashish@microsoft.com>
- Loading branch information
1 parent
63e086e
commit 52190ed
Showing
6 changed files
with
187 additions
and
39 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...g/Azure.Developer.MicrosoftPlaywrightTesting.TestLogger/src/Implementation/BlobService.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,63 @@ | ||
// 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; | ||
using System.IO; | ||
|
||
namespace Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Implementation | ||
{ | ||
internal class BlobService:IBlobService | ||
{ | ||
private readonly ILogger _logger; | ||
|
||
public BlobService(ILogger? logger) | ||
{ | ||
_logger = logger ?? new 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}"); | ||
} | ||
} | ||
public void UploadBlobFile(string uri, string fileRelativePath, string filePath) | ||
{ | ||
string cloudFilePath = GetCloudFilePath(uri, fileRelativePath); | ||
BlobClient blobClient = new(new Uri(cloudFilePath)); | ||
blobClient.Upload(filePath, overwrite: true); | ||
_logger.Info($"Uploaded file {filePath} to {fileRelativePath}"); | ||
} | ||
public 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}"; | ||
} | ||
public string? GetCloudFileName(string filePath, string testExecutionId) | ||
{ | ||
var fileName = Path.GetFileName(filePath); | ||
if (fileName == null) | ||
{ | ||
return null; | ||
} | ||
return $"{testExecutionId}/{fileName}"; // TODO check if we need to add {Guid.NewGuid()} for file with same name | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...sting/Azure.Developer.MicrosoftPlaywrightTesting.TestLogger/src/Interface/IBlobService.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,21 @@ | ||
// 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); | ||
string GetCloudFilePath(string uri, string fileRelativePath); | ||
void UploadBlobFile(string uri, string fileRelativePath, string filePath); | ||
public string? GetCloudFileName(string filePath, string testExecutionId); | ||
} | ||
} |
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
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
62 changes: 62 additions & 0 deletions
62
....Developer.MicrosoftPlaywrightTesting.TestLogger/tests/Implementation/BlobServiceTests.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,62 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Implementation; | ||
using Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Interface; | ||
using Moq; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Developer.MicrosoftPlaywrightTesting.TestLogger.Tests.Implementation | ||
{ | ||
[TestFixture] | ||
[Parallelizable(ParallelScope.Self)] | ||
public class BlobServiceTests | ||
{ | ||
private Mock<ILogger>? _loggerMock; | ||
private BlobService? _blobService; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_loggerMock = new Mock<ILogger>(); | ||
_blobService = new BlobService(_loggerMock.Object); | ||
} | ||
[Test] | ||
public async Task UploadBufferAsync_WithException_LogsError() | ||
{ | ||
string uri = "invalid_uri"; | ||
string buffer = "Test buffer"; | ||
string fileRelativePath = "test/path"; | ||
|
||
await _blobService!.UploadBufferAsync(uri, buffer, fileRelativePath); | ||
|
||
_loggerMock!.Verify(logger => logger.Error(It.IsAny<string>()), Times.Once); | ||
} | ||
|
||
[Test] | ||
public void GetCloudFilePath_WithValidParameters_ReturnsCorrectPath() | ||
{ | ||
string uri = "https://example.com/container"; | ||
string fileRelativePath = "test/path"; | ||
string expectedPath = "https://example.com/container/test/path?"; | ||
|
||
string? result = _blobService?.GetCloudFilePath(uri, fileRelativePath); | ||
|
||
Assert.AreEqual(expectedPath, result); | ||
} | ||
|
||
[Test] | ||
public void GetCloudFilePath_WithSasUri_ReturnsCorrectPath() | ||
{ | ||
string uri = "https://example.com/container?sasToken"; | ||
string fileRelativePath = "test/path"; | ||
string expectedPath = "https://example.com/container/test/path?sasToken"; | ||
|
||
string? result = _blobService?.GetCloudFilePath(uri, fileRelativePath); | ||
|
||
Assert.AreEqual(expectedPath, result); | ||
} | ||
} | ||
} |
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