From 6b104a5a785444716bedabd97b3728fa306bf36e Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Thu, 5 Jan 2023 09:11:59 -0800 Subject: [PATCH] remove system io abstraction (#1157) --- .../KubernetesClient.Classic.csproj | 3 +- src/KubernetesClient/CertUtils.cs | 2 +- src/KubernetesClient/FileSystem.cs | 57 +++++++++++++++++++ src/KubernetesClient/FileUtils.cs | 33 ----------- src/KubernetesClient/KubernetesClient.csproj | 1 - ...KubernetesClientConfiguration.InCluster.cs | 8 +-- .../KubernetesClientConfigurationTests.cs | 32 ++++++++++- 7 files changed, 92 insertions(+), 44 deletions(-) create mode 100644 src/KubernetesClient/FileSystem.cs delete mode 100644 src/KubernetesClient/FileUtils.cs diff --git a/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj b/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj index cfd14ed..240ebbf 100644 --- a/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj +++ b/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj @@ -7,7 +7,6 @@ - @@ -19,7 +18,7 @@ - + diff --git a/src/KubernetesClient/CertUtils.cs b/src/KubernetesClient/CertUtils.cs index ad4fad9..21f3699 100644 --- a/src/KubernetesClient/CertUtils.cs +++ b/src/KubernetesClient/CertUtils.cs @@ -24,7 +24,7 @@ namespace k8s public static X509Certificate2Collection LoadPemFileCert(string file) { var certCollection = new X509Certificate2Collection(); - using (var stream = FileUtils.FileSystem().File.OpenRead(file)) + using (var stream = FileSystem.Current.OpenRead(file)) { #if NET5_0_OR_GREATER certCollection.ImportFromPem(new StreamReader(stream).ReadToEnd()); diff --git a/src/KubernetesClient/FileSystem.cs b/src/KubernetesClient/FileSystem.cs new file mode 100644 index 0000000..22736ef --- /dev/null +++ b/src/KubernetesClient/FileSystem.cs @@ -0,0 +1,57 @@ +using System.IO; + +namespace k8s +{ + internal static class FileSystem + { + public interface IFileSystem + { + Stream OpenRead(string path); + + bool Exists(string path); + + string ReadAllText(string path); + } + + public static IFileSystem Current { get; private set; } = new RealFileSystem(); + + public static IDisposable With(IFileSystem fileSystem) + { + return new InjectedFileSystem(fileSystem); + } + + private class InjectedFileSystem : IDisposable + { + private readonly IFileSystem _original; + + public InjectedFileSystem(IFileSystem fileSystem) + { + _original = Current; + Current = fileSystem; + } + + public void Dispose() + { + Current = _original; + } + } + + private class RealFileSystem : IFileSystem + { + public bool Exists(string path) + { + return File.Exists(path); + } + + public Stream OpenRead(string path) + { + return File.OpenRead(path); + } + + public string ReadAllText(string path) + { + return File.ReadAllText(path); + } + } + } +} diff --git a/src/KubernetesClient/FileUtils.cs b/src/KubernetesClient/FileUtils.cs deleted file mode 100644 index 61fca2f..0000000 --- a/src/KubernetesClient/FileUtils.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.IO.Abstractions; - -namespace k8s -{ - internal static class FileUtils - { - private static readonly IFileSystem RealFileSystem = new FileSystem(); - private static IFileSystem currentFileSystem = null; - - public static void InjectFilesystem(IFileSystem fs) - { - currentFileSystem = fs; - } - - public static IFileSystem FileSystem() - { - return currentFileSystem != null ? currentFileSystem : RealFileSystem; - } - - public sealed class InjectedFileSystem : IDisposable - { - public InjectedFileSystem(IFileSystem fs) - { - InjectFilesystem(fs); - } - - public void Dispose() - { - InjectFilesystem(null); - } - } - } -} diff --git a/src/KubernetesClient/KubernetesClient.csproj b/src/KubernetesClient/KubernetesClient.csproj index 60f79d5..8af8245 100644 --- a/src/KubernetesClient/KubernetesClient.csproj +++ b/src/KubernetesClient/KubernetesClient.csproj @@ -8,7 +8,6 @@ - diff --git a/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs b/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs index 5107442..41df247 100644 --- a/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs +++ b/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs @@ -30,13 +30,13 @@ namespace k8s } var tokenPath = Path.Combine(ServiceAccountPath, ServiceAccountTokenKeyFileName); - if (!FileUtils.FileSystem().File.Exists(tokenPath)) + if (!FileSystem.Current.Exists(tokenPath)) { return false; } var certPath = Path.Combine(ServiceAccountPath, ServiceAccountRootCAKeyFileName); - return FileUtils.FileSystem().File.Exists(certPath); + return FileSystem.Current.Exists(certPath); } public static KubernetesClientConfiguration InClusterConfig() @@ -68,9 +68,9 @@ namespace k8s }; var namespaceFile = Path.Combine(ServiceAccountPath, ServiceAccountNamespaceFileName); - if (FileUtils.FileSystem().File.Exists(namespaceFile)) + if (FileSystem.Current.Exists(namespaceFile)) { - result.Namespace = FileUtils.FileSystem().File.ReadAllText(namespaceFile); + result.Namespace = FileSystem.Current.ReadAllText(namespaceFile); } return result; diff --git a/tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs b/tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs index 7d23404..ed664fb 100644 --- a/tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs +++ b/tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs @@ -4,6 +4,7 @@ using k8s.KubeConfigModels; using System; using System.Collections.Generic; using System.IO; +using System.IO.Abstractions; using System.IO.Abstractions.TestingHelpers; using System.Linq; using System.Runtime.InteropServices; @@ -694,6 +695,31 @@ namespace k8s.Tests } } + private class FileSystemAdapter : FileSystem.IFileSystem + { + private readonly IFileSystem io; + + public FileSystemAdapter(System.IO.Abstractions.IFileSystem io) + { + this.io = io; + } + + public bool Exists(string path) + { + return io.File.Exists(path); + } + + public Stream OpenRead(string path) + { + return io.File.OpenRead(path); + } + + public string ReadAllText(string path) + { + return io.File.ReadAllText(path); + } + } + /// /// Test in cluster configuration. /// @@ -713,7 +739,7 @@ namespace k8s.Tests { tokenPath, new MockFileData("foo") }, { certPath, new MockFileData("bar") }, }); - using (new FileUtils.InjectedFileSystem(fileSystem)) + using (FileSystem.With(new FileSystemAdapter(fileSystem))) { Assert.True(KubernetesClientConfiguration.IsInCluster()); } @@ -737,7 +763,7 @@ namespace k8s.Tests { certPath, new MockFileData("bar") }, }); - using (new FileUtils.InjectedFileSystem(fileSystem)) + using (FileSystem.With(new FileSystemAdapter(fileSystem))) { var config = KubernetesClientConfiguration.InClusterConfig(); Assert.Equal("https://other.default.svc:443/", config.Host); @@ -764,7 +790,7 @@ namespace k8s.Tests { namespacePath, new MockFileData("some namespace") }, }); - using (new FileUtils.InjectedFileSystem(fileSystem)) + using (FileSystem.With(new FileSystemAdapter(fileSystem))) { var config = KubernetesClientConfiguration.InClusterConfig(); Assert.Equal("https://kubernetes.default.svc:443/", config.Host);