Skip to content
This repository was archived by the owner on Apr 10, 2024. It is now read-only.

Commit cc73de4

Browse files
committed
fix: refactorings and additional test cases
1 parent b0cb0b1 commit cc73de4

File tree

13 files changed

+181
-76
lines changed

13 files changed

+181
-76
lines changed

pom.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
<assertj.version>3.24.2</assertj.version>
2121
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2222
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
23-
<maven.compiler.target>11</maven.compiler.target>
24-
<maven.compiler.source>11</maven.compiler.source>
25-
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
26-
<maven-surefire-plugin.version>3.0.0-M9</maven-surefire-plugin.version>
2723
<commons-compress.version>1.22</commons-compress.version>
2824
<google.cloud.libraries.version>26.9.0</google.cloud.libraries.version>
2925
<commons-io.version>2.11.0</commons-io.version>
3026
<bouncycastle.version>1.72</bouncycastle.version>
27+
<mokito.version>5.1.1</mokito.version>
28+
<maven.compiler.target>11</maven.compiler.target>
29+
<maven.compiler.source>11</maven.compiler.source>
30+
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
31+
<maven-surefire-plugin.version>3.0.0-M9</maven-surefire-plugin.version>
3132
</properties>
3233

3334
<dependencyManagement>
@@ -74,6 +75,11 @@
7475
<artifactId>junit-jupiter-engine</artifactId>
7576
<version>${junit.version}</version>
7677
</dependency>
78+
<dependency>
79+
<groupId>org.mockito</groupId>
80+
<artifactId>mockito-core</artifactId>
81+
<version>${mokito.version}</version>
82+
</dependency>
7783
<dependency>
7884
<groupId>org.slf4j</groupId>
7985
<artifactId>slf4j-api</artifactId>

src/main/java/com/csviri/jenvtest/APIServer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.csviri.jenvtest;
22

33
import com.csviri.jenvtest.binary.BinaryManager;
4+
import com.csviri.jenvtest.process.*;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
67

src/main/java/com/csviri/jenvtest/Utils.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.csviri.jenvtest;
22

3+
import com.csviri.jenvtest.binary.OSInfoProvider;
34
import org.slf4j.Logger;
45

56
import java.io.InputStream;
6-
import java.io.OutputStream;
77
import java.util.Comparator;
88
import java.util.List;
99
import java.util.Scanner;
@@ -15,19 +15,6 @@ private Utils() {
1515

1616
public static final SemverComparator SEMVER_COMPARATOR = new SemverComparator();
1717

18-
public static String getOSName() {
19-
String os = System.getProperty("os.name").toLowerCase();
20-
if (os.contains("win")) {
21-
return "windows";
22-
} else {
23-
return os;
24-
}
25-
}
26-
27-
public static String getOSArch() {
28-
return System.getProperty("os.arch").toLowerCase();
29-
}
30-
3118
public static String getLatestVersion(List<String> versions) {
3219
versions.sort(SEMVER_COMPARATOR);
3320
return versions.get(versions.size()-1);
@@ -62,4 +49,8 @@ public static void redirectProcessOutputToLogger(InputStream outputStream, Logge
6249
}).start();
6350
}
6451

52+
public static String platformSuffix(OSInfoProvider osInfoProvider) {
53+
return "-" + osInfoProvider.getOSName() + "-" + osInfoProvider.getOSArch();
54+
}
55+
6556
}

src/main/java/com/csviri/jenvtest/binary/BinaryDownloader.java

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,57 @@
22

33
import com.csviri.jenvtest.JenvtestException;
44
import com.csviri.jenvtest.Utils;
5-
import com.google.cloud.storage.Storage;
6-
import com.google.cloud.storage.StorageOptions;
75
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
86
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
97
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
10-
import org.apache.commons.io.FileUtils;
118
import org.slf4j.Logger;
129
import org.slf4j.LoggerFactory;
1310

1411
import java.io.BufferedInputStream;
1512
import java.io.File;
1613
import java.io.FileInputStream;
1714
import java.io.IOException;
18-
import java.net.URL;
1915
import java.nio.file.Files;
2016
import java.util.stream.Collectors;
21-
import java.util.stream.StreamSupport;
2217

2318
public class BinaryDownloader {
2419

2520
private static final Logger log = LoggerFactory.getLogger(BinaryDownloader.class);
2621

27-
private static final String BUCKET_NAME = "kubebuilder-tools";
28-
private static final String TAR_PREFIX = "kubebuilder-tools-";
22+
private static final String OBJECT_TAR_PREFIX = "kubebuilder-tools-";
2923

30-
private String jenvtestDir;
24+
private final String jenvtestDir;
25+
private final BinaryRepo binaryRepo;
26+
private final OSInfoProvider osInfoProvider;
3127

32-
public BinaryDownloader(String jenvtestDir) {
28+
public BinaryDownloader(String jenvtestDir, OSInfoProvider osInfoProvider) {
3329
this.jenvtestDir = jenvtestDir;
30+
this.osInfoProvider = osInfoProvider;
31+
this.binaryRepo = new BinaryRepo(osInfoProvider);
32+
3433
}
3534

36-
public File download(String version) {
37-
try {
38-
log.info("Downloading binaries with version: {}", version);
39-
String url = "https://storage.googleapis.com/kubebuilder-tools/kubebuilder-tools-" + version +
40-
"-" + Utils.getOSName() + "-" + Utils.getOSArch() + ".tar.gz";
35+
BinaryDownloader(String jenvtestDir, BinaryRepo binaryRepo,OSInfoProvider osInfoProvider) {
36+
this.jenvtestDir = jenvtestDir;
37+
this.binaryRepo = binaryRepo;
38+
this.osInfoProvider = osInfoProvider;
39+
}
4140

42-
File tempFile = File.createTempFile("kubebuilder-tools", ".tar.gz");
43-
log.debug("Downloading binary from url: {} to Temp file: {}", url, tempFile.getPath());
44-
FileUtils.copyURLToFile(new URL(url), tempFile);
45-
File dir = createDirForBinaries(version);
46-
extractFiles(tempFile, dir);
47-
var deleted = tempFile.delete();
48-
if (!deleted) {
49-
log.warn("Unable to delete temp file: {}", tempFile.getPath());
50-
}
51-
return dir;
52-
} catch (IOException e) {
53-
throw new JenvtestException(e);
41+
public File download(String version) {
42+
log.info("Downloading binaries with version: {}", version);
43+
var tempFile = binaryRepo.downloadVersionToTempFile(version);
44+
File dir = createDirForBinaries(version);
45+
extractFiles(tempFile, dir);
46+
var deleted = tempFile.delete();
47+
if (!deleted) {
48+
log.warn("Unable to delete temp file: {}", tempFile.getPath());
5449
}
50+
return dir;
51+
}
52+
53+
public File downloadLatest() {
54+
String latest = findLatestVersion();
55+
return download(latest);
5556
}
5657

5758
private void extractFiles(File tempFile, File dir) {
@@ -61,8 +62,8 @@ private void extractFiles(File tempFile, File dir) {
6162
while (entry != null) {
6263
if (!entry.isDirectory()) {
6364
File file = extractEntry(entry, dir, tarIn);
64-
if(!file.setExecutable(true)) {
65-
throw new JenvtestException("Cannot make the file executable: "+file.getPath());
65+
if (!file.setExecutable(true)) {
66+
throw new JenvtestException("Cannot make the file executable: " + file.getPath());
6667
}
6768
}
6869
entry = tarIn.getNextTarEntry();
@@ -90,26 +91,20 @@ private File extractEntry(TarArchiveEntry entry, File dir, TarArchiveInputStream
9091

9192
private File createDirForBinaries(String version) {
9293
var dir = new File(jenvtestDir, BinaryManager.BINARY_LIST_DIR + File.separator
93-
+ version + BinaryManager.PLATFORM_SUFFIX);
94+
+ version + Utils.platformSuffix(osInfoProvider));
9495
if (!dir.mkdirs()) {
9596
throw new JenvtestException("Cannot created director: " + dir.getPath());
9697
}
9798
return dir;
9899
}
99100

100-
public File downloadLatest() {
101-
String latest = findLatestVersion();
102-
return download(latest);
103-
}
104-
105-
private String findLatestVersion() {
106-
Storage storage = StorageOptions.getDefaultInstance().getService();
107-
var blobs = storage.get(BUCKET_NAME).list();
108-
var allRelevantVersions = StreamSupport.stream(blobs.iterateAll().spliterator(), false).filter(b ->
109-
b.asBlobInfo().getName().contains(Utils.getOSName())
110-
&& b.asBlobInfo().getName().contains(Utils.getOSArch()))
111-
.map(b -> {
112-
String stripped = b.asBlobInfo().getName().replace(TAR_PREFIX, "");
101+
public String findLatestVersion() {
102+
var objects = binaryRepo.listObjectNames();
103+
var allRelevantVersions = objects.filter(o ->
104+
o.contains(osInfoProvider.getOSName())
105+
&& o.contains(osInfoProvider.getOSArch()))
106+
.map(o -> {
107+
String stripped = o.replace(OBJECT_TAR_PREFIX, "");
113108
String version = stripped.substring(0, stripped.indexOf("-"));
114109
if (version.startsWith("v")) {
115110
version = version.substring(1);

src/main/java/com/csviri/jenvtest/binary/BinaryManager.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414
public class BinaryManager {
1515

1616
public static final String BINARY_LIST_DIR = "k8s";
17-
public static final String PLATFORM_SUFFIX = "-" + Utils.getOSName() + "-" + Utils.getOSArch();
1817

1918
private Binaries binaries;
2019
private final APIServerConfig config;
21-
private BinaryDownloader downloader;
20+
private final BinaryDownloader downloader;
21+
private final OSInfoProvider osInfoProvider;
2222

2323
public BinaryManager(APIServerConfig config) {
2424
this.config = config;
25-
downloader = new BinaryDownloader(config.getJenvtestDir());
25+
this.osInfoProvider = new OSInfoProvider();
26+
this.downloader = new BinaryDownloader(config.getJenvtestDir(), osInfoProvider);
2627
}
2728

2829
public void initAndDownloadIfRequired() {
@@ -63,22 +64,24 @@ public Binaries binaries() {
6364
}
6465

6566
private Optional<File> findLatestBinariesAvailable() {
67+
var platformSuffix = Utils.platformSuffix(osInfoProvider);
6668
if (config.getApiServerVersion().isPresent()) {
6769
return Optional.of(new File(config.getJenvtestDir(), BINARY_LIST_DIR
68-
+ File.separator + config.getApiServerVersion() + PLATFORM_SUFFIX));
70+
+ File.separator + config.getApiServerVersion() + platformSuffix));
6971
}
7072
File binariesListDir = new File(config.getJenvtestDir(), BINARY_LIST_DIR);
7173
if (!binariesListDir.exists()) {
7274
return Optional.empty();
7375
}
74-
var dirVersionList = List.of(binariesListDir.list((dir, name) -> name.endsWith(PLATFORM_SUFFIX)))
75-
.stream().map(s -> s.substring(0, s.indexOf(PLATFORM_SUFFIX)))
76+
var dirVersionList = List.of(binariesListDir.list((dir, name) -> name != null
77+
&& name.endsWith(platformSuffix)))
78+
.stream().map(s -> s.substring(0, s.indexOf(platformSuffix)))
7679
.collect(Collectors.toList());
7780

7881
if (dirVersionList.isEmpty()) {
7982
return Optional.empty();
8083
}
81-
String latest = Utils.getLatestVersion(dirVersionList) + PLATFORM_SUFFIX;
84+
String latest = Utils.getLatestVersion(dirVersionList) + platformSuffix;
8285
return Optional.of(new File(binariesListDir, latest));
8386
}
8487

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.csviri.jenvtest.binary;
2+
3+
import com.csviri.jenvtest.JenvtestException;
4+
import com.csviri.jenvtest.Utils;
5+
import com.google.cloud.storage.Storage;
6+
import com.google.cloud.storage.StorageOptions;
7+
import org.apache.commons.io.FileUtils;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import java.io.File;
12+
import java.io.IOException;
13+
import java.net.URL;
14+
import java.util.stream.Stream;
15+
import java.util.stream.StreamSupport;
16+
17+
public class BinaryRepo {
18+
19+
private static final Logger log = LoggerFactory.getLogger(BinaryRepo.class);
20+
21+
private static final String BUCKET_NAME = "kubebuilder-tools";
22+
23+
private final OSInfoProvider osInfoProvider;
24+
25+
public BinaryRepo(OSInfoProvider osInfoProvider) {
26+
this.osInfoProvider = osInfoProvider;
27+
}
28+
29+
30+
public File downloadVersionToTempFile(String version) {
31+
try {
32+
String url = "https://storage.googleapis.com/kubebuilder-tools/kubebuilder-tools-" + version +
33+
"-" + osInfoProvider.getOSName() + "-" + osInfoProvider.getOSArch() + ".tar.gz";
34+
35+
File tempFile = File.createTempFile("kubebuilder-tools-"+version, ".tar.gz");
36+
log.debug("Downloading binary from url: {} to Temp file: {}", url, tempFile.getPath());
37+
FileUtils.copyURLToFile(new URL(url), tempFile);
38+
return tempFile;
39+
} catch (IOException e) {
40+
throw new JenvtestException(e);
41+
}
42+
}
43+
44+
45+
public Stream<String> listObjectNames() {
46+
Storage storage = StorageOptions.getDefaultInstance().getService();
47+
var blobs = storage.get(BUCKET_NAME).list();
48+
return StreamSupport.stream(blobs.iterateAll().spliterator(), false).map(b -> b.asBlobInfo().getName());
49+
}
50+
51+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.csviri.jenvtest.binary;
2+
3+
public class OSInfoProvider {
4+
5+
public String getOSName() {
6+
String os = System.getProperty("os.name").toLowerCase();
7+
if (os.contains("win")) {
8+
return "windows";
9+
} else {
10+
return os;
11+
}
12+
}
13+
14+
public String getOSArch() {
15+
return System.getProperty("os.arch").toLowerCase();
16+
}
17+
18+
19+
}

src/main/java/com/csviri/jenvtest/APIServerProcessManager.java renamed to src/main/java/com/csviri/jenvtest/process/APIServerProcessManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package com.csviri.jenvtest;
1+
package com.csviri.jenvtest.process;
22

3+
import com.csviri.jenvtest.*;
34
import com.csviri.jenvtest.binary.BinaryManager;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;

src/main/java/com/csviri/jenvtest/EtcdProcessManager.java renamed to src/main/java/com/csviri/jenvtest/process/EtcdProcessManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
package com.csviri.jenvtest;
1+
package com.csviri.jenvtest.process;
22

3+
import com.csviri.jenvtest.JenvtestException;
4+
import com.csviri.jenvtest.Utils;
35
import com.csviri.jenvtest.binary.BinaryManager;
46
import org.apache.commons.io.FileUtils;
57
import org.slf4j.Logger;

src/main/java/com/csviri/jenvtest/UnexpectedProcessStopHandler.java renamed to src/main/java/com/csviri/jenvtest/process/UnexpectedProcessStopHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.csviri.jenvtest;
1+
package com.csviri.jenvtest.process;
22

33
public interface UnexpectedProcessStopHandler {
44

0 commit comments

Comments
 (0)