Пример #1
0
        public async Task ConnectToPodPortAsync_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>(() => client.ConnectToPodPortAsync(null, 1, default).AsTask()).ConfigureAwait(false);

                await Assert.ThrowsAsync <ValidationException>(() => client.ConnectToPodPortAsync(new V1Pod {
                }, 1, default).AsTask()).ConfigureAwait(false);

                await Assert.ThrowsAsync <ValidationException>(() => client.ConnectToPodPortAsync(new V1Pod {
                    Metadata = new V1ObjectMeta()
                }, 1, default).AsTask()).ConfigureAwait(false);

                await Assert.ThrowsAsync <ValidationException>(() => client.ConnectToPodPortAsync(new V1Pod {
                    Metadata = new V1ObjectMeta()
                    {
                        Name = "a"
                    }
                }, 1, default).AsTask()).ConfigureAwait(false);

                await Assert.ThrowsAsync <ValidationException>(() => client.ConnectToPodPortAsync(new V1Pod {
                    Metadata = new V1ObjectMeta()
                    {
                        NamespaceProperty = "b"
                    }
                }, 1, default).AsTask()).ConfigureAwait(false);
            }

            protocol.Verify();
        }
Пример #2
0
        public async Task ConnectToPodPortAsync_UsesPortForwarding_Async()
        {
            var pod = new V1Pod()
            {
                Metadata = new V1ObjectMeta()
                {
                    NamespaceProperty = "my-namespace",
                    Name = "usbmuxd-abcd",
                },
            };

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

            protocol
            .Setup(k => k.WebSocketNamespacedPodPortForwardAsync("usbmuxd-abcd", "my-namespace", new int[] { 27015 }, null, null, default))
            .ReturnsAsync(websocket)
            .Verifiable();
            protocol.Setup(p => p.Dispose()).Verifiable();

            using (var client = new KubernetesClient(protocol.Object, KubernetesOptions.Default, NullLogger <KubernetesClient> .Instance, NullLoggerFactory.Instance))
                using (var stream = await client.ConnectToPodPortAsync(pod, 27015, default))
                {
                    var portForwardStream = Assert.IsType <PortForwardStream>(stream);
                    Assert.Same(websocket, portForwardStream.WebSocket);
                }

            protocol.Verify();
        }
Пример #3
0
        public async Task ApiServer_Is_Running_Async()
        {
            var config = KubernetesClientConfiguration.BuildDefaultConfig();

            if (config.Namespace == null)
            {
                config.Namespace = "default";
            }

            using (var kubernetes = new KubernetesProtocol(
                       config,
                       this.loggerFactory.CreateLogger <KubernetesProtocol>(),
                       this.loggerFactory))
                using (var client = new KubernetesClient(
                           kubernetes,
                           KubernetesOptions.Default,
                           this.output.BuildLoggerFor <KubernetesClient>(),
                           this.loggerFactory))
                {
                    // There's at least one operator pod
                    var pods = await kubernetes.ListNamespacedPodAsync(config.Namespace, labelSelector : "app.kubernetes.io/component=operator");

                    Assert.NotEmpty(pods.Items);
                    var pod = pods.Items[0];

                    // The pod is in the running state
                    pod = await client.WaitForPodRunningAsync(pod, TimeSpan.FromMinutes(5), default).ConfigureAwait(false);

                    Assert.Equal("Running", pod.Status.Phase);

                    // We can connect to port 80 and fetch the status
                    using (var httpClient = new HttpClient(
                               new SocketsHttpHandler()
                    {
                        ConnectCallback = (context, cancellationToken) => client.ConnectToPodPortAsync(pod, 80, cancellationToken),
                    }))
                    {
                        httpClient.BaseAddress = new Uri("http://localhost:80/");

                        var urls = new string[] { "/", "/health/ready", "/health/alive" };

                        foreach (var url in urls)
                        {
                            var response = await httpClient.GetAsync(url).ConfigureAwait(false);

                            Assert.True(response.IsSuccessStatusCode);
                            Assert.True(response.Headers.TryGetValues("X-Kaponata-Version", out var values));
                            Assert.Equal(ThisAssembly.AssemblyInformationalVersion, Assert.Single(values));
                        }
                    }
                }
        }
Пример #4
0
        public async Task Guacd_PerformsHandshake_Async()
        {
            var config = KubernetesClientConfiguration.BuildDefaultConfig();

            if (config.Namespace == null)
            {
                config.Namespace = "default";
            }

            using (var kubernetes = new KubernetesProtocol(
                       config,
                       this.loggerFactory.CreateLogger <KubernetesProtocol>(),
                       this.loggerFactory))
                using (var client = new KubernetesClient(
                           kubernetes,
                           KubernetesOptions.Default,
                           this.output.BuildLoggerFor <KubernetesClient>(),
                           this.loggerFactory))
                {
                    // There's at least one guacd pod
                    var pods = await kubernetes.ListNamespacedPodAsync(config.Namespace, labelSelector : "app.kubernetes.io/name=guacamole,app.kubernetes.io/component=guacd");

                    Assert.NotEmpty(pods.Items);
                    var pod = pods.Items[0];

                    // The pod is in the running state
                    pod = await client.WaitForPodRunningAsync(pod, Timeout, default).ConfigureAwait(false);

                    Assert.Equal("Running", pod.Status.Phase);

                    // Try to perform a handshake
                    using (var connection = await client.ConnectToPodPortAsync(pod, 4822, default).ConfigureAwait(false))
                    {
                        GuacdProtocol protocol = new GuacdProtocol(connection);

                        // Handshake phase
                        await protocol.SendInstructionAsync("select", new string[] { "vnc" }, default).ConfigureAwait(false);

                        var response = await protocol.ReadInstructionAsync(default);