Skip to content

Commit b370b92

Browse files
Merge pull request #51 from tg123/minor_style_fix
cleanup unused code and functions and fix unittest naming style
2 parents d89293b + cdeebc1 commit b370b92

10 files changed

+32
-98
lines changed

src/Utils.cs renamed to src/CertUtils.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,8 @@
1212

1313
namespace k8s
1414
{
15-
public static class Utils
15+
public static class CertUtils
1616
{
17-
/// <summary>
18-
/// Encode string in base64 format.
19-
/// </summary>
20-
/// <param name="text">string to be encoded.</param>
21-
/// <returns>Encoded string.</returns>
22-
public static string Base64Encode(string text)
23-
{
24-
return Convert.ToBase64String(Encoding.UTF8.GetBytes(text));
25-
}
26-
27-
/// <summary>
28-
/// Encode string in base64 format.
29-
/// </summary>
30-
/// <param name="text">string to be encoded.</param>
31-
/// <returns>Encoded string.</returns>
32-
public static string Base64Decode(string text)
33-
{
34-
return Encoding.UTF8.GetString(Convert.FromBase64String(text));
35-
}
36-
3717
/// <summary>
3818
/// Load pem encoded cert file
3919
/// </summary>

src/DotNetUtilities.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/Kubernetes.Auth.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private void SetCredentials(KubernetesClientConfiguration config, HttpClientHand
7777
(!string.IsNullOrWhiteSpace(config.ClientCertificateKeyData) ||
7878
!string.IsNullOrWhiteSpace(config.ClientKeyFilePath)))
7979
{
80-
var cert = Utils.GeneratePfx(config);
80+
var cert = CertUtils.GeneratePfx(config);
8181

8282
handler.ClientCertificates.Add(cert);
8383
}

src/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private void Initialize(K8SConfiguration k8SConfig, string currentContext = null
130130
if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthorityData))
131131
{
132132
string data = clusterDetails.ClusterEndpoint.CertificateAuthorityData;
133-
this.SslCaCert = new X509Certificate2(System.Text.Encoding.UTF8.GetBytes(Utils.Base64Decode(data)));
133+
this.SslCaCert = new X509Certificate2(Convert.FromBase64String(data));
134134
}
135135
else if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthority))
136136
{
@@ -215,4 +215,4 @@ private static K8SConfiguration LoadKubeConfig(FileInfo kubeconfig)
215215
return deserializer.Deserialize<K8SConfiguration>(kubeconfigContent);
216216
}
217217
}
218-
}
218+
}

src/KubernetesClientConfiguration.InCluster.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public static KubernetesClientConfiguration InClusterConfig()
2828
{
2929
Host = new UriBuilder("https", host, Convert.ToInt32(port)).ToString(),
3030
AccessToken = token,
31-
SslCaCert = Utils.LoadPemFileCert(rootCAFile)
31+
SslCaCert = CertUtils.LoadPemFileCert(rootCAFile)
3232
};
3333
}
3434
}
35-
}
35+
}

src/Watcher.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.IO;
33
using System.Runtime.Serialization;
44
using System.Threading;
@@ -140,4 +140,4 @@ public static Watcher<T> Watch<T>(this HttpOperationResponse<T> response,
140140
return Watch((HttpOperationResponse) response, onEvent, onError);
141141
}
142142
}
143-
}
143+
}

tests/AuthTests.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using System;
1+
using System;
22
using System.IO;
33
using System.Linq;
44
using System.Net;
55
using System.Net.Http.Headers;
66
using System.Security.Cryptography.X509Certificates;
7+
using System.Text;
78
using System.Threading.Tasks;
89
using k8s.Models;
910
using k8s.Tests.Mock;
@@ -22,7 +23,7 @@ private static HttpOperationResponse<Corev1PodList> ExecuteListPods(IKubernetes
2223
}
2324

2425
[Fact]
25-
public void TestAnonymous()
26+
public void Anonymous()
2627
{
2728
using (var server = new MockKubeApiServer())
2829
{
@@ -55,7 +56,7 @@ public void TestAnonymous()
5556
}
5657

5758
[Fact]
58-
public void TestBasicAuth()
59+
public void BasicAuth()
5960
{
6061
const string testName = "test_name";
6162
const string testPassword = "test_password";
@@ -64,7 +65,7 @@ public void TestBasicAuth()
6465
{
6566
var header = cxt.Request.Headers["Authorization"].FirstOrDefault();
6667

67-
var expect = new AuthenticationHeaderValue("Basic", Utils.Base64Encode($"{testName}:{testPassword}"))
68+
var expect = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{testName}:{testPassword}")))
6869
.ToString();
6970

7071
if (header != expect)
@@ -154,7 +155,7 @@ public void TestBasicAuth()
154155
}
155156

156157
[Fact]
157-
public void TestCert()
158+
public void Cert()
158159
{
159160
var serverCertificateData = File.ReadAllText("assets/apiserver-pfx-data.txt");
160161

@@ -244,7 +245,7 @@ public void TestCert()
244245
}
245246

246247
[Fact]
247-
public void TestToken()
248+
public void Token()
248249
{
249250
const string token = "testingtoken";
250251

@@ -314,4 +315,4 @@ public void TestToken()
314315
}
315316
}
316317
}
317-
}
318+
}

tests/UtilTests.cs renamed to tests/CertUtilsTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace k8s.Tests
77
{
8-
public class UtilsTests
8+
public class CertUtilsTests
99
{
1010
/// <summary>
1111
/// This file contains a sample kubeconfig file
@@ -22,7 +22,7 @@ public void LoadFromFiles()
2222
var cfg = new KubernetesClientConfiguration(fi, "federal-context");
2323

2424
// Just validate that this doesn't throw and private key is non-null
25-
var cert = Utils.GeneratePfx(cfg);
25+
var cert = CertUtils.GeneratePfx(cfg);
2626
Assert.NotNull(cert.PrivateKey);
2727
}
2828

@@ -36,8 +36,8 @@ public void LoadFromInlineData()
3636
var cfg = new KubernetesClientConfiguration(fi, "victorian-context");
3737

3838
// Just validate that this doesn't throw and private key is non-null
39-
var cert = Utils.GeneratePfx(cfg);
39+
var cert = CertUtils.GeneratePfx(cfg);
4040
Assert.NotNull(cert.PrivateKey);
4141
}
4242
}
43-
}
43+
}

tests/KubernetesClientConfigurationTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void DefaultConfigurationLoaded()
8383
[Theory]
8484
[InlineData("federal-context", "https://horse.org:4443")]
8585
[InlineData("queen-anne-context", "https://pig.org:443")]
86-
public void ContextHostTest(string context, string host)
86+
public void ContextHost(string context, string host)
8787
{
8888
var fi = new FileInfo(kubeConfigFileName);
8989
var cfg = new KubernetesClientConfiguration(fi, context);
@@ -94,11 +94,10 @@ public void ContextHostTest(string context, string host)
9494
/// Checks if user-based token is loaded properly from the config file, per context
9595
/// </summary>
9696
/// <param name="context"></param>
97-
/// <param name="username"></param>
9897
/// <param name="token"></param>
9998
[Theory]
10099
[InlineData("queen-anne-context", "black-token")]
101-
public void ContextUserTokenTest(string context, string token)
100+
public void ContextUserToken(string context, string token)
102101
{
103102
var fi = new FileInfo(kubeConfigFileName);
104103
var cfg = new KubernetesClientConfiguration(fi, context);
@@ -145,7 +144,7 @@ public void ClientDataTest(string context)
145144
/// Test that an Exception is thrown when initializating a KubernetClientConfiguration whose config file Context is not present
146145
/// </summary>
147146
[Fact]
148-
public void ContextNotFoundTest()
147+
public void ContextNotFound()
149148
{
150149
var fi = new FileInfo(kubeConfigFileName);
151150
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi, "context-not-found"));

tests/WatchTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
@@ -42,7 +42,7 @@ private static async Task WriteStreamLine(HttpContext httpContext, string repons
4242
}
4343

4444
[Fact]
45-
public void TestCannotWatch()
45+
public void CannotWatch()
4646
{
4747
using (var server = new MockKubeApiServer())
4848
{
@@ -74,7 +74,7 @@ public void TestCannotWatch()
7474
}
7575

7676
[Fact]
77-
public void TestSuriveBadLine()
77+
public void SuriveBadLine()
7878
{
7979
using (var server = new MockKubeApiServer(async httpContext =>
8080
{
@@ -133,7 +133,7 @@ public void TestSuriveBadLine()
133133
}
134134

135135
[Fact]
136-
public void TestDisposeWatch()
136+
public void DisposeWatch()
137137
{
138138
using (var server = new MockKubeApiServer(async httpContext =>
139139
{
@@ -163,7 +163,7 @@ public void TestDisposeWatch()
163163
);
164164

165165
// wait at least an event
166-
Thread.Sleep(TimeSpan.FromMilliseconds(300));
166+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
167167

168168
Assert.NotEmpty(events);
169169
Assert.True(watcher.Watching);
@@ -173,15 +173,15 @@ public void TestDisposeWatch()
173173
events.Clear();
174174

175175
// make sure wait event called
176-
Thread.Sleep(TimeSpan.FromMilliseconds(300));
176+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
177177
Assert.Empty(events);
178178
Assert.False(watcher.Watching);
179179

180180
}
181181
}
182182

183183
[Fact]
184-
public void TestWatchAllEvents()
184+
public void WatchAllEvents()
185185
{
186186
using (var server = new MockKubeApiServer(async httpContext =>
187187
{
@@ -237,7 +237,7 @@ public void TestWatchAllEvents()
237237
}
238238

239239
[Fact]
240-
public void TestWatchServerDisconnect()
240+
public void WatchServerDisconnect()
241241
{
242242
Watcher<Corev1Pod> watcher;
243243
Exception exceptionCatched = null;
@@ -271,4 +271,4 @@ public void TestWatchServerDisconnect()
271271
Assert.IsType<IOException>(exceptionCatched);
272272
}
273273
}
274-
}
274+
}

0 commit comments

Comments
 (0)