Fix backup file validation error message (#472)

* Fix backup error message

* check for file existence for restore

* chang error message

* address pr comment

* cleanup
This commit is contained in:
Kate Shin
2017-10-02 15:45:28 -07:00
committed by GitHub
parent e7756b0bf1
commit 8e78ecf9a4
5 changed files with 66 additions and 64 deletions

View File

@@ -44,15 +44,10 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
connection = ReliableConnectionHelper.GetAsSqlConnection(dbConnection); connection = ReliableConnectionHelper.GetAsSqlConnection(dbConnection);
} }
} }
}
if (connection != null) if (connection != null)
{ {
bool isLocal = false;
if (string.Compare(GetMachineName(connection.DataSource), Environment.MachineName, StringComparison.OrdinalIgnoreCase) == 0)
{
isLocal = true;
}
foreach (string filePath in args.FilePaths) foreach (string filePath in args.FilePaths)
{ {
bool isFolder; bool isFolder;
@@ -62,22 +57,45 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
{ {
if (isFolder) if (isFolder)
{ {
errorMessage = string.Format(SR.BackupPathIsFolderError, filePath); errorMessage = SR.BackupPathIsFolderError;
result = false;
break;
} }
} }
else else
{
if (args.ServiceType == FileValidationServiceConstants.Backup)
{
errorMessage = IsFolderPathExisting(connection, filePath);
}
else if (args.ServiceType == FileValidationServiceConstants.Restore)
{
errorMessage = SR.InvalidBackupPathError;
}
}
if (!string.IsNullOrEmpty(errorMessage))
{
result = false;
break;
}
}
}
return result;
}
#region private methods
internal static string IsFolderPathExisting(SqlConnection connection, string filePath)
{ {
// If the file path doesn't exist, check if the folder exists // If the file path doesn't exist, check if the folder exists
string folderPath = PathWrapper.GetDirectoryName(filePath); string folderPath = PathWrapper.GetDirectoryName(filePath);
if (isLocal) string errorMessage = string.Empty;
if (string.Compare(GetMachineName(connection.DataSource), Environment.MachineName, StringComparison.OrdinalIgnoreCase) == 0)
{ {
if (!string.IsNullOrEmpty(folderPath) && !Directory.Exists(folderPath)) if (!string.IsNullOrEmpty(folderPath) && !Directory.Exists(folderPath))
{ {
errorMessage = string.Format(SR.InvalidBackupPathError, folderPath); errorMessage = SR.InvalidBackupPathError;
result = false;
break;
} }
} }
else else
@@ -86,29 +104,13 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
bool existsOnRemote = IsPathExisting(connection, folderPath, out isFolderOnRemote); bool existsOnRemote = IsPathExisting(connection, folderPath, out isFolderOnRemote);
if (!existsOnRemote) if (!existsOnRemote)
{ {
errorMessage = string.Format(SR.InvalidBackupPathError, folderPath); errorMessage = SR.InvalidBackupPathError;
result = false;
break;
} }
} }
}
}
}
else
{
result = false;
}
}
else
{
result = false;
}
return result; return errorMessage;
} }
#region private methods
internal static bool IsPathExisting(SqlConnection connection, string path, out bool isFolder) internal static bool IsPathExisting(SqlConnection connection, string path, out bool isFolder)
{ {
Request req = new Request Request req = new Request

View File

@@ -1816,11 +1816,11 @@
<comment></comment> <comment></comment>
</data> </data>
<data name="BackupPathIsFolderError" xml:space="preserve"> <data name="BackupPathIsFolderError" xml:space="preserve">
<value>The provided path specifies a directory but a file path is required: {0}</value> <value>Please provide a file path instead of directory path</value>
<comment></comment> <comment></comment>
</data> </data>
<data name="InvalidBackupPathError" xml:space="preserve"> <data name="InvalidBackupPathError" xml:space="preserve">
<value> Cannot verify the existence of the backup file location: {0}</value> <value> The provided path is invalid</value>
<comment></comment> <comment></comment>
</data> </data>
<data name="TaskInProgress" xml:space="preserve"> <data name="TaskInProgress" xml:space="preserve">

View File

@@ -808,8 +808,8 @@ prototype_file_noApplicableFileGroup = No Applicable Filegroup
BackupTaskName = Backup Database BackupTaskName = Backup Database
# Backup File Validation Errors # Backup File Validation Errors
BackupPathIsFolderError = The provided path specifies a directory but a file path is required: {0} BackupPathIsFolderError = Please provide a file path instead of directory path
InvalidBackupPathError = Cannot verify the existence of the backup file location: {0} InvalidBackupPathError = The provided path is invalid
############################################################################ ############################################################################
# Task Service # Task Service

View File

@@ -2256,12 +2256,12 @@
<note></note> <note></note>
</trans-unit> </trans-unit>
<trans-unit id="BackupPathIsFolderError"> <trans-unit id="BackupPathIsFolderError">
<source>The provided path specifies a directory but a file path is required: {0}</source> <source>Please provide a file path instead of directory path</source>
<target state="new">The file name specified is also a directory name: {0}</target> <target state="new">The file name specified is also a directory name: {0}</target>
<note></note> <note></note>
</trans-unit> </trans-unit>
<trans-unit id="InvalidBackupPathError"> <trans-unit id="InvalidBackupPathError">
<source> Cannot verify the existence of the backup file location: {0}</source> <source> The provided path is invalid</source>
<target state="new"> Cannot verify the existence of the backup file location: {0}</target> <target state="new"> Cannot verify the existence of the backup file location: {0}</target>
<note></note> <note></note>
</trans-unit> </trans-unit>

View File

@@ -14,11 +14,11 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.DisasterRecovery
public class DisasterRecoveryFileValidatorUnitTests public class DisasterRecoveryFileValidatorUnitTests
{ {
[Fact] [Fact]
public void ValidatorShouldReturnFalseForNullArgument() public void ValidatorShouldReturnTrueForNullArgument()
{ {
string message; string message;
bool result = DisasterRecoveryFileValidator.ValidatePaths(null, out message); bool result = DisasterRecoveryFileValidator.ValidatePaths(null, out message);
Assert.False(result); Assert.True(result);
} }
[Fact] [Fact]