Пример #1
0
    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))
Пример #2
0
    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)
Пример #3
0
 def test_source(self):
     sut = Configuration('Foo')
     with self.assertRaises(AttributeError):
         sut.source
     source = Mock(Source)
     sut.source = source
     self.assertEquals(sut.source, source)
     with self.assertRaises(AttributeError):
         sut.source = source
Пример #4
0
 def test_target(self):
     sut = Configuration('Foo')
     with self.assertRaises(AttributeError):
         sut.target
     target = Mock(Target)
     sut.target = target
     self.assertEquals(sut.target, target)
     with self.assertRaises(AttributeError):
         sut.target = target
Пример #5
0
 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
Пример #6
0
 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)
Пример #7
0
    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))
Пример #8
0
 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)
Пример #9
0
 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)
Пример #10
0
 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)
Пример #11
0
 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)
Пример #12
0
 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)
Пример #13
0
 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)
Пример #14
0
 def test_new_notifier_with_fallback_only(self):
     configuration = Configuration('Foo')
     data = {
         'fallback': ['some', 'fallback'],
     }
     notifier = new_notifier(configuration, 'command', data)
     self.assertIsInstance(notifier, CommandNotifier)
Пример #15
0
    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)
Пример #16
0
 def test_new_notifier_with_fallback_only(self):
     with NamedTemporaryFile(mode='a+t') as fallback_file:
         configuration = Configuration('Foo')
         data = {
             'fallback': fallback_file.name,
         }
         notifier = new_notifier(configuration, 'file', data)
         self.assertIsInstance(notifier, FileNotifier)
Пример #17
0
    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'))
Пример #18
0
 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)
Пример #19
0
 def test_new_notifier_without_alert_and_fallback(self):
     configuration = Configuration('Foo')
     data = {
         'state': ['some', 'state'],
         'inform': ['some', 'inform'],
         'confirm': ['some', 'confirm'],
     }
     with self.assertRaises(ValueError):
         new_notifier(configuration, 'command', data)
Пример #20
0
 def test_new_notifier_without_fallback(self):
     configuration = Configuration('Foo')
     data = {
         'state': ['some', 'state'],
         'inform': ['some', 'inform'],
         'confirm': ['some', 'confirm'],
         'alert': ['some', 'alert'],
     }
     notifier = new_notifier(configuration, 'command', data)
     self.assertIsInstance(notifier, CommandNotifier)
Пример #21
0
 def test_new_notifier_without_fallback(self):
     with NamedTemporaryFile(mode='a+t') as state_file:
         with NamedTemporaryFile(mode='a+t') as inform_file:
             with NamedTemporaryFile(mode='a+t') as confirm_file:
                 with NamedTemporaryFile(mode='a+t') as alert_file:
                     configuration = Configuration('Foo')
                     data = {
                         'state': state_file.name,
                         'inform': inform_file.name,
                         'confirm': confirm_file.name,
                         'alert': alert_file.name,
                     }
                     notifier = new_notifier(configuration, 'file', data)
                     self.assertIsInstance(notifier, FileNotifier)
Пример #22
0
    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'))
Пример #23
0
 def test_verbose(self):
     sut = Configuration('Foo', verbose=True)
     self.assertTrue(sut.verbose)
     sut.verbose = False
     self.assertFalse(sut.verbose)
Пример #24
0
 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)
Пример #25
0
 def test_without_working_directory(self):
     sut = Configuration('Foo')
     with self.assertRaises(AttributeError):
         sut.working_directory
Пример #26
0
 def test_name_with_name(self):
     name = 'Foo'
     sut = Configuration(name)
     self.assertEquals(sut.name, name)
Пример #27
0
 def test_logger(self):
     sut = Configuration('Foo')
     logger = sut.logger
     self.assertIsInstance(logger, Logger)
Пример #28
0
 def test_working_directory(self):
     sut = Configuration('Foo', working_directory=CONFIGURATION_PATH)
     self.assertEquals(sut.working_directory, CONFIGURATION_PATH)