def test_freezer_fs_backup_bad_checksum(self):
        # as above, but we'll ignore the computed checksum
        metadata_path = self.create_local_backup(consistency_check=True)
        metadata = base.load_metadata(metadata_path)

        # make a failing sha256 checksum (assuming no added path string)
        bad_checksum = '0' * 64

        # attempt to restore using the bad checksum
        restore_args = ['freezer-agent',
                        '--action', 'restore',
                        '--restore-abs-path', self.dest_tree.path,
                        '--container', metadata['container'],
                        '--backup-name', self.backup_name,
                        '--storage', 'local',
                        '--consistency-checksum', bad_checksum]

        process = subprocess.Popen(restore_args,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE,
                                   env=self.environ, shell=False)
        out, err = process.communicate()

        # make sure the subprocess exist with an error due to checksum mismatch
        message = '{0} Output: {1} Error: {2}'.format(
            'Restore process should fail with checksum error.',
            out, err)
        self.assertEqual(1, process.returncode, message)
        self.assertEqual('', out, message)
        self.assertNotEqual('', err, message)
    def test_freezer_fs_backup_valid_checksum(self):
        # perform a normal backup, but enable consistency checks and save the
        # metadata to disk
        metadata_path = self.create_local_backup(consistency_check=True)

        metadata = base.load_metadata(metadata_path)

        # load the stored metadata to retrieve the computed checksum
        self.assertIn('consistency_checksum', metadata,
                      'Checksum must exist in stored metadata.')

        checksum = metadata['consistency_checksum']
        restore_args = [
            'freezer-agent', '--action', 'restore', '--restore-abs-path',
            self.dest_tree.path, '--container', metadata['container'],
            '--backup-name', self.backup_name, '--storage', 'local',
            '--consistency-checksum', checksum
        ]

        self.run_subprocess(
            restore_args, 'Test restore from local storage with '
            'computed checksum.')