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
+ /// <summary>
17
+ /// Invalid test file with no context on purpose
18
+ /// </summary>
19
+ private static readonly string kubeConfigNoContexts = "assets/kubeconfig-no-context.yml" ;
20
+
21
+ /// <summary>
22
+ /// Sample configuration file with user/password authentication
23
+ /// </summary>
24
+ private static readonly string kubeConfigUserPassword = "assets/kubeconfig.user-pass.yml" ;
25
+
26
+ /// <summary>
27
+ /// Sample configuration file with incorrect user credentials structures on purpose
28
+ /// </summary>
29
+ private static readonly string kubeConfigNoCredentials = "assets/kubeconfig.no-credentials.yml" ;
30
+
31
+ /// <summary>
32
+ /// Sample configuration file with incorrect cluster/server structure on purpose
33
+ /// </summary>
34
+ private static readonly string kubeConfigNoServer = "assets/kubeconfig.no-server.yml" ;
35
+
36
+ /// <summary>
37
+ /// Sample configuration file with incorrect cluster/server structure on purpose
38
+ /// </summary>
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" ;
55
+
56
+ /// <summary>
57
+ /// The configuration file is not present. An KubeConfigException should be thrown
58
+ /// </summary>
59
+ [ Fact ]
60
+ public void ConfigurationFileNotFound ( )
61
+ {
62
+ var fi = new FileInfo ( "/path/to/nowhere" ) ;
63
+ Assert . Throws < k8s . Exceptions . KubeConfigException > ( ( ) => new KubernetesClientConfiguration ( fi ) ) ;
64
+ }
65
+
9
66
/// <summary>
10
67
/// Checks Host is loaded from the default configuration file
11
68
/// </summary>
@@ -15,18 +72,170 @@ public void DefaultConfigurationLoaded()
15
72
var cfg = new KubernetesClientConfiguration ( ) ;
16
73
Assert . NotNull ( cfg . Host ) ;
17
74
}
75
+
76
+ /// <summary>
77
+ /// Check if host is properly loaded, per context
78
+ /// </summary>
79
+ [ Theory ]
80
+ [ InlineData ( "federal-context" , "https://horse.org:4443" ) ]
81
+ [ InlineData ( "queen-anne-context" , "https://pig.org:443" ) ]
82
+ public void ContextHostTest ( string context , string host )
83
+ {
84
+ var fi = new FileInfo ( kubeConfigFileName ) ;
85
+ var cfg = new KubernetesClientConfiguration ( fi , context ) ;
86
+ Assert . Equal ( host , cfg . Host ) ;
87
+ }
88
+
89
+ /// <summary>
90
+ /// Checks if user-based token is loaded properly from the config file, per context
91
+ /// </summary>
92
+ /// <param name="context"></param>
93
+ /// <param name="username"></param>
94
+ /// <param name="token"></param>
95
+ [ Theory ]
96
+ [ InlineData ( "queen-anne-context" , "black-token" ) ]
97
+ public void ContextUserTokenTest ( string context , string token )
98
+ {
99
+ var fi = new FileInfo ( kubeConfigFileName ) ;
100
+ var cfg = new KubernetesClientConfiguration ( fi , context ) ;
101
+ Assert . Equal ( context , cfg . CurrentContext ) ;
102
+ Assert . Null ( cfg . Username ) ;
103
+ Assert . Equal ( token , cfg . AccessToken ) ;
104
+ }
105
+
106
+ /// <summary>
107
+ /// Checks if certificate-based authentication is loaded properly from the config file, per context
108
+ /// </summary>
109
+ /// <param name="context">Context to retreive the configuration</param>
110
+ /// <param name="clientCertData">'client-certificate-data' node content</param>
111
+ /// <param name="clientCertKey">'client-key-data' content</param>
112
+ [ Theory ]
113
+ [ InlineData ( "federal-context" , "path/to/my/client/cert" , "path/to/my/client/key" ) ]
114
+ public void ContextCertificateTest ( string context , string clientCertData , string clientCertKey )
115
+ {
116
+ var fi = new FileInfo ( kubeConfigFileName ) ;
117
+ var cfg = new KubernetesClientConfiguration ( fi , context ) ;
118
+ Assert . Equal ( context , cfg . CurrentContext ) ;
119
+ Assert . Equal ( cfg . ClientCertificateData , clientCertData ) ;
120
+ Assert . Equal ( cfg . ClientCertificateKey , clientCertKey ) ;
121
+ }
122
+
123
+ /// <summary>
124
+ /// Test that an Exception is thrown when initializating a KubernetClientConfiguration whose config file Context is not present
125
+ /// </summary>
126
+ [ Fact ]
127
+ public void ContextNotFoundTest ( )
128
+ {
129
+ var fi = new FileInfo ( kubeConfigFileName ) ;
130
+ Assert . Throws < k8s . Exceptions . KubeConfigException > ( ( ) => new KubernetesClientConfiguration ( fi , "context-not-found" ) ) ;
131
+ }
18
132
19
133
/// <summary>
20
- /// Checks if the are pods
134
+ /// Test if KubeConfigException is thrown when no Contexts and we use the default context name
21
135
/// </summary>
22
136
[ Fact ]
23
- public void ListDefaultNamespacedPod ( )
137
+ public void NoContexts ( )
24
138
{
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 ) ;
139
+ var fi = new FileInfo ( kubeConfigNoContexts ) ;
140
+ Assert . Throws < k8s . Exceptions . KubeConfigException > ( ( ) => new KubernetesClientConfiguration ( fi ) ) ;
141
+ }
142
+
143
+ /// <summary>
144
+ /// Test if KubeConfigException is thrown when no Contexts are set and we specify a concrete context name
145
+ /// </summary>
146
+ [ Fact ]
147
+ public void NoContextsExplicit ( )
148
+ {
149
+ var fi = new FileInfo ( kubeConfigNoContexts ) ;
150
+ Assert . Throws < k8s . Exceptions . KubeConfigException > ( ( ) => new KubernetesClientConfiguration ( fi , "context" ) ) ;
30
151
}
152
+
153
+ /// <summary>
154
+ /// Checks user/password authentication information is read properly
155
+ /// </summary>
156
+ [ Fact ]
157
+ public void UserPasswordAuthentication ( )
158
+ {
159
+ var fi = new FileInfo ( kubeConfigUserPassword ) ;
160
+ var cfg = new KubernetesClientConfiguration ( fi ) ;
161
+ Assert . Equal ( "admin" , cfg . Username ) ;
162
+ Assert . Equal ( "secret" , cfg . Password ) ;
163
+ }
164
+
165
+ /// <summary>
166
+ /// Checks that a KubeConfigException is thrown when incomplete user credentials
167
+ /// </summary>
168
+ [ Fact ]
169
+ public void IncompleteUserCredentials ( )
170
+ {
171
+ var fi = new FileInfo ( kubeConfigNoCredentials ) ;
172
+ Assert . Throws < k8s . Exceptions . KubeConfigException > ( ( ) => new KubernetesClientConfiguration ( fi ) ) ;
173
+ }
174
+
175
+ /// <summary>
176
+ /// Checks that a KubeConfigException is thrown when the server property is not set in cluster
177
+ /// </summary>
178
+ [ Fact ]
179
+ public void ServerNotFound ( )
180
+ {
181
+ var fi = new FileInfo ( kubeConfigNoServer ) ;
182
+ Assert . Throws < k8s . Exceptions . KubeConfigException > ( ( ) => new KubernetesClientConfiguration ( fi ) ) ;
183
+ }
184
+
185
+ /// <summary>
186
+ /// Checks that a KubeConfigException is thrown when the clusters section is missing
187
+ /// </summary>
188
+ [ Fact ]
189
+ public void ClusterNotFound ( )
190
+ {
191
+ var fi = new FileInfo ( kubeConfigNoCluster ) ;
192
+ 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 ) ;
226
+ }
227
+
228
+ // /// <summary>
229
+ // /// Checks if the are pods
230
+ // /// </summary>
231
+ // [Fact]
232
+ // public void ListDefaultNamespacedPod()
233
+ // {
234
+ // var k8sClientConfig = new KubernetesClientConfiguration();
235
+ // IKubernetes client = new Kubernetes(k8sClientConfig);
236
+ // var listTask = client.ListNamespacedPodWithHttpMessagesAsync("default").Result;
237
+ // var list = listTask.Body;
238
+ // Assert.NotEqual(0, list.Items.Count);
239
+ // }
31
240
}
32
241
}
0 commit comments