Skip to content

Commit

Permalink
test: update integration test to use new connection string builder
Browse files Browse the repository at this point in the history
  • Loading branch information
karenc-bq committed Jan 22, 2025
1 parent 31b2a7f commit 1f6ad2d
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 127 deletions.
14 changes: 8 additions & 6 deletions integration/base_failover_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@
#include <sql.h>
#include <sqlext.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#if defined(__APPLE__) || defined(__linux__)
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#endif

#include "connection_string_builder.h"
#include "integration_test_utils.h"
Expand Down Expand Up @@ -90,7 +92,7 @@ class BaseFailoverIntegrationTest : public testing::Test {
int MYSQL_PROXY_PORT = INTEGRATION_TEST_UTILS::str_to_int(std::getenv("MYSQL_PROXY_PORT"));
Aws::String cluster_id = MYSQL_CLUSTER_URL.substr(0, MYSQL_CLUSTER_URL.find('.'));

ConnectionStringBuilder builder;
std::string default_connection_string;
std::string connection_string;

SQLCHAR conn_in[4096] = "\0", conn_out[4096] = "\0", sqlstate[6] = "\0", message[SQL_MAX_MESSAGE_LENGTH] = "\0";
Expand Down
4 changes: 3 additions & 1 deletion integration/connection_string_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class ConnectionStringBuilder {
length += sprintf(conn_in, "DSN=%s;SERVER=%s;PORT=%d;", dsn.c_str(), server.c_str(), port);
}

ConnectionStringBuilder(const std::string& str) { length += sprintf(conn_in, "%s", str.c_str()); }

ConnectionStringBuilder& withUID(const std::string& uid) {
length += sprintf(conn_in + length, "UID=%s;", uid.c_str());
return *this;
Expand Down Expand Up @@ -164,7 +166,7 @@ class ConnectionStringBuilder {
return *this;
}

std::string get_string() const { return conn_in; }
std::string getString() const { return conn_in; }

private:
char conn_in[4096] = "\0";
Expand Down
10 changes: 5 additions & 5 deletions integration/connection_string_builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ TEST_F(ConnectionStringBuilderTest, test_complete_string) {
.withFailureDetectionCount(4)
.withMonitorDisposalTime(300)
.withEnableClusterFailover(true)
.get_string();
.getString();

const std::string expected =
"DSN=testDSN;SERVER=testServer;PORT=3306;UID=testUser;PWD=testPwd;DATABASE=testDb;LOG_QUERY=0;MULTI_STATEMENTS=0;"
Expand All @@ -64,7 +64,7 @@ TEST_F(ConnectionStringBuilderTest, test_complete_string) {
// No optional fields are set in the builder. Build will succeed. Connection string with required fields.
TEST_F(ConnectionStringBuilderTest, test_only_required_fields) {
ConnectionStringBuilder builder("testDSN", "testServer", 3306);
const std::string connection_string = builder.get_string();
const std::string connection_string = builder.getString();

const std::string expected = "DSN=testDSN;SERVER=testServer;PORT=3306;";
EXPECT_EQ(0, expected.compare(connection_string));
Expand All @@ -74,7 +74,7 @@ TEST_F(ConnectionStringBuilderTest, test_only_required_fields) {
// Connection string with required fields and ONLY the fields that were set.
TEST_F(ConnectionStringBuilderTest, test_some_optional) {
ConnectionStringBuilder builder("testDSN", "testServer", 3306);
const std::string connection_string = builder.withUID("testUser").withPWD("testPwd").get_string();
const std::string connection_string = builder.withUID("testUser").withPWD("testPwd").getString();

const std::string expected("DSN=testDSN;SERVER=testServer;PORT=3306;UID=testUser;PWD=testPwd;");
EXPECT_EQ(0, expected.compare(connection_string));
Expand All @@ -89,7 +89,7 @@ TEST_F(ConnectionStringBuilderTest, test_setting_boolean_fields) {
.withMultiStatements(true)
.withEnableClusterFailover(false)
.withEnableFailureDetection(true)
.get_string();
.getString();

const std::string expected(
"DSN=testDSN;SERVER=testServer;PORT=3306;UID=testUser;PWD=testPwd;LOG_QUERY=0;MULTI_STATEMENTS=1;ENABLE_CLUSTER_"
Expand All @@ -101,7 +101,7 @@ TEST_F(ConnectionStringBuilderTest, test_setting_boolean_fields) {
// Build will succeed.
TEST_F(ConnectionStringBuilderTest, test_setting_multiple_steps_1) {
ConnectionStringBuilder builder("testDSN", "testServer", 3306);
const std::string connection_string = builder.withUID("testUser").withPWD("testPwd").withLogQuery(true).get_string();
const std::string connection_string = builder.withUID("testUser").withPWD("testPwd").withLogQuery(true).getString();

const std::string expected("DSN=testDSN;SERVER=testServer;PORT=3306;UID=testUser;PWD=testPwd;LOG_QUERY=1;");
EXPECT_EQ(0, expected.compare(connection_string));
Expand Down
60 changes: 35 additions & 25 deletions integration/failover_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ class FailoverIntegrationTest : public BaseFailoverIntegrationTest {
reader_endpoint = get_proxied_endpoint(reader_id);
target_writer_id = get_random_DB_cluster_reader_instance_id(readers);

builder = ConnectionStringBuilder();
builder.withPort(MYSQL_PORT)
.withLogQuery(true)
.withEnableFailureDetection(true);
default_connection_string = ConnectionStringBuilder(dsn, writer_endpoint, MYSQL_PORT)
.withLogQuery(true)
.withEnableFailureDetection(true)
.withUID(user)
.withPWD(pwd)
.withDatabase(db)
.getString();
}

void TearDown() override {
Expand All @@ -96,10 +99,10 @@ class FailoverIntegrationTest : public BaseFailoverIntegrationTest {
* writer. Driver failover occurs when executing a method against the connection
*/
TEST_F(FailoverIntegrationTest, test_failFromWriterToNewWriter_failOnConnectionInvocation) {
connection_string = builder.withDSN(dsn).withServer(writer_endpoint).withUID(user).withPWD(pwd).withDatabase(db).build();
SQLCHAR conn_out[4096] = "\0";
SQLSMALLINT len;
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(connection_string.c_str()), SQL_NTS, conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(default_connection_string.c_str()), SQL_NTS,
conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));

failover_cluster_and_wait_until_writer_changed(rds_client, cluster_id, writer_id, target_writer_id);
assert_query_failed(dbc, SERVER_ID_QUERY, ERROR_COMM_LINK_CHANGED);
Expand All @@ -117,12 +120,16 @@ TEST_F(FailoverIntegrationTest, test_takeOverConnectionProperties) {

// Establish the topology cache so that we can later assert that new connections does not inherit properties from
// cached connection either before or after failover
connection_string = builder.withDSN(dsn).withServer(writer_endpoint).withUID(user).withPWD(pwd).withMultiStatements(0).build();
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(connection_string.c_str()), SQL_NTS, conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));
const auto connection_string_without_multi_statements =
ConnectionStringBuilder(default_connection_string).withMultiStatements(0).getString();
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(connection_string_without_multi_statements.c_str()),
SQL_NTS, conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));
EXPECT_EQ(SQL_SUCCESS, SQLDisconnect(dbc));

connection_string = builder.withDSN(dsn).withServer(writer_endpoint).withUID(user).withPWD(pwd).withMultiStatements(1).build();
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(connection_string.c_str()), SQL_NTS, conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));
const auto connection_string_with_multi_statements =
ConnectionStringBuilder(default_connection_string).withMultiStatements(1).getString();
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(connection_string_with_multi_statements.c_str()),
SQL_NTS, conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));

SQLHSTMT handle;
const auto query = AS_SQLCHAR("select @@aurora_server_id; select 1; select 2;");
Expand All @@ -145,11 +152,10 @@ TEST_F(FailoverIntegrationTest, test_takeOverConnectionProperties) {

/** Writer fails within a transaction. Open transaction with "SET autocommit = 0" */
TEST_F(FailoverIntegrationTest, test_writerFailWithinTransaction_setAutocommitSqlZero) {
connection_string = builder.withDSN(dsn).withServer(writer_endpoint).withUID(user).withPWD(pwd).withDatabase(db).build();
SQLCHAR conn_out[4096] = "\0", message[SQL_MAX_MESSAGE_LENGTH] = "\0";
SQLINTEGER native_error;
SQLSMALLINT len;
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(connection_string.c_str()), SQL_NTS, conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(default_connection_string.c_str()), SQL_NTS, conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));

// Setup tests
SQLHSTMT handle;
Expand Down Expand Up @@ -196,11 +202,11 @@ TEST_F(FailoverIntegrationTest, test_writerFailWithinTransaction_setAutocommitSq

/** Writer fails within a transaction. Open transaction with SQLSetConnectAttr */
TEST_F(FailoverIntegrationTest, test_writerFailWithinTransaction_setAutoCommitFalse) {
connection_string = builder.withDSN(dsn).withServer(writer_endpoint).withUID(user).withPWD(pwd).withDatabase(db).build();
SQLCHAR conn_out[4096] = "\0", message[SQL_MAX_MESSAGE_LENGTH] = "\0";
SQLINTEGER native_error;
SQLSMALLINT len;
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(connection_string.c_str()), SQL_NTS, conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(default_connection_string.c_str()), SQL_NTS,
conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));

// Setup tests
SQLHSTMT handle;
Expand Down Expand Up @@ -249,11 +255,10 @@ TEST_F(FailoverIntegrationTest, test_writerFailWithinTransaction_setAutoCommitFa

/** Writer fails within a transaction. Open transaction with "START TRANSACTION". */
TEST_F(FailoverIntegrationTest, test_writerFailWithinTransaction_startTransaction) {
connection_string = builder.withDSN(dsn).withServer(writer_endpoint).withUID(user).withPWD(pwd).withDatabase(db).build();
SQLCHAR conn_out[4096] = "\0", message[SQL_MAX_MESSAGE_LENGTH] = "\0";
SQLINTEGER native_error;
SQLSMALLINT len;
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(connection_string.c_str()), SQL_NTS, conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(default_connection_string.c_str()), SQL_NTS, conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));

// Setup tests
SQLHSTMT handle;
Expand Down Expand Up @@ -300,10 +305,10 @@ TEST_F(FailoverIntegrationTest, test_writerFailWithinTransaction_startTransactio

/* Writer fails within NO transaction. */
TEST_F(FailoverIntegrationTest, test_writerFailWithNoTransaction) {
connection_string = builder.withDSN(dsn).withServer(writer_endpoint).withUID(user).withPWD(pwd).withDatabase(db).build();
SQLCHAR conn_out[4096] = "\0";
SQLSMALLINT len;
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(connection_string.c_str()), SQL_NTS, conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(default_connection_string.c_str()), SQL_NTS,
conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));

// Setup tests
SQLHSTMT handle;
Expand Down Expand Up @@ -366,11 +371,17 @@ TEST_F(FailoverIntegrationTest, test_failFromReaderToWriterToAnyAvailableInstanc
SQLCHAR conn_out[4096];
SQLSMALLINT len;

ConnectionStringBuilder proxied_builder = ConnectionStringBuilder();
proxied_builder.withDSN(dsn).withUID(user).withPWD(pwd).withConnectTimeout(10).withNetworkTimeout(10);
proxied_builder.withPort(MYSQL_PROXY_PORT).withHostPattern(PROXIED_CLUSTER_TEMPLATE).withLogQuery(true);
connection_string = proxied_builder.withServer(initial_reader_endpoint).withFailoverMode("reader or writer").build();
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(connection_string.c_str()), SQL_NTS, conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));
connection_string = ConnectionStringBuilder(dsn, initial_reader_endpoint, MYSQL_PROXY_PORT)
.withUID(user)
.withPWD(pwd)
.withConnectTimeout(10)
.withNetworkTimeout(10)
.withHostPattern(PROXIED_CLUSTER_TEMPLATE)
.withLogQuery(true)
.withFailoverMode("reader or writer")
.getString();
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(connection_string.c_str()), SQL_NTS, conn_out,
MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));

disable_instance(initial_reader_id);

Expand Down Expand Up @@ -407,10 +418,9 @@ TEST_F(FailoverIntegrationTest, test_pooledWriterConnection_BasicFailover) {
EXPECT_EQ(SQL_SUCCESS, SQLSetEnvAttr(NULL, SQL_ATTR_CONNECTION_POOLING, reinterpret_cast<SQLPOINTER>(SQL_CP_ONE_PER_DRIVER), 0));
EXPECT_EQ(SQL_SUCCESS, SQLSetEnvAttr(env, SQL_ATTR_CP_MATCH, SQL_CP_STRICT_MATCH, 0));

connection_string = builder.withDSN(dsn).withServer(writer_endpoint).withUID(user).withPWD(pwd).withDatabase(db).build();
SQLCHAR conn_out[4096] = "\0";
SQLSMALLINT len;
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(connection_string.c_str()), SQL_NTS, conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));
EXPECT_EQ(SQL_SUCCESS, SQLDriverConnect(dbc, nullptr, AS_SQLCHAR(default_connection_string.c_str()), SQL_NTS, conn_out, MAX_NAME_LEN, &len, SQL_DRIVER_NOPROMPT));

failover_cluster_and_wait_until_writer_changed(rds_client, cluster_id, writer_id, nominated_writer_id);
assert_query_failed(dbc, SERVER_ID_QUERY, ERROR_COMM_LINK_CHANGED);
Expand Down
Loading

0 comments on commit 1f6ad2d

Please sign in to comment.