using System.IO; using k8s.Exceptions; using Xunit; namespace k8s.Tests { public class KubernetesClientConfigurationTests { /// /// Check if host is properly loaded, per context /// [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); Assert.Equal(host, cfg.Host); } /// /// Checks if user-based token is loaded properly from the config file, per context /// /// /// [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); } /// /// Checks if certificate-based authentication is loaded properly from the config file, per context /// /// Context to retreive the configuration /// 'client-certificate-data' node content /// 'client-key-data' content [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); Assert.Equal(context, cfg.CurrentContext); Assert.Equal(cfg.ClientCertificateFilePath, clientCert); Assert.Equal(cfg.ClientKeyFilePath, clientCertKey); } /// /// Checks if certificate-based authentication is loaded properly from the config file, per context /// /// Context to retreive the configuration [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.SslCaCert); Assert.Equal(File.ReadAllText("assets/client-certificate-data.txt"), cfg.ClientCertificateData); Assert.Equal(File.ReadAllText("assets/client-key-data.txt"), cfg.ClientCertificateKeyData); } /// /// Checks that a KubeConfigException is thrown when no certificate-authority-data is set and user do not require tls /// skip /// [Fact] public void CheckClusterTlsCorrectness() { var fi = new FileInfo("assets/kubeconfig.tls-no-skip-error.yml"); Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); } /// /// Checks that a KubeConfigException is thrown when no certificate-authority-data is set and user do not require tls /// skip /// [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.SslCaCert); Assert.True(cfg.SkipTlsVerify); } /// /// Checks that a KubeConfigException is thrown when the cluster defined in clusters and contexts do not match /// [Fact] public void ClusterNameMissmatch() { var fi = new FileInfo("assets/kubeconfig.cluster-missmatch.yml"); Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); } /// /// Checks that a KubeConfigException is thrown when the clusters section is missing /// [Fact] public void ClusterNotFound() { var fi = new FileInfo("assets/kubeconfig.no-cluster.yml"); Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); } /// /// The configuration file is not present. An KubeConfigException should be thrown /// [Fact] public void ConfigurationFileNotFound() { var fi = new FileInfo("/path/to/nowhere"); Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); } /// /// Test that an Exception is thrown when initializating a KubernetClientConfiguration whose config file Context is not /// present /// [Fact] public void ContextNotFound() { var fi = new FileInfo("assets/kubeconfig.yml"); Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "context-not-found")); } /// /// Checks Host is loaded from the default configuration file /// [Fact] public void DefaultConfigurationLoaded() { var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(new FileInfo("assets/kubeconfig.yml")); Assert.NotNull(cfg.Host); } /// /// Checks that a KubeConfigException is thrown when incomplete user credentials /// [Fact] public void IncompleteUserCredentials() { var fi = new FileInfo("assets/kubeconfig.no-credentials.yml"); Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); } /// /// Test if KubeConfigException is thrown when no Contexts and we use the default context name /// [Fact] public void NoContexts() { var fi = new FileInfo("assets/kubeconfig.no-context.yml"); Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); } /// /// Test if KubeConfigException is thrown when no Contexts are set and we specify a concrete context name /// [Fact] public void NoContextsExplicit() { var fi = new FileInfo("assets/kubeconfig-no-context.yml"); Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "context")); } /// /// Checks that a KubeConfigException is thrown when the server property is not set in cluster /// [Fact] public void ServerNotFound() { var fi = new FileInfo("assets/kubeconfig.no-server.yml"); Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); } /// /// Checks user/password authentication information is read properly /// [Fact] public void UserPasswordAuthentication() { var fi = new FileInfo("assets/kubeconfig.user-pass.yml"); var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi); Assert.Equal("admin", cfg.Username); Assert.Equal("secret", cfg.Password); } /// /// Checks that a KubeConfigException is thrown when user cannot be found in users /// [Fact] public void UserNotFound() { var fi = new FileInfo("assets/kubeconfig.user-not-found.yml"); Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); } /// /// Make sure that user is not a necessary field. set #issue 24 /// [Fact] public void EmptyUserNotFound() { var fi = new FileInfo("assets/kubeconfig.no-user.yml"); var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi); Assert.NotEmpty(cfg.Host); } /// /// Make sure Host is replaced by masterUrl /// [Fact] public void OverrideByMasterUrl() { var fi = new FileInfo("assets/kubeconfig.yml"); var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, masterUrl: "http://test.server"); Assert.Equal("http://test.server", cfg.Host); } /// /// Make sure that http urls are loaded even if insecure-skip-tls-verify === true /// [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); } /// /// Checks config could work well when current-context is not set but masterUrl is set. #issue 24 /// [Fact] public void NoCurrentContext() { var fi = new FileInfo("assets/kubeconfig.no-current-context.yml"); // failed if cannot infer any server host Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); // survive when masterUrl is set var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, masterUrl: "http://test.server"); Assert.Equal("http://test.server", cfg.Host); } } }