remove system io abstraction (#1157)
This commit is contained in:
@@ -7,7 +7,6 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
|
||||
<PackageReference Include="System.IO.Abstractions" Version="19.1.5" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.25.1" />
|
||||
<PackageReference Include="IdentityModel.OidcClient" Version="5.2.1" />
|
||||
</ItemGroup>
|
||||
@@ -19,7 +18,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\KubernetesClient\CertUtils.cs" />
|
||||
<Compile Include="..\KubernetesClient\FileUtils.cs" />
|
||||
<Compile Include="..\KubernetesClient\FileSystem.cs" />
|
||||
<Compile Include="..\KubernetesClient\IKubernetes.cs" />
|
||||
<Compile Include="..\KubernetesClient\Kubernetes.ConfigInit.cs" />
|
||||
<Compile Include="..\KubernetesClient\Kubernetes.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());
|
||||
|
||||
57
src/KubernetesClient/FileSystem.cs
Normal file
57
src/KubernetesClient/FileSystem.cs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,6 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="prometheus-net" Version="7.0.0" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.25.1" />
|
||||
<PackageReference Include="System.IO.Abstractions" Version="19.1.5" />
|
||||
<PackageReference Include="IdentityModel.OidcClient" Version="5.2.1" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test in cluster configuration.
|
||||
/// </summary>
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user