def test_notifier(self): sut = Configuration('Foo') with self.assertRaises(AttributeError): sut.notifier notifier = Mock(Notifier) sut.notifier = notifier self.assertEquals(sut.notifier, notifier) with self.assertRaises(AttributeError): sut.notifier = notifier
def test_restore_with_file_path(self, path): # Create the target directory. with TemporaryDirectory() as target_path: latest_path = os.path.join(target_path, 'latest') os.makedirs(latest_path) build_files_stage_1(latest_path) # Create the source directory. with TemporaryDirectory() as source_path: configuration = Configuration('Foo', verbose=True) configuration.notifier = Mock(Notifier) configuration.source = PathSource(configuration.logger, configuration.notifier, source_path + '/') configuration.target = PathTarget(configuration.logger, configuration.notifier, target_path) result = restore(configuration, path) self.assertTrue(result) with self.assertRaises(AssertionError): assert_paths_identical(self, source_path, os.path.join(target_path, 'latest')) assert_paths_identical( self, os.path.join(source_path, path), os.path.join(target_path, 'latest', path))
def test_backup_all(self): # Create the source directory. with TemporaryDirectory() as source_path: build_files_stage_1(source_path) # Create the target directory. with TemporaryDirectory() as target_path: configuration = Configuration('Foo', verbose=True) configuration.notifier = Mock(Notifier) configuration.source = PathSource(configuration.logger, configuration.notifier, source_path + '/') configuration.target = PathTarget(configuration.logger, configuration.notifier, target_path) # Back up the first time. result = backup(configuration) self.assertTrue(result) assert_paths_identical(self, source_path, os.path.join(target_path, 'latest')) real_snapshot_1_path = subprocess.check_output( ['readlink', '-f', 'latest'], cwd=target_path).decode('utf-8').strip() # Sleep for two seconds, so we are (hopefully) absolutely sure the time-based snapshot name generator # will not generate identical names for all snapshots. time.sleep(2) result = backup(configuration) self.assertTrue(result) assert_paths_identical(self, os.path.join(target_path, 'latest'), source_path) real_snapshot_2_path = subprocess.check_output( ['readlink', '-f', 'latest'], cwd=target_path).decode('utf-8').strip() # Ensure the previous snapshot has not changed. assert_paths_identical(self, real_snapshot_1_path, source_path) # Sleep for two seconds, so we are (hopefully) absolutely sure the time-based snapshot name generator # will not generate identical names for all snapshots. time.sleep(2) # Change the source data and create another snapshot. Confirm the first two snapshots remain untouched, # and only the new one contains the changes. build_files_stage_2(source_path) result = backup(configuration) self.assertTrue(result) assert_paths_identical(self, os.path.join(target_path, 'latest'), source_path) # Ensure the changes made to the source did not affect the previous snapshots. with self.assertRaises(AssertionError): assert_paths_identical(self, real_snapshot_1_path, source_path) with self.assertRaises(AssertionError): assert_paths_identical(self, real_snapshot_2_path, source_path)
def test_new_target(self): configuration = Configuration('Foo', working_directory='/') configuration.notifier = Mock(Notifier) path = '/var/cache' configuration_data = { 'path': path, } target = new_target(configuration, 'path', configuration_data) self.assertEquals(target.path, path)
def test_backup_with_file_path(self): path = 'sub/some.file.in.subdirectory' # Create the source directory. with TemporaryDirectory() as source_path: build_files_stage_1(source_path) # Create the target directory. with TemporaryDirectory() as target_path: configuration = Configuration('Foo', verbose=True) configuration.notifier = Mock(Notifier) configuration.source = PathSource(configuration.logger, configuration.notifier, source_path + '/') configuration.target = PathTarget(configuration.logger, configuration.notifier, target_path) # Back up the first time. result = backup(configuration, path) self.assertTrue(result) real_snapshot_1_path = subprocess.check_output( ['readlink', '-f', 'latest'], cwd=target_path).decode('utf-8').strip() with self.assertRaises(AssertionError): assert_paths_identical(self, source_path, real_snapshot_1_path) assert_paths_identical( self, os.path.join(source_path, path), os.path.join(real_snapshot_1_path, path)) # Sleep for two seconds, so we are (hopefully) absolutely sure the time-based snapshot name generator # will not generate identical names for all snapshots. time.sleep(2) result = backup(configuration, path) self.assertTrue(result) real_snapshot_2_path = subprocess.check_output( ['readlink', '-f', 'latest'], cwd=target_path).decode('utf-8').strip() with self.assertRaises(AssertionError): assert_paths_identical(self, source_path, real_snapshot_2_path) assert_paths_identical( self, os.path.join(source_path, path), os.path.join(real_snapshot_2_path, path)) # Ensure the previous snapshot has not changed. with self.assertRaises(AssertionError): assert_paths_identical(self, source_path, real_snapshot_1_path) assert_paths_identical( self, os.path.join(real_snapshot_1_path, path), os.path.join(source_path, path))
def test_new_target_with_default_port(self): configuration = Configuration('Foo') configuration.notifier = Mock(Notifier) user = '******' host = 'example.com' path = '/var/cache' configuration_data = { 'user': user, 'host': host, 'path': path, } sut = new_target(configuration, 'ssh', configuration_data) self.assertEquals(sut.port, 22)
def test_new_target_without_path(self): configuration = Configuration('Foo') configuration.notifier = Mock(Notifier) user = '******' host = 'example.com' port = 666 configuration_data = { 'user': user, 'host': host, 'port': port, } with self.assertRaises(ValueError): new_target(configuration, 'ssh', configuration_data)
def test_new_target_without_host(self): configuration = Configuration('Foo') configuration.notifier = Mock(Notifier) user = '******' port = 666 path = '/var/cache' configuration_data = { 'user': user, 'port': port, 'path': path, } with self.assertRaises(ValueError): new_target(configuration, 'ssh', configuration_data)
def test_new_target_without_user(self): configuration = Configuration('Foo') configuration.notifier = Mock(Notifier) host = 'example.com' port = 666 path = '/var/cache' configuration_data = { 'host': host, 'port': port, 'path': path, } with self.assertRaises(ValueError): new_target(configuration, 'ssh', configuration_data)
def test_new_target_with_invalid_port_too_high(self): configuration = Configuration('Foo') configuration.notifier = Mock(Notifier) user = '******' host = 'example.com' port = 65536 path = '/var/cache' configuration_data = { 'user': user, 'host': host, 'port': port, 'path': path, } with self.assertRaises(ValueError): new_target(configuration, 'ssh', configuration_data)
def test_restore_with_unavailable_target(self): # Create the source directory. with TemporaryDirectory() as source_path: # Create the target directory. with TemporaryDirectory() as target_path: configuration = Configuration('Foo', verbose=True) configuration.notifier = Mock(Notifier) configuration.source = PathSource(configuration.logger, configuration.notifier, source_path) configuration.target = PathTarget( configuration.logger, configuration.notifier, target_path + '/NonExistentPath') result = restore(configuration) self.assertFalse(result)
def test_restore_with_subprocess_error(self, m): m.side_effect = subprocess.CalledProcessError(7, '') # Create the target directory. with TemporaryDirectory() as target_path: # Create the source directory. with TemporaryDirectory() as source_path: configuration = Configuration('Foo', verbose=True) configuration.notifier = Mock(Notifier) configuration.source = PathSource(configuration.logger, configuration.notifier, source_path + '/') configuration.target = PathTarget(configuration.logger, configuration.notifier, target_path) result = restore(configuration) self.assertFalse(result)
def test_new_target(self): configuration = Configuration('Foo') configuration.notifier = Mock(Notifier) user = '******' host = 'example.com' port = 666 path = '/var/cache' configuration_data = { 'user': user, 'host': host, 'port': port, 'path': path, } target = new_target(configuration, 'ssh', configuration_data) self.assertEquals(target.path, path) self.assertEquals(target.user, user) self.assertEquals(target.host, host) self.assertEquals(target.port, port)
def test_backup(self): with TemporaryDirectory() as target_path: with TemporaryDirectory() as mirrored_local_source_path: self._container = SshLocationContainer( mount_point=mirrored_local_source_path) self._container.start() build_files_stage_1(mirrored_local_source_path) configuration = Configuration('Foo', verbose=True) configuration.notifier = Mock(Notifier) configuration.source = self._container.source(configuration) configuration.target = PathTarget(configuration.logger, configuration.notifier, target_path) result = backup(configuration) self.assertTrue(result) assert_paths_identical(self, mirrored_local_source_path, os.path.join( target_path, 'latest'))
def test_restore_all(self): # Create the target directory. with TemporaryDirectory() as target_path: latest_path = os.path.join(target_path, 'latest') os.makedirs(latest_path) build_files_stage_1(latest_path) # Create the source directory. with TemporaryDirectory() as source_path: configuration = Configuration('Foo', verbose=True) configuration.notifier = Mock(Notifier) configuration.source = PathSource(configuration.logger, configuration.notifier, source_path + '/') configuration.target = PathTarget(configuration.logger, configuration.notifier, target_path) result = restore(configuration) self.assertTrue(result) subprocess.check_call(['ls', '-la', source_path]) assert_paths_identical(self, source_path, os.path.join(target_path, 'latest'))
def test_new_target_without_path(self): configuration = Configuration('Foo', working_directory='/') configuration.notifier = Mock(Notifier) configuration_data = {} with self.assertRaises(ValueError): new_target(configuration, 'path', configuration_data)