Files
csharp/tests/KubernetesClient.Tests/KubernetesExecTests.cs
Boshi Lian c0e96f516b deprecate net452 + generate model based on v1.20.0 (#536)
* remove most net452 related code

* first net4 remove

* migrate test proj to net5 base

* fix format

* update sta xunit to fix platform not support

* Squashed commit of the following:

commit 16e1f819058ad281e1571b356c10d4d6ce77cf38
Author: Boshi Lian <farmer1992@gmail.com>
Date:   Tue Dec 8 22:42:57 2020 -0800

    temp disable some version converter

commit 7d1a651f4e7d27d1e61c91f46f73ac8d04ea8ab9
Author: Boshi Lian <farmer1992@gmail.com>
Date:   Tue Dec 8 20:55:44 2020 -0800

    add missing watcher generator files

commit 3f3199aad269bf89406ea71d0bc63f1a7ec23245
Author: Boshi Lian <farmer1992@gmail.com>
Date:   Tue Dec 8 22:14:47 2020 +0000

    gen v1.20.0

* bump version to 4.0

* support empty spec

* fix version converter for generator

* add generated header

* fix warning

* rerun generator
2020-12-13 19:55:27 -08:00

161 lines
6.9 KiB
C#

#if !NETSTANDARD2_1
/*
* These tests are only for the netstandard version of the client (there are separate tests for netcoreapp that connect to a local test-hosted server).
*/
using k8s.Tests.Mock;
using Microsoft.Rest;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace k8s.Tests
{
public class KubernetesExecTests
{
/// <summary>
/// Tests the <see cref="Kubernetes.WebSocketNamespacedPodExecWithHttpMessagesAsync(string, string, string, string, bool, bool, bool, bool, Dictionary{string, List{string}}, CancellationToken)"/>
/// method. Changes the <see cref="WebSocketBuilder"/> used by the client with a mock builder, so this test never hits the network.
/// </summary>
/// <returns>
/// A <see cref="Task"/> which represents the asynchronous test.
/// </returns>
[Fact]
public async Task WebSocketNamespacedPodExecAsync()
{
var credentials = new BasicAuthenticationCredentials()
{
UserName = "my-user",
Password = "my-secret-password",
};
Kubernetes client = new Kubernetes(credentials);
client.BaseUri = new Uri("http://localhost");
MockWebSocketBuilder mockWebSocketBuilder = new MockWebSocketBuilder();
client.CreateWebSocketBuilder = () => mockWebSocketBuilder;
var webSocket = await client.WebSocketNamespacedPodExecAsync(
name: "mypod",
@namespace: "mynamespace",
command: new string[] { "/bin/bash", "-c", $"echo Hello, World\nexit 0\n" },
container: "mycontainer",
stderr: true,
stdin: true,
stdout: true,
tty: true,
customHeaders: new Dictionary<string, List<string>>()
{
{ "X-My-Header", new List<string>() { "myHeaderValue", "myHeaderValue2" } },
},
cancellationToken: CancellationToken.None).ConfigureAwait(false);
var expectedHeaders = new Dictionary<string, string>()
{
{ "X-My-Header", "myHeaderValue myHeaderValue2" },
{ "Authorization", "Basic bXktdXNlcjpteS1zZWNyZXQtcGFzc3dvcmQ=" },
};
Assert.Equal(
mockWebSocketBuilder.PublicWebSocket,
webSocket); // Did the method return the correct web socket?
Assert.Equal(
new Uri(
"ws://localhost/api/v1/namespaces/mynamespace/pods/mypod/exec?command=%2Fbin%2Fbash&command=-c&command=echo%20Hello%2C%20World%0Aexit%200%0A&container=mycontainer&stderr=1&stdin=1&stdout=1&tty=1"),
mockWebSocketBuilder.Uri); // Did we connect to the correct URL?
Assert.Empty(mockWebSocketBuilder.Certificates); // No certificates were used in this test
Assert.Equal(expectedHeaders, mockWebSocketBuilder.RequestHeaders); // Did we use the expected headers
}
[Fact]
public async Task WebSocketNamespacedPodPortForwardAsync()
{
var credentials = new BasicAuthenticationCredentials()
{
UserName = "my-user",
Password = "my-secret-password",
};
Kubernetes client = new Kubernetes(credentials);
client.BaseUri = new Uri("http://localhost");
MockWebSocketBuilder mockWebSocketBuilder = new MockWebSocketBuilder();
client.CreateWebSocketBuilder = () => mockWebSocketBuilder;
var webSocket = await client.WebSocketNamespacedPodPortForwardAsync(
name: "mypod",
@namespace: "mynamespace",
ports: new int[] { 80, 8080 },
customHeaders: new Dictionary<string, List<string>>()
{
{ "X-My-Header", new List<string>() { "myHeaderValue", "myHeaderValue2" } },
},
cancellationToken: CancellationToken.None).ConfigureAwait(false);
var expectedHeaders = new Dictionary<string, string>()
{
{ "X-My-Header", "myHeaderValue myHeaderValue2" },
{ "Authorization", "Basic bXktdXNlcjpteS1zZWNyZXQtcGFzc3dvcmQ=" },
};
Assert.Equal(
mockWebSocketBuilder.PublicWebSocket,
webSocket); // Did the method return the correct web socket?
Assert.Equal(
new Uri("ws://localhost/api/v1/namespaces/mynamespace/pods/mypod/portforward?ports=80&ports=8080"),
mockWebSocketBuilder.Uri); // Did we connect to the correct URL?
Assert.Empty(mockWebSocketBuilder.Certificates); // No certificates were used in this test
Assert.Equal(expectedHeaders, mockWebSocketBuilder.RequestHeaders); // Did we use the expected headers
}
[Fact]
public async Task WebSocketNamespacedPodAttachAsync()
{
var credentials = new BasicAuthenticationCredentials()
{
UserName = "my-user",
Password = "my-secret-password",
};
Kubernetes client = new Kubernetes(credentials);
client.BaseUri = new Uri("http://localhost");
MockWebSocketBuilder mockWebSocketBuilder = new MockWebSocketBuilder();
client.CreateWebSocketBuilder = () => mockWebSocketBuilder;
var webSocket = await client.WebSocketNamespacedPodAttachAsync(
name: "mypod",
@namespace: "mynamespace",
container: "my-container",
stderr: true,
stdin: true,
stdout: true,
tty: true,
customHeaders: new Dictionary<string, List<string>>()
{
{ "X-My-Header", new List<string>() { "myHeaderValue", "myHeaderValue2" } },
},
cancellationToken: CancellationToken.None).ConfigureAwait(false);
var expectedHeaders = new Dictionary<string, string>()
{
{ "X-My-Header", "myHeaderValue myHeaderValue2" },
{ "Authorization", "Basic bXktdXNlcjpteS1zZWNyZXQtcGFzc3dvcmQ=" },
};
Assert.Equal(
mockWebSocketBuilder.PublicWebSocket,
webSocket); // Did the method return the correct web socket?
Assert.Equal(
new Uri(
"ws://localhost:80/api/v1/namespaces/mynamespace/pods/mypod/attach?stderr=1&stdin=1&stdout=1&tty=1&container=my-container"),
mockWebSocketBuilder.Uri); // Did we connect to the correct URL?
Assert.Empty(mockWebSocketBuilder.Certificates); // No certificates were used in this test
Assert.Equal(expectedHeaders, mockWebSocketBuilder.RequestHeaders); // Did we use the expected headers
}
}
}
#endif