Skip to content

Commit de9edb1

Browse files
committed
Added sample kubeconfig files to testing project
1 parent 799a6bf commit de9edb1

File tree

3 files changed

+100
-13
lines changed

3 files changed

+100
-13
lines changed

src/KubernetesClient.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<OutputType>Library</OutputType>
44
<TargetFramework>netcoreapp1.1</TargetFramework>
55
</PropertyGroup>
6-
<ItemGroup>
7-
<Compile Remove="GlobalSuppressions.cs" />
6+
<ItemGroup>
7+
<Compile Remove="GlobalSuppressions.cs" />
88
</ItemGroup>
99
<ItemGroup>
1010
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="1.1.0" />

src/KubernetesClientConfiguration.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,14 @@ private void Initialize(K8SConfiguration k8SConfig, string currentContext = null
9696
{
9797
Context activeContext;
9898

99+
if (k8SConfig.Contexts == null)
100+
{
101+
throw new KubeConfigException("No contexts found in kubeconfig");
102+
}
103+
99104
// set the currentCOntext to passed context if not null
100105
if (!string.IsNullOrWhiteSpace(currentContext))
101106
{
102-
if (k8SConfig.Contexts == null)
103-
{
104-
throw new KubeConfigException("No contexts found in kubeconfig");
105-
}
106107

107108
activeContext = k8SConfig.Contexts.FirstOrDefault(c => c.Name.Equals(currentContext, StringComparison.OrdinalIgnoreCase));
108109
if (activeContext != null)
Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
using System;
22
using Xunit;
33
using k8s;
4+
using System.IO;
45

56
namespace k8s.Tests
67
{
78
public class KubernetesClientConfigurationTests
89
{
10+
11+
/// <summary>
12+
/// This file contains a sample kubeconfig file
13+
/// </summary>
14+
private static readonly string kubeConfigFileName = "assets/kubeconfig.yml";
15+
16+
private static readonly string kubeConfigNoContexts = "assets/kubeconfig-no-context.yml";
17+
918
/// <summary>
1019
/// Checks Host is loaded from the default configuration file
1120
/// </summary>
@@ -15,18 +24,95 @@ public void DefaultConfigurationLoaded()
1524
var cfg = new KubernetesClientConfiguration();
1625
Assert.NotNull(cfg.Host);
1726
}
27+
28+
/// <summary>
29+
/// Check if host is properly loaded, per context
30+
/// </summary>
31+
[Theory]
32+
[InlineData("federal-context", "https://horse.org:4443")]
33+
[InlineData("queen-anne-context", "https://pig.org:443")]
34+
public void ContextHostTest(string context, string host)
35+
{
36+
var fi = new FileInfo(kubeConfigFileName);
37+
var cfg = new KubernetesClientConfiguration(fi, context);
38+
Assert.Equal(host, cfg.Host);
39+
}
1840

1941
/// <summary>
20-
/// Checks if the are pods
42+
/// Checks if user-based token is loaded properly from the config file, per context
43+
/// </summary>
44+
/// <param name="context"></param>
45+
/// <param name="username"></param>
46+
/// <param name="token"></param>
47+
[Theory]
48+
[InlineData("queen-anne-context", "black-user" ,"black-token")]
49+
public void ContextUserTokenTest(string context, string username, string token)
50+
{
51+
var fi = new FileInfo(kubeConfigFileName);
52+
var cfg = new KubernetesClientConfiguration(fi, context);
53+
Assert.Equal(context, cfg.CurrentContext);
54+
Assert.Equal(username, cfg.Username);
55+
Assert.Equal(token, cfg.AccessToken);
56+
}
57+
58+
/// <summary>
59+
/// Checks if certificate-based authentication is loaded properly from the config file, per context
60+
/// </summary>
61+
/// <param name="context">Context to retreive the configuration</param>
62+
/// <param name="clientCertData">'client-certificate-data' node content</param>
63+
/// <param name="clientCertKey">'client-key-data' content</param>
64+
[Theory]
65+
[InlineData("federal-context", "path/to/my/client/cert" ,"path/to/my/client/key")]
66+
public void ContextCertificateTest(string context, string clientCertData, string clientCertKey)
67+
{
68+
var fi = new FileInfo(kubeConfigFileName);
69+
var cfg = new KubernetesClientConfiguration(fi, context);
70+
Assert.Equal(context, cfg.CurrentContext);
71+
Assert.Equal(cfg.ClientCertificateData, clientCertData);
72+
Assert.Equal(cfg.ClientCertificateKey, clientCertKey);
73+
}
74+
75+
/// <summary>
76+
/// Test that an Exception is thrown when initializating a KubernetClientConfiguration whose config file Context is not present
2177
/// </summary>
2278
[Fact]
23-
public void ListDefaultNamespacedPod()
79+
public void ContextNotFoundTest()
2480
{
25-
var k8sClientConfig = new KubernetesClientConfiguration();
26-
IKubernetes client = new Kubernetes(k8sClientConfig);
27-
var listTask = client.ListNamespacedPodWithHttpMessagesAsync("default").Result;
28-
var list = listTask.Body;
29-
Assert.NotEqual(0, list.Items.Count);
81+
var fi = new FileInfo(kubeConfigFileName);
82+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi, "context-not-found"));
3083
}
84+
85+
/// <summary>
86+
/// Test if KubeConfigException is thrown when no Contexts and we use the default context name
87+
/// </summary>
88+
[Fact]
89+
public void NoContexts()
90+
{
91+
var fi = new FileInfo(kubeConfigNoContexts);
92+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
93+
}
94+
95+
/// <summary>
96+
/// Test if KubeConfigException is thrown when no Contexts are set and we specify a concrete context name
97+
/// </summary>
98+
[Fact]
99+
public void NoContextsExplicit()
100+
{
101+
var fi = new FileInfo(kubeConfigNoContexts);
102+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi, "context"));
103+
}
104+
105+
// /// <summary>
106+
// /// Checks if the are pods
107+
// /// </summary>
108+
// [Fact]
109+
// public void ListDefaultNamespacedPod()
110+
// {
111+
// var k8sClientConfig = new KubernetesClientConfiguration();
112+
// IKubernetes client = new Kubernetes(k8sClientConfig);
113+
// var listTask = client.ListNamespacedPodWithHttpMessagesAsync("default").Result;
114+
// var list = listTask.Body;
115+
// Assert.NotEqual(0, list.Items.Count);
116+
// }
31117
}
32118
}

0 commit comments

Comments
 (0)