Skip to content

Commit

Permalink
feat: add support for more platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyouping committed Nov 8, 2022
1 parent 1ad2d53 commit 7ae197c
Show file tree
Hide file tree
Showing 18 changed files with 207 additions and 36 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ An embedded Redis cluster for Java integration test.

## Supported Platforms

More platforms except Windows will be supported in the future. Currently only the following platforms are supported.
Only the following platforms are supported.

- macOS
- macOS arm64
- macOS x86_64
- Ubuntu arm64
- Ubuntu x86_64
- Debian x86_64
- RedHat x86_64
- CentOS x86_64

## Usage

Expand All @@ -17,7 +23,7 @@ Add this library to Maven pom.xml
<dependency>
<groupId>io.github.shiyouping</groupId>
<artifactId>embedded-redis-cluster</artifactId>
<version>0.0.3</version>
<version>0.0.4</version>
<scope>test</scope>
</dependency>
````
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.shiyouping</groupId>
<artifactId>embedded-redis-cluster</artifactId>
<version>0.0.3</version>
<version>0.0.4</version>

<name>Embedded Redis Cluster</name>
<description>An embedded Redis cluster for Java integration test</description>
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/github/shiyouping/redis/embedded/Platform.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.shiyouping.redis.embedded;

import lombok.Data;

/**
* Platform.
*
* @author [email protected]
* @since 08/11/2022
*/
@Data
public class Platform {

private OS os;
private ARCH arch;

public enum OS {
MACOS, DEBIAN, REDHAT, UBUNTU
}

public enum ARCH {
X86_64, ARM64
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import com.github.shiyouping.redis.embedded.util.TgzUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.github.shiyouping.redis.embedded.util.Preconditions.checkNotNull;
Expand Down Expand Up @@ -184,11 +182,11 @@ private void execute(final String command) {
try {
RedisCli.log.info("Executing command: {}", command);
final Process process = Runtime.getRuntime().exec(command);
this.logOutput(process.getInputStream());
this.logOutput(process);
final int result = process.waitFor();
RedisCli.log.info("Execution result={}", result);
} catch (final Exception e) {
final String message = "Failed to execute command=" + command;
final String message = "Failed to getOutput command=" + command;
RedisCli.log.error(message, e);
throw new EmbeddedRedisException(message, e);
}
Expand All @@ -205,12 +203,12 @@ private void execute(final List<String> commandList) {
builder.redirectErrorStream(true);

final Process process = builder.start();
this.logOutput(process.getInputStream());
this.logOutput(process);

final int result = process.waitFor();
RedisCli.log.info("Execution result={}", result);
} catch (final Exception e) {
final String message = "Failed to execute command=" + command;
final String message = "Failed to getOutput command=" + command;
RedisCli.log.error(message, e);
throw new EmbeddedRedisException(message, e);
}
Expand All @@ -220,17 +218,12 @@ private int getNumOfNode() {
return this.config.getMasterNodes() * (this.config.getClusterReplicas() + 1);
}

private void logOutput(final InputStream inputStream) {
checkNotNull(inputStream, "inputStream cannot be null");
private void logOutput(final Process process) {
checkNotNull(process, "process cannot be null");

try (final BufferedReader output = new BufferedReader(new InputStreamReader(inputStream))) {
final String message = output.lines().collect(Collectors.joining(RedisCli.ENTER));

if (message.isEmpty()) {
return;
}

RedisCli.log.debug(message);
try {
RedisCli.log.info(IOUtils.toString(process.getInputStream(), Charset.defaultCharset()));
RedisCli.log.error(IOUtils.toString(process.getErrorStream(), Charset.defaultCharset()));
} catch (final Exception e) {
final String message = "Failed to log the command output";
RedisCli.log.error(message, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.shiyouping.redis.embedded.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.nio.charset.Charset;

import static com.github.shiyouping.redis.embedded.util.Preconditions.checkNotBlank;

/**
* CommandLine.
*
* @author [email protected]
* @since 08/11/2022
*/
@Slf4j
public final class CommandLine {


public static String getOutput(final String command) {
checkNotBlank(command, "command cannot be null");

try {
final Process process = Runtime.getRuntime().exec(command);
final String output = IOUtils.toString(process.getInputStream(), Charset.defaultCharset());
CommandLine.log.info("The output of command={} is {}", command, output);
return output.trim();
} catch (final Exception e) {
CommandLine.log.error("Failed to getOutput the command " + command, e);
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public final class Environment {
public static final String REDIS_MASTER_NODES = "REDIS_MASTER_NODES";
public static final String REDIS_CLUSTER_NODE_TIMEOUT = "REDIS_CLUSTER_NODE_TIMEOUT";

private Environment() {
}

public static int getInt(final String name, final int defaultValue) {
try {
final String value = System.getenv(name);
Expand All @@ -21,11 +24,7 @@ public static int getInt(final String name, final int defaultValue) {
}
return Integer.parseInt(value);
} catch (final Exception e) {
Environment.log.error(
"Failed to get the environment variable for {}, use the default value={} instead. Error={}",
name,
defaultValue,
e.getMessage());
Environment.log.error("Failed to get the environment variable for {}, use the default value={} instead. Error={}", name, defaultValue, e.getMessage());
return defaultValue;
}
}
Expand All @@ -38,11 +37,7 @@ public static String getString(final String name, final String defaultValue) {
}
return value;
} catch (final Exception e) {
Environment.log.error(
"Failed to get the environment variable for {}, use the default value={} instead. Error={}",
name,
defaultValue,
e.getMessage());
Environment.log.error("Failed to get the environment variable for {}, use the default value={} instead. Error={}", name, defaultValue, e.getMessage());
return defaultValue;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.github.shiyouping.redis.embedded.util;

import com.github.shiyouping.redis.embedded.Platform;
import com.github.shiyouping.redis.embedded.exception.EmbeddedRedisException;
import lombok.extern.slf4j.Slf4j;

import static com.github.shiyouping.redis.embedded.Platform.ARCH;
import static com.github.shiyouping.redis.embedded.Platform.OS;

/**
* PlatformUtil.
*
* @author [email protected]
* @since 08/11/2022
*/
@Slf4j
public final class PlatformUtil {

private PlatformUtil() {
}


public static Platform getPlatform() {
final String os = System.getProperty("os.name").toLowerCase();
PlatformUtil.log.info("The operation system is {}", os);

if (!os.contains("mac") && !os.contains("linux")) {
throw new EmbeddedRedisException("Unsupported OS: " + os);
}

final Platform platform = new Platform();
platform.setArch(PlatformUtil.getArch());

if (os.contains("mac")) {
platform.setOs(OS.MACOS);
return platform;
}

platform.setOs(PlatformUtil.getLinuxOs());
return platform;
}

private static ARCH getArch() {
String arch = CommandLine.getOutput("arch");
PlatformUtil.log.info("The architecture is {}", arch);

if (arch == null) {
throw new EmbeddedRedisException("Unable to get macos architecture");
}

arch = arch.toLowerCase();

if (arch.contains("arm") || arch.contains("aarch")) {
return ARCH.ARM64;
}

return ARCH.X86_64;
}

private static OS getLinuxOs() {
String os = CommandLine.getOutput("grep '^NAME' /etc/os-release");
if (os == null) {
throw new EmbeddedRedisException("Unable to get linux os");
}

os = os.toLowerCase();

if (os.contains("ubuntu")) {
return OS.UBUNTU;
}

if (os.contains("debian")) {
return OS.DEBIAN;
}

if (os.contains("red hat")) {
return OS.REDHAT;
}

throw new EmbeddedRedisException("Unsupported linux os " + os);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.shiyouping.redis.embedded.util;

import com.github.shiyouping.redis.embedded.Platform;
import com.github.shiyouping.redis.embedded.exception.EmbeddedRedisException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.ArchiveEntry;
Expand All @@ -25,7 +26,10 @@
* @since 23/10/2022
*/
@Slf4j
public class TgzUtil {
public final class TgzUtil {

private TgzUtil() {
}

public static void copyTgz(final String source, final Path targetDir) {
checkNotBlank(source, "source cannot be blank");
Expand Down Expand Up @@ -91,9 +95,13 @@ public static String getTgzName() {
try (final InputStream inputStream = TgzUtil.class.getClassLoader().getResourceAsStream("config.properties")) {
final Properties properties = new Properties();
properties.load(inputStream);

final String version = properties.getProperty("redis.version");
return String.format("macos-redis-%s", version);

final Platform platform = PlatformUtil.getPlatform();
final String os = platform.getOs().toString().toLowerCase();
final String arch = platform.getArch().toString().toLowerCase();

return String.format("%s-%s-%s", os, arch, version);
} catch (final Exception e) {
final String message = "Failed to get tgz name";
TgzUtil.log.error(message, e);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
redis.version=6.2.7
redis.version=6.2.4
Binary file added src/main/resources/debian-x86_64-6.2.4.tgz
Binary file not shown.
Binary file added src/main/resources/macos-arm64-6.2.4.tgz
Binary file not shown.
Binary file removed src/main/resources/macos-redis-6.2.7.tgz
Binary file not shown.
Binary file added src/main/resources/macos-x86_64-6.2.4.tgz
Binary file not shown.
Binary file added src/main/resources/redhat-x86_64-6.2.4.tgz
Binary file not shown.
Binary file added src/main/resources/ubuntu-arm64-6.2.4.tgz
Binary file not shown.
Binary file added src/main/resources/ubuntu-x86_64-6.2.4.tgz
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.shiyouping.redis.embedded.util;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class CommandLineTest {

@Test
public void shouldGetOutput() {
assertThat(CommandLine.getOutput("date")).isNotBlank();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.shiyouping.redis.embedded.util;

import com.github.shiyouping.redis.embedded.Platform;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class PlatformUtilTest {

@Test
public void shouldGetPlatform() {
final Platform platform = PlatformUtil.getPlatform();
assertThat(platform).isNotNull();
assertThat(platform.getArch()).isNotNull();
assertThat(platform.getOs()).isNotNull();
}
}

0 comments on commit 7ae197c

Please sign in to comment.