Merge pull request #6 from sesispla/master

Basic tests for configuration and credentials
This commit is contained in:
Brendan Burns
2017-06-28 20:37:45 -07:00
committed by GitHub
16 changed files with 512 additions and 16 deletions

View File

@@ -6,7 +6,7 @@
public class User
{
[YamlMember(Alias = "user")]
public UserCrednetials UserCredentials { get; set; }
public UserCredentials UserCredentials { get; set; }
[YamlMember(Alias = "name")]
public string Name { get; set; }

View File

@@ -3,7 +3,7 @@
using YamlDotNet.RepresentationModel;
using YamlDotNet.Serialization;
public class UserCrednetials
public class UserCredentials
{
[YamlMember(Alias = "client-certificate-data")]
public string ClientCertificateData { get; set; }
@@ -14,7 +14,7 @@
[YamlMember(Alias = "token")]
public string Token { get; set; }
[YamlMember(Alias = "userName")]
[YamlMember(Alias = "username")]
public string UserName { get; set; }
[YamlMember(Alias = "password")]

View File

@@ -3,8 +3,8 @@
<OutputType>Library</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Remove="GlobalSuppressions.cs" />
<ItemGroup>
<Compile Remove="GlobalSuppressions.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="1.1.0" />

View File

@@ -96,13 +96,14 @@ namespace k8s
{
Context activeContext;
if (k8SConfig.Contexts == null)
{
throw new KubeConfigException("No contexts found in kubeconfig");
}
// set the currentCOntext to passed context if not null
if (!string.IsNullOrWhiteSpace(currentContext))
{
if (k8SConfig.Contexts == null)
{
throw new KubeConfigException("No contexts found in kubeconfig");
}
activeContext = k8SConfig.Contexts.FirstOrDefault(c => c.Name.Equals(currentContext, StringComparison.OrdinalIgnoreCase));
if (activeContext != null)
@@ -127,6 +128,11 @@ namespace k8s
this.CurrentContext = activeContext.Name;
}
if (k8SConfig.Clusters == null)
{
throw new KubeConfigException($"clusters not found for current-context :{activeContext} in kubeconfig");
}
var clusterDetails = k8SConfig.Clusters.FirstOrDefault(c => c.Name.Equals(activeContext.ContextDetails.Cluster, StringComparison.OrdinalIgnoreCase));
if (clusterDetails?.ClusterEndpoint != null)
{

View File

@@ -1,11 +1,68 @@
using System;
using Xunit;
using k8s;
using System.IO;
namespace k8s.Tests
{
public class KubernetesClientConfigurationTests
{
/// <summary>
/// This file contains a sample kubeconfig file
/// </summary>
private static readonly string kubeConfigFileName = "assets/kubeconfig.yml";
/// <summary>
/// Invalid test file with no context on purpose
/// </summary>
private static readonly string kubeConfigNoContexts = "assets/kubeconfig-no-context.yml";
/// <summary>
/// Sample configuration file with user/password authentication
/// </summary>
private static readonly string kubeConfigUserPassword = "assets/kubeconfig.user-pass.yml";
/// <summary>
/// Sample configuration file with incorrect user credentials structures on purpose
/// </summary>
private static readonly string kubeConfigNoCredentials = "assets/kubeconfig.no-credentials.yml";
/// <summary>
/// Sample configuration file with incorrect cluster/server structure on purpose
/// </summary>
private static readonly string kubeConfigNoServer = "assets/kubeconfig.no-server.yml";
/// <summary>
/// Sample configuration file with incorrect cluster/server structure on purpose
/// </summary>
private static readonly string kubeConfigNoCluster = "assets/kubeconfig.no-cluster.yml";
/// <summary>
/// Sample configuration file with incorrect match in cluster name
/// </summary>
private static readonly string kubeConfigClusterMissmatch = "assets/kubeconfig.cluster-missmatch.yml";
/// <summary>
/// Sample configuration file with incorrect TLS configuration in cluster section
/// </summary>
private static readonly string kubeConfigTlsNoSkipError = "assets/kubeconfig.tls-no-skip-error.yml";
/// <summary>
/// Sample configuration file with incorrect TLS configuration in cluster section
/// </summary>
private static readonly string kubeConfigTlsSkip = "assets/kubeconfig.tls-skip.yml";
/// <summary>
/// The configuration file is not present. An KubeConfigException should be thrown
/// </summary>
[Fact]
public void ConfigurationFileNotFound()
{
var fi = new FileInfo("/path/to/nowhere");
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
}
/// <summary>
/// Checks Host is loaded from the default configuration file
/// </summary>
@@ -15,18 +72,170 @@ namespace k8s.Tests
var cfg = new KubernetesClientConfiguration();
Assert.NotNull(cfg.Host);
}
/// <summary>
/// Check if host is properly loaded, per context
/// </summary>
[Theory]
[InlineData("federal-context", "https://horse.org:4443")]
[InlineData("queen-anne-context", "https://pig.org:443")]
public void ContextHostTest(string context, string host)
{
var fi = new FileInfo(kubeConfigFileName);
var cfg = new KubernetesClientConfiguration(fi, context);
Assert.Equal(host, cfg.Host);
}
/// <summary>
/// Checks if the are pods
/// Checks if user-based token is loaded properly from the config file, per context
/// </summary>
/// <param name="context"></param>
/// <param name="username"></param>
/// <param name="token"></param>
[Theory]
[InlineData("queen-anne-context", "black-token")]
public void ContextUserTokenTest(string context, string token)
{
var fi = new FileInfo(kubeConfigFileName);
var cfg = new KubernetesClientConfiguration(fi, context);
Assert.Equal(context, cfg.CurrentContext);
Assert.Null(cfg.Username);
Assert.Equal(token, cfg.AccessToken);
}
/// <summary>
/// Checks if certificate-based authentication is loaded properly from the config file, per context
/// </summary>
/// <param name="context">Context to retreive the configuration</param>
/// <param name="clientCertData">'client-certificate-data' node content</param>
/// <param name="clientCertKey">'client-key-data' content</param>
[Theory]
[InlineData("federal-context", "path/to/my/client/cert" ,"path/to/my/client/key")]
public void ContextCertificateTest(string context, string clientCertData, string clientCertKey)
{
var fi = new FileInfo(kubeConfigFileName);
var cfg = new KubernetesClientConfiguration(fi, context);
Assert.Equal(context, cfg.CurrentContext);
Assert.Equal(cfg.ClientCertificateData, clientCertData);
Assert.Equal(cfg.ClientCertificateKey, clientCertKey);
}
/// <summary>
/// Test that an Exception is thrown when initializating a KubernetClientConfiguration whose config file Context is not present
/// </summary>
[Fact]
public void ListDefaultNamespacedPod()
public void ContextNotFoundTest()
{
var k8sClientConfig = new KubernetesClientConfiguration();
IKubernetes client = new Kubernetes(k8sClientConfig);
var listTask = client.ListNamespacedPodWithHttpMessagesAsync("default").Result;
var list = listTask.Body;
Assert.NotEqual(0, list.Items.Count);
var fi = new FileInfo(kubeConfigFileName);
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi, "context-not-found"));
}
/// <summary>
/// Test if KubeConfigException is thrown when no Contexts and we use the default context name
/// </summary>
[Fact]
public void NoContexts()
{
var fi = new FileInfo(kubeConfigNoContexts);
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
}
/// <summary>
/// Test if KubeConfigException is thrown when no Contexts are set and we specify a concrete context name
/// </summary>
[Fact]
public void NoContextsExplicit()
{
var fi = new FileInfo(kubeConfigNoContexts);
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi, "context"));
}
/// <summary>
/// Checks user/password authentication information is read properly
/// </summary>
[Fact]
public void UserPasswordAuthentication()
{
var fi = new FileInfo(kubeConfigUserPassword);
var cfg = new KubernetesClientConfiguration(fi);
Assert.Equal("admin", cfg.Username);
Assert.Equal("secret", cfg.Password);
}
/// <summary>
/// Checks that a KubeConfigException is thrown when incomplete user credentials
/// </summary>
[Fact]
public void IncompleteUserCredentials()
{
var fi = new FileInfo(kubeConfigNoCredentials);
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
}
/// <summary>
/// Checks that a KubeConfigException is thrown when the server property is not set in cluster
/// </summary>
[Fact]
public void ServerNotFound()
{
var fi = new FileInfo(kubeConfigNoServer);
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
}
/// <summary>
/// Checks that a KubeConfigException is thrown when the clusters section is missing
/// </summary>
[Fact]
public void ClusterNotFound()
{
var fi = new FileInfo(kubeConfigNoCluster);
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
}
/// <summary>
/// Checks that a KubeConfigException is thrown when the cluster defined in clusters and contexts do not match
/// </summary>
[Fact]
public void ClusterNameMissmatch()
{
var fi = new FileInfo(kubeConfigClusterMissmatch);
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
}
/// <summary>
/// Checks that a KubeConfigException is thrown when no certificate-authority-data is set and user do not require tls skip
/// </summary>
[Fact]
public void CheckClusterTlsCorrectness()
{
var fi = new FileInfo(kubeConfigTlsNoSkipError);
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
}
/// <summary>
/// Checks that a KubeConfigException is thrown when no certificate-authority-data is set and user do not require tls skip
/// </summary>
[Fact]
public void CheckClusterTlsSkipCorrectness()
{
var fi = new FileInfo(kubeConfigTlsSkip);
var cfg = new KubernetesClientConfiguration(fi);
Assert.NotNull(cfg.Host);
Assert.Null(cfg.SslCaCert);
Assert.True(cfg.SkipTlsVerify);
}
// /// <summary>
// /// Checks if the are pods
// /// </summary>
// [Fact]
// public void ListDefaultNamespacedPod()
// {
// var k8sClientConfig = new KubernetesClientConfiguration();
// IKubernetes client = new Kubernetes(k8sClientConfig);
// var listTask = client.ListNamespacedPodWithHttpMessagesAsync("default").Result;
// var list = listTask.Body;
// Assert.NotEqual(0, list.Items.Count);
// }
}
}

View File

@@ -0,0 +1,60 @@
using System;
using Xunit;
using k8s;
using System.IO;
namespace k8s.Tests
{
public class KubernetesClientCredentialsTests
{
/// <summary>
/// Checks that a ArgumentNullException is thrown when trying to create a KubernetesClientCredentials with null token
/// </summary>
[Fact]
public void TokenNull()
{
Assert.Throws<ArgumentNullException>(() => new KubernetesClientCredentials(null));
}
/// <summary>
/// Checks that a ArgumentNullException is thrown when trying to create a KubernetesClientCredentials with null username
/// </summary>
[Fact]
public void UsernameNull()
{
Assert.Throws<ArgumentNullException>(() => new KubernetesClientCredentials(null,"password"));
}
/// <summary>
/// Checks that a ArgumentNullException is thrown when trying to create a KubernetesClientCredentials with null password
/// </summary>
[Fact]
public void PasswordNull()
{
Assert.Throws<ArgumentNullException>(() => new KubernetesClientCredentials("username", null));
}
/// <summary>
/// Checks that the Token is set with no exceptions
/// </summary>
[Fact]
public void ValidTokenIsSet()
{
var token = "mytoken";
var credentials = new KubernetesClientCredentials(token);
Assert.NotNull(credentials);
}
/// <summary>
/// Checks that the Username and Password is set with no exceptions
/// </summary>
[Fact]
public void ValidUserPasswordIsSet()
{
var username = "myuser";
var password = "mypassword";
var credentials = new KubernetesClientCredentials(username, password);
Assert.NotNull(credentials);
}
}
}

View File

@@ -0,0 +1,29 @@
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
# WARNING: File includes minor fixes
---
current-context: federal-context
apiVersion: v1
clusters:
- cluster:
server: http://cow.org:8080
name: cow-cluster
- cluster:
certificate-authority-data: path/to/my/cafile
server: https://horse.org:4443
name: horse-cluster
- cluster:
insecure-skip-tls-verify: true
server: https://pig.org:443
name: pig-cluster
kind: Config
users:
- name: blue-user
user:
token: blue-token
- name: green-user
user:
client-certificate-data: path/to/my/client/cert
client-key-data: path/to/my/client/key
- name: black-user
user:
token: black-token

View File

@@ -0,0 +1,22 @@
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
# WARNING: File includes minor fixes
---
current-context: federal-context
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: path/to/my/cafile
server: https://horse.org:4443
name: bad-name-cluster
contexts:
- context:
cluster: horse-cluster
namespace: chisel-ns
user: green-user
name: federal-context
kind: Config
users:
- name: green-user
user:
password: secret
username: admin

View File

@@ -0,0 +1,17 @@
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
# WARNING: File includes minor fixes
---
current-context: federal-context
apiVersion: v1
contexts:
- context:
cluster: horse-cluster
namespace: chisel-ns
user: green-user
name: federal-context
kind: Config
users:
- name: green-user
user:
password: secret
username: admin

View File

@@ -0,0 +1,20 @@
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
# WARNING: File includes minor fixes
---
current-context: federal-context
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: path/to/my/cafile
server: https://horse.org:4443
name: horse-cluster
contexts:
- context:
cluster: horse-cluster
namespace: chisel-ns
user: green-user
name: federal-context
kind: Config
users:
- name: green-user
user:

View File

@@ -0,0 +1,21 @@
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
# WARNING: File includes minor fixes
---
current-context: federal-context
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: path/to/my/cafile
name: horse-cluster
contexts:
- context:
cluster: horse-cluster
namespace: chisel-ns
user: green-user
name: federal-context
kind: Config
users:
- name: green-user
user:
password: secret
username: admin

View File

@@ -0,0 +1,25 @@
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
# WARNING: File includes minor fixes
---
current-context: federal-context
apiVersion: v1
clusters:
- cluster:
server: http://cow.org:8080
name: cow-cluster
- cluster:
# certificate-authority-data: path/to/my/cafile
server: https://horse.org:4443
name: horse-cluster
contexts:
- context:
cluster: horse-cluster
namespace: chisel-ns
user: green-user
name: federal-context
kind: Config
users:
- name: green-user
user:
client-certificate-data: path/to/my/client/cert
client-key-data: path/to/my/client/key

View File

@@ -0,0 +1,22 @@
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
# WARNING: File includes minor fixes
---
current-context: federal-context
apiVersion: v1
clusters:
- cluster:
insecure-skip-tls-verify: true
server: https://horse.org:443
name: horse-cluster
contexts:
- context:
cluster: horse-cluster
namespace: chisel-ns
user: green-user
name: federal-context
kind: Config
users:
- name: green-user
user:
password: secret
username: admin

View File

@@ -0,0 +1,22 @@
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
# WARNING: File includes minor fixes
---
current-context: federal-context
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: path/to/my/cafile
server: https://horse.org:4443
name: horse-cluster
contexts:
- context:
cluster: horse-cluster
namespace: chisel-ns
user: green-user
name: federal-context
kind: Config
users:
- name: green-user
user:
password: secret
username: admin

View File

@@ -0,0 +1,40 @@
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
# WARNING: File includes minor fixes
---
current-context: federal-context
apiVersion: v1
clusters:
- cluster:
server: http://cow.org:8080
name: cow-cluster
- cluster:
certificate-authority-data: path/to/my/cafile
server: https://horse.org:4443
name: horse-cluster
- cluster:
insecure-skip-tls-verify: true
server: https://pig.org:443
name: pig-cluster
contexts:
- context:
cluster: horse-cluster
namespace: chisel-ns
user: green-user
name: federal-context
- context:
cluster: pig-cluster
namespace: saw-ns
user: black-user
name: queen-anne-context
kind: Config
users:
- name: blue-user
user:
token: blue-token
- name: green-user
user:
client-certificate-data: path/to/my/client/cert
client-key-data: path/to/my/client/key
- name: black-user
user:
token: black-token

View File

@@ -10,6 +10,9 @@
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
<PackageReference Include="YamlDotNet.NetCore" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<None Include="assets/*" CopyToOutputDirectory="Always" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\KubernetesClient.csproj" />
</ItemGroup>