public static IServiceCollection AddFsReleaseArtifactService(this IServiceCollection services, IConfiguration configuration) { //Check file permissions var artifactRootDir = new DirectoryInfo(configuration["ArtifactRootDirectory"]); var backupRootDir = new DirectoryInfo(configuration["BackupRootDirectory"]); if (!artifactRootDir.Exists) { artifactRootDir.Create(); } if (!backupRootDir.Exists) { backupRootDir.Create(); } artifactRootDir.EnsureWritable(); backupRootDir.EnsureWritable(); services.AddSingleton <IReleaseArtifactService>(serviceProvider => { var releaseArtifactRepository = new FsReleaseArtifactRepository( serviceProvider.GetRequiredService <ILogger <FsReleaseArtifactRepository> >(), artifactRootDir, backupRootDir ); return(new FsReleaseArtifactService( releaseArtifactRepository, serviceProvider.GetRequiredService <ILogger <FsReleaseArtifactService> >() )); }); return(services); }
public void TestRunBackup() { //Setup var testArtifactRoot = new DirectoryInfo(Path.Combine(projectDirectory, "TestArtifactRoot")); var testBackupDir = new DirectoryInfo(Path.Combine(projectDirectory, "TestBackup")); //Cleanup test dir from old tests (if they failed before) TestUtils.CleanupDirIfExists(testArtifactRoot.FullName); TestUtils.CleanupDirIfExists(testBackupDir.FullName); if (!testArtifactRoot.Exists) { testArtifactRoot.Create(); } if (!testBackupDir.Exists) { testBackupDir.Create(); } var customRepository = new FsReleaseArtifactRepository( Substitute.For <ILogger <FsReleaseArtifactRepository> >(), testArtifactRoot, testBackupDir ); var expectedFile = File.ReadAllBytes(Path.Combine(projectDirectory, "TestData", "test_zip.zip")); //Copy to the test file to the custom ArtifactRootDirectory File.Copy(Path.Combine(projectDirectory, "TestData", "test_zip.zip"), Path.Combine(testArtifactRoot.FullName, "test_zip.zip")); //Act customRepository.RunBackup(); var backupFiles = Directory.GetFiles(testBackupDir.FullName); //There is only one file -> .First() is used ZipFile.ExtractToDirectory(backupFiles.First(), testBackupDir.FullName); File.Delete(backupFiles.First()); //Get the actual file in the directory backupFiles = Directory.GetFiles(testBackupDir.FullName); var backupFile = File.ReadAllBytes(backupFiles.First()); //Assert Assert.Equal(expectedFile, backupFile); //Cleanup testArtifactRoot.Delete(true); testBackupDir.Delete(true); }
public void TestRestore() { var testArtifactRoot = new DirectoryInfo(Path.Combine(projectDirectory, "TestArtifactRoot")); var testBackupDir = new DirectoryInfo(Path.Combine(projectDirectory, "TestBackup")); //Cleanup test dir from old tests (if they failed before) TestUtils.CleanupDirIfExists(testArtifactRoot.FullName); TestUtils.CleanupDirIfExists(testBackupDir.FullName); if (!testArtifactRoot.Exists) { testArtifactRoot.Create(); } if (!testBackupDir.Exists) { testBackupDir.Create(); } var customRepository = new FsReleaseArtifactRepository( Substitute.For <ILogger <FsReleaseArtifactRepository> >(), testArtifactRoot, testBackupDir ); var testBackupZip = new ZipArchive(File.OpenRead(Path.Combine(projectDirectory, "TestData", "restoreTest", "testFile.zip"))); var expectedFile = File.ReadAllBytes(Path.Combine(projectDirectory, "TestData", "restoreTest", "testFile.txt")); //Act customRepository.RestoreBackup(testBackupZip); //Get the (only) one file in the testArtifactRootDirectory var artifactFiles = Directory.GetFiles(testArtifactRoot.FullName); var testFile = File.ReadAllBytes(artifactFiles.First()); //Assert Assert.Equal(expectedFile, testFile); //Cleanup testArtifactRoot.Delete(true); testBackupDir.Delete(true); }