Skip to content

Commit

Permalink
Fix #2218: Uberjar: Package rewrite issues in META-INF/services
Browse files Browse the repository at this point in the history
Looks like space in `resource-handler-services.vm` file was the root cause of
the issue 😆
  • Loading branch information
rohanKanojia committed May 18, 2020
1 parent d073f75 commit 80bbd0a
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
#set ($packageSuffix = $annotation.getParameters().get("value"))
#end
#end
io.fabric8.kubernetes.client.handlers${packageSuffix}.${m.name}Handler

io.fabric8.kubernetes.client.handlers${packageSuffix}.${m.name}Handler
#end
37 changes: 37 additions & 0 deletions uberjar/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@
<artifactId>generex</artifactId>
<version>${generex.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -369,6 +380,32 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*UberJarTest.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>test</goal>
</goals>
<phase>integration-test</phase>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/*UberJarTest.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Expand Down
211 changes: 211 additions & 0 deletions uberjar/src/test/java/io/fabric8/kubernetes/clnt/UberJarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.clnt;

import org.junit.Rule;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import org.junit.rules.TemporaryFolder;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@EnableRuleMigrationSupport
public class UberJarTest {
private static final String OUTPUT_DIR = System.getProperty("user.dir") + File.separator + "target";
private static final File pomFile = new File(System.getProperty("user.dir") + File.separator + "pom.xml");
private static final String JAR_NAME_SEPARATOR = "-";
private static final String JAR_SUFFIX = ".jar";
private static final String ARTIFACT_ID = "kubernetes-openshift-uberjar";

@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test
@DisplayName("UberJar should be generated and should contain necessary files")
public void testUberJar() throws IOException {
// Given
String projectVersion = getProjectVersion();
assertNotNull(projectVersion);
String uberJarFilePath = OUTPUT_DIR + File.separator + ARTIFACT_ID + JAR_NAME_SEPARATOR + projectVersion + JAR_SUFFIX;

// When
File uberJar = new File(uberJarFilePath);
File jarExtractedDir = folder.newFolder("extractedJar");
unzip(uberJar.getAbsolutePath(), jarExtractedDir.getAbsolutePath());

// Then
assertTrue(uberJar.exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/builder/Builder.class").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/builder/Visitor.class").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/admission").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/apps").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/autoscaling").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/coordination").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/extensions").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/policy").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/settings").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/admissionregistration").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/authentication").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/batch").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/discovery").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/metrics").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/rbac").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/storage").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/authorization").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/client/dsl").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/client/handlers").exists());
File resourceHandlerServiceFile = getFileInDirectory(jarExtractedDir, "META-INF/services/io.fabric8.kubernetes.client.ResourceHandler");
assertTrue(resourceHandlerServiceFile.exists());
assertTrue(containsOpenShiftKubernetesResources(resourceHandlerServiceFile, null, null));
assertTrue(getFileInDirectory(jarExtractedDir, "META-INF/services/io.fabric8.kubernetes.client.ExtensionAdapter").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "META-INF/services/io.fabric8.kubernetes.client.ServiceToURLProvider").exists());
}

@Test
public void testUberJarVersioned() throws IOException {
// Given
String projectVersion = getProjectVersion();
assertNotNull(projectVersion);
String majorVersion = getMajorVersion(projectVersion);
String minorVersion = getMinorVersion(projectVersion);
String versionedJarFilePath = OUTPUT_DIR + File.separator + ARTIFACT_ID + JAR_NAME_SEPARATOR + projectVersion + JAR_NAME_SEPARATOR + "versioned" + JAR_SUFFIX;

// When
File uberJarVersioned = new File(versionedJarFilePath);
File jarExtractedDir = folder.newFolder("extractedJar");
unzip(uberJarVersioned.getAbsolutePath(), jarExtractedDir.getAbsolutePath());

// Then
assertTrue(uberJarVersioned.exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/builder/v" + majorVersion + "_" + minorVersion + "/Builder.class").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/builder/v" + majorVersion + "_" + minorVersion + "/Visitor.class").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/v" + majorVersion + "_" + minorVersion + "/admission").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/v" + majorVersion + "_" + minorVersion + "/apps").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/v" + majorVersion + "_" + minorVersion + "/autoscaling").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/v" + majorVersion + "_" + minorVersion + "/coordination").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/v" + majorVersion + "_" + minorVersion + "/extensions").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/v" + majorVersion + "_" + minorVersion + "/policy").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/v" + majorVersion + "_" + minorVersion + "/settings").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/v" + majorVersion + "_" + minorVersion + "/admissionregistration").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/v" + majorVersion + "_" + minorVersion + "/authentication").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/v" + majorVersion + "_" + minorVersion + "/batch").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/v" + majorVersion + "_" + minorVersion + "/discovery").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/v" + majorVersion + "_" + minorVersion + "/metrics").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/v" + majorVersion + "_" + minorVersion + "/rbac").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/v" + majorVersion + "_" + minorVersion + "/storage").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/api/model/v" + majorVersion + "_" + minorVersion + "/authorization").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/clnt/v" + majorVersion + "_" + minorVersion + "/dsl").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "io/fabric8/kubernetes/clnt/v" + majorVersion + "_" + minorVersion + "/handlers").exists());
File resourceHandlerServiceFile = getFileInDirectory(jarExtractedDir, "META-INF/services/io.fabric8.kubernetes.clnt.v" + majorVersion + "_" + minorVersion + ".ResourceHandler");
assertTrue(resourceHandlerServiceFile.exists());
assertTrue(containsOpenShiftKubernetesResources(resourceHandlerServiceFile, majorVersion, minorVersion));
assertTrue(getFileInDirectory(jarExtractedDir, "META-INF/services/io.fabric8.kubernetes.clnt.v" + majorVersion + "_" + minorVersion + ".ExtensionAdapter").exists());
assertTrue(getFileInDirectory(jarExtractedDir, "META-INF/services/io.fabric8.kubernetes.clnt.v" + majorVersion + "_" + minorVersion + ".ServiceToURLProvider").exists());
}

private boolean containsOpenShiftKubernetesResources(File resourceHandlerServiceFile, String majorVersion, String minorVersion) throws IOException {
List<String> lines = Files.readAllLines(resourceHandlerServiceFile.toPath());
final String deploymentConfigOpenShiftHandler = (majorVersion != null && minorVersion != null) ?
"io.fabric8.openshift.clnt.v" + majorVersion + "_" + minorVersion + ".handlers.DeploymentConfigHandler" :
"io.fabric8.openshift.client.handlers.DeploymentConfigHandler";
final String deploymentKubernetesHandler = (majorVersion != null && minorVersion != null) ?
"io.fabric8.kubernetes.clnt.v" + majorVersion + "_" + minorVersion + ".handlers.apps.v1.DeploymentHandler" :
"io.fabric8.kubernetes.client.handlers.apps.v1.DeploymentHandler";

return lines.stream().anyMatch(line -> line.contains(deploymentConfigOpenShiftHandler)) &&
lines.stream().anyMatch(line -> line.contains(deploymentKubernetesHandler));
}

private String getMajorVersion(String projectVersion) {
return projectVersion.split("\\.")[0];
}

private String getMinorVersion(String projectVersion) {
String[] versionParts = projectVersion.split("\\.");
String minorPatchVersion = projectVersion.substring(versionParts[0].length() + 1);
if (minorPatchVersion.contains("-")) { // SNAPSHOT VERSION
return minorPatchVersion.split("-")[0];
} else { // RELEASE VERSION
return versionParts[1];
}
}

private File getFileInDirectory(File parentFile, String pathToFile) {
pathToFile = pathToFile.replaceAll("/", File.separator);
return new File(parentFile, pathToFile);
}

private String getProjectVersion() throws IOException {
List<String> pomLines = readPomLines();
for (String line : pomLines) {
if (line.contains("<version>")) {
line = line.replaceAll("version>", "");
line = line.replaceAll("/", "");
line = line.replaceAll("<", "");
line = line.replaceAll(" ", "");
return line;
}
}
return null;
}

private List<String> readPomLines() throws IOException {
return Files.readAllLines(pomFile.toPath());
}

public void unzip(String zipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
extractFile(zipIn, filePath);
} else {
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}

private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[4096];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
}
}

0 comments on commit 80bbd0a

Please sign in to comment.