From de9edb1c23b5872f5e3637816dc2bd23e8c4ed54 Mon Sep 17 00:00:00 2001 From: Sergio Sisternes Date: Mon, 26 Jun 2017 23:08:29 +0200 Subject: [PATCH] Added sample kubeconfig files to testing project --- src/KubernetesClient.csproj | 4 +- src/KubernetesClientConfiguration.cs | 9 +- tests/KubernetesClientConfigurationTests.cs | 100 ++++++++++++++++++-- 3 files changed, 100 insertions(+), 13 deletions(-) diff --git a/src/KubernetesClient.csproj b/src/KubernetesClient.csproj index 6cf6d0a..3162f0c 100644 --- a/src/KubernetesClient.csproj +++ b/src/KubernetesClient.csproj @@ -3,8 +3,8 @@ Library netcoreapp1.1 - - + + diff --git a/src/KubernetesClientConfiguration.cs b/src/KubernetesClientConfiguration.cs index 33e3e5b..3b552a9 100644 --- a/src/KubernetesClientConfiguration.cs +++ b/src/KubernetesClientConfiguration.cs @@ -96,13 +96,14 @@ namespace k8s { Context activeContext; + if (k8SConfig.Contexts == null) + { + throw new KubeConfigException("No contexts found in kubeconfig"); + } + // set the currentCOntext to passed context if not null if (!string.IsNullOrWhiteSpace(currentContext)) { - if (k8SConfig.Contexts == null) - { - throw new KubeConfigException("No contexts found in kubeconfig"); - } activeContext = k8SConfig.Contexts.FirstOrDefault(c => c.Name.Equals(currentContext, StringComparison.OrdinalIgnoreCase)); if (activeContext != null) diff --git a/tests/KubernetesClientConfigurationTests.cs b/tests/KubernetesClientConfigurationTests.cs index d1d0c89..f4f9e91 100755 --- a/tests/KubernetesClientConfigurationTests.cs +++ b/tests/KubernetesClientConfigurationTests.cs @@ -1,11 +1,20 @@ 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"; + + private static readonly string kubeConfigNoContexts = "assets/kubeconfig-no-context.yml"; + /// /// Checks Host is loaded from the default configuration file /// @@ -15,18 +24,95 @@ namespace k8s.Tests 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 the are pods + /// Checks if user-based token is loaded properly from the config file, per context + /// + /// + /// + /// + [Theory] + [InlineData("queen-anne-context", "black-user" ,"black-token")] + public void ContextUserTokenTest(string context, string username, string token) + { + var fi = new FileInfo(kubeConfigFileName); + var cfg = new KubernetesClientConfiguration(fi, context); + Assert.Equal(context, cfg.CurrentContext); + Assert.Equal(username, 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 ListDefaultNamespacedPod() + public void ContextNotFoundTest() { - 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); + 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 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); + // } } }