Ejemplo n.º 1
0
        public async Task DeletePodAsync_ApiDisconnects_Errors_Async()
        {
            var pod =
                new V1Pod()
            {
                Kind     = V1Pod.KubeKind,
                Metadata = new V1ObjectMeta()
                {
                    Name = "my-pod",
                    NamespaceProperty = "default",
                },
                Status = new V1PodStatus()
                {
                    Phase = "Pending",
                },
            };

            WatchEventDelegate <V1Pod>             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.DeleteNamespacedPodWithHttpMessagesAsync(pod.Metadata.Name, pod.Metadata.NamespaceProperty, null, null, null, null, null, null, null, default))
            .Returns(Task.FromResult(new HttpOperationResponse <V1Pod>()
            {
                Body = pod, Response = new HttpResponseMessage(HttpStatusCode.OK)
            })).Verifiable();

            protocol
            .Setup(p => p.WatchNamespacedObjectAsync(pod, protocol.Object.ListNamespacedPodWithHttpMessagesAsync, It.IsAny <WatchEventDelegate <V1Pod> >(), It.IsAny <CancellationToken>()))
            .Returns <V1Pod, ListNamespacedObjectWithHttpMessagesAsync <V1Pod, V1PodList>, WatchEventDelegate <V1Pod>, CancellationToken>((pod, list, watcher, ct) =>
            {
                Assert.NotNull(list);
                callback = watcher;
                return(watchTask.Task);
            });

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

                // Simulate the watch task stopping
                watchTask.SetResult(WatchExitReason.ServerDisconnected);

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

                Assert.Equal("The API server unexpectedly closed the connection while watching Pod 'my-pod'.", ex.Message);
            }

            protocol.Verify();
        }
Ejemplo n.º 2
0
        public async Task DeletePodAsync_PodDeleted_Returns_Async()
        {
            var pod =
                new V1Pod()
            {
                Metadata = new V1ObjectMeta()
                {
                    Name = "my-pod",
                    NamespaceProperty = "default",
                },
                Status = new V1PodStatus()
                {
                    Phase = "Pending",
                },
            };

            WatchEventDelegate <V1Pod>             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.DeleteNamespacedPodWithHttpMessagesAsync(pod.Metadata.Name, pod.Metadata.NamespaceProperty, null, null, null, null, null, null, null, default))
            .Returns(Task.FromResult(new HttpOperationResponse <V1Pod>()
            {
                Body = pod, Response = new HttpResponseMessage(HttpStatusCode.OK)
            })).Verifiable();

            protocol
            .Setup(p => p.WatchNamespacedObjectAsync(pod, protocol.Object.ListNamespacedPodWithHttpMessagesAsync, It.IsAny <WatchEventDelegate <V1Pod> >(), It.IsAny <CancellationToken>()))
            .Returns <V1Pod, ListNamespacedObjectWithHttpMessagesAsync <V1Pod, V1PodList>, WatchEventDelegate <V1Pod>, CancellationToken>((pod, list, watcher, ct) =>
            {
                Assert.NotNull(list);
                callback = watcher;
                return(watchTask.Task);
            });

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

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

                await task.ConfigureAwait(false);
            }

            protocol.Verify();
        }
Ejemplo n.º 3
0
        public async Task DeletePodAsync_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.DeletePodAsync(null, TimeSpan.FromMinutes(1), default)).ConfigureAwait(false);
            }

            protocol.Verify();
        }