|
| 1 | +/* |
| 2 | + Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + |
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + You may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | + |
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + |
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { TestEnvironment } from "./utils/test_environment"; |
| 18 | +import { DriverHelper } from "./utils/driver_helper"; |
| 19 | +import { AuroraTestUtility } from "./utils/aurora_test_utility"; |
| 20 | +import { ProxyHelper } from "./utils/proxy_helper"; |
| 21 | +import { logger } from "../../../../common/logutils"; |
| 22 | +import { features, instanceCount } from "./config"; |
| 23 | +import { TestEnvironmentFeatures } from "./utils/test_environment_features"; |
| 24 | +import { PluginManager } from "../../../../common/lib"; |
| 25 | +import { ErrorSimulatorManager } from "../../../../common/lib/plugins/dev/error_simulator_manager"; |
| 26 | +import { DeveloperConnectionPlugin } from "../../../../common/lib/plugins/dev/developer_connection_plugin"; |
| 27 | +import { ErrorSimulatorMethodCallback } from "../../../../common/lib/plugins/dev/error_simulator_method_callback"; |
| 28 | +import { ErrorSimulator } from "../../../../common/lib/plugins/dev/error_simulator"; |
| 29 | + |
| 30 | +const itIf = |
| 31 | + !features.includes(TestEnvironmentFeatures.PERFORMANCE) && |
| 32 | + !features.includes(TestEnvironmentFeatures.RUN_AUTOSCALING_TESTS_ONLY) && |
| 33 | + instanceCount >= 2 |
| 34 | + ? it.skip // TODO: investigate tests failing on github actions at getPluginInstance(), passing locally |
| 35 | + : it.skip; |
| 36 | + |
| 37 | +let env: TestEnvironment; |
| 38 | +let driver; |
| 39 | +let client: any; |
| 40 | +let initClientFunc: (props: any) => any; |
| 41 | +let auroraTestUtility: AuroraTestUtility; |
| 42 | + |
| 43 | +async function initDefaultConfig(host: string, port: number): Promise<any> { |
| 44 | + let config: any = { |
| 45 | + user: env.databaseInfo.username, |
| 46 | + host: host, |
| 47 | + database: env.databaseInfo.defaultDbName, |
| 48 | + password: env.databaseInfo.password, |
| 49 | + port: port, |
| 50 | + plugins: "dev" |
| 51 | + }; |
| 52 | + config = DriverHelper.addDriverSpecificConfiguration(config, env.engine); |
| 53 | + return config; |
| 54 | +} |
| 55 | + |
| 56 | +class TestErrorCallback implements ErrorSimulatorMethodCallback { |
| 57 | + getErrorToRaise<T>(methodName: string, methodArgs: any): Error | null { |
| 58 | + if (methodName == "query" && methodArgs == "select 1") { |
| 59 | + return new Error("test_query"); |
| 60 | + } |
| 61 | + return null; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +describe("aurora developer plugin", () => { |
| 66 | + beforeEach(async () => { |
| 67 | + logger.info(`Test started: ${expect.getState().currentTestName}`); |
| 68 | + env = await TestEnvironment.getCurrent(); |
| 69 | + |
| 70 | + auroraTestUtility = new AuroraTestUtility(env.region); |
| 71 | + driver = DriverHelper.getDriverForDatabaseEngine(env.engine); |
| 72 | + initClientFunc = DriverHelper.getClient(driver); |
| 73 | + await ProxyHelper.enableAllConnectivity(); |
| 74 | + await TestEnvironment.verifyClusterStatus(); |
| 75 | + |
| 76 | + client = null; |
| 77 | + }, 1320000); |
| 78 | + |
| 79 | + afterEach(async () => { |
| 80 | + if (client !== null) { |
| 81 | + try { |
| 82 | + await client.end(); |
| 83 | + } catch (error) { |
| 84 | + // pass |
| 85 | + } |
| 86 | + } |
| 87 | + await PluginManager.releaseResources(); |
| 88 | + logger.info(`Test finished: ${expect.getState().currentTestName}`); |
| 89 | + }, 1320000); |
| 90 | + |
| 91 | + itIf( |
| 92 | + "error on next connect", |
| 93 | + async () => { |
| 94 | + const config = await initDefaultConfig(env.databaseInfo.writerInstanceEndpoint, env.databaseInfo.instanceEndpointPort); |
| 95 | + client = initClientFunc(config); |
| 96 | + |
| 97 | + const testErrorToRaise: Error = new Error("test_connect"); |
| 98 | + ErrorSimulatorManager.raiseErrorOnNextConnect(testErrorToRaise); |
| 99 | + |
| 100 | + await expect(async () => { |
| 101 | + await client.connect(); |
| 102 | + }).rejects.toThrow(Error("test_connect")); |
| 103 | + |
| 104 | + // Connects with no error. |
| 105 | + await client.connect(); |
| 106 | + }, |
| 107 | + 1320000 |
| 108 | + ); |
| 109 | + |
| 110 | + itIf( |
| 111 | + "error on next query on opened connection", |
| 112 | + async () => { |
| 113 | + const config = await initDefaultConfig(env.databaseInfo.writerInstanceEndpoint, env.databaseInfo.instanceEndpointPort); |
| 114 | + client = initClientFunc(config); |
| 115 | + |
| 116 | + const simulator: ErrorSimulator = client.getPluginInstance(DeveloperConnectionPlugin); |
| 117 | + const testErrorToRaise: Error = new Error("test_query"); |
| 118 | + simulator.raiseErrorOnNextCall(testErrorToRaise, "query"); |
| 119 | + |
| 120 | + // No error thrown on connect call. |
| 121 | + await client.connect(); |
| 122 | + |
| 123 | + await expect(async () => { |
| 124 | + await client.query("select 1"); |
| 125 | + }).rejects.toThrow(Error("test_query")); |
| 126 | + |
| 127 | + // No error thrown on next query. |
| 128 | + await client.query("select 1"); |
| 129 | + }, |
| 130 | + 1320000 |
| 131 | + ); |
| 132 | + |
| 133 | + itIf( |
| 134 | + "error on query callback", |
| 135 | + async () => { |
| 136 | + const config = await initDefaultConfig(env.databaseInfo.writerInstanceEndpoint, env.databaseInfo.instanceEndpointPort); |
| 137 | + client = initClientFunc(config); |
| 138 | + const simulator: ErrorSimulator = client.getPluginInstance(DeveloperConnectionPlugin); |
| 139 | + simulator.setCallback(new TestErrorCallback()); |
| 140 | + |
| 141 | + // No error thrown on connect call. |
| 142 | + await client.connect(); |
| 143 | + |
| 144 | + // Executes normally. |
| 145 | + await client.query("select 2"); |
| 146 | + |
| 147 | + await expect(async () => { |
| 148 | + await client.query("select 1"); |
| 149 | + }).rejects.toThrow(Error("test_query")); |
| 150 | + }, |
| 151 | + 1320000 |
| 152 | + ); |
| 153 | +}); |
0 commit comments