Redesigned some tests based on recent test results (#992)

This commit is contained in:
Jenny Tam 2019-05-17 11:36:24 -07:00 committed by GitHub
parent 7e0bf91eec
commit ac59cfd56a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 23 deletions

View file

@ -1026,7 +1026,7 @@ void core_sqlsrv_sensitivity_metadata( _Inout_ sqlsrv_stmt* stmt TSRMLS_DC )
throw core::CoreException();
}
CHECK_CUSTOM_ERROR(true, stmt, SQLSRV_ERROR_DATA_CLASSIFICATION_FAILED, "Unexpected SQL Error state") {
CHECK_CUSTOM_ERROR(true, stmt, SQLSRV_ERROR_DATA_CLASSIFICATION_FAILED, "Check if ODBC driver or the server supports the Data Classification feature.") {
throw core::CoreException();
}
}

View file

@ -57,7 +57,7 @@ function testConnAttrCases()
}
}
function testNotAvailable($conn, $tableName, $isSupported)
function testNotAvailable($conn, $tableName, $isSupported, $driverCapable)
{
// If supported, the query should return a column with no classification
$options = array(PDO::SQLSRV_ATTR_DATA_CLASSIFICATION => true);
@ -66,25 +66,31 @@ function testNotAvailable($conn, $tableName, $isSupported)
$stmt->execute();
$notAvailableErr = '*Failed to retrieve Data Classification Sensitivity Metadata. If the driver and the server both support the Data Classification feature, check whether the query returns columns with classification information.';
$unexpectedErrorState = '*Failed to retrieve Data Classification Sensitivity Metadata: Check if ODBC driver or the server supports the Data Classification feature.';
$error = ($driverCapable) ? $notAvailableErr : $unexpectedErrorState;
try {
$metadata = $stmt->getColumnMeta(0);
echo "testNotAvailable: expected getColumnMeta to fail\n";
} catch (PDOException $e) {
if (!fnmatch($notAvailableErr, $e->getMessage())) {
if (!fnmatch($error, $e->getMessage())) {
echo "testNotAvailable: exception unexpected\n";
var_dump($e->getMessage());
}
}
}
function isDataClassSupported($conn)
function isDataClassSupported($conn, &$driverCapable)
{
// Check both SQL Server version and ODBC driver version
$msodbcsqlVer = $conn->getAttribute(PDO::ATTR_CLIENT_VERSION)["DriverVer"];
$version = explode(".", $msodbcsqlVer);
// ODBC Driver must be 17.2 or above
$driverCapable = true;
if ($version[0] < 17 || $version[1] < 2) {
$driverCapable = false;
return false;
}
@ -238,7 +244,8 @@ try {
testConnAttrCases();
$conn = connect();
$isSupported = isDataClassSupported($conn);
$driverCapable = true;
$isSupported = isDataClassSupported($conn, $driverCapable);
// Create a test table
$tableName = 'pdoPatients';
@ -274,7 +281,7 @@ try {
}
// Test another error condition
testNotAvailable($conn, $tableName, $isSupported);
testNotAvailable($conn, $tableName, $isSupported, $driverCapable);
// Run the query without data classification metadata
$tsql = "SELECT * FROM $tableName";

View file

@ -10,7 +10,7 @@ PHPT_EXEC=true
<?php
$dataClassKey = 'Data Classification';
function testErrorCases($conn, $tableName, $isSupported)
function testErrorCases($conn, $tableName, $isSupported, $driverCapable)
{
// This function will check two error cases:
// (1) if supported, the query should return a column with no classification
@ -22,13 +22,17 @@ function testErrorCases($conn, $tableName, $isSupported)
}
$notAvailableErr = '*Failed to retrieve Data Classification Sensitivity Metadata. If the driver and the server both support the Data Classification feature, check whether the query returns columns with classification information.';
$unexpectedErrorState = '*Failed to retrieve Data Classification Sensitivity Metadata: Check if ODBC driver or the server supports the Data Classification feature.';
$error = ($driverCapable) ? $notAvailableErr : $unexpectedErrorState;
$metadata = sqlsrv_field_metadata($stmt);
if ($metadata) {
echo "testErrorCases (1): expected sqlsrv_field_metadata to fail\n";
}
if (!fnmatch($notAvailableErr, sqlsrv_errors()[0]['message'])) {
if (!fnmatch($error, sqlsrv_errors()[0]['message'])) {
var_dump(sqlsrv_errors());
}
@ -49,14 +53,16 @@ function testErrorCases($conn, $tableName, $isSupported)
}
}
function isDataClassSupported($conn)
function isDataClassSupported($conn, &$driverCapable)
{
// Check both SQL Server version and ODBC driver version
$msodbcsqlVer = sqlsrv_client_info($conn)['DriverVer'];
$version = explode(".", $msodbcsqlVer);
// ODBC Driver must be 17.2 or above
$driverCapable = true;
if ($version[0] < 17 || $version[1] < 2) {
$driverCapable = false;
return false;
}
@ -221,7 +227,8 @@ if (!$conn) {
fatalError("Failed to connect.\n");
}
$isSupported = isDataClassSupported($conn);
$driverCapable = true;
$isSupported = isDataClassSupported($conn, $driverCapable);
// Create a test table
$tableName = 'srvPatients';
@ -262,7 +269,7 @@ if ($isSupported) {
}
}
testErrorCases($conn, $tableName, $isSupported);
testErrorCases($conn, $tableName, $isSupported, $driverCapable);
// Run the query without data classification metadata
$tsql = "SELECT * FROM $tableName";

View file

@ -9,25 +9,42 @@ Intentionally provide an invalid server name and set LoginTimeout. Verify the ti
$serverName = "WRONG_SERVER_NAME";
$t0 = microtime(true);
// Based on the following reference, a login timeout of less than approximately 10 seconds
// Based on the following reference, a login timeout of less than approximately 10 seconds
// is not reliable. The defaut is 15 seconds so we fix it at 20 seconds.
// https://docs.microsoft.com/sql/connect/odbc/windows/features-of-the-microsoft-odbc-driver-for-sql-server-on-windows
$timeout = 20;
$conn = sqlsrv_connect($serverName , array("LoginTimeout" => $timeout));
$timeout = 20;
$maxAttempts = 3;
$numAttempts = 0;
$leeway = 1.0;
$missed = false;
$t1 = microtime(true);
do {
$t0 = microtime(true);
$elapsed = $t1 - $t0;
$diff = abs($elapsed - $timeout);
$conn = sqlsrv_connect($serverName , array("LoginTimeout" => $timeout));
$numAttempts++;
if ($elapsed < $timeout || $diff > 1.0) {
echo "Connection failed at $elapsed secs. Leeway is 1.0 sec but the difference is $diff\n";
}
$t1 = microtime(true);
print "Done";
// Sometimes time elapsed might be less than expected timeout, such as 19.99*
// something, but 1.0 second leeway should be reasonable
$elapsed = $t1 - $t0;
$diff = abs($elapsed - $timeout);
$missed = ($diff > $leeway);
if ($missed) {
if ($numAttempts == $maxAttempts) {
echo "Connection failed at $elapsed secs. Leeway is $leeway sec but the difference is $diff\n";
} else {
// The test will fail but this helps us decide if this test should be redesigned
echo "$numAttempts\t";
sleep(5);
}
}
} while ($missed && $numAttempts < $maxAttempts);
print "Done\n";
?>
--EXPECT--
Done