diff --git a/src/PodmanClient/PodmanClientImage.cs b/src/PodmanClient/PodmanClientImage.cs index 1447a75..c47d658 100644 --- a/src/PodmanClient/PodmanClientImage.cs +++ b/src/PodmanClient/PodmanClientImage.cs @@ -21,7 +21,6 @@ namespace MaksIT.PodmanClientDotNet { var response = await _httpClient.PostAsync($"/{_apiVersion}/libpod/images/pull?{query}", null); if (response.IsSuccessStatusCode) { - var responseStream = await response.Content.ReadAsStreamAsync(); using (var reader = new StreamReader(responseStream)) { string line; diff --git a/src/PodmanClientDotNet.Tests/PodmanClientContainersTests.cs b/src/PodmanClientDotNet.Tests/PodmanClientContainersTests.cs new file mode 100644 index 0000000..aceceb8 --- /dev/null +++ b/src/PodmanClientDotNet.Tests/PodmanClientContainersTests.cs @@ -0,0 +1,106 @@ +using Microsoft.Extensions.Logging; +using Xunit; +using System.Threading.Tasks; + +namespace MaksIT.PodmanClientDotNet.Tests { + public class PodmanClientContainersTests { + private readonly PodmanClient _client; + + public PodmanClientContainersTests() { + // Initialize the logger + var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); + var logger = loggerFactory.CreateLogger(); + + // Initialize PodmanClient with real HttpClient + _client = new PodmanClient(logger, "http://wks0002.corp.maks-it.com:8080", 60); + } + + #region Success + [Fact] + public async Task PodmanClient_ContainerLifecycle_Success() { + // Arrange + string containerName = "test-container"; + string image = "alpine:latest"; + + // Act & Assert + await PullImageAsync(image); + var containerId = await CreateContainerAsync(containerName, image); + await StartContainerAsync(containerId); + await StopContainerAsync(containerId); + await ForceDeleteContainerAsync(containerId); + } + + private async Task PullImageAsync(string image) { + // Implement the logic to pull the image + var exception = await Record.ExceptionAsync(() => _client.PullImageAsync(image)); + Assert.Null(exception); // Expect no exceptions if the pull was successful + } + + private async Task CreateContainerAsync(string containerName, string image) { + var createResponse = await _client.CreateContainerAsync( + name: containerName, + image: image, + command: new List { + "sh", "-c", + "sleep infinity" + }); + Assert.NotNull(createResponse); + Assert.False(string.IsNullOrEmpty(createResponse.Id)); // Ensure a valid container ID is returned + return createResponse.Id; + } + + private async Task StartContainerAsync(string containerId) { + var exception = await Record.ExceptionAsync(() => _client.StartContainerAsync(containerId)); + Assert.Null(exception); // Expect no exceptions if the container was started successfully + } + + private async Task StopContainerAsync(string containerId) { + var exception = await Record.ExceptionAsync(() => _client.StopContainerAsync(containerId)); + Assert.Null(exception); // Expect no exceptions if the container was stopped successfully + } + + private async Task ForceDeleteContainerAsync(string containerId) { + var exception = await Record.ExceptionAsync(() => _client.ForceDeleteContainerAsync(containerId)); + Assert.Null(exception); // Expect no exceptions if the container was deleted successfully + } + #endregion + + #region Fail + [Fact] + public async Task StartContainerAsync_Should_HandleErrors() { + // Arrange + string invalidContainerId = "invalid-container-id"; + + // Act + var exception = await Record.ExceptionAsync(() => _client.StartContainerAsync(invalidContainerId)); + + // Assert + Assert.NotNull(exception); // Expect an exception due to invalid container ID + } + + [Fact] + public async Task StopContainerAsync_Should_HandleErrors() { + // Arrange + string invalidContainerId = "invalid-container-id"; + + // Act + var exception = await Record.ExceptionAsync(() => _client.StopContainerAsync(invalidContainerId)); + + // Assert + Assert.NotNull(exception); // Expect an exception due to invalid container ID + } + + [Fact] + public async Task ForceDeleteContainerAsync_Should_HandleErrors() { + // Arrange + string invalidContainerId = "invalid-container-id"; + + // Act + var exception = await Record.ExceptionAsync(() => _client.ForceDeleteContainerAsync(invalidContainerId)); + + // Assert + Assert.NotNull(exception); // Expect an exception due to invalid container ID + } + #endregion + } +} diff --git a/src/PodmanClientDotNet.Tests/PodmanClientImagesTests.cs b/src/PodmanClientDotNet.Tests/PodmanClientImagesTests.cs new file mode 100644 index 0000000..72fe949 --- /dev/null +++ b/src/PodmanClientDotNet.Tests/PodmanClientImagesTests.cs @@ -0,0 +1,79 @@ + +using Microsoft.Extensions.Logging; + + +namespace MaksIT.PodmanClientDotNet.Tests; +public class PodmanClientImagesTests { + private readonly PodmanClient _client; + + public PodmanClientImagesTests() { + // Initialize the logger + var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); + var logger = loggerFactory.CreateLogger(); + + // Initialize PodmanClient with real HttpClient + _client = new PodmanClient(logger, "http://wks0002.corp.maks-it.com:8080", 60); + } + + [Fact] + public async Task PodmanClient_IntegrationTests() { + // Test 1: Pull Image - Success + await PullImageAsync_Should_Succeed(); + + // Test 2: Tag Image - Success + await TagImageAsync_Should_Succeed(); + } + + private async Task PullImageAsync_Should_Succeed() { + // Arrange + string imageReference = "alpine:latest"; // Example image + + // Act + var exception = await Record.ExceptionAsync(() => _client.PullImageAsync(imageReference)); + + // Assert + Assert.Null(exception); // Expect no exceptions if the pull was successful + } + + private async Task TagImageAsync_Should_Succeed() { + // Arrange + string image = "alpine:latest"; // Example image + string repo = "myrepo"; + string tag = "v1"; + + // Act + var exception = await Record.ExceptionAsync(() => _client.TagImageAsync(image, repo, tag)); + + // Assert + Assert.Null(exception); // Expect no exceptions if the tagging was successful + } + + [Fact] + public async Task PodmanClient_PullImage_Errors() { + + // Arrange + string imageReference = "dghdfdghmhgn:latest"; // Intentionally wrong image + + // Act + var exception = await Record.ExceptionAsync(() => _client.PullImageAsync(imageReference)); + + // Assert + Assert.NotNull(exception); // Expect an exception due to nonexistent image + Assert.IsType(exception); // Ensure it's the expected type + } + + [Fact] + public async Task PodmanClient_TagImage_Errors() { + // Arrange + string image = "dghdfdghmhgn:latest"; // Intentionally wrong image + string repo = "myrepo"; + string tag = "v1"; + + // Act + var exception = await Record.ExceptionAsync(() => _client.TagImageAsync(image, repo, tag)); + + // Assert + Assert.NotNull(exception); // Expect an exception due to nonexistent image + Assert.IsType(exception); // Ensure it's the expected type + } +} diff --git a/src/PodmanClientDotNet.Tests/PodmanClientTests.cs b/src/PodmanClientDotNet.Tests/PodmanClientTests.cs deleted file mode 100644 index b5827be..0000000 --- a/src/PodmanClientDotNet.Tests/PodmanClientTests.cs +++ /dev/null @@ -1,90 +0,0 @@ - -using Microsoft.Extensions.Logging; -using static System.Net.Mime.MediaTypeNames; - - -namespace MaksIT.PodmanClientDotNet.Tests { - public class PodmanClientIntegrationTests { - private readonly PodmanClient _client; - - public PodmanClientIntegrationTests() { - // Initialize the logger - var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); - var logger = loggerFactory.CreateLogger(); - - // Initialize PodmanClient with real HttpClient - _client = new PodmanClient(logger, "http://wks0002.corp.maks-it.com:8080", 60); - } - - [Fact] - public async Task PodmanClient_IntegrationTests() { - // Test 1: Pull Image - Success - await PullImageAsync_Should_Succeed(); - - // Test 2: Tag Image - Success - await TagImageAsync_Should_Succeed(); - } - - private async Task PullImageAsync_Should_Succeed() { - // Arrange - string imageReference = "alpine:latest"; // Example image - - // Act - var exception = await Record.ExceptionAsync(() => _client.PullImageAsync(imageReference)); - - // Assert - Assert.Null(exception); // Expect no exceptions if the pull was successful - } - - private async Task TagImageAsync_Should_Succeed() { - // Arrange - string image = "alpine:latest"; // Example image - string repo = "myrepo"; - string tag = "v1"; - - // Act - var exception = await Record.ExceptionAsync(() => _client.TagImageAsync(image, repo, tag)); - - // Assert - Assert.Null(exception); // Expect no exceptions if the tagging was successful - } - - [Fact] - public async Task PodmanClient_PullImage_Errors() { - - // Arrange - string imageReference = "dghdfdghmhgn:latest"; // Intentionally wrong image - - // Act - var exception = await Record.ExceptionAsync(() => _client.PullImageAsync(imageReference)); - - // Assert - Assert.NotNull(exception); // Expect an exception due to nonexistent image - Assert.IsType(exception); // Ensure it's the expected type - } - - [Fact] - public async Task PodmanClient_TagImage_Errors() { - // Arrange - string image = "dghdfdghmhgn:latest"; // Intentionally wrong image - string repo = "myrepo"; - string tag = "v1"; - - // Act - var exception = await Record.ExceptionAsync(() => _client.TagImageAsync(image, repo, tag)); - - // Assert - Assert.NotNull(exception); // Expect an exception due to nonexistent image - Assert.IsType(exception); // Ensure it's the expected type - } - - - - - - - - - - } -}