13
13
14
14
namespace k8s . Tests
15
15
{
16
- public class KubernetesClientConfigurationTests
16
+ public class KubernetesClientConfigurationTests : IDisposable
17
17
{
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
+
18
33
/// <summary>
19
34
/// Check if host is properly loaded, per context
20
35
/// </summary>
@@ -683,6 +698,9 @@ public void IsInCluster()
683
698
{
684
699
Assert . False ( KubernetesClientConfiguration . IsInCluster ( ) ) ;
685
700
701
+ Environment . SetEnvironmentVariable ( "KUBERNETES_SERVICE_HOST" , "kubernetes" ) ;
702
+ Environment . SetEnvironmentVariable ( "KUBERNETES_SERVICE_PORT" , "443" ) ;
703
+
686
704
var tokenPath = Path . Combine ( KubernetesClientConfiguration . ServiceAccountPath , KubernetesClientConfiguration . ServiceAccountTokenKeyFileName ) ;
687
705
var certPath = Path . Combine ( KubernetesClientConfiguration . ServiceAccountPath , KubernetesClientConfiguration . ServiceAccountRootCAKeyFileName ) ;
688
706
@@ -703,6 +721,9 @@ public void IsInCluster()
703
721
[ Fact ]
704
722
public void LoadInCluster ( )
705
723
{
724
+ Environment . SetEnvironmentVariable ( "KUBERNETES_SERVICE_HOST" , "other.default.svc" ) ;
725
+ Environment . SetEnvironmentVariable ( "KUBERNETES_SERVICE_PORT" , "443" ) ;
726
+
706
727
var tokenPath = Path . Combine ( KubernetesClientConfiguration . ServiceAccountPath , KubernetesClientConfiguration . ServiceAccountTokenKeyFileName ) ;
707
728
var certPath = Path . Combine ( KubernetesClientConfiguration . ServiceAccountPath , KubernetesClientConfiguration . ServiceAccountRootCAKeyFileName ) ;
708
729
@@ -715,7 +736,7 @@ public void LoadInCluster()
715
736
using ( new FileUtils . InjectedFileSystem ( fileSystem ) )
716
737
{
717
738
var config = KubernetesClientConfiguration . InClusterConfig ( ) ;
718
- Assert . Equal ( "https://kubernetes .default.svc:443/" , config . Host ) ;
739
+ Assert . Equal ( "https://other .default.svc:443/" , config . Host ) ;
719
740
Assert . Null ( config . Namespace ) ;
720
741
}
721
742
}
@@ -726,6 +747,9 @@ public void LoadInCluster()
726
747
[ Fact ]
727
748
public void LoadInClusterNamespace ( )
728
749
{
750
+ Environment . SetEnvironmentVariable ( "KUBERNETES_SERVICE_HOST" , "kubernetes.default.svc" ) ;
751
+ Environment . SetEnvironmentVariable ( "KUBERNETES_SERVICE_PORT" , "443" ) ;
752
+
729
753
var tokenPath = Path . Combine ( KubernetesClientConfiguration . ServiceAccountPath , KubernetesClientConfiguration . ServiceAccountTokenKeyFileName ) ;
730
754
var certPath = Path . Combine ( KubernetesClientConfiguration . ServiceAccountPath , KubernetesClientConfiguration . ServiceAccountRootCAKeyFileName ) ;
731
755
var namespacePath = Path . Combine ( KubernetesClientConfiguration . ServiceAccountPath , KubernetesClientConfiguration . ServiceAccountNamespaceFileName ) ;
0 commit comments