-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(testing): add possibility to mock and test finalizers.
This relates to #8.
- Loading branch information
Showing
11 changed files
with
198 additions
and
25 deletions.
There are no files selected for viewing
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
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,84 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using k8s; | ||
using k8s.Models; | ||
using KubeOps.Operator.Client; | ||
using KubeOps.Operator.Client.LabelSelectors; | ||
|
||
namespace KubeOps.Testing | ||
{ | ||
public class MockKubernetesClient : IKubernetesClient | ||
{ | ||
public IKubernetes ApiClient { get; } = new Kubernetes(KubernetesClientConfiguration.BuildDefaultConfig()); | ||
|
||
public object? GetResult { get; set; } | ||
|
||
public IList<object>? ListResult { get; set; } | ||
|
||
public object? SaveResult { get; set; } | ||
|
||
public object? CreateResult { get; set; } | ||
|
||
public object? UpdateResult { get; set; } | ||
|
||
public Task<TResource?> Get<TResource>(string name, string? @namespace = null) | ||
where TResource : class, IKubernetesObject<V1ObjectMeta> | ||
=> Task.FromResult(GetResult as TResource); | ||
|
||
public Task<IList<TResource>> List<TResource>(string? @namespace = null, string? labelSelector = null) | ||
where TResource : IKubernetesObject<V1ObjectMeta> | ||
=> Task.FromResult(ListResult as IList<TResource> ?? new List<TResource>()); | ||
|
||
public Task<IList<TResource>> List<TResource>(string? @namespace = null, params ILabelSelector[] labelSelectors) | ||
where TResource : IKubernetesObject<V1ObjectMeta> | ||
=> Task.FromResult(ListResult as IList<TResource> ?? new List<TResource>()); | ||
|
||
public Task<TResource> Save<TResource>(TResource resource) | ||
where TResource : class, IKubernetesObject<V1ObjectMeta> | ||
=> Task.FromResult(SaveResult as TResource)!; | ||
|
||
public Task<TResource> Create<TResource>(TResource resource) | ||
where TResource : IKubernetesObject<V1ObjectMeta> | ||
=> Task.FromResult((TResource) CreateResult!)!; | ||
|
||
public Task<TResource> Update<TResource>(TResource resource) | ||
where TResource : IKubernetesObject<V1ObjectMeta> | ||
=> Task.FromResult((TResource) UpdateResult!)!; | ||
|
||
public Task UpdateStatus<TStatus>(IStatus<TStatus> resource) | ||
=> Task.CompletedTask; | ||
|
||
public Task Delete<TResource>(TResource resource) | ||
where TResource : IKubernetesObject<V1ObjectMeta> | ||
=> Task.CompletedTask; | ||
|
||
public Task Delete<TResource>(IEnumerable<TResource> resources) | ||
where TResource : IKubernetesObject<V1ObjectMeta> | ||
=> Task.CompletedTask; | ||
|
||
public Task Delete<TResource>(params TResource[] resources) | ||
where TResource : IKubernetesObject<V1ObjectMeta> | ||
=> Task.CompletedTask; | ||
|
||
public Task Delete<TResource>(string name, string? @namespace = null) | ||
where TResource : IKubernetesObject<V1ObjectMeta> | ||
=> Task.CompletedTask; | ||
|
||
public Task<Watcher<TResource>> Watch<TResource>( | ||
TimeSpan timeout, | ||
Action<WatchEventType, TResource> onEvent, | ||
Action<Exception>? onError = null, | ||
Action? onClose = null, | ||
string? @namespace = null, | ||
CancellationToken cancellationToken = default) | ||
where TResource : IKubernetesObject<V1ObjectMeta> | ||
=> Task.FromResult( | ||
new Watcher<TResource>( | ||
() => Task.FromResult(new StreamReader(new MemoryStream())), | ||
(_, __) => { }, | ||
_ => { })); | ||
} | ||
} |
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,6 @@ | ||
using Xunit; | ||
|
||
[assembly: CollectionBehavior( | ||
CollectionBehavior.CollectionPerAssembly, | ||
MaxParallelThreads = 1, | ||
DisableTestParallelization = true)] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Threading.Tasks; | ||
using k8s.Models; | ||
using KubeOps.Testing; | ||
using KubeOps.TestOperator.Entities; | ||
using KubeOps.TestOperator.Finalizer; | ||
using KubeOps.TestOperator.TestManager; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace KubeOps.TestOperator.Test | ||
{ | ||
public class TestFinalizerTest | ||
{ | ||
private readonly Mock<IManager> _mock = new Mock<IManager>(); | ||
|
||
private readonly KubernetesTestOperator _operator; | ||
|
||
public TestFinalizerTest() | ||
{ | ||
_operator = new Operator() | ||
.ConfigureServices( | ||
services => | ||
{ | ||
services.RemoveAll(typeof(IManager)); | ||
services.AddSingleton(typeof(IManager), _mock.Object); | ||
}) | ||
.ToKubernetesTestOperator(); | ||
} | ||
|
||
[Fact] | ||
public async Task Test_If_Manager_Finalized_Is_Called() | ||
{ | ||
await _operator.Run(); | ||
_mock.Setup(o => o.Finalized(It.IsAny<TestEntity>())); | ||
_mock.Verify(o => o.Finalized(It.IsAny<TestEntity>()), Times.Never); | ||
_operator.MockedClient.UpdateResult = new TestEntity(); | ||
var queue = _operator.GetMockedEventQueue<TestEntity>(); | ||
queue.Finalizing( | ||
new TestEntity | ||
{ | ||
Metadata = new V1ObjectMeta | ||
{ | ||
Finalizers = new[] { new TestEntityFinalizer(_mock.Object).Identifier }, | ||
} | ||
}); | ||
_mock.Verify(o => o.Finalized(It.IsAny<TestEntity>()), Times.Once); | ||
} | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
tests/KubeOps.TestOperator/Finalizer/TestEntityFinalizer.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,23 @@ | ||
using System.Threading.Tasks; | ||
using KubeOps.Operator.Finalizer; | ||
using KubeOps.TestOperator.Entities; | ||
using KubeOps.TestOperator.TestManager; | ||
|
||
namespace KubeOps.TestOperator.Finalizer | ||
{ | ||
public class TestEntityFinalizer : ResourceFinalizerBase<TestEntity> | ||
{ | ||
private readonly IManager _manager; | ||
|
||
public TestEntityFinalizer(IManager manager) | ||
{ | ||
_manager = manager; | ||
} | ||
|
||
public override Task Finalize(TestEntity resource) | ||
{ | ||
_manager.Finalized(resource); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
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
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