Skip to content

Commit 1571aa4

Browse files
committed
Added new tests to check cluster and tls correctness
1 parent c38ee42 commit 1571aa4

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

tests/KubernetesClientConfigurationTests.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,22 @@ public class KubernetesClientConfigurationTests
3636
/// <summary>
3737
/// Sample configuration file with incorrect cluster/server structure on purpose
3838
/// </summary>
39-
private static readonly string kubeConfigNoCluster = "assets/kubeconfig.no-cluster.yml";
39+
private static readonly string kubeConfigNoCluster = "assets/kubeconfig.no-cluster.yml";
40+
41+
/// <summary>
42+
/// Sample configuration file with incorrect match in cluster name
43+
/// </summary>
44+
private static readonly string kubeConfigClusterMissmatch = "assets/kubeconfig.cluster-missmatch.yml";
45+
46+
/// <summary>
47+
/// Sample configuration file with incorrect TLS configuration in cluster section
48+
/// </summary>
49+
private static readonly string kubeConfigTlsNoSkipError = "assets/kubeconfig.tls-no-skip-error.yml";
50+
51+
/// <summary>
52+
/// Sample configuration file with incorrect TLS configuration in cluster section
53+
/// </summary>
54+
private static readonly string kubeConfigTlsSkip = "assets/kubeconfig.tls-skip.yml";
4055

4156
/// <summary>
4257
/// The configuration file is not present. An KubeConfigException should be thrown
@@ -175,6 +190,39 @@ public void ClusterNotFound()
175190
{
176191
var fi = new FileInfo(kubeConfigNoCluster);
177192
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
193+
}
194+
195+
/// <summary>
196+
/// Checks that a KubeConfigException is thrown when the cluster defined in clusters and contexts do not match
197+
/// </summary>
198+
[Fact]
199+
public void ClusterNameMissmatch()
200+
{
201+
var fi = new FileInfo(kubeConfigClusterMissmatch);
202+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
203+
}
204+
205+
/// <summary>
206+
/// Checks that a KubeConfigException is thrown when no certificate-authority-data is set and user do not require tls skip
207+
/// </summary>
208+
[Fact]
209+
public void CheckClusterTlsCorrectness()
210+
{
211+
var fi = new FileInfo(kubeConfigTlsNoSkipError);
212+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
213+
}
214+
215+
/// <summary>
216+
/// Checks that a KubeConfigException is thrown when no certificate-authority-data is set and user do not require tls skip
217+
/// </summary>
218+
[Fact]
219+
public void CheckClusterTlsSkipCorrectness()
220+
{
221+
var fi = new FileInfo(kubeConfigTlsSkip);
222+
var cfg = new KubernetesClientConfiguration(fi);
223+
Assert.NotNull(cfg.Host);
224+
Assert.Null(cfg.SslCaCert);
225+
Assert.True(cfg.SkipTlsVerify);
178226
}
179227

180228
// /// <summary>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
2+
# WARNING: File includes minor fixes
3+
---
4+
current-context: federal-context
5+
apiVersion: v1
6+
clusters:
7+
- cluster:
8+
certificate-authority-data: path/to/my/cafile
9+
server: https://horse.org:4443
10+
name: bad-name-cluster
11+
contexts:
12+
- context:
13+
cluster: horse-cluster
14+
namespace: chisel-ns
15+
user: green-user
16+
name: federal-context
17+
kind: Config
18+
users:
19+
- name: green-user
20+
user:
21+
password: secret
22+
username: admin
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
2+
# WARNING: File includes minor fixes
3+
---
4+
current-context: federal-context
5+
apiVersion: v1
6+
clusters:
7+
- cluster:
8+
server: http://cow.org:8080
9+
name: cow-cluster
10+
- cluster:
11+
# certificate-authority-data: path/to/my/cafile
12+
server: https://horse.org:4443
13+
name: horse-cluster
14+
contexts:
15+
- context:
16+
cluster: horse-cluster
17+
namespace: chisel-ns
18+
user: green-user
19+
name: federal-context
20+
kind: Config
21+
users:
22+
- name: green-user
23+
user:
24+
client-certificate-data: path/to/my/client/cert
25+
client-key-data: path/to/my/client/key

tests/assets/kubeconfig.tls-skip.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
2+
# WARNING: File includes minor fixes
3+
---
4+
current-context: federal-context
5+
apiVersion: v1
6+
clusters:
7+
- cluster:
8+
insecure-skip-tls-verify: true
9+
server: https://horse.org:443
10+
name: horse-cluster
11+
contexts:
12+
- context:
13+
cluster: horse-cluster
14+
namespace: chisel-ns
15+
user: green-user
16+
name: federal-context
17+
kind: Config
18+
users:
19+
- name: green-user
20+
user:
21+
password: secret
22+
username: admin

0 commit comments

Comments
 (0)