(refactor): containers tests
This commit is contained in:
parent
acdcb1300d
commit
b68d8c1bca
@ -21,7 +21,6 @@ namespace MaksIT.PodmanClientDotNet {
|
|||||||
var response = await _httpClient.PostAsync($"/{_apiVersion}/libpod/images/pull?{query}", null);
|
var response = await _httpClient.PostAsync($"/{_apiVersion}/libpod/images/pull?{query}", null);
|
||||||
|
|
||||||
if (response.IsSuccessStatusCode) {
|
if (response.IsSuccessStatusCode) {
|
||||||
|
|
||||||
var responseStream = await response.Content.ReadAsStreamAsync();
|
var responseStream = await response.Content.ReadAsStreamAsync();
|
||||||
using (var reader = new StreamReader(responseStream)) {
|
using (var reader = new StreamReader(responseStream)) {
|
||||||
string line;
|
string line;
|
||||||
|
|||||||
106
src/PodmanClientDotNet.Tests/PodmanClientContainersTests.cs
Normal file
106
src/PodmanClientDotNet.Tests/PodmanClientContainersTests.cs
Normal file
@ -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<PodmanClient>();
|
||||||
|
|
||||||
|
// 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<string> CreateContainerAsync(string containerName, string image) {
|
||||||
|
var createResponse = await _client.CreateContainerAsync(
|
||||||
|
name: containerName,
|
||||||
|
image: image,
|
||||||
|
command: new List<string> {
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
}
|
||||||
79
src/PodmanClientDotNet.Tests/PodmanClientImagesTests.cs
Normal file
79
src/PodmanClientDotNet.Tests/PodmanClientImagesTests.cs
Normal file
@ -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<PodmanClient>();
|
||||||
|
|
||||||
|
// 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<HttpRequestException>(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<HttpRequestException>(exception); // Ensure it's the expected type
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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<PodmanClient>();
|
|
||||||
|
|
||||||
// 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<HttpRequestException>(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<HttpRequestException>(exception); // Ensure it's the expected type
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user