using System; using Xunit; using k8s; using System.IO; namespace k8s.Tests { public class KubernetesClientConfigurationTests { /// /// This file contains a sample kubeconfig file /// private static readonly string kubeConfigFileName = "assets/kubeconfig.yml"; /// /// Invalid test file with no context on purpose /// private static readonly string kubeConfigNoContexts = "assets/kubeconfig-no-context.yml"; /// /// Sample configuration file with user/password authentication /// private static readonly string kubeConfigUserPassword = "assets/kubeconfig.user-pass.yml"; /// /// Sample configuration file with incorrect user credentials structures on purpose /// private static readonly string kubeConfigNoCredentials = "assets/kubeconfig.no-credentials.yml"; /// /// Sample configuration file with incorrect cluster/server structure on purpose /// private static readonly string kubeConfigNoServer = "assets/kubeconfig.no-server.yml"; /// /// Sample configuration file with incorrect cluster/server structure on purpose /// private static readonly string kubeConfigNoCluster = "assets/kubeconfig.no-cluster.yml"; /// /// Sample configuration file with incorrect match in cluster name /// private static readonly string kubeConfigClusterMissmatch = "assets/kubeconfig.cluster-missmatch.yml"; /// /// Sample configuration file with incorrect TLS configuration in cluster section /// private static readonly string kubeConfigTlsNoSkipError = "assets/kubeconfig.tls-no-skip-error.yml"; /// /// Sample configuration file with incorrect TLS configuration in cluster section /// private static readonly string kubeConfigTlsSkip = "assets/kubeconfig.tls-skip.yml"; /// /// The configuration file is not present. An KubeConfigException should be thrown /// [Fact] public void ConfigurationFileNotFound() { var fi = new FileInfo("/path/to/nowhere"); Assert.Throws(() => new KubernetesClientConfiguration(fi)); } /// /// Checks Host is loaded from the default configuration file /// [Fact] public void DefaultConfigurationLoaded() { var cfg = new KubernetesClientConfiguration(); Assert.NotNull(cfg.Host); } /// /// 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 ContextHostTest(string context, string host) { var fi = new FileInfo(kubeConfigFileName); var cfg = new KubernetesClientConfiguration(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 ContextUserTokenTest(string context, string token) { var fi = new FileInfo(kubeConfigFileName); var cfg = new KubernetesClientConfiguration(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", "path/to/my/client/cert" ,"path/to/my/client/key")] public void ContextCertificateTest(string context, string clientCertData, string clientCertKey) { var fi = new FileInfo(kubeConfigFileName); var cfg = new KubernetesClientConfiguration(fi, context); Assert.Equal(context, cfg.CurrentContext); Assert.Equal(cfg.ClientCertificateData, clientCertData); Assert.Equal(cfg.ClientCertificateKey, clientCertKey); } /// /// Test that an Exception is thrown when initializating a KubernetClientConfiguration whose config file Context is not present /// [Fact] public void ContextNotFoundTest() { var fi = new FileInfo(kubeConfigFileName); Assert.Throws(() => new KubernetesClientConfiguration(fi, "context-not-found")); } /// /// Test if KubeConfigException is thrown when no Contexts and we use the default context name /// [Fact] public void NoContexts() { var fi = new FileInfo(kubeConfigNoContexts); Assert.Throws(() => new KubernetesClientConfiguration(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(kubeConfigNoContexts); Assert.Throws(() => new KubernetesClientConfiguration(fi, "context")); } /// /// Checks user/password authentication information is read properly /// [Fact] public void UserPasswordAuthentication() { var fi = new FileInfo(kubeConfigUserPassword); var cfg = new KubernetesClientConfiguration(fi); Assert.Equal("admin", cfg.Username); Assert.Equal("secret", cfg.Password); } /// /// Checks that a KubeConfigException is thrown when incomplete user credentials /// [Fact] public void IncompleteUserCredentials() { var fi = new FileInfo(kubeConfigNoCredentials); Assert.Throws(() => new KubernetesClientConfiguration(fi)); } /// /// Checks that a KubeConfigException is thrown when the server property is not set in cluster /// [Fact] public void ServerNotFound() { var fi = new FileInfo(kubeConfigNoServer); Assert.Throws(() => new KubernetesClientConfiguration(fi)); } /// /// Checks that a KubeConfigException is thrown when the clusters section is missing /// [Fact] public void ClusterNotFound() { var fi = new FileInfo(kubeConfigNoCluster); Assert.Throws(() => new KubernetesClientConfiguration(fi)); } /// /// 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(kubeConfigClusterMissmatch); Assert.Throws(() => new KubernetesClientConfiguration(fi)); } /// /// 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(kubeConfigTlsNoSkipError); Assert.Throws(() => new KubernetesClientConfiguration(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(kubeConfigTlsSkip); var cfg = new KubernetesClientConfiguration(fi); Assert.NotNull(cfg.Host); Assert.Null(cfg.SslCaCert); Assert.True(cfg.SkipTlsVerify); } // /// // /// Checks if the are pods // /// // [Fact] // public void ListDefaultNamespacedPod() // { // var k8sClientConfig = new KubernetesClientConfiguration(); // IKubernetes client = new Kubernetes(k8sClientConfig); // var listTask = client.ListNamespacedPodWithHttpMessagesAsync("default").Result; // var list = listTask.Body; // Assert.NotEqual(0, list.Items.Count); // } } }