Пример #1
0
        public async Task DeleteCustomResourceDefinitionAsync_ValidatesArguments_Async()
        {
            var protocol = new Mock <IKubernetesProtocol>(MockBehavior.Strict);

            protocol.Setup(p => p.Dispose()).Verifiable();

            using (var client = new KubernetesClient(protocol.Object, KubernetesOptions.Default, NullLogger <KubernetesClient> .Instance, NullLoggerFactory.Instance))
            {
                await Assert.ThrowsAsync <ArgumentNullException>("value", () => client.DeleteCustomResourceDefinitionAsync(null, TimeSpan.FromMinutes(1), default)).ConfigureAwait(false);
            }

            protocol.Verify();
        }
Пример #2
0
        public async Task DeleteCustomResourceDefinitionAsync_CustomResourceDefinitionDeleted_Returns_Async()
        {
            var customResourceDefinition =
                new V1CustomResourceDefinition()
            {
                Metadata = new V1ObjectMeta()
                {
                    Name = "my-crd",
                },
                Status = new V1CustomResourceDefinitionStatus()
                {
                },
            };

            WatchEventDelegate <V1CustomResourceDefinition> callback  = null;
            TaskCompletionSource <WatchExitReason>          watchTask = new TaskCompletionSource <WatchExitReason>();

            var protocol = new Mock <IKubernetesProtocol>(MockBehavior.Strict);

            protocol.Setup(p => p.Dispose()).Verifiable();
            protocol
            .Setup(p => p.DeleteCustomResourceDefinitionWithHttpMessagesAsync(customResourceDefinition.Metadata.Name, null, null, null, null, null, null, null, default))
            .Returns(Task.FromResult(new HttpOperationResponse <V1Status>()
            {
                Body = new V1Status(), Response = new HttpResponseMessage(HttpStatusCode.OK)
            })).Verifiable();

            protocol
            .Setup(p => p.WatchCustomResourceDefinitionAsync(customResourceDefinition, It.IsAny <WatchEventDelegate <V1CustomResourceDefinition> >(), It.IsAny <CancellationToken>()))
            .Returns <V1CustomResourceDefinition, WatchEventDelegate <V1CustomResourceDefinition>, CancellationToken>((customResourceDefinition, watcher, ct) =>
            {
                callback = watcher;
                return(watchTask.Task);
            });

            using (var client = new KubernetesClient(protocol.Object, KubernetesOptions.Default, NullLogger <KubernetesClient> .Instance, NullLoggerFactory.Instance))
            {
                var task = client.DeleteCustomResourceDefinitionAsync(customResourceDefinition, TimeSpan.FromMinutes(1), default);
                Assert.NotNull(callback);

                // The callback continues watching until the CustomResourceDefinition is deleted
                Assert.Equal(WatchResult.Continue, await callback(WatchEventType.Modified, customResourceDefinition).ConfigureAwait(false));
                Assert.Equal(WatchResult.Stop, await callback(WatchEventType.Deleted, customResourceDefinition).ConfigureAwait(false));
                watchTask.SetResult(WatchExitReason.ClientDisconnected);

                await task.ConfigureAwait(false);
            }

            protocol.Verify();
        }
Пример #3
0
        public async Task DeleteCustomResourceDefinitionAsync_RespectsTimeout_Async()
        {
            var customResourceDefinition =
                new V1CustomResourceDefinition()
            {
                Kind     = V1CustomResourceDefinition.KubeKind,
                Metadata = new V1ObjectMeta()
                {
                    Name = "my-crd",
                },
                Status = new V1CustomResourceDefinitionStatus()
                {
                },
            };

            WatchEventDelegate <V1CustomResourceDefinition> callback  = null;
            TaskCompletionSource <WatchExitReason>          watchTask = new TaskCompletionSource <WatchExitReason>();

            var protocol = new Mock <IKubernetesProtocol>(MockBehavior.Strict);

            protocol.Setup(p => p.Dispose()).Verifiable();
            protocol
            .Setup(p => p.DeleteCustomResourceDefinitionWithHttpMessagesAsync(customResourceDefinition.Metadata.Name, null, null, null, null, null, null, null, default))
            .Returns(Task.FromResult(new HttpOperationResponse <V1Status>()
            {
                Body = new V1Status(), Response = new HttpResponseMessage(HttpStatusCode.OK)
            })).Verifiable();

            protocol
            .Setup(p => p.WatchCustomResourceDefinitionAsync(customResourceDefinition, It.IsAny <WatchEventDelegate <V1CustomResourceDefinition> >(), It.IsAny <CancellationToken>()))
            .Returns <V1CustomResourceDefinition, WatchEventDelegate <V1CustomResourceDefinition>, CancellationToken>((customResourceDefinition, watcher, ct) =>
            {
                callback = watcher;
                return(watchTask.Task);
            });

            using (var client = new KubernetesClient(protocol.Object, KubernetesOptions.Default, NullLogger <KubernetesClient> .Instance, NullLoggerFactory.Instance))
            {
                var task = client.DeleteCustomResourceDefinitionAsync(customResourceDefinition, TimeSpan.Zero, default);
                Assert.NotNull(callback);

                // The watch completes with an exception.
                var ex = await Assert.ThrowsAsync <KubernetesException>(() => task).ConfigureAwait(false);

                Assert.Equal("The CustomResourceDefinition 'my-crd' was not deleted within a timeout of 0 seconds.", ex.Message);
            }

            protocol.Verify();
        }