1
1
using System ;
2
2
using Xunit ;
3
3
using k8s ;
4
+ using System . IO ;
4
5
5
6
namespace k8s . Tests
6
7
{
7
8
public class KubernetesClientConfigurationTests
8
9
{
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
+
9
18
/// <summary>
10
19
/// Checks Host is loaded from the default configuration file
11
20
/// </summary>
@@ -15,18 +24,95 @@ public void DefaultConfigurationLoaded()
15
24
var cfg = new KubernetesClientConfiguration ( ) ;
16
25
Assert . NotNull ( cfg . Host ) ;
17
26
}
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
+ }
18
40
19
41
/// <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
21
77
/// </summary>
22
78
[ Fact ]
23
- public void ListDefaultNamespacedPod ( )
79
+ public void ContextNotFoundTest ( )
24
80
{
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" ) ) ;
30
83
}
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
+ // }
31
117
}
32
118
}
0 commit comments