Skip to content

Commit

Permalink
GEODE-8504: add redis info command (apache#5526)
Browse files Browse the repository at this point in the history
Co-authored-by: john Hutchison <[email protected]>
Co-authored-by: Darrel Schneider <[email protected]>
  • Loading branch information
3 people authored Sep 22, 2020
1 parent 5468d65 commit 0c86d4f
Show file tree
Hide file tree
Showing 15 changed files with 374 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You 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 org.apache.geode.redis.internal.executor.server;

import org.junit.BeforeClass;
import org.junit.ClassRule;
import redis.clients.jedis.Jedis;

import org.apache.geode.NativeRedisTestRule;

public class InfoNativeRedisAcceptanceTest extends InfoIntegrationTest {

@ClassRule
public static NativeRedisTestRule redis = new NativeRedisTestRule();

@Override
protected int getTCPPort() {
return redis.getExposedPort();
}

@BeforeClass
public static void setUp() {
jedis = new Jedis("localhost", redis.getPort(), REDIS_CLIENT_TIMEOUT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ public class NativeRedisTestRule extends ExternalResource implements Serializabl

private GenericContainer<?> redisContainer;
private final RuleChain delegate;
private final int PORT_TO_EXPOSE = 6379;

public NativeRedisTestRule() {
delegate = RuleChain
// Docker compose does not work on windows in CI. Ignore this test on windows
// Using a RuleChain to make sure we ignore the test before the rule comes into play
.outerRule(new IgnoreOnWindowsRule())
// The ryuk container is responsible for cleanup at JVM exit. Since this rule already closes
// the
// The ryuk container is responsible for cleanup at JVM exit.
// Since this rule already closes the
// container it has started, we do not need the ryuk container.
.around(new EnvironmentVariables().set("TESTCONTAINERS_RYUK_DISABLED", "true"));
}
Expand All @@ -46,12 +47,20 @@ public int getPort() {
return redisContainer.getFirstMappedPort();
}

public int getExposedPort() {
return redisContainer.getExposedPorts().get(0);
}

@Override
public Statement apply(Statement base, Description description) {
Statement containerStatement = new Statement() {
@Override
public void evaluate() throws Throwable {
redisContainer = new GenericContainer<>("redis:5.0.6").withExposedPorts(6379);

redisContainer =
new GenericContainer<>("redis:5.0.6")
.withExposedPorts(PORT_TO_EXPOSE);

redisContainer.start();
try {
base.evaluate(); // This will run the test.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You 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 org.apache.geode.redis.internal.executor.server;

import static org.apache.geode.distributed.ConfigurationProperties.LOG_LEVEL;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.Arrays;
import java.util.List;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Protocol;

import org.apache.geode.redis.GeodeRedisServerRule;

public class InfoIntegrationTest {
public static Jedis jedis;

public static int REDIS_CLIENT_TIMEOUT = 10000;

@ClassRule
public static GeodeRedisServerRule server =
new GeodeRedisServerRule()
.withProperty(LOG_LEVEL, "info");

@BeforeClass
public static void setUp() {
jedis =
new Jedis("localhost",
server.getPort(),
REDIS_CLIENT_TIMEOUT);
}

@AfterClass
public static void tearDown() {
jedis.close();
}

protected int getTCPPort() {
return server.getPort();
}

@Test
public void shouldReturnRedisVersion() {
final String EXPECTED_RESULT = "redis_version:5.0.6";

String actualResult = jedis.info();

assertThat(actualResult).contains(EXPECTED_RESULT);
}

@Test
public void shouldReturnTCPPort() {
final int EXPECTED_PORT = getTCPPort();
final String EXPECTED_RESULT = "tcp_port:" + EXPECTED_PORT;

String actualResult = jedis.info();

assertThat(actualResult).contains(EXPECTED_RESULT);
}

@Test
public void shouldReturnRedisMode() {
final String EXPECTED_RESULT = "redis_mode:standalone";

String actualResult = jedis.info();

assertThat(actualResult).contains(EXPECTED_RESULT);
}

@Test
public void shouldReturnLoadingProperty() {
final String EXPECTED_RESULT = "loading:0";

String actualResult = jedis.info();

assertThat(actualResult).contains(EXPECTED_RESULT);
}

@Test
public void shouldReturnClusterEnabledProperty() {
final String EXPECTED_RESULT = "cluster_enabled:0";

String actualResult = jedis.info();

assertThat(actualResult).contains(EXPECTED_RESULT);
}

final List<String> SERVER_PROPERTIES =
Arrays.asList(
"# Server",
"redis_version:",
"tcp_port:",
"redis_mode:");

final List<String> PERSISTENCE_PROPERTIES =
Arrays.asList(
"# Persistence",
"loading:");

final List<String> CLUSTER_PROPERTIES =
Arrays.asList(
"# Cluster",
"cluster_enabled:");

@Test
public void shouldReturnServerSectionsGivenServerSectionParameter() {
String actualResult = jedis.info("server");

assertThat(actualResult).contains(SERVER_PROPERTIES);
assertThat(actualResult).doesNotContain(CLUSTER_PROPERTIES);
assertThat(actualResult).doesNotContain(PERSISTENCE_PROPERTIES);
}

@Test
public void shouldReturnClusterSectionsGivenClusterSectionParameter() {
String actualResult = jedis.info("cluster");

assertThat(actualResult).contains(CLUSTER_PROPERTIES);
assertThat(actualResult).doesNotContain(SERVER_PROPERTIES);
assertThat(actualResult).doesNotContain(PERSISTENCE_PROPERTIES);
}

@Test
public void shouldReturnPersistenceSectionsGivenPersistenceSectionParameter() {
String actualResult = jedis.info("persistence");

assertThat(actualResult).contains(PERSISTENCE_PROPERTIES);
assertThat(actualResult).doesNotContain(SERVER_PROPERTIES);
assertThat(actualResult).doesNotContain(CLUSTER_PROPERTIES);
}

@Test
public void shouldReturnEmptyStringGivenUnknownParameter() {
String actualResult = jedis.info("nonesuch");
assertThat(actualResult).isEqualTo("");
}


@Test
public void shouldReturnDefaultsGivenDefaultParameter() {
String actualResult = jedis.info("default");
assertThat(actualResult).contains(CLUSTER_PROPERTIES);
assertThat(actualResult).contains(SERVER_PROPERTIES);
assertThat(actualResult).contains(PERSISTENCE_PROPERTIES);
}

@Test
public void shouldReturnDefaultsGivenAllParameter() {
String actualResult = jedis.info("all");
assertThat(actualResult).contains(CLUSTER_PROPERTIES);
assertThat(actualResult).contains(SERVER_PROPERTIES);
assertThat(actualResult).contains(PERSISTENCE_PROPERTIES);
}

@Test
public void shouldThrowExceptionIfGivenMoreThanOneParameter() {
assertThatThrownBy(
() -> jedis.sendCommand(
Protocol.Command.INFO, "Server", "Cluster")).hasMessageContaining("ERR syntax error");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void setUp() {
}

@After
public void classLevelTearDown() {
public void tearDown() {
jedis.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class EvenParameterRequirements implements ParameterRequirements {
@Override
public void checkParameters(Command command, ExecutionHandlerContext executionHandlerContext) {
if (!isEven(command.getProcessedCommand().size())) {
throw new RedisParametersMismatchException(command.wrongNumberOfArgumentsError());
throw new RedisParametersMismatchException(command.wrongNumberOfArgumentsErrorMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ExactParameterRequirements(int number) {
@Override
public void checkParameters(Command command, ExecutionHandlerContext executionHandlerContext) {
if (command.getProcessedCommand().size() != number) {
throw new RedisParametersMismatchException(command.wrongNumberOfArgumentsError());
throw new RedisParametersMismatchException(command.wrongNumberOfArgumentsErrorMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,32 @@
import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;

public class MaximumParameterRequirements implements ParameterRequirements {
private int maximum;
private final int maximum;
private final String exceptionMessage;

public MaximumParameterRequirements(int maximum) {
this.maximum = maximum;
this.exceptionMessage = null;
}

public MaximumParameterRequirements(int maximum, String exceptionMessage) {
this.maximum = maximum;
this.exceptionMessage = exceptionMessage;
}


@Override
public void checkParameters(Command command, ExecutionHandlerContext executionHandlerContext) {
String message;

if (this.exceptionMessage != null) {
message = this.exceptionMessage;
} else {
message = command.wrongNumberOfArgumentsErrorMessage();
}

if (command.getProcessedCommand().size() > maximum) {
throw new RedisParametersMismatchException(command.wrongNumberOfArgumentsError());
throw new RedisParametersMismatchException(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.apache.geode.redis.internal.netty.ExecutionHandlerContext;

public class MinimumParameterRequirements implements ParameterRequirements {
private int minimum;
private final int minimum;

public MinimumParameterRequirements(int minimum) {
this.minimum = minimum;
Expand All @@ -28,7 +28,7 @@ public MinimumParameterRequirements(int minimum) {
@Override
public void checkParameters(Command command, ExecutionHandlerContext context) {
if (command.getProcessedCommand().size() < minimum) {
throw new RedisParametersMismatchException(command.wrongNumberOfArgumentsError());
throw new RedisParametersMismatchException(command.wrongNumberOfArgumentsErrorMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ void checkParameters(Command command,
default ParameterRequirements and(ParameterRequirements moreRequirements) {
return new MultipleParameterRequirements(this, moreRequirements);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.apache.geode.redis.internal.executor.pubsub.UnsubscribeExecutor;
import org.apache.geode.redis.internal.executor.server.DBSizeExecutor;
import org.apache.geode.redis.internal.executor.server.FlushAllExecutor;
import org.apache.geode.redis.internal.executor.server.InfoExecutor;
import org.apache.geode.redis.internal.executor.server.ShutDownExecutor;
import org.apache.geode.redis.internal.executor.server.TimeExecutor;
import org.apache.geode.redis.internal.executor.set.SAddExecutor;
Expand Down Expand Up @@ -170,7 +171,6 @@ public enum RedisCommandType {

UNKNOWN(new UnknownExecutor(), SUPPORTED),


/***************************************
*** Unsupported Commands ***
***************************************/
Expand Down Expand Up @@ -255,6 +255,10 @@ public enum RedisCommandType {
DBSIZE(new DBSizeExecutor(), UNSUPPORTED),
FLUSHALL(new FlushAllExecutor(), UNSUPPORTED),
FLUSHDB(new FlushAllExecutor(), UNSUPPORTED),
INFO(new InfoExecutor(),
UNSUPPORTED,
new MaximumParameterRequirements(2,
RedisConstants.ERROR_SYNTAX)),
SHUTDOWN(new ShutDownExecutor(), UNSUPPORTED),
TIME(new TimeExecutor(), UNSUPPORTED),

Expand Down Expand Up @@ -286,7 +290,6 @@ public enum RedisCommandType {
GEORADIUS(null, UNIMPLEMENTED),
GEORADIUSBYMEMBER(null, UNIMPLEMENTED),
HELLO(null, UNIMPLEMENTED),
INFO(null, UNIMPLEMENTED),
LATENCY(null, UNIMPLEMENTED),
LASTSAVE(null, UNIMPLEMENTED),
LINDEX(null, UNIMPLEMENTED),
Expand Down
Loading

0 comments on commit 0c86d4f

Please sign in to comment.