Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
crystall-bitquill committed Feb 1, 2025
1 parent 499575e commit bddb730
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 112 deletions.
6 changes: 6 additions & 0 deletions common/lib/plugin_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ export class PluginService implements ErrorHandler, HostListProviderService {
}

private async setHostList(oldHosts: HostInfo[], newHosts: HostInfo[]) {
logger.silly("Setting host list");
console.log(oldHosts);
console.log(newHosts);
const oldHostMap: Map<string, HostInfo> = new Map(oldHosts.map((e) => [e.url, e]));
const newHostMap: Map<string, HostInfo> = new Map(newHosts.map((e) => [e.url, e]));

Expand All @@ -267,7 +270,10 @@ export class PluginService implements ErrorHandler, HostListProviderService {
}
});

console.log("changes: ");
console.log(changes);
if (changes.size > 0) {
logger.silly("Changes found, setting hosts");
this.hosts = newHosts ? newHosts : [];
await this.pluginServiceManagerContainer.pluginManager!.notifyHostListChanged(changes);
}
Expand Down
2 changes: 2 additions & 0 deletions common/lib/plugins/failover/failover_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@ export class FailoverPlugin extends AbstractConnectionPlugin {
throw new AwsWrapperError(Messages.get("Failover.unableToDetermineWriter"));
}

logger.silly(`Failover writer complete with writer host: ${writerHostInfo.host}`);
const allowedHosts = this.pluginService.getHosts();
console.log(allowedHosts);
if (!allowedHosts.some((hostInfo: HostInfo) => hostInfo.host === writerHostInfo?.host)) {
const failoverErrorMessage = Messages.get(
"Failover.newWriterNotAllowed",
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/container/tests/autoscaling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { DriverHelper } from "./utils/driver_helper";
import { AuroraTestUtility } from "./utils/aurora_test_utility";
import { logger } from "../../../../common/logutils";
import { TestEnvironmentFeatures } from "./utils/test_environment_features";
import { features, instanceCount } from "./config";
import { features, instanceCount, runTests } from "./config";
import { InternalPooledConnectionProvider } from "../../../../common/lib/internal_pooled_connection_provider";
import { AwsPoolConfig } from "../../../../common/lib/aws_pool_config";
import { TestInstanceInfo } from "./utils/test_instance_info";
Expand All @@ -28,6 +28,7 @@ import { FailoverSuccessError } from "../../../../common/lib/utils/errors";
import { PluginManager } from "../../../../common/lib";

const itIf =
runTests &&
!features.includes(TestEnvironmentFeatures.PERFORMANCE) &&
features.includes(TestEnvironmentFeatures.RUN_AUTOSCALING_TESTS_ONLY) &&
instanceCount >= 2
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/container/tests/basic_connectivity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import { AuroraTestUtility } from "./utils/aurora_test_utility";
import { logger } from "../../../../common/logutils";
import { DatabaseEngine } from "./utils/database_engine";
import { TestEnvironmentFeatures } from "./utils/test_environment_features";
import { features } from "./config";
import { features, runTests } from "./config";
import { DatabaseEngineDeployment } from "./utils/database_engine_deployment";
import { PluginManager } from "../../../../common/lib";

const itIf =
!features.includes(TestEnvironmentFeatures.PERFORMANCE) && !features.includes(TestEnvironmentFeatures.RUN_AUTOSCALING_TESTS_ONLY) ? it : it.skip;
runTests && !features.includes(TestEnvironmentFeatures.PERFORMANCE) && !features.includes(TestEnvironmentFeatures.RUN_AUTOSCALING_TESTS_ONLY)
? it
: it.skip;

let client: any;
let auroraTestUtility: AuroraTestUtility;
Expand Down
1 change: 1 addition & 0 deletions tests/integration/container/tests/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ const testInfo = JSON.parse(infoJson);
const request = testInfo.request;
export const features = request.features;
export const instanceCount = request.numOfInstances;
export const runTests = false; // TODO: remove
3 changes: 2 additions & 1 deletion tests/integration/container/tests/iam_authentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import { AwsMySQLClient } from "../../../../mysql/lib";
import { IamAuthenticationPlugin } from "../../../../common/lib/authentication/iam_authentication_plugin";
import { logger } from "../../../../common/logutils";
import { TestEnvironmentFeatures } from "./utils/test_environment_features";
import { features } from "./config";
import { features, runTests } from "./config";
import { PluginManager } from "../../../../common/lib";
import { jest } from "@jest/globals";

const itIf =
runTests &&
!features.includes(TestEnvironmentFeatures.PERFORMANCE) &&
!features.includes(TestEnvironmentFeatures.RUN_AUTOSCALING_TESTS_ONLY) &&
features.includes(TestEnvironmentFeatures.IAM)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import { QueryResult } from "pg";
import { ProxyHelper } from "./utils/proxy_helper";
import { logger } from "../../../../common/logutils";
import { TestEnvironmentFeatures } from "./utils/test_environment_features";
import { features, instanceCount } from "./config";
import { features, instanceCount, runTests } from "./config";
import { InternalPooledConnectionProvider } from "../../../../common/lib/internal_pooled_connection_provider";
import { AwsPoolConfig } from "../../../../common/lib/aws_pool_config";
import { InternalPoolMapping } from "../../../../common/lib/utils/internal_pool_mapping";
import { HostInfo } from "../../../../common/lib/host_info";
import { PluginManager } from "../../../../common/lib";

const itIf =
runTests &&
!features.includes(TestEnvironmentFeatures.PERFORMANCE) &&
features.includes(TestEnvironmentFeatures.IAM) &&
!features.includes(TestEnvironmentFeatures.RUN_AUTOSCALING_TESTS_ONLY) &&
Expand Down
220 changes: 113 additions & 107 deletions tests/integration/container/tests/session_state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { DriverHelper } from "./utils/driver_helper";
import { logger } from "../../../../common/logutils";
import { DatabaseEngine } from "./utils/database_engine";
import { TestEnvironmentFeatures } from "./utils/test_environment_features";
import { features } from "./config";
import { features, runTests } from "./config";
import { DatabaseEngineDeployment } from "./utils/database_engine_deployment";
import { PluginManager } from "../../../../common/lib";
import { AwsPGClient } from "../../../../pg/lib";
Expand All @@ -30,7 +30,9 @@ import { AwsMySQLClient } from "../../../../mysql/lib";
import { TransactionIsolationLevel } from "../../../../common/lib/utils/transaction_isolation_level";

const itIf =
!features.includes(TestEnvironmentFeatures.PERFORMANCE) && !features.includes(TestEnvironmentFeatures.RUN_AUTOSCALING_TESTS_ONLY) ? it : it.skip;
runTests && !features.includes(TestEnvironmentFeatures.PERFORMANCE) && !features.includes(TestEnvironmentFeatures.RUN_AUTOSCALING_TESTS_ONLY)
? it
: it.skip;

let client: any;

Expand Down Expand Up @@ -74,111 +76,115 @@ class TestAwsPGClient extends AwsPGClient {
}

describe("session state", () => {
it.only("test update state", async () => {
const env = await TestEnvironment.getCurrent();
const driver = DriverHelper.getDriverForDatabaseEngine(env.engine);
let initClientFunc;
switch (driver) {
case TestDriver.MYSQL:
initClientFunc = (options: any) => new TestAwsMySQLClient(options);
break;
case TestDriver.PG:
initClientFunc = (options: any) => new TestAwsPGClient(options);
break;
default:
throw new Error("invalid driver");
}

let props = {
user: env.databaseInfo.username,
host: env.databaseInfo.clusterEndpoint,
database: env.databaseInfo.defaultDbName,
password: env.databaseInfo.password,
port: env.databaseInfo.clusterEndpointPort
};
props = DriverHelper.addDriverSpecificConfiguration(props, env.engine);
client = initClientFunc(props);

const newClient = initClientFunc(props);
itIf(
"test update state",
async () => {
const env = await TestEnvironment.getCurrent();
const driver = DriverHelper.getDriverForDatabaseEngine(env.engine);
let initClientFunc;
switch (driver) {
case TestDriver.MYSQL:
initClientFunc = (options: any) => new TestAwsMySQLClient(options);
break;
case TestDriver.PG:
initClientFunc = (options: any) => new TestAwsPGClient(options);
break;
default:
throw new Error("invalid driver");
}

try {
await client.connect();
await newClient.connect();
const targetClient = client.targetClient;
const newTargetClient = newClient.targetClient;

expect(targetClient).not.toEqual(newTargetClient);
if (driver === TestDriver.MYSQL) {
await DriverHelper.executeQuery(env.engine, client, "CREATE DATABASE IF NOT EXISTS testSessionState");
await client.setReadOnly(true);
await client.setCatalog("testSessionState");
await client.setTransactionIsolation(TransactionIsolationLevel.TRANSACTION_SERIALIZABLE);
await client.setAutoCommit(false);

// Assert new client's session states are using server default values.
let readOnly = await DriverHelper.executeQuery(env.engine, newClient, "SELECT @@SESSION.transaction_read_only AS readonly");
let catalog = await DriverHelper.executeQuery(env.engine, newClient, "SELECT DATABASE() AS catalog");
let autoCommit = await DriverHelper.executeQuery(env.engine, newClient, "SELECT @@SESSION.autocommit AS autocommit");
let transactionIsolation = await DriverHelper.executeQuery(env.engine, newClient, "SELECT @@SESSION.transaction_isolation AS level");
expect(readOnly[0][0].readonly).toEqual(0);
expect(catalog[0][0].catalog).toEqual(env.databaseInfo.defaultDbName);
expect(autoCommit[0][0].autocommit).toEqual(1);
expect(transactionIsolation[0][0].level).toEqual("REPEATABLE-READ");

await client.getPluginService().setCurrentClient(newClient.targetClient);

expect(client.targetClient).not.toEqual(targetClient);
expect(client.targetClient).toEqual(newTargetClient);

// Assert new client's session states are set.
readOnly = await DriverHelper.executeQuery(env.engine, newClient, "SELECT @@SESSION.transaction_read_only AS readonly");
catalog = await DriverHelper.executeQuery(env.engine, newClient, "SELECT DATABASE() AS catalog");
autoCommit = await DriverHelper.executeQuery(env.engine, newClient, "SELECT @@SESSION.autocommit AS autocommit");
transactionIsolation = await DriverHelper.executeQuery(env.engine, newClient, "SELECT @@SESSION.transaction_isolation AS level");
expect(readOnly[0][0].readonly).toEqual(1);
expect(catalog[0][0].catalog).toEqual("testSessionState");
expect(autoCommit[0][0].autocommit).toEqual(0);
expect(transactionIsolation[0][0].level).toEqual("SERIALIZABLE");

await client.setReadOnly(false);
await client.setAutoCommit(true);
await DriverHelper.executeQuery(env.engine, client, "DROP DATABASE IF EXISTS testSessionState");
} else if (driver === TestDriver.PG) {
// End any current transaction before we can create a new test database.
await DriverHelper.executeQuery(env.engine, client, "END TRANSACTION");
await DriverHelper.executeQuery(env.engine, client, "DROP DATABASE IF EXISTS testSessionState");
await DriverHelper.executeQuery(env.engine, client, "CREATE DATABASE testSessionState");
await client.setReadOnly(true);
await client.setSchema("testSessionState");
await client.setTransactionIsolation(TransactionIsolationLevel.TRANSACTION_SERIALIZABLE);

// Assert new client's session states are using server default values.
let readOnly = await DriverHelper.executeQuery(env.engine, newClient, "SHOW transaction_read_only");
let schema = await DriverHelper.executeQuery(env.engine, newClient, "SHOW search_path");
let transactionIsolation = await DriverHelper.executeQuery(env.engine, newClient, "SHOW TRANSACTION ISOLATION LEVEL");
expect(readOnly.rows[0]["transaction_read_only"]).toEqual("off");
expect(schema.rows[0]["search_path"]).not.toEqual("testSessionState");
expect(transactionIsolation.rows[0]["transaction_isolation"]).toEqual("read committed");

await client.getPluginService().setCurrentClient(newClient.targetClient);
expect(client.targetClient).not.toEqual(targetClient);
expect(client.targetClient).toEqual(newTargetClient);

// Assert new client's session states are set.
readOnly = await DriverHelper.executeQuery(env.engine, newClient, "SHOW transaction_read_only");
schema = await DriverHelper.executeQuery(env.engine, newClient, "SHOW search_path");
transactionIsolation = await DriverHelper.executeQuery(env.engine, newClient, "SHOW TRANSACTION ISOLATION LEVEL");
expect(readOnly.rows[0]["transaction_read_only"]).toEqual("on");
expect(schema.rows[0]["search_path"]).toEqual("testsessionstate");
expect(transactionIsolation.rows[0]["transaction_isolation"]).toEqual("serializable");

await client.setReadOnly(false);
await DriverHelper.executeQuery(env.engine, client, "DROP DATABASE IF EXISTS testSessionState");
let props = {
user: env.databaseInfo.username,
host: env.databaseInfo.clusterEndpoint,
database: env.databaseInfo.defaultDbName,
password: env.databaseInfo.password,
port: env.databaseInfo.clusterEndpointPort
};
props = DriverHelper.addDriverSpecificConfiguration(props, env.engine);
client = initClientFunc(props);

const newClient = initClientFunc(props);

try {
await client.connect();
await newClient.connect();
const targetClient = client.targetClient;
const newTargetClient = newClient.targetClient;

expect(targetClient).not.toEqual(newTargetClient);
if (driver === TestDriver.MYSQL) {
await DriverHelper.executeQuery(env.engine, client, "CREATE DATABASE IF NOT EXISTS testSessionState");
await client.setReadOnly(true);
await client.setCatalog("testSessionState");
await client.setTransactionIsolation(TransactionIsolationLevel.TRANSACTION_SERIALIZABLE);
await client.setAutoCommit(false);

// Assert new client's session states are using server default values.
let readOnly = await DriverHelper.executeQuery(env.engine, newClient, "SELECT @@SESSION.transaction_read_only AS readonly");
let catalog = await DriverHelper.executeQuery(env.engine, newClient, "SELECT DATABASE() AS catalog");
let autoCommit = await DriverHelper.executeQuery(env.engine, newClient, "SELECT @@SESSION.autocommit AS autocommit");
let transactionIsolation = await DriverHelper.executeQuery(env.engine, newClient, "SELECT @@SESSION.transaction_isolation AS level");
expect(readOnly[0][0].readonly).toEqual(0);
expect(catalog[0][0].catalog).toEqual(env.databaseInfo.defaultDbName);
expect(autoCommit[0][0].autocommit).toEqual(1);
expect(transactionIsolation[0][0].level).toEqual("REPEATABLE-READ");

await client.getPluginService().setCurrentClient(newClient.targetClient);

expect(client.targetClient).not.toEqual(targetClient);
expect(client.targetClient).toEqual(newTargetClient);

// Assert new client's session states are set.
readOnly = await DriverHelper.executeQuery(env.engine, newClient, "SELECT @@SESSION.transaction_read_only AS readonly");
catalog = await DriverHelper.executeQuery(env.engine, newClient, "SELECT DATABASE() AS catalog");
autoCommit = await DriverHelper.executeQuery(env.engine, newClient, "SELECT @@SESSION.autocommit AS autocommit");
transactionIsolation = await DriverHelper.executeQuery(env.engine, newClient, "SELECT @@SESSION.transaction_isolation AS level");
expect(readOnly[0][0].readonly).toEqual(1);
expect(catalog[0][0].catalog).toEqual("testSessionState");
expect(autoCommit[0][0].autocommit).toEqual(0);
expect(transactionIsolation[0][0].level).toEqual("SERIALIZABLE");

await client.setReadOnly(false);
await client.setAutoCommit(true);
await DriverHelper.executeQuery(env.engine, client, "DROP DATABASE IF EXISTS testSessionState");
} else if (driver === TestDriver.PG) {
// End any current transaction before we can create a new test database.
await DriverHelper.executeQuery(env.engine, client, "END TRANSACTION");
await DriverHelper.executeQuery(env.engine, client, "DROP DATABASE IF EXISTS testSessionState");
await DriverHelper.executeQuery(env.engine, client, "CREATE DATABASE testSessionState");
await client.setReadOnly(true);
await client.setSchema("testSessionState");
await client.setTransactionIsolation(TransactionIsolationLevel.TRANSACTION_SERIALIZABLE);

// Assert new client's session states are using server default values.
let readOnly = await DriverHelper.executeQuery(env.engine, newClient, "SHOW transaction_read_only");
let schema = await DriverHelper.executeQuery(env.engine, newClient, "SHOW search_path");
let transactionIsolation = await DriverHelper.executeQuery(env.engine, newClient, "SHOW TRANSACTION ISOLATION LEVEL");
expect(readOnly.rows[0]["transaction_read_only"]).toEqual("off");
expect(schema.rows[0]["search_path"]).not.toEqual("testSessionState");
expect(transactionIsolation.rows[0]["transaction_isolation"]).toEqual("read committed");

await client.getPluginService().setCurrentClient(newClient.targetClient);
expect(client.targetClient).not.toEqual(targetClient);
expect(client.targetClient).toEqual(newTargetClient);

// Assert new client's session states are set.
readOnly = await DriverHelper.executeQuery(env.engine, newClient, "SHOW transaction_read_only");
schema = await DriverHelper.executeQuery(env.engine, newClient, "SHOW search_path");
transactionIsolation = await DriverHelper.executeQuery(env.engine, newClient, "SHOW TRANSACTION ISOLATION LEVEL");
expect(readOnly.rows[0]["transaction_read_only"]).toEqual("on");
expect(schema.rows[0]["search_path"]).toEqual("testsessionstate");
expect(transactionIsolation.rows[0]["transaction_isolation"]).toEqual("serializable");

await client.setReadOnly(false);
await DriverHelper.executeQuery(env.engine, client, "DROP DATABASE IF EXISTS testSessionState");
}
} catch (e) {
await client.end();
await newClient.end();
throw e;
}
} catch (e) {
await client.end();
await newClient.end();
throw e;
}
}, 1320000);
},
1320000
);
});

0 comments on commit bddb730

Please sign in to comment.