Esempio n. 1
0
        public async Task WatchNamespacedObjectAsync_ValidatesArguments_Async()
        {
            var pod = new V1Pod()
            {
                Metadata = new V1ObjectMeta()
                {
                    Name = "name", NamespaceProperty = "namespace"
                }
            };

            var protocol = new KubernetesProtocol(new DummyHandler(), this.loggerFactory.CreateLogger <KubernetesProtocol>(), this.loggerFactory);
            await Assert.ThrowsAsync <ArgumentNullException>("value", () => protocol.WatchNamespacedObjectAsync <V1Pod, V1PodList>(null, protocol.ListNamespacedPodWithHttpMessagesAsync, (eventType, result) => Task.FromResult(WatchResult.Continue), default)).ConfigureAwait(false);

            await Assert.ThrowsAsync <ValidationException>(() => protocol.WatchNamespacedObjectAsync <V1Pod, V1PodList>(new V1Pod()
            {
            }, null, (eventType, result) => Task.FromResult(WatchResult.Continue), default)).ConfigureAwait(false);

            await Assert.ThrowsAsync <ValidationException>(() => protocol.WatchNamespacedObjectAsync <V1Pod, V1PodList>(new V1Pod()
            {
                Metadata = new V1ObjectMeta()
            }, null, (eventType, result) => Task.FromResult(WatchResult.Continue), default)).ConfigureAwait(false);

            await Assert.ThrowsAsync <ValidationException>(() => protocol.WatchNamespacedObjectAsync <V1Pod, V1PodList>(new V1Pod()
            {
                Metadata = new V1ObjectMeta()
                {
                    Name = "test"
                }
            }, null, (eventType, result) => Task.FromResult(WatchResult.Continue), default)).ConfigureAwait(false);

            await Assert.ThrowsAsync <ValidationException>(() => protocol.WatchNamespacedObjectAsync <V1Pod, V1PodList>(new V1Pod()
            {
                Metadata = new V1ObjectMeta()
                {
                    NamespaceProperty = "test"
                }
            }, null, (eventType, result) => Task.FromResult(WatchResult.Continue), default)).ConfigureAwait(false);

            await Assert.ThrowsAsync <ArgumentNullException>("listOperation", () => protocol.WatchNamespacedObjectAsync <V1Pod, V1PodList>(pod, null, (eventType, result) => Task.FromResult(WatchResult.Continue), default)).ConfigureAwait(false);

            await Assert.ThrowsAsync <ArgumentNullException>("eventHandler", () => protocol.WatchNamespacedObjectAsync <V1Pod, V1PodList>(pod, protocol.ListNamespacedPodWithHttpMessagesAsync, null, default)).ConfigureAwait(false);

            await Assert.ThrowsAsync <ArgumentNullException>("namespace", () => protocol.WatchNamespacedObjectAsync <V1Pod, V1PodList>(null, string.Empty, string.Empty, string.Empty, null, protocol.ListNamespacedPodWithHttpMessagesAsync, (eventType, result) => Task.FromResult(WatchResult.Continue), default)).ConfigureAwait(false);

            await Assert.ThrowsAsync <ArgumentNullException>("listOperation", () => protocol.WatchNamespacedObjectAsync <V1Pod, V1PodList>("default", null, null, null, null, null, (eventType, result) => Task.FromResult(WatchResult.Continue), default)).ConfigureAwait(false);

            await Assert.ThrowsAsync <ArgumentNullException>("eventHandler", () => protocol.WatchNamespacedObjectAsync <V1Pod, V1PodList>("default", null, null, null, null, protocol.ListNamespacedPodWithHttpMessagesAsync, null, default)).ConfigureAwait(false);
        }
Esempio n. 2
0
        public async Task WatchNamespacedObjectAsync_CancelsIfNeeded_Async()
        {
            var readTaskCompletion = new TaskCompletionSource <int>();

            var stream = new Mock <Stream>(MockBehavior.Strict);

            stream.Setup(s => s.CanSeek).Returns(false);
            stream.Setup(s => s.CanRead).Returns(true);
            stream
            .Setup(s => s.ReadAsync(It.IsAny <Memory <byte> >(), It.IsAny <CancellationToken>()))
            .Returns <Memory <byte>, CancellationToken>(async(memory, cancellationToken) => await readTaskCompletion.Task);
            stream
            .Setup(s => s.Close())
            .Callback(() => readTaskCompletion.TrySetException(new ObjectDisposedException("Stream", "Stream is being disposed, and all pending I/O cancelled")))
            .Verifiable();

            var handler = new DummyHandler();

            handler.Responses.Enqueue(
                new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new WatchHttpContent(
                    new StreamContent(stream.Object)),
            });

            Collection <(WatchEventType, V1Pod)> events = new Collection <(WatchEventType, V1Pod)>();

            var protocol = new KubernetesProtocol(handler, this.loggerFactory.CreateLogger <KubernetesProtocol>(), this.loggerFactory);
            var cts      = new CancellationTokenSource();

            var watchTask = protocol.WatchNamespacedObjectAsync(
                new V1Pod()
            {
                Metadata = new V1ObjectMeta(name: "pod", namespaceProperty: "default", resourceVersion: "1"),
            },
                protocol.ListNamespacedPodWithHttpMessagesAsync,
                (eventType, result) =>
            {
                events.Add((eventType, result));
                return(Task.FromResult(WatchResult.Continue));
            },
                cts.Token);

            Assert.False(watchTask.IsCompleted);
            cts.Cancel();

            await Task.WhenAny(
                watchTask,
                Task.Delay(TimeSpan.FromSeconds(5))).ConfigureAwait(false);

            Assert.True(watchTask.IsCompletedSuccessfully);

            stream.Verify();
        }
Esempio n. 3
0
        public async Task WatchNamespacedObjectAsync_ThrowsExceptionIfNeeded_Async()
        {
            var handler = new DummyHandler();

            handler.Responses.Enqueue(
                new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new WatchHttpContent(
                    new StringContent(
                        JsonConvert.SerializeObject(
                            new V1WatchEvent()
                {
                    Type           = nameof(WatchEventType.Error),
                    ObjectProperty =
                        new V1Status()
                    {
                        Kind    = "Status",
                        Message = "ErrorMessage",
                    },
                }))),
            });

            Collection <(WatchEventType, V1Pod)> events = new Collection <(WatchEventType, V1Pod)>();

            var protocol = new KubernetesProtocol(handler, this.loggerFactory.CreateLogger <KubernetesProtocol>(), this.loggerFactory);
            var cts      = new CancellationTokenSource();

            var ex = await Assert.ThrowsAsync <KubernetesException>(
                () => protocol.WatchNamespacedObjectAsync(
                    new V1Pod()
            {
                Metadata = new V1ObjectMeta(name: "pod", namespaceProperty: "default", resourceVersion: "1"),
            },
                    protocol.ListNamespacedPodWithHttpMessagesAsync,
                    (eventType, result) =>
            {
                events.Add((eventType, result));
                return(Task.FromResult(WatchResult.Continue));
            },
                    cts.Token)).ConfigureAwait(false);

            Assert.Equal("ErrorMessage", ex.Message);
        }
Esempio n. 4
0
        public async Task WatchNamespacedObjectAsync_EmptyContent_Completes_Async()
        {
            var handler = new DummyHandler();

            handler.Responses.Enqueue(
                new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new WatchHttpContent(
                    new StringContent(string.Empty)),
            });

            Collection <(WatchEventType, V1Pod)> events = new Collection <(WatchEventType, V1Pod)>();

            var protocol = new KubernetesProtocol(handler, this.loggerFactory.CreateLogger <KubernetesProtocol>(), this.loggerFactory);
            var result   = await protocol.WatchNamespacedObjectAsync <V1Pod, V1PodList>(
                new V1Pod()
            {
                Metadata = new V1ObjectMeta(name: "pod", namespaceProperty: "default", resourceVersion: "1"),
            },
                protocol.ListNamespacedPodWithHttpMessagesAsync,
                (eventType, result) =>
            {
                events.Add((eventType, result));
                return(Task.FromResult(WatchResult.Continue));
            },
                default).ConfigureAwait(false);

            Assert.Equal(WatchExitReason.ServerDisconnected, result);
            Assert.Empty(events);
            Assert.Collection(
                handler.Requests,
                r =>
            {
                Assert.Equal(new Uri("http://localhost/api/v1/namespaces/default/pods?allowWatchBookmarks=true&fieldSelector=metadata.name%3Dpod&resourceVersion=1&watch=true"), r.RequestUri);
            });
        }
Esempio n. 5
0
        public async Task WatchNamespacedObjectAsync_ClientCanStopLoop_Async()
        {
            using (var stream = new SimplexStream())
                using (var writer = new StreamWriter(stream))
                {
                    var handler = new DummyHandler();
                    handler.Responses.Enqueue(
                        new HttpResponseMessage()
                    {
                        StatusCode = HttpStatusCode.OK,
                        Content    = new WatchHttpContent(
                            new StreamContent(stream)),
                    });

                    Collection <(WatchEventType, V1Pod)> events = new Collection <(WatchEventType, V1Pod)>();

                    var protocol = new KubernetesProtocol(handler, this.loggerFactory.CreateLogger <KubernetesProtocol>(), this.loggerFactory);
                    var cts      = new CancellationTokenSource();

                    var watchTask = protocol.WatchNamespacedObjectAsync(
                        new V1Pod()
                    {
                        Metadata = new V1ObjectMeta(name: "pod", namespaceProperty: "default", resourceVersion: "1"),
                    },
                        protocol.ListNamespacedPodWithHttpMessagesAsync,
                        (eventType, result) =>
                    {
                        events.Add((eventType, result));
                        return(Task.FromResult(events.Count == 1 ? WatchResult.Continue : WatchResult.Stop));
                    },
                        cts.Token);

                    Assert.True(!watchTask.IsCompleted);

                    await writer.WriteAsync(
                        JsonConvert.SerializeObject(
                            new V1WatchEvent()
                    {
                        Type           = nameof(WatchEventType.Deleted),
                        ObjectProperty = new V1Pod()
                        {
                            Metadata = new V1ObjectMeta()
                            {
                                NamespaceProperty = "some-namespace",
                                Name = "some-name",
                            },
                        },
                    }));

                    await writer.WriteAsync('\n').ConfigureAwait(false);

                    await writer.FlushAsync().ConfigureAwait(false);

                    Assert.True(!watchTask.IsCompleted);

                    await writer.WriteAsync(
                        JsonConvert.SerializeObject(
                            new V1WatchEvent()
                    {
                        Type           = nameof(WatchEventType.Deleted),
                        ObjectProperty = new V1Pod()
                        {
                            Metadata = new V1ObjectMeta()
                            {
                                NamespaceProperty = "some-namespace2",
                                Name = "some-name2",
                            },
                        },
                    }));

                    await writer.WriteAsync('\n').ConfigureAwait(false);

                    await writer.FlushAsync().ConfigureAwait(false);

                    var result = await watchTask;
                    Assert.Equal(WatchExitReason.ClientDisconnected, result);

                    Assert.Collection(
                        events,
                        e =>
                    {
                        Assert.Equal(WatchEventType.Deleted, e.Item1);
                        Assert.Equal("some-namespace", e.Item2.Metadata.NamespaceProperty);
                        Assert.Equal("some-name", e.Item2.Metadata.Name);
                    },
                        e =>
                    {
                        Assert.Equal(WatchEventType.Deleted, e.Item1);
                        Assert.Equal("some-namespace2", e.Item2.Metadata.NamespaceProperty);
                        Assert.Equal("some-name2", e.Item2.Metadata.Name);
                    });
                }
        }