1313
1414namespace k8s . Tests
1515{
16- public class KubernetesClientConfigurationTests
16+ public class KubernetesClientConfigurationTests : IDisposable
1717 {
18+ /// <summary>
19+ /// Not all tests set these, but no harm in clearing them.
20+ /// </summary>
21+ public void Dispose ( )
22+ {
23+ Dispose ( true ) ;
24+ GC . SuppressFinalize ( this ) ;
25+ }
26+
27+ protected virtual void Dispose ( bool disposing )
28+ {
29+ Environment . SetEnvironmentVariable ( "KUBERNETES_SERVICE_HOST" , null ) ;
30+ Environment . SetEnvironmentVariable ( "KUBERNETES_SERVICE_PORT" , null ) ;
31+ }
32+
1833 /// <summary>
1934 /// Check if host is properly loaded, per context
2035 /// </summary>
@@ -683,6 +698,9 @@ public void IsInCluster()
683698 {
684699 Assert . False ( KubernetesClientConfiguration . IsInCluster ( ) ) ;
685700
701+ Environment . SetEnvironmentVariable ( "KUBERNETES_SERVICE_HOST" , "kubernetes" ) ;
702+ Environment . SetEnvironmentVariable ( "KUBERNETES_SERVICE_PORT" , "443" ) ;
703+
686704 var tokenPath = Path . Combine ( KubernetesClientConfiguration . ServiceAccountPath , KubernetesClientConfiguration . ServiceAccountTokenKeyFileName ) ;
687705 var certPath = Path . Combine ( KubernetesClientConfiguration . ServiceAccountPath , KubernetesClientConfiguration . ServiceAccountRootCAKeyFileName ) ;
688706
@@ -703,6 +721,9 @@ public void IsInCluster()
703721 [ Fact ]
704722 public void LoadInCluster ( )
705723 {
724+ Environment . SetEnvironmentVariable ( "KUBERNETES_SERVICE_HOST" , "other.default.svc" ) ;
725+ Environment . SetEnvironmentVariable ( "KUBERNETES_SERVICE_PORT" , "443" ) ;
726+
706727 var tokenPath = Path . Combine ( KubernetesClientConfiguration . ServiceAccountPath , KubernetesClientConfiguration . ServiceAccountTokenKeyFileName ) ;
707728 var certPath = Path . Combine ( KubernetesClientConfiguration . ServiceAccountPath , KubernetesClientConfiguration . ServiceAccountRootCAKeyFileName ) ;
708729
@@ -715,7 +736,7 @@ public void LoadInCluster()
715736 using ( new FileUtils . InjectedFileSystem ( fileSystem ) )
716737 {
717738 var config = KubernetesClientConfiguration . InClusterConfig ( ) ;
718- Assert . Equal ( "https://kubernetes .default.svc:443/" , config . Host ) ;
739+ Assert . Equal ( "https://other .default.svc:443/" , config . Host ) ;
719740 Assert . Null ( config . Namespace ) ;
720741 }
721742 }
@@ -726,6 +747,9 @@ public void LoadInCluster()
726747 [ Fact ]
727748 public void LoadInClusterNamespace ( )
728749 {
750+ Environment . SetEnvironmentVariable ( "KUBERNETES_SERVICE_HOST" , "kubernetes.default.svc" ) ;
751+ Environment . SetEnvironmentVariable ( "KUBERNETES_SERVICE_PORT" , "443" ) ;
752+
729753 var tokenPath = Path . Combine ( KubernetesClientConfiguration . ServiceAccountPath , KubernetesClientConfiguration . ServiceAccountTokenKeyFileName ) ;
730754 var certPath = Path . Combine ( KubernetesClientConfiguration . ServiceAccountPath , KubernetesClientConfiguration . ServiceAccountRootCAKeyFileName ) ;
731755 var namespacePath = Path . Combine ( KubernetesClientConfiguration . ServiceAccountPath , KubernetesClientConfiguration . ServiceAccountNamespaceFileName ) ;
0 commit comments