|
| 1 | +package com.datastax.oss.driver.api.core.cloud; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.mockito.Matchers.any; |
| 5 | +import static org.mockito.Mockito.times; |
| 6 | +import static org.mockito.Mockito.verify; |
| 7 | + |
| 8 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 9 | +import com.datastax.oss.driver.api.core.DefaultProtocolVersion; |
| 10 | +import com.datastax.oss.driver.api.core.config.DefaultDriverOption; |
| 11 | +import com.datastax.oss.driver.api.core.config.DriverConfigLoader; |
| 12 | +import com.datastax.oss.driver.api.core.metadata.EndPoint; |
| 13 | +import com.datastax.oss.driver.api.core.metadata.Node; |
| 14 | +import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata; |
| 15 | +import com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListener; |
| 16 | +import com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListenerBase; |
| 17 | +import com.datastax.oss.driver.api.core.metadata.schema.TableMetadata; |
| 18 | +import com.datastax.oss.driver.api.testinfra.CassandraSkip; |
| 19 | +import com.datastax.oss.driver.api.testinfra.ccm.CustomCcmRule; |
| 20 | +import com.datastax.oss.driver.categories.IsolatedTests; |
| 21 | +import com.datastax.oss.driver.internal.core.config.scyllacloud.ScyllaCloudConnectionConfig; |
| 22 | +import java.io.File; |
| 23 | +import java.io.IOException; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.UUID; |
| 27 | +import org.junit.ClassRule; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.experimental.categories.Category; |
| 30 | +import org.mockito.Mockito; |
| 31 | + |
| 32 | +@Category(IsolatedTests.class) |
| 33 | +@CassandraSkip |
| 34 | +public class ScyllaCloudMultiNodeIT { |
| 35 | + |
| 36 | + private static final int NUMBER_OF_NODES = 3; |
| 37 | + private static final int SNI_PORT = 0; // Let CCM pick |
| 38 | + |
| 39 | + @ClassRule |
| 40 | + public static CustomCcmRule CCM_RULE = |
| 41 | + CustomCcmRule.builder().withNodes(NUMBER_OF_NODES).withSniProxy(SNI_PORT).build(); |
| 42 | + |
| 43 | + @Test |
| 44 | + public void connect_w_simple_operations_protocol_v4() { |
| 45 | + String configPath = CCM_RULE.getCcmBridge().getScyllaCloudConfigPathString(); |
| 46 | + File configFile = new File(configPath); |
| 47 | + DriverConfigLoader loader = |
| 48 | + DriverConfigLoader.programmaticBuilder() |
| 49 | + .withString(DefaultDriverOption.PROTOCOL_VERSION, DefaultProtocolVersion.V4.toString()) |
| 50 | + .build(); |
| 51 | + SchemaChangeListener mockListener = Mockito.mock(SchemaChangeListenerBase.class); |
| 52 | + try (CqlSession session = |
| 53 | + CqlSession.builder() |
| 54 | + .withConfigLoader(loader) |
| 55 | + .withScyllaCloudSecureConnectBundle(configFile.toPath()) |
| 56 | + // Currently ccm produces cloud config with eu-west-1 dc name but uses dc1 |
| 57 | + .withLocalDatacenter("dc1") |
| 58 | + .withSchemaChangeListener(mockListener) |
| 59 | + .build()) { |
| 60 | + |
| 61 | + session.execute( |
| 62 | + String.format( |
| 63 | + "CREATE KEYSPACE %s " |
| 64 | + + "WITH replication = {'class': 'SimpleStrategy', 'replication_factor': %d}", |
| 65 | + "testks", NUMBER_OF_NODES)); |
| 66 | + session.execute("CREATE TABLE testks.testtab (a int PRIMARY KEY, b int);"); |
| 67 | + |
| 68 | + verify(mockListener, times(1)).onTableCreated(any(TableMetadata.class)); |
| 69 | + verify(mockListener, times(1)).onKeyspaceCreated(any(KeyspaceMetadata.class)); |
| 70 | + |
| 71 | + int sniPort = |
| 72 | + ScyllaCloudConnectionConfig.fromInputStream(Files.newInputStream(configFile.toPath())) |
| 73 | + .getCurrentDatacenter() |
| 74 | + .getServer() |
| 75 | + .getPort(); |
| 76 | + Map<UUID, Node> map = session.getMetadata().getNodes(); |
| 77 | + assertThat(map.size()).isEqualTo(NUMBER_OF_NODES); |
| 78 | + String expectedEndpointPrefix = CCM_RULE.getCcmBridge().getIpPrefix() + "1:" + sniPort + ":"; |
| 79 | + for (Map.Entry<UUID, Node> entry : map.entrySet()) { |
| 80 | + EndPoint endPoint = entry.getValue().getEndPoint(); |
| 81 | + assertThat(endPoint.toString()).startsWith(expectedEndpointPrefix); |
| 82 | + assertThat(endPoint.toString()).contains(entry.getKey().toString()); |
| 83 | + } |
| 84 | + } catch (IOException e) { |
| 85 | + throw new RuntimeException(e); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments