|
| 1 | +using FluentAssertions; |
| 2 | + |
| 3 | +using k8s; |
| 4 | +using k8s.Models; |
| 5 | + |
| 6 | +using KubeOps.Abstractions.Controller; |
| 7 | +using KubeOps.KubernetesClient; |
| 8 | +using KubeOps.Operator.Test.TestEntities; |
| 9 | +using KubeOps.Transpiler; |
| 10 | + |
| 11 | +using Microsoft.Extensions.DependencyInjection; |
| 12 | + |
| 13 | +namespace KubeOps.Operator.Test; |
| 14 | + |
| 15 | +public class NamespacedOperatorIntegrationTest : IntegrationTestBase, IAsyncLifetime |
| 16 | +{ |
| 17 | + private static readonly InvocationCounter<V1IntegrationTestEntity> Mock = new(); |
| 18 | + private IKubernetesClient<V1IntegrationTestEntity> _client = null!; |
| 19 | + private IKubernetesClient<V1Namespace> _nsClient = null!; |
| 20 | + |
| 21 | + public NamespacedOperatorIntegrationTest(HostBuilder hostBuilder) : base(hostBuilder) |
| 22 | + { |
| 23 | + Mock.Clear(); |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public async Task Should_Call_Reconcile_On_Entity_In_Namespace() |
| 28 | + { |
| 29 | + var watcherCounter = new InvocationCounter<V1IntegrationTestEntity> { TargetInvocationCount = 2 }; |
| 30 | + using var watcher = _client.Watch((_, e) => watcherCounter.Invocation(e)); |
| 31 | + |
| 32 | + await _client.CreateAsync(new V1IntegrationTestEntity("test-entity", "username", "foobar")); |
| 33 | + await _client.CreateAsync(new V1IntegrationTestEntity("test-entity", "username", "default")); |
| 34 | + await Mock.WaitForInvocations; |
| 35 | + await watcherCounter.WaitForInvocations; |
| 36 | + Mock.Invocations.Count.Should().Be(1); |
| 37 | + watcherCounter.Invocations.Count.Should().Be(2); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public async Task Should_Not_Call_Reconcile_On_Entity_In_Other_Namespace() |
| 42 | + { |
| 43 | + var watcherCounter = new InvocationCounter<V1IntegrationTestEntity> { TargetInvocationCount = 1 }; |
| 44 | + using var watcher = _client.Watch((_, e) => watcherCounter.Invocation(e)); |
| 45 | + |
| 46 | + await _client.CreateAsync(new V1IntegrationTestEntity("test-entity2", "username", "default")); |
| 47 | + await watcherCounter.WaitForInvocations; |
| 48 | + Mock.Invocations.Count.Should().Be(0); |
| 49 | + watcherCounter.Invocations.Count.Should().Be(1); |
| 50 | + } |
| 51 | + |
| 52 | + public async Task InitializeAsync() |
| 53 | + { |
| 54 | + var meta = Entities.ToEntityMetadata(typeof(V1IntegrationTestEntity)).Metadata; |
| 55 | + _client = new KubernetesClient<V1IntegrationTestEntity>(meta); |
| 56 | + _nsClient = new KubernetesClient<V1Namespace>(new(V1Namespace.KubeKind, V1Namespace.KubeApiVersion, |
| 57 | + V1Namespace.KubeGroup, V1Namespace.KubePluralName)); |
| 58 | + await _nsClient.SaveAsync(new V1Namespace(metadata: new(name: "foobar")).Initialize()); |
| 59 | + await _hostBuilder.ConfigureAndStart(builder => builder.Services |
| 60 | + .AddSingleton(Mock) |
| 61 | + .AddKubernetesOperator(s => s.Namespace = "foobar") |
| 62 | + .AddControllerWithEntity<TestController, V1IntegrationTestEntity>(meta)); |
| 63 | + } |
| 64 | + |
| 65 | + public async Task DisposeAsync() |
| 66 | + { |
| 67 | + var entities = await _client.ListAsync("default"); |
| 68 | + await _nsClient.DeleteAsync("foobar"); |
| 69 | + while (await _nsClient.GetAsync("foobar") is not null) |
| 70 | + { |
| 71 | + await Task.Delay(100); |
| 72 | + } |
| 73 | + await _client.DeleteAsync(entities); |
| 74 | + _client.Dispose(); |
| 75 | + } |
| 76 | + |
| 77 | + private class TestController : IEntityController<V1IntegrationTestEntity> |
| 78 | + { |
| 79 | + private readonly InvocationCounter<V1IntegrationTestEntity> _svc; |
| 80 | + |
| 81 | + public TestController(InvocationCounter<V1IntegrationTestEntity> svc) |
| 82 | + { |
| 83 | + _svc = svc; |
| 84 | + } |
| 85 | + |
| 86 | + public Task ReconcileAsync(V1IntegrationTestEntity entity) |
| 87 | + { |
| 88 | + _svc.Invocation(entity); |
| 89 | + return Task.CompletedTask; |
| 90 | + } |
| 91 | + |
| 92 | + public Task DeletedAsync(V1IntegrationTestEntity entity) |
| 93 | + { |
| 94 | + _svc.Invocation(entity); |
| 95 | + return Task.CompletedTask; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments