-
Notifications
You must be signed in to change notification settings - Fork 5.1k
fix(playwrighttesting): Using dependency injection instead of BlobClient in TestProcessor #46807
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6b1646f
fix(playwrighttesting): Using dependency injection instead of BlobCli…
1d07edf
update
bb7a732
update
8db369d
removed previous commit
f808eda
added test for bobService
0b4692b
Merge remote-tracking branch 'origin/main' into users/guptaKashish/DI
8471713
update
16d266b
update test
f5a8805
added test for sas uri.
eb4ab12
updated UploadBlobFile
53990b7
update
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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); | ||
kashish2508 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| void UploadBlobFile(string uri, string fileRelativePath, string filePath); | ||
Sid200026 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public string? GetCloudFileName(string filePath, string testExecutionId); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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"; | ||
kashish2508 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.