commit dc93612024202e651a9cbe4194c1495c823bff12
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 16:24:33 2020 -0700
fix SA1505
commit dc9fdbc4a4fbce7f4362a24e1ff98be4d27e16a8
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 16:24:02 2020 -0700
add ()
commit 16fb7357fcd7e288a4b8fb201fda2b0aae92e5bc
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 16:21:37 2020 -0700
disable SA1117
commit 544a7e5891e853e2e222f855e5446f3fd79ce2ba
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 16:21:16 2020 -0700
fix SA1508
commit 4e998adf440dda4f13512d1e10f8cb5d5fbc6bd9
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 16:08:28 2020 -0700
allow sa1623
commit baf787255c657a00a6074598c6875e0ab4c9d065
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 16:07:23 2020 -0700
fix SA1413
commit 5ef2ca65de62e6c3cbe513902e3954d78f6dc315
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 16:05:45 2020 -0700
fix SA1413
commit 6cb71f08060b8252a18b01a5788eb2ddcee67c3e
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 06:44:55 2020 -0700
fix throw stack
commit e6ada0b1cb3aa72df5fcaa0b4690aadcbd4bda5a
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 06:44:35 2020 -0700
allow CA2225
commit 2e79edec5843c20b7e8f8e9ec5b61cf95284466a
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 06:35:50 2020 -0700
allow SA1507
commit 108f5a6361f4faa211a8e01f783803295fac0453
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 06:35:31 2020 -0700
force SA1413
commit 20f33b64972bfafeada513ae1a46a030934673fd
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 06:30:58 2020 -0700
force SA1413
commit 6b0de102d68a116e149868731e155bc374f56cc8
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 06:28:33 2020 -0700
fix encoding
commit 4bd8892c2f0e0fa3666e59b0b77f5b23a2e4ca50
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 06:26:00 2020 -0700
fix xunit order
commit e28556b37ecd782df2d740321e782622ecd277ca
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 06:10:20 2020 -0700
fix spacing SA1012 SA1004
commit e8cf4b1e0be951babe04cc3674e17718319b8476
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 06:04:44 2020 -0700
fix SA1211
commit b4164446f7f9d82fb872243e59e3f5c46fbb1f3c
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 06:02:34 2020 -0700
fix attribute related warning
commit 2f17ef45947f6ade36593ede6ba4d27bd1991508
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 05:56:53 2020 -0700
allow ca1801 ca1052 ca1054
commit 49b857f3f1b4a44a809c9186108caab0412c101e
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 05:50:07 2020 -0700
fix SA1001
commit 3389662a32cfc481a3fdf50b6fd651e23aadd9dd
Author: Boshi LIAN <bolian@microsoft.com>
Date: Fri Oct 9 06:24:32 2020 -0700
fix dotnet format
commit f9d55fc925e8a7d2f2b403bd3ae35673068134da
Merge: 8e81532 0d68823
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 05:44:30 2020 -0700
Merge branch 'master' into style_fix0
commit 8e815324040837714efb323580cc5dcd79e58310
Author: Boshi Lian <farmer1992@gmail.com>
Date: Fri Oct 9 05:33:02 2020 -0700
fix remaing build err
commit ecf0152f9e989c4c68274b488d4b3ed6ee88daf9
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 05:24:00 2020 -0700
fix SA1707
commit 462d94794848ebfcd102b56a4344ffc33b50f591
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 05:19:38 2020 -0700
fix underscore naming
commit 5271b113603e469021348523f19555e6be22aebc
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 05:19:12 2020 -0700
allow CA1822
commit 602713ce631026e88d8ff7e8803bb12c2addc3c2
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 04:37:16 2020 -0700
fix CA1822
commit bd4fee4d31c1054eadf6d03aa10f443eee9654c0
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 04:36:36 2020 -0700
fix CA1822
commit 257d461f21ef7df65fbc787d5c42c59a89d0eced
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 04:34:25 2020 -0700
introduce dispose pattern
commit 1d668c7926f877ea196edb67acbfe9bfeddb9e15
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 04:23:09 2020 -0700
allow CA2008
commit e4fa6acaf36b84298c8c2ab125ff8aa9efc097b7
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 04:20:28 2020 -0700
allow CA1827
commit dd931d99fa3a95f936ed566320fffa85efb22838
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 04:14:35 2020 -0700
allow SA1314 CA1825
commit 13b6cf11df439be8020e17bc5d30addc62f90c39
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 04:13:51 2020 -0700
Revert "fix CA1825"
This reverts commit 17e03bcd4e0f129a64e57d54fbe72acb7d1d226b.
commit 368664139c75d61ab5a0c432a7fbbdad956c54cf
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 04:09:52 2020 -0700
move class to single files
commit 0015631805d6bc31e4695881989058bb3955766f
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 04:09:27 2020 -0700
disable CA2000 / TODO
commit 0a1241e84ba1247c8ab4ab8d32bd5d800114420b
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 04:07:23 2020 -0700
allow SA1715
commit 17e03bcd4e0f129a64e57d54fbe72acb7d1d226b
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 04:06:57 2020 -0700
fix CA1825
commit 7baf350ca93cb45e2587d86fb6ab6e4cf665b6da
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 03:42:04 2020 -0700
fix SA1312 SA1306
commit 44ad5934182adfc871215637e9612295bc26e6f2
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 03:30:35 2020 -0700
fix CA2007
commit 325fa2c2d16d541db6e21b791c5170f39f832d43
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 03:25:11 2020 -0700
fix SA1131
commit 8f1f46b065dd7e9b316491676bb0b93ef91d0595
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 03:17:08 2020 -0700
allow SA1119
commit 57c0fe7cc26932cc30b4d7cc75a809746d74d5aa
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 03:14:14 2020 -0700
fix SA1400
commit 0afcbbc09d5ef66fbbd4b291d14e7804a8e5a1d3
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 03:12:18 2020 -0700
fix SA1513
commit 45f2424531d35a2a106e10e788aff1a18d745078
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 03:09:17 2020 -0700
allow ca1720 ca1716 sa1405
commit 3403814130a1bf730c4e275f74e9cf5d03bedb41
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 02:16:37 2020 -0700
fix model oper not contains generated header
commit 11377d916cf8cd3ad9109388aff6cf989ff4b7b0
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 02:14:05 2020 -0700
fix SA1649
commit 92b00051a8c80542a63e1dddbb6eed4e98ad26f9
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 02:11:16 2020 -0700
fix SA1124
commit 901a9dd2426fa316bcc5a3c2fc411e583f0e07df
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 02:09:27 2020 -0700
save 1122
commit a8f17b6bac1f1c115b7ed9ebb70d16697a3e81b7
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 02:09:07 2020 -0700
1507 followup
commit a143184921abb38a09e28a7ef07379003fb19563
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 02:07:38 2020 -0700
fix sa1507
commit 54b56026265cbbbfa6e5b8b4dcfab281ffbfa272
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 02:06:44 2020 -0700
fix sa1513
commit 53a009205c88a1d63d8daf32599bbc6428619638
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 02:05:36 2020 -0700
fix SA1649
commit 26d3e78f61ffc381887baaf5c8b56d92aa0ec563
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 02:01:01 2020 -0700
fix ca1816
commit 1ce5a04ce7a32d901cbece3e18d59e3c068cfd27
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed Oct 7 01:56:43 2020 -0700
readable ruleset
commit dafc55f1c2cdc8466919276291333ba46176161a
Author: Boshi Lian <farmer1992@gmail.com>
Date: Wed May 27 19:13:56 2020 -0700
sync none from guideline
611 lines
24 KiB
C#
Executable File
611 lines
24 KiB
C#
Executable File
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading;
|
|
using k8s.Exceptions;
|
|
using k8s.KubeConfigModels;
|
|
using Xunit;
|
|
|
|
namespace k8s.Tests
|
|
{
|
|
public class KubernetesClientConfigurationTests
|
|
{
|
|
/// <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 ContextHost(string context, string host)
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.yml");
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context, useRelativePaths: false);
|
|
Assert.Equal(host, cfg.Host);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if namespace is properly loaded, per context
|
|
/// </summary>
|
|
[Theory]
|
|
[InlineData("federal-context", "chisel-ns")]
|
|
[InlineData("queen-anne-context", "saw-ns")]
|
|
public void ContextNamespace(string context, string @namespace)
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.yml");
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context, useRelativePaths: false);
|
|
Assert.Equal(@namespace, cfg.Namespace);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks if user-based token is loaded properly from the config file, per context
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <param name="token"></param>
|
|
[Theory]
|
|
[InlineData("queen-anne-context", "black-token")]
|
|
public void ContextUserToken(string context, string token)
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.yml");
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(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="clientCert">'client-certificate-data' node content</param>
|
|
/// <param name="clientCertKey">'client-key-data' content</param>
|
|
[Theory]
|
|
[InlineData("federal-context", "assets/client.crt", "assets/client.key")]
|
|
public void ContextCertificate(string context, string clientCert, string clientCertKey)
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.yml");
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context, useRelativePaths: false);
|
|
Assert.Equal(context, cfg.CurrentContext);
|
|
Assert.Equal(cfg.ClientCertificateFilePath, clientCert);
|
|
Assert.Equal(cfg.ClientKeyFilePath, clientCertKey);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks for loading of elliptical curve keys
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
[Theory]
|
|
[InlineData("elliptic-context")]
|
|
public void ContextEllipticKey(string context)
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.yml");
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context, useRelativePaths: false);
|
|
var pfx = CertUtils.GeneratePfx(cfg);
|
|
Assert.NotNull(pfx);
|
|
}
|
|
|
|
/// <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>
|
|
[Theory]
|
|
[InlineData("victorian-context")]
|
|
public void ClientData(string context)
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.yml");
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context);
|
|
Assert.Equal(context, cfg.CurrentContext);
|
|
Assert.NotNull(cfg.SslCaCerts);
|
|
Assert.Equal(File.ReadAllText("assets/client-certificate-data.txt"), cfg.ClientCertificateData);
|
|
Assert.Equal(File.ReadAllText("assets/client-key-data.txt"), cfg.ClientCertificateKeyData);
|
|
}
|
|
|
|
/// <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("assets/kubeconfig.tls-skip.yml");
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi);
|
|
Assert.NotNull(cfg.Host);
|
|
Assert.Null(cfg.SslCaCerts);
|
|
Assert.True(cfg.SkipTlsVerify);
|
|
}
|
|
|
|
/// <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("assets/kubeconfig.cluster-missmatch.yml");
|
|
Assert.Throws<KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks that a KubeConfigException is thrown when the clusters section is missing
|
|
/// </summary>
|
|
[Fact]
|
|
public void ClusterNotFound()
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.no-cluster.yml");
|
|
Assert.Throws<KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
|
|
}
|
|
|
|
/// <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<KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Test that an Exception is thrown when initializating a KubernetClientConfiguration whose config file Context is not
|
|
/// present
|
|
/// </summary>
|
|
[Fact]
|
|
public void ContextNotFound()
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.yml");
|
|
Assert.Throws<KubeConfigException>(() =>
|
|
KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "context-not-found"));
|
|
}
|
|
|
|
[Fact]
|
|
public void CreatedFromPreLoadedConfig()
|
|
{
|
|
var k8sConfig =
|
|
KubernetesClientConfiguration.LoadKubeConfig(new FileInfo("assets/kubeconfig.yml"),
|
|
useRelativePaths: false);
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigObject(k8sConfig);
|
|
Assert.NotNull(cfg.Host);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks Host is loaded from the default configuration file
|
|
/// </summary>
|
|
[Fact]
|
|
public void DefaultConfigurationLoaded()
|
|
{
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(new FileInfo("assets/kubeconfig.yml"),
|
|
useRelativePaths: false);
|
|
Assert.NotNull(cfg.Host);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks that a KubeConfigException is thrown when incomplete user credentials
|
|
/// </summary>
|
|
[Fact]
|
|
public void IncompleteUserCredentials()
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.no-credentials.yml");
|
|
Assert.Throws<KubeConfigException>(() =>
|
|
KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, useRelativePaths: false));
|
|
}
|
|
|
|
/// <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("assets/kubeconfig.no-context.yml");
|
|
Assert.Throws<KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(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("assets/kubeconfig-no-context.yml");
|
|
Assert.Throws<KubeConfigException>(() =>
|
|
KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "context"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks that a KubeConfigException is thrown when the current context exists but has no details specified
|
|
/// </summary>
|
|
[Fact]
|
|
public void ContextNoDetails()
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.no-context-details.yml");
|
|
Assert.Throws<KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(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("assets/kubeconfig.no-server.yml");
|
|
Assert.Throws<KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks user/password authentication information is read properly
|
|
/// </summary>
|
|
[Fact]
|
|
public void UserPasswordAuthentication()
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.user-pass.yml");
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, useRelativePaths: false);
|
|
Assert.Equal("admin", cfg.Username);
|
|
Assert.Equal("secret", cfg.Password);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks that a KubeConfigException is thrown when user cannot be found in users
|
|
/// </summary>
|
|
[Fact]
|
|
public void UserNotFound()
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.user-not-found.yml");
|
|
Assert.Throws<KubeConfigException>(() =>
|
|
KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, useRelativePaths: false));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Make sure that user is not a necessary field. set #issue 24
|
|
/// </summary>
|
|
[Fact]
|
|
public void EmptyUserNotFound()
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.no-user.yml");
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, useRelativePaths: false);
|
|
|
|
Assert.NotEmpty(cfg.Host);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Make sure Host is replaced by masterUrl
|
|
/// </summary>
|
|
[Fact]
|
|
public void OverrideByMasterUrl()
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.yml");
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, masterUrl: "http://test.server",
|
|
useRelativePaths: false);
|
|
Assert.Equal("http://test.server", cfg.Host);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Make sure that http urls are loaded even if insecure-skip-tls-verify === true
|
|
/// </summary>
|
|
[Fact]
|
|
public void SmartSkipTlsVerify()
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.tls-skip-http.yml");
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi);
|
|
Assert.False(cfg.SkipTlsVerify);
|
|
Assert.Equal("http://horse.org", cfg.Host);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks config could work well when current-context is not set but masterUrl is set. #issue 24
|
|
/// </summary>
|
|
[Fact]
|
|
public void NoCurrentContext()
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.no-current-context.yml");
|
|
|
|
// failed if cannot infer any server host
|
|
Assert.Throws<KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
|
|
|
|
// survive when masterUrl is set
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, masterUrl: "http://test.server");
|
|
Assert.Equal("http://test.server", cfg.Host);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks that loading a configuration from a file leaves no outstanding handles to the file.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This test fails only on Windows.
|
|
/// </remarks>
|
|
[Fact]
|
|
public void DeletedConfigurationFile()
|
|
{
|
|
var assetFileInfo = new FileInfo("assets/kubeconfig.yml");
|
|
var tempFileInfo = new FileInfo(Path.GetTempFileName());
|
|
|
|
File.Copy(assetFileInfo.FullName, tempFileInfo.FullName, /* overwrite: */ true);
|
|
|
|
KubernetesClientConfiguration config;
|
|
|
|
try
|
|
{
|
|
config = KubernetesClientConfiguration.BuildConfigFromConfigFile(tempFileInfo, useRelativePaths: false);
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(tempFileInfo.FullName);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks Host is loaded from the default configuration file as string
|
|
/// </summary>
|
|
[Fact]
|
|
public void DefaultConfigurationAsStringLoaded()
|
|
{
|
|
var filePath = "assets/kubeconfig.yml";
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(filePath, null, null,
|
|
useRelativePaths: false);
|
|
Assert.NotNull(cfg.Host);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Checks Host is loaded from the default configuration file as stream
|
|
/// </summary>
|
|
[Fact]
|
|
public void DefaultConfigurationAsStreamLoaded()
|
|
{
|
|
using (var stream = File.OpenRead("assets/kubeconfig.yml"))
|
|
{
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(stream);
|
|
Assert.NotNull(cfg.Host);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks users.as-user-extra is loaded correctly from a configuration file.
|
|
/// </summary>
|
|
[Fact]
|
|
public void AsUserExtra()
|
|
{
|
|
var filePath = "assets/kubeconfig.as-user-extra.yml";
|
|
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(filePath, null, null,
|
|
useRelativePaths: false);
|
|
Assert.NotNull(cfg.Host);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ensures Kube config file is loaded from explicit file
|
|
/// </summary>
|
|
[Fact]
|
|
public void LoadKubeConfigExplicitFilePath()
|
|
{
|
|
var txt = File.ReadAllText("assets/kubeconfig.yml");
|
|
var expectedCfg = Yaml.LoadFromString<K8SConfiguration>(txt);
|
|
|
|
var cfg = KubernetesClientConfiguration.LoadKubeConfig("assets/kubeconfig.yml");
|
|
|
|
Assert.NotNull(cfg);
|
|
AssertConfigEqual(expectedCfg, cfg);
|
|
}
|
|
|
|
[Fact]
|
|
public void LoadKubeConfigFileInfo()
|
|
{
|
|
var filePath = "assets/kubeconfig.yml";
|
|
var txt = File.ReadAllText(filePath);
|
|
var expectedCfg = Yaml.LoadFromString<K8SConfiguration>(txt);
|
|
|
|
var fileInfo = new FileInfo(filePath);
|
|
var cfg = KubernetesClientConfiguration.LoadKubeConfig(fileInfo);
|
|
|
|
Assert.NotNull(cfg);
|
|
AssertConfigEqual(expectedCfg, cfg);
|
|
}
|
|
|
|
[Fact]
|
|
public void LoadKubeConfigStream()
|
|
{
|
|
var filePath = "assets/kubeconfig.yml";
|
|
var txt = File.ReadAllText(filePath);
|
|
var expectedCfg = Yaml.LoadFromString<K8SConfiguration>(txt);
|
|
|
|
var fileInfo = new FileInfo(filePath);
|
|
K8SConfiguration cfg;
|
|
using (var stream = fileInfo.OpenRead())
|
|
{
|
|
cfg = KubernetesClientConfiguration.LoadKubeConfig(stream);
|
|
}
|
|
|
|
Assert.NotNull(cfg);
|
|
AssertConfigEqual(expectedCfg, cfg);
|
|
}
|
|
|
|
[Fact]
|
|
public void LoadKubeConfigFromEnvironmentVariable()
|
|
{
|
|
// BuildDefaultConfig assumes UseRelativePaths: true, which isn't
|
|
// done by any tests.
|
|
var filePath = Path.GetFullPath("assets/kubeconfig.relative.yml");
|
|
var environmentVariable = "KUBECONFIG_LoadKubeConfigFromEnvironmentVariable";
|
|
|
|
Environment.SetEnvironmentVariable(environmentVariable, filePath);
|
|
KubernetesClientConfiguration.KubeConfigEnvironmentVariable = environmentVariable;
|
|
|
|
var cfg = KubernetesClientConfiguration.BuildDefaultConfig();
|
|
|
|
Assert.NotNull(cfg);
|
|
}
|
|
|
|
[Fact]
|
|
public void LoadKubeConfigFromEnvironmentVariableMultipleConfigs()
|
|
{
|
|
// This test makes sure that a list of environment variables works (no exceptions),
|
|
// doesn't check validity of configuration, which is done in other tests.
|
|
|
|
var filePath = Path.GetFullPath("assets/kubeconfig.relative.yml");
|
|
var environmentVariable = "KUBECONFIG_LoadKubeConfigFromEnvironmentVariable_MultipleConfigs";
|
|
|
|
Environment.SetEnvironmentVariable(environmentVariable,
|
|
string.Concat(filePath, RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ';' : ':', filePath));
|
|
KubernetesClientConfiguration.KubeConfigEnvironmentVariable = environmentVariable;
|
|
|
|
var cfg = KubernetesClientConfiguration.BuildDefaultConfig();
|
|
|
|
Assert.NotNull(cfg);
|
|
}
|
|
|
|
[Fact]
|
|
public void LoadSameKubeConfigFromEnvironmentVariableUnmodified()
|
|
{
|
|
var txt = File.ReadAllText("assets/kubeconfig.yml");
|
|
var expectedCfg = Yaml.LoadFromString<K8SConfiguration>(txt);
|
|
|
|
var fileInfo = new FileInfo(Path.GetFullPath("assets/kubeconfig.yml"));
|
|
|
|
var cfg = KubernetesClientConfiguration.LoadKubeConfig(new FileInfo[] { fileInfo, fileInfo });
|
|
|
|
AssertConfigEqual(expectedCfg, cfg);
|
|
}
|
|
|
|
[Fact]
|
|
public void MergeKubeConfigNoDuplicates()
|
|
{
|
|
var firstPath = Path.GetFullPath("assets/kubeconfig.as-user-extra.yml");
|
|
var secondPath = Path.GetFullPath("assets/kubeconfig.yml");
|
|
|
|
var cfg = KubernetesClientConfiguration.LoadKubeConfig(new FileInfo[]
|
|
{
|
|
new FileInfo(firstPath), new FileInfo(secondPath),
|
|
});
|
|
|
|
// Merged file has 6 users now.
|
|
Assert.Equal(6, cfg.Users.Count());
|
|
Assert.Equal(5, cfg.Clusters.Count());
|
|
Assert.Equal(5, cfg.Contexts.Count());
|
|
}
|
|
|
|
[Fact]
|
|
public void AlwaysPicksFirstOccurence()
|
|
{
|
|
var firstPath = Path.GetFullPath("assets/kubeconfig.no-cluster.yml");
|
|
var secondPath = Path.GetFullPath("assets/kubeconfig.no-context.yml");
|
|
|
|
var cfg = KubernetesClientConfiguration.LoadKubeConfig(new FileInfo[]
|
|
{
|
|
new FileInfo(firstPath), new FileInfo(secondPath),
|
|
});
|
|
|
|
var user = cfg.Users.Where(u => u.Name == "green-user").Single();
|
|
Assert.NotNull(user.UserCredentials.Password);
|
|
Assert.Null(user.UserCredentials.ClientCertificate);
|
|
}
|
|
|
|
[Fact]
|
|
public void ContextFromSecondWorks()
|
|
{
|
|
var firstPath = Path.GetFullPath("assets/kubeconfig.no-current-context.yml");
|
|
var secondPath = Path.GetFullPath("assets/kubeconfig.no-user.yml");
|
|
|
|
var cfg = KubernetesClientConfiguration.LoadKubeConfig(new FileInfo[]
|
|
{
|
|
new FileInfo(firstPath), new FileInfo(secondPath),
|
|
});
|
|
|
|
// green-user
|
|
Assert.NotNull(cfg.CurrentContext);
|
|
}
|
|
|
|
[Fact]
|
|
public void ContextPreferencesExtensionsMergeWithDuplicates()
|
|
{
|
|
var path = Path.GetFullPath("assets/kubeconfig.preferences-extensions.yml");
|
|
|
|
var cfg = KubernetesClientConfiguration.LoadKubeConfig(new FileInfo[]
|
|
{
|
|
new FileInfo(path), new FileInfo(path),
|
|
});
|
|
|
|
Assert.Equal(1, cfg.Extensions.Count);
|
|
Assert.Equal(1, cfg.Preferences.Count);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ensures Kube config file can be loaded from within a non-default <see cref="SynchronizationContext"/>.
|
|
/// The use of <see cref="UIFactAttribute"/> ensures the test is run from within a UI-like <see cref="SynchronizationContext"/>.
|
|
/// </summary>
|
|
[UIFact]
|
|
public void BuildConfigFromConfigFileInfoOnNonDefaultSynchronizationContext()
|
|
{
|
|
var fi = new FileInfo("assets/kubeconfig.yml");
|
|
KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "federal-context", useRelativePaths: false);
|
|
}
|
|
|
|
private void AssertConfigEqual(K8SConfiguration expected, K8SConfiguration actual)
|
|
{
|
|
Assert.Equal(expected.ApiVersion, actual.ApiVersion);
|
|
Assert.Equal(expected.CurrentContext, actual.CurrentContext);
|
|
|
|
foreach (var expectedContext in expected.Contexts)
|
|
{
|
|
// Will throw exception if not found
|
|
var actualContext = actual.Contexts.First(c => c.Name.Equals(expectedContext.Name));
|
|
AssertContextEqual(expectedContext, actualContext);
|
|
}
|
|
|
|
foreach (var expectedCluster in expected.Clusters)
|
|
{
|
|
// Will throw exception if not found
|
|
var actualCluster = actual.Clusters.First(c => c.Name.Equals(expectedCluster.Name));
|
|
AssertClusterEqual(expectedCluster, actualCluster);
|
|
}
|
|
|
|
foreach (var expectedUser in expected.Users)
|
|
{
|
|
// Will throw exception if not found
|
|
var actualUser = actual.Users.First(u => u.Name.Equals(expectedUser.Name));
|
|
AssertUserEqual(expectedUser, actualUser);
|
|
}
|
|
}
|
|
|
|
private static void AssertContextEqual(Context expected, Context actual)
|
|
{
|
|
Assert.Equal(expected.Name, actual.Name);
|
|
Assert.Equal(expected.ContextDetails.Cluster, actual.ContextDetails.Cluster);
|
|
Assert.Equal(expected.ContextDetails.User, actual.ContextDetails.User);
|
|
Assert.Equal(expected.ContextDetails.Namespace, actual.ContextDetails.Namespace);
|
|
}
|
|
|
|
private static void AssertClusterEqual(Cluster expected, Cluster actual)
|
|
{
|
|
Assert.Equal(expected.Name, actual.Name);
|
|
Assert.Equal(expected.ClusterEndpoint.CertificateAuthority, actual.ClusterEndpoint.CertificateAuthority);
|
|
Assert.Equal(expected.ClusterEndpoint.CertificateAuthorityData,
|
|
actual.ClusterEndpoint.CertificateAuthorityData);
|
|
Assert.Equal(expected.ClusterEndpoint.Server, actual.ClusterEndpoint.Server);
|
|
Assert.Equal(expected.ClusterEndpoint.SkipTlsVerify, actual.ClusterEndpoint.SkipTlsVerify);
|
|
}
|
|
|
|
private static void AssertUserEqual(User expected, User actual)
|
|
{
|
|
Assert.Equal(expected.Name, actual.Name);
|
|
|
|
var expectedCreds = expected.UserCredentials;
|
|
var actualCreds = actual.UserCredentials;
|
|
|
|
Assert.Equal(expectedCreds.ClientCertificateData, actualCreds.ClientCertificateData);
|
|
Assert.Equal(expectedCreds.ClientCertificate, actualCreds.ClientCertificate);
|
|
Assert.Equal(expectedCreds.ClientKeyData, actualCreds.ClientKeyData);
|
|
Assert.Equal(expectedCreds.ClientKey, actualCreds.ClientKey);
|
|
Assert.Equal(expectedCreds.Token, actualCreds.Token);
|
|
Assert.Equal(expectedCreds.Impersonate, actualCreds.Impersonate);
|
|
Assert.Equal(expectedCreds.UserName, actualCreds.UserName);
|
|
Assert.Equal(expectedCreds.Password, actualCreds.Password);
|
|
|
|
Assert.True(expectedCreds.ImpersonateGroups.All(x => actualCreds.ImpersonateGroups.Contains(x)));
|
|
Assert.True(expectedCreds.ImpersonateUserExtra.All(x => actualCreds.ImpersonateUserExtra.Contains(x)));
|
|
|
|
if (expectedCreds.AuthProvider != null)
|
|
{
|
|
Assert.True(expectedCreds.AuthProvider.Config.All(x => actualCreds.AuthProvider.Config.Contains(x)));
|
|
}
|
|
}
|
|
}
|
|
}
|