Skip to content

Commit

Permalink
Fix tests for Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Nov 23, 2021
1 parent bad85c5 commit be50feb
Show file tree
Hide file tree
Showing 18 changed files with 154 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,22 @@
public class KubectlApplyTest {

private static final String DISCOVERY_API =
KubectlApplyTest.class.getClassLoader().getResource("discovery-api.json").getPath();
new java.io.File(
KubectlApplyTest.class.getClassLoader().getResource("discovery-api.json").getPath())
.toString();

private static final String DISCOVERY_APIV1 =
KubectlApplyTest.class.getClassLoader().getResource("discovery-api-v1.json").getPath();
new java.io.File(
KubectlApplyTest.class
.getClassLoader()
.getResource("discovery-api-v1.json")
.getPath())
.toString();

private static final String DISCOVERY_APIS =
KubectlApplyTest.class.getClassLoader().getResource("discovery-apis.json").getPath();
new java.io.File(
KubectlApplyTest.class.getClassLoader().getResource("discovery-apis.json").getPath())
.toString();

private ApiClient apiClient;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.kubernetes.client.openapi.models.V1ConfigMap;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import io.kubernetes.client.util.ClientBuilder;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -32,13 +33,21 @@
public class KubectlCreateTest {

private static final String DISCOVERY_API =
KubectlCreateTest.class.getClassLoader().getResource("discovery-api.json").getPath();
new File(KubectlCreateTest.class.getClassLoader().getResource("discovery-api.json").getPath())
.toString();

private static final String DISCOVERY_APIV1 =
KubectlCreateTest.class.getClassLoader().getResource("discovery-api-v1.json").getPath();
new File(
KubectlCreateTest.class
.getClassLoader()
.getResource("discovery-api-v1.json")
.getPath())
.toString();

private static final String DISCOVERY_APIS =
KubectlCreateTest.class.getClassLoader().getResource("discovery-apis.json").getPath();
new File(
KubectlCreateTest.class.getClassLoader().getResource("discovery-apis.json").getPath())
.toString();

private ApiClient apiClient;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.ModelMapper;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -41,7 +42,8 @@
public class KubectlDrainTest {

private static final String POD_LIST_API =
KubectlDrainTest.class.getClassLoader().getResource("pod-list.json").getPath();
new File(KubectlDrainTest.class.getClassLoader().getResource("pod-list.json").getPath())
.toString();

private ApiClient apiClient;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.models.V1ConfigMap;
import io.kubernetes.client.util.ClientBuilder;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -37,13 +38,20 @@
public class KubectlPatchTest {

private static final String DISCOVERY_API =
KubectlPatchTest.class.getClassLoader().getResource("discovery-api.json").getPath();
new File(KubectlPatchTest.class.getClassLoader().getResource("discovery-api.json").getPath())
.toString();

private static final String DISCOVERY_APIV1 =
KubectlPatchTest.class.getClassLoader().getResource("discovery-api-v1.json").getPath();
new File(
KubectlPatchTest.class
.getClassLoader()
.getResource("discovery-api-v1.json")
.getPath())
.toString();

private static final String DISCOVERY_APIS =
KubectlPatchTest.class.getClassLoader().getResource("discovery-apis.json").getPath();
new File(KubectlPatchTest.class.getClassLoader().getResource("discovery-apis.json").getPath())
.toString();

private ApiClient apiClient;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.kubernetes.client.openapi.models.V1StatefulSetList;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.ModelMapper;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -50,34 +51,56 @@ public class KubectlRolloutTest {
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());

private static final String DEPLOYMENT =
KubectlRolloutTest.class.getClassLoader().getResource("deployment.json").getPath();
new File(KubectlRolloutTest.class.getClassLoader().getResource("deployment.json").getPath())
.toString();

private static final String REPLICASET_LIST =
KubectlRolloutTest.class.getClassLoader().getResource("replicaset-list.json").getPath();
new File(
KubectlRolloutTest.class
.getClassLoader()
.getResource("replicaset-list.json")
.getPath())
.toString();

private static final String DAEMON_SET =
KubectlRolloutTest.class.getClassLoader().getResource("daemonset.json").getPath();
new File(KubectlRolloutTest.class.getClassLoader().getResource("daemonset.json").getPath())
.toString();

private static final String PATCHED_DAEMON_SET =
KubectlRolloutTest.class.getClassLoader().getResource("patched-daemonset.json").getPath();
new File(
KubectlRolloutTest.class
.getClassLoader()
.getResource("patched-daemonset.json")
.getPath())
.toString();

private static final String DAEMON_SET_CONTROLLER_REVISION_LIST =
KubectlRolloutTest.class
.getClassLoader()
.getResource("daemonset-controllerrevision-list.json")
.getPath();
new File(
KubectlRolloutTest.class
.getClassLoader()
.getResource("daemonset-controllerrevision-list.json")
.getPath())
.toString();

private static final String STATEFUL_SET =
KubectlRolloutTest.class.getClassLoader().getResource("statefulset.json").getPath();
new File(KubectlRolloutTest.class.getClassLoader().getResource("statefulset.json").getPath())
.toString();

private static final String PATCHED_STATEFUL_SET =
KubectlRolloutTest.class.getClassLoader().getResource("patched-statefulset.json").getPath();
new File(
KubectlRolloutTest.class
.getClassLoader()
.getResource("patched-statefulset.json")
.getPath())
.toString();

private static final String STATEFUL_SET_CONTROLLER_REVISION_LIST =
KubectlRolloutTest.class
.getClassLoader()
.getResource("statefulset-controllerrevision-list.json")
.getPath();
new File(
KubectlRolloutTest.class
.getClassLoader()
.getResource("statefulset-controllerrevision-list.json")
.getPath())
.toString();

@Before
public void setup() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.kubernetes.client.openapi.models.V1ReplicaSet;
import io.kubernetes.client.openapi.models.V1ReplicaSetList;
import io.kubernetes.client.util.ClientBuilder;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -46,10 +47,16 @@ public class DeploymentHelperTest {
@Rule public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort(), false);

private static final String DEPLOYMENT =
DeploymentHelperTest.class.getClassLoader().getResource("deployment.json").getPath();
new File(DeploymentHelperTest.class.getClassLoader().getResource("deployment.json").getPath())
.toString();

private static final String REPLICASET_LIST =
DeploymentHelperTest.class.getClassLoader().getResource("replicaset-list.json").getPath();
new File(
DeploymentHelperTest.class
.getClassLoader()
.getResource("replicaset-list.json")
.getPath())
.toString();

@Before
public void setup() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.kubernetes.client.openapi.models.V1Namespace;
import io.kubernetes.client.openapi.models.V1NamespaceList;
import io.kubernetes.client.util.ClientBuilder;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -46,15 +47,20 @@ public class PagerTest {

private ApiClient client;
private static final String LIST_PAGE0_FILE_PATH =
PagerTest.class.getClassLoader().getResource("namespace-list-pager0.json").getPath();
new File(PagerTest.class.getClassLoader().getResource("namespace-list-pager0.json").getPath())
.toString();
private static final String LIST_PAGE1_FILE_PATH =
PagerTest.class.getClassLoader().getResource("namespace-list-pager1.json").getPath();
new File(PagerTest.class.getClassLoader().getResource("namespace-list-pager1.json").getPath())
.toString();
private static final String LIST_PAGE2_FILE_PATH =
PagerTest.class.getClassLoader().getResource("namespace-list-pager2.json").getPath();
new File(PagerTest.class.getClassLoader().getResource("namespace-list-pager2.json").getPath())
.toString();
private static final String LIST_STATUS_FILE_PATH =
PagerTest.class.getClassLoader().getResource("status-400.json").getPath();
new File(PagerTest.class.getClassLoader().getResource("status-400.json").getPath())
.toString();
private static final String STATUS_BAD_TOKEN_FILE_PATH =
PagerTest.class.getClassLoader().getResource("bad-token-status.json").getPath();
new File(PagerTest.class.getClassLoader().getResource("bad-token-status.json").getPath())
.toString();
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());

@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import io.kubernetes.client.spring.extended.manifests.annotation.FromYaml;
import io.kubernetes.client.util.Yaml;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
Expand Down Expand Up @@ -91,7 +92,8 @@ private Object loadFromYaml(String targetFilePath) {
Path targetPath = Paths.get(targetFilePath);
if (!Files.exists(Paths.get(targetFilePath))) { // checks if it exists on the machine
// otherwise use load from classpath resources
Path classPath = Paths.get(getClass().getClassLoader().getResource(targetFilePath).getPath());
Path classPath =
new File(getClass().getClassLoader().getResource(targetFilePath).getFile()).toPath();
if (Files.exists(classPath)) { // use classpath it works
targetPath = classPath;
} else {
Expand Down
1 change: 1 addition & 0 deletions util/src/main/java/io/kubernetes/client/Copy.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ public Future<Integer> copyFileToPodAsync(

private Process execCopyToPod(String namespace, String pod, String container, Path destPath)
throws ApiException, IOException {
// TODO: This assumes Linux and won't work on Windows Containers (for many reasons...)
String parentPath = destPath.getParent() != null ? destPath.getParent().toString() : ".";
parentPath = parentPath.replace("\\", "/");
return this.exec(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ private static File findConfigFromEnv() {
if (kubeConfigPath == null) {
return null;
}

final File kubeConfig = new File(kubeConfigPath);
if (kubeConfig.exists()) {
return kubeConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ public class ClientBuilderTest {
Resources.getResource("kubeconfig-https").getPath();
private static final String KUBECONFIG_HTTPS_X509_FILE_PATH =
Resources.getResource("kubeconfig-https-x509").getPath();
private static final String SSL_CA_CERT_PATH = Resources.getResource("ca-cert.pem").getPath();
private static final String SSL_CA_CERT_PATH =
new File(Resources.getResource("ca-cert.pem").getPath()).toString();
private static final String INVALID_SSL_CA_CERT_PATH =
Resources.getResource("ca-cert-invalid.pem").getPath();
new File(Resources.getResource("ca-cert-invalid.pem").getPath()).toString();

private String basePath = "http://localhost";
private String apiKey = "ABCD";
Expand All @@ -62,6 +63,8 @@ public class ClientBuilderTest {
public void testDefaultClientWithNoFiles() throws Exception {
String path =
withEnvironmentVariable("HOME", "/non-existent")
.and("HOMEDRIVE", null)
.and("USERPROFILE", null)
.and("KUBECONFIG", null)
.execute(
() -> {
Expand Down Expand Up @@ -139,6 +142,8 @@ public void testInvalidKubeconfig() throws Exception {
String path =
withEnvironmentVariable("KUBECONFIG", "/non-existent")
.and("HOME", "/none-existent")
.and("HOMEDRIVE", null)
.and("USERPROFILE", null)
.execute(
() -> {
final ApiClient client = ClientBuilder.standard().build();
Expand Down
2 changes: 2 additions & 0 deletions util/src/test/java/io/kubernetes/client/util/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public void testDefaultClientNothingPresent() {
try {
String path =
withEnvironmentVariable("HOME", "/non-existent")
.and("HOMEDRIVE", null)
.and("USERPROFILE", null)
.execute(
() -> {
ApiClient client = Config.defaultClient();
Expand Down
18 changes: 18 additions & 0 deletions util/src/test/java/io/kubernetes/client/util/KubeConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,21 @@ public void testRefreshToken() {

@Test
public void testExecCredentials() throws Exception {
// TODO: test exec on Windows
if (System.getProperty("os.name").contains("Windows")) {
return;
}
KubeConfig kc = KubeConfig.loadKubeConfig(new StringReader(KUBECONFIG_EXEC));
kc.setFile(folder.newFile()); // just making sure it is ignored
assertEquals("abc123", kc.getAccessToken());
}

@Test
public void testExecCredentialsAlpha1() throws Exception {
// TODO: test exec on Windows
if (System.getProperty("os.name").contains("Windows")) {
return;
}
KubeConfig kc =
KubeConfig.loadKubeConfig(new StringReader(KUBECONFIG_EXEC.replace("v1beta1", "v1alpha1")));
assertEquals("abc123", kc.getAccessToken());
Expand Down Expand Up @@ -350,6 +358,11 @@ public void testExecCredentialsAlpha1() throws Exception {

@Test
public void testExecCredentialsEnv() throws Exception {
// TODO: test exec on Windows
if (System.getProperty("os.name").contains("Windows")) {
return;
}

KubeConfig kc = KubeConfig.loadKubeConfig(new StringReader(KUBECONFIG_EXEC_ENV));
assertEquals("abc123", kc.getAccessToken());
}
Expand All @@ -374,6 +387,11 @@ public void testExecCredentialsEnv() throws Exception {

@Test
public void testExecCredentialsBasedir() throws Exception {
// TODO: test exec on Windows
if (System.getProperty("os.name").contains("Windows")) {
return;
}

File basedir = folder.newFolder();
File config = new File(basedir, ".kubeconfig");
try (FileWriter writer = new FileWriter(config)) {
Expand Down
10 changes: 6 additions & 4 deletions util/src/test/java/io/kubernetes/client/util/SSLUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package io.kubernetes.client.util;

import io.kubernetes.client.Resources;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -23,15 +24,16 @@

public class SSLUtilsTest extends TestCase {

private static final String CLIENT_KEY_PATH = Resources.getResource("clientauth.key").getPath();
private static final String CLIENT_KEY_PATH =
new File(Resources.getResource("clientauth.key").getPath()).toString();
private static final String CLIENT_KEY_RSA_PATH =
Resources.getResource("clientauth-rsa.key").getPath();
new File(Resources.getResource("clientauth-rsa.key").getPath()).toString();
private static final String CLIENT_KEY_EC_PATH =
Resources.getResource("clientauth-ec.key").getPath();
new File(Resources.getResource("clientauth-ec.key").getPath()).toString();

public void testPKCS8KeyLoadDump()
throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
byte[] loaded = Files.readAllBytes(Paths.get(CLIENT_KEY_PATH));
byte[] loaded = Files.readAllBytes(Paths.get(CLIENT_KEY_PATH.replace("C:/", "")));
PrivateKey privateKey = SSLUtils.loadKey(loaded);
byte[] dumped = SSLUtils.dumpKey(privateKey);
PrivateKey reloaded = SSLUtils.loadKey(dumped);
Expand Down
Loading

0 comments on commit be50feb

Please sign in to comment.