Skip to content

Commit

Permalink
Add cluster-mode tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Carbonetto <[email protected]>
  • Loading branch information
acarbonetto committed Nov 19, 2024
1 parent e2af4fa commit 5120e01
Showing 1 changed file with 99 additions and 32 deletions.
131 changes: 99 additions & 32 deletions java/integTest/src/test/java/glide/cluster/ClusterClientTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
package glide.cluster;

import static glide.TestConfiguration.SERVER_VERSION;
import static glide.TestUtilities.commonClientConfig;
import static glide.TestUtilities.commonClusterClientConfig;
import static glide.TestUtilities.getRandomString;
import static glide.api.BaseClient.OK;
import static glide.api.models.configuration.RequestRoutingConfiguration.SimpleMultiNodeRoute.ALL_NODES;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import glide.api.GlideClient;
import glide.api.GlideClusterClient;
import glide.api.models.configuration.ServerCredentials;
import glide.api.models.exceptions.ClosingException;
Expand All @@ -20,13 +23,16 @@
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import lombok.SneakyThrows;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@Timeout(10) // seconds
@Timeout(30) // seconds
public class ClusterClientTests {

@SneakyThrows
Expand Down Expand Up @@ -167,49 +173,110 @@ public void closed_client_throws_ExecutionException_with_ClosingException_as_cau
}

@SneakyThrows
@ParameterizedTest
@ValueSource(booleans = {true, false})
public void password_update(boolean immediateAuth) {
GlideClusterClient client =
GlideClusterClient.createClient(commonClusterClientConfig().build()).get();
@Test
public void test_update_connection_password() {
GlideClusterClient adminClient = GlideClusterClient.createClient(commonClusterClientConfig().build()).get();
String pwd = UUID.randomUUID().toString();

try (GlideClusterClient testClient = GlideClusterClient.createClient(commonClusterClientConfig().build()).get()) {
// validate that we can use the client
assertNotNull(testClient.info().get());

// Update password without re-authentication
assertEquals(OK, testClient.updateConnectionPassword(pwd, false).get());

// Verify client still works with old auth
assertNotNull(testClient.info().get());

// Update server password
// Kill all other clients to force reconnection
assertEquals("OK", adminClient.configSet(Map.of("requirepass", pwd)).get());
adminClient.customCommand(new String[] {"CLIENT", "KILL", "TYPE", "NORMAL"}).get();

Thread.sleep(1000);

// Verify client auto-reconnects with new password
assertNotNull(testClient.info().get());
} finally {
adminClient.configSet(Map.of("requirepass", "")).get();
adminClient.close();
}
}

@SneakyThrows
@Test
public void test_update_connection_password_auth_non_valid_pass() {
// Test Client fails on call to updateConnectionPassword with invalid parameters
try (GlideClusterClient testClient = GlideClusterClient.createClient(commonClusterClientConfig().build()).get()) {
var emptyPasswordException =
assertThrows(
ExecutionException.class, () -> testClient.updateConnectionPassword("", true).get());
assertInstanceOf(RequestException.class, emptyPasswordException.getCause());

var noPasswordException =
assertThrows(
ExecutionException.class, () -> testClient.updateConnectionPassword(true).get());
assertInstanceOf(RequestException.class, noPasswordException.getCause());
}
}

var key = UUID.randomUUID().toString();
@SneakyThrows
@Test
public void test_update_connection_password_no_server_auth() {
var pwd = UUID.randomUUID().toString();
client.set(key, "meow meow").get();

try (var testClient =
GlideClusterClient.createClient(commonClusterClientConfig().build()).get()) {
try (GlideClusterClient testClient = GlideClusterClient.createClient(commonClusterClientConfig().build()).get()) {
// validate that we can use the client
assertNotNull(testClient.info().get());

// validate that we can get the value
assertEquals("meow meow", testClient.get(key).get());
// Test that immediate re-authentication fails when no server password is set.
var exception =
assertThrows(
ExecutionException.class, () -> testClient.updateConnectionPassword(pwd, true).get());
assertInstanceOf(RequestException.class, exception.getCause());
}
}

// set the password and forcefully drop connection for the second client
assertEquals("OK", client.configSet(Map.of("requirepass", pwd)).get());
if (immediateAuth) testClient.customCommand(new String[] {"RESET"}, ALL_NODES).get();
else client.customCommand(new String[] {"CLIENT", "KILL", "TYPE", "NORMAL"}, ALL_NODES).get();
@SneakyThrows
@Test
public void test_update_connection_password_long() {
var pwd = RandomStringUtils.randomAlphabetic(1000);

try (GlideClusterClient testClient = GlideClusterClient.createClient(commonClusterClientConfig().build()).get()) {
// validate that we can use the client
assertNotNull(testClient.info().get());

// client should reconnect, but will receive NOAUTH error
var exception = assertThrows(ExecutionException.class, () -> testClient.get(key).get());
assertInstanceOf(ConnectionException.class, exception.getCause());
assertTrue(exception.getMessage().toLowerCase().contains("noauth"));
// Test replacing connection password with a long password string.
assertEquals(OK, testClient.updateConnectionPassword(pwd, false).get());
}
}

assertEquals("OK", testClient.updateConnectionPassword(pwd, immediateAuth).get());
@Timeout(50)
@SneakyThrows
@Test
public void test_replace_password_immediateAuth_wrong_password() {
var pwd = UUID.randomUUID().toString();
var notThePwd = UUID.randomUUID().toString();

// after setting new password we should be able to work with the server
assertEquals("meow meow", testClient.get(key).get());
GlideClusterClient adminClient = GlideClusterClient.createClient(commonClusterClientConfig().build()).get();
try (GlideClusterClient testClient = GlideClusterClient.createClient(commonClusterClientConfig().build()).get()) {
// validate that we can use the client
assertNotNull(testClient.info().get());

// unset the password and drop connection again
assertEquals("OK", client.configSet(Map.of("requirepass", "")).get());
// set the password to something else
adminClient.configSet(Map.of("requirepass", notThePwd)).get();

client.customCommand(new String[] {"CLIENT", "KILL", "TYPE", "NORMAL"}, ALL_NODES).get();
// Test that re-authentication fails when using wrong password.
var exception =
assertThrows(
ExecutionException.class, () -> testClient.updateConnectionPassword(pwd, true).get());
assertInstanceOf(RequestException.class, exception.getCause());

// client should reconnect, but since no auth configured, able to get a value
assertEquals("meow meow", testClient.get(key).get());
} catch (Exception e) {
e.printStackTrace();
// But using something else password returns OK
assertEquals(OK, testClient.updateConnectionPassword(notThePwd, true).get());
} finally {
client.configSet(Map.of("requirepass", "")).get();
client.close();
adminClient.configSet(Map.of("requirepass", "")).get();
adminClient.close();
}
}
}

0 comments on commit 5120e01

Please sign in to comment.