Skip to content

Commit

Permalink
Overwrite export archive if exists (#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArinGhazarian authored Feb 16, 2023
1 parent 5fe0057 commit c5746cf
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public async Task Download_Returns_Downloaded_Archive_Full_Name()
_mockSmbFileStore.Verify(m => m.GetFileInformation(out fileStandardInformation, sharedFileHandle, FileInformationClass.FileStandardInformation), Times.Once);
_mockSmbFileStore.Verify(m => m.ReadFile(out data, sharedFileHandle, It.IsAny<long>(), It.IsAny<int>()), Times.Exactly(3));
_mockFileSystemProvider.Verify(m => m.CreateDirectory(TARGET_DIRECTORY), Times.Once);
_mockFileSystemProvider.Verify(m => m.Open(expectedTargetArchiveFullName, FileMode.CreateNew), Times.Once);
_mockFileSystemProvider.Verify(m => m.Open(expectedTargetArchiveFullName, FileMode.Create), Times.Once);
_mockFileSystemProvider.Verify(m => m.WriteAsync(It.IsAny<FileStream>(), data, It.IsAny<CancellationToken>()), Times.Exactly(2));

actualTargetArchiveFullName.Should().Be(expectedTargetArchiveFullName);
Expand Down Expand Up @@ -187,19 +187,4 @@ await _bbsArchiveDownloader
.ThrowExactlyAsync<OctoshiftCliException>()
.WithMessage($"*{NTStatus.STATUS_OBJECT_NAME_NOT_FOUND}*");
}

[Fact]
public async Task Download_Throws_When_Target_Export_Archive_Already_Exists()
{
// Arrange
var targetArchiveFullName = Path.Join(TARGET_DIRECTORY, _exportArchiveFilename).ToUnixPath();
_mockFileSystemProvider.Setup(m => m.FileExists(targetArchiveFullName)).Returns(true);

// Act, Assert
await _bbsArchiveDownloader
.Invoking(x => x.Download(EXPORT_JOB_ID, TARGET_DIRECTORY))
.Should()
.ThrowExactlyAsync<OctoshiftCliException>()
.WithMessage($"*{targetArchiveFullName}*");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,10 @@ public async Task Download_Returns_Downloaded_Archive_Full_Name()
null,
It.IsAny<Action<ulong>>()));

_mockFileSystemProvider.Verify(m => m.Open(expectedTargetArchiveFullName, FileMode.CreateNew));
_mockFileSystemProvider.Verify(m => m.Open(expectedTargetArchiveFullName, FileMode.Create));
actualDownloadedArchiveFullName.Should().Be(expectedTargetArchiveFullName);
}

[Fact]
public async Task Download_Throws_When_Target_Export_Archive_Already_Exists()
{
// Arrange
_mockFileSystemProvider.Setup(m => m.FileExists(It.Is<string>(x => x.Contains(_exportArchiveFilename)))).Returns(true);

// Act, Assert
await _bbsArchiveDownloader.Invoking(x => x.Download(EXPORT_JOB_ID)).Should().ThrowExactlyAsync<OctoshiftCliException>();
}

[Fact]
public async Task Download_Throws_When_Source_Export_Archive_Does_Not_Exist()
{
Expand Down
7 changes: 1 addition & 6 deletions src/bbs2gh/Services/BbsSmbArchiveDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,8 @@ private string GetLogFriendlySize(long size)

private FileStream OpenWriteTargetExportArchive(string targetExportArchiveFullPath)
{
if (_fileSystemProvider.FileExists(targetExportArchiveFullPath))
{
throw new OctoshiftCliException($"Target export archive ({targetExportArchiveFullPath}) already exists.");
}

_fileSystemProvider.CreateDirectory(Path.GetDirectoryName(targetExportArchiveFullPath));
return _fileSystemProvider.Open(targetExportArchiveFullPath, FileMode.CreateNew);
return _fileSystemProvider.Open(targetExportArchiveFullPath, FileMode.Create);
}

private void ConnectToHost()
Expand Down
7 changes: 1 addition & 6 deletions src/bbs2gh/Services/BbsSshArchiveDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ public async Task<string> Download(long exportJobId, string targetDirectory = IB
var targetExportArchiveFullPath =
Path.Join(targetDirectory ?? IBbsArchiveDownloader.DEFAULT_TARGET_DIRECTORY, IBbsArchiveDownloader.GetExportArchiveFileName(exportJobId)).ToUnixPath();

if (_fileSystemProvider.FileExists(targetExportArchiveFullPath))
{
throw new OctoshiftCliException($"Target export archive ({targetExportArchiveFullPath}) already exists.");
}

if (_sftpClient is BaseClient { IsConnected: false } client)
{
client.Connect();
Expand All @@ -105,7 +100,7 @@ public async Task<string> Download(long exportJobId, string targetDirectory = IB
_fileSystemProvider.CreateDirectory(targetDirectory);

var sourceExportArchiveSize = _sftpClient.GetAttributes(sourceExportArchiveFullPath)?.Size ?? long.MaxValue;
await using var targetExportArchive = _fileSystemProvider.Open(targetExportArchiveFullPath, FileMode.CreateNew);
await using var targetExportArchive = _fileSystemProvider.Open(targetExportArchiveFullPath, FileMode.Create);
await Task.Factory.FromAsync(
_sftpClient.BeginDownloadFile(
sourceExportArchiveFullPath,
Expand Down

0 comments on commit c5746cf

Please sign in to comment.