Beispiel #1
0
class SwiftStorageUtils(testtools.TestCase):

    def setUp(self):
        super(SwiftStorageUtils, self).setUp()
        context = TroveContext()
        swift_client = FakeSwiftConnection()
        when(swift).create_swift_client(context).thenReturn(swift_client)
        self.swift = SwiftStorage(context)

    def tearDown(self):
        super(SwiftStorageUtils, self).tearDown()

    def test_explode_location(self):
        location = 'http://mockswift.com/v1/545433/backups/mybackup.tar'
        url, container, filename = self.swift._explodeLocation(location)
        self.assertEqual(url, 'http://mockswift.com/v1/545433')
        self.assertEqual(container, 'backups')
        self.assertEqual(filename, 'mybackup.tar')

    def test_validate_checksum_good(self):
        match = self.swift._verify_checksum('"my-good-etag"', 'my-good-etag')
        self.assertTrue(match)

    def test_verify_checksum_bad(self):
        self.assertRaises(SwiftDownloadIntegrityError,
                          self.swift._verify_checksum,
                          '"THE-GOOD-THE-BAD"',
                          'AND-THE-UGLY')
Beispiel #2
0
    def test_swift_segment_checksum_etag_mismatch(self):
        """This tests that when etag doesn't match segment uploaded checksum
        False is returned and None for checksum and location"""
        context = TroveContext()
        # this backup_id will trigger fake swift client with calculate_etag
        # enabled to spit out a bad etag when a segment object is uploaded
        backup_id = 'bad_segment_etag_123'
        user = '******'
        password = '******'
        backup_container = 'database_backups'

        swift_client = FakeSwiftConnectionWithRealEtag()
        when(swift).create_swift_client(context).thenReturn(swift_client)
        storage_strategy = SwiftStorage(context)

        with MockBackupRunner(filename=backup_id,
                              user=user,
                              password=password) as runner:
            (success,
             note,
             checksum,
             location) = storage_strategy.save(backup_container, runner)

        self.assertEqual(success, False,
                         "The backup should have failed!")
        self.assertTrue(note.startswith("Error saving data to Swift!"))
        self.assertIsNone(checksum,
                          "Swift checksum should be None for failed backup.")
        self.assertIsNone(location,
                          "Swift location should be None for failed backup.")
Beispiel #3
0
class SwiftStorageUtils(testtools.TestCase):
    def setUp(self):
        super(SwiftStorageUtils, self).setUp()
        context = TroveContext()
        swift_client = FakeSwiftConnection()
        swift.create_swift_client = MagicMock(return_value=swift_client)
        self.swift = SwiftStorage(context)

    def tearDown(self):
        super(SwiftStorageUtils, self).tearDown()

    def test_explode_location(self):
        location = 'http://mockswift.com/v1/545433/backups/mybackup.tar'
        url, container, filename = self.swift._explodeLocation(location)
        self.assertEqual(url, 'http://mockswift.com/v1/545433')
        self.assertEqual(container, 'backups')
        self.assertEqual(filename, 'mybackup.tar')

    def test_validate_checksum_good(self):
        match = self.swift._verify_checksum('"my-good-etag"', 'my-good-etag')
        self.assertTrue(match)

    def test_verify_checksum_bad(self):
        self.assertRaises(SwiftDownloadIntegrityError,
                          self.swift._verify_checksum, '"THE-GOOD-THE-BAD"',
                          'AND-THE-UGLY')
Beispiel #4
0
 def setUp(self):
     super(SwiftMetadataTests, self).setUp()
     self.swift_client = FakeSwiftConnection()
     self.context = TroveContext()
     when(swift).create_swift_client(self.context).thenReturn(
         self.swift_client)
     self.swift = SwiftStorage(self.context)
Beispiel #5
0
    def test_swift_checksum_save(self):
        """This tests that SwiftStorage.save returns the swift checksum"""
        context = TroveContext()
        backup_id = '123'
        user = '******'
        password = '******'

        swift_client = FakeSwiftConnection()
        with patch.object(swift, 'create_swift_client',
                          return_value=swift_client):
            storage_strategy = SwiftStorage(context)

            with MockBackupRunner(filename=backup_id,
                                  user=user,
                                  password=password) as runner:
                (success,
                 note,
                 checksum,
                 location) = storage_strategy.save(runner.manifest, runner)

        self.assertTrue(success, "The backup should have been successful.")
        self.assertIsNotNone(note, "A note should have been returned.")
        self.assertEqual('http://mockswift/v1/database_backups/123.gz.enc',
                         location,
                         "Incorrect swift location was returned.")
Beispiel #6
0
    def test_run_verify_checksum(self):
        """This tests that swift download cmd runs if original backup checksum
        matches swift object etag"""

        context = TroveContext()
        location = "/backup/location/123"
        is_zipped = False
        backup_checksum = "fake-md5-sum"

        swift_client = FakeSwiftConnection()
        when(swift).create_swift_client(context).thenReturn(swift_client)
        download_process = MockProcess()
        subprocess = mock(swift.subprocess)
        when(subprocess).Popen(any(), any(),
                               any(), any()).thenReturn(download_process)
        when(swift.utils).raise_if_process_errored().thenReturn(None)

        storage_strategy = SwiftStorage(context)
        download_stream = storage_strategy.load(context,
                                                location,
                                                is_zipped,
                                                backup_checksum)

        self.assertEqual(download_stream.container, "location")
        self.assertEqual(download_stream.filename, "123")

        with download_stream as stream:
            print "Testing SwiftDownloadStream context manager: %s" % stream

        self.assertIsNotNone(download_stream.process,
                             "SwiftDownloadStream process/cmd is supposed "
                             "to run.")
        self.assertIsNotNone(download_stream.pid,
                             "SwiftDownloadStream process/cmd is supposed "
                             "to run.")
Beispiel #7
0
    def test_swift_checksum_etag_mismatch(self):
        """This tests that when etag doesn't match swift checksum False is
        returned and None for checksum and location"""
        context = TroveContext()
        # this backup_id will trigger fake swift client with calculate_etag
        # enabled to spit out a bad etag when a segment object is uploaded
        backup_id = 'bad_manifest_etag_123'
        user = '******'
        password = '******'
        backup_container = 'database_backups'

        swift_client = fake_create_swift_client(calculate_etag=True)
        when(swift).create_swift_client(context).thenReturn(swift_client)
        storage_strategy = SwiftStorage(context)

        with MockBackupRunner(filename=backup_id, user=user,
                              password=password) as runner:
            (success, note, checksum,
             location) = storage_strategy.save(backup_container, runner)

        self.assertEqual(success, False, "The backup should have failed!")
        self.assertTrue(note.startswith("Error saving data to Swift!"))
        self.assertIsNone(checksum,
                          "Swift checksum should be None for failed backup.")
        self.assertIsNone(location,
                          "Swift location should be None for failed backup.")
Beispiel #8
0
    def test_swift_checksum_save(self):
        """This tests that SwiftStorage.save returns the swift checksum"""
        context = TroveContext()
        backup_id = '123'
        user = '******'
        password = '******'
        backup_container = 'database_backups'

        swift_client = FakeSwiftConnectionWithRealEtag()
        when(swift).create_swift_client(context).thenReturn(swift_client)
        storage_strategy = SwiftStorage(context)

        with MockBackupRunner(filename=backup_id,
                              user=user,
                              password=password) as runner:
            (success,
             note,
             checksum,
             location) = storage_strategy.save(backup_container, runner)

        self.assertEqual(success, True,
                         "The backup should have been successful.")
        self.assertIsNotNone(note, "A note should have been returned.")
        self.assertEqual(location, 'http://mockswift/v1/database_backups/123',
                         "Incorrect swift location was returned.")
Beispiel #9
0
    def test_run_verify_checksum(self):
        """This tests that swift download cmd runs if original backup checksum
        matches swift object etag"""

        context = TroveContext()
        location = "/backup/location/123"
        is_zipped = False
        backup_checksum = "fake-md5-sum"

        swift_client = fake_create_swift_client()
        when(swift).create_swift_client(context).thenReturn(swift_client)
        download_process = MockProcess()
        subprocess = mock(swift.subprocess)
        when(subprocess).Popen(any(), any(), any(),
                               any()).thenReturn(download_process)
        when(swift.utils).raise_if_process_errored().thenReturn(None)

        storage_strategy = SwiftStorage(context)
        download_stream = storage_strategy.load(context, location, is_zipped,
                                                backup_checksum)

        self.assertEqual(download_stream.container, "location")
        self.assertEqual(download_stream.filename, "123")

        with download_stream as stream:
            print "Testing SwiftDownloadStream context manager: %s" % stream

        self.assertIsNotNone(
            download_stream.process,
            "SwiftDownloadStream process/cmd is supposed "
            "to run.")
        self.assertIsNotNone(
            download_stream.pid, "SwiftDownloadStream process/cmd is supposed "
            "to run.")
Beispiel #10
0
    def test_run_verify_checksum_mismatch(self):
        """This tests that SwiftDownloadIntegrityError is raised and swift
        download cmd does not run when original backup checksum does not match
        swift object etag"""

        context = TroveContext()
        location = "/backup/location/123"
        is_zipped = False
        backup_checksum = "checksum_different_then_fake_swift_etag"

        swift_client = fake_create_swift_client()
        when(swift).create_swift_client(context).thenReturn(swift_client)

        storage_strategy = SwiftStorage(context)
        download_stream = storage_strategy.load(context, location, is_zipped,
                                                backup_checksum)

        self.assertEqual(download_stream.container, "location")
        self.assertEqual(download_stream.filename, "123")

        self.assertRaises(SwiftDownloadIntegrityError,
                          download_stream.__enter__)

        self.assertEqual(
            download_stream.process, None,
            "SwiftDownloadStream process/cmd was not supposed"
            "to run.")
Beispiel #11
0
    def test_swift_checksum_save(self):
        """This tests that SwiftStorage.save returns the swift checksum."""
        context = TroveContext()
        backup_id = '123'
        user = '******'
        password = '******'

        swift_client = FakeSwiftConnection()
        with patch.object(swift, 'create_swift_client',
                          return_value=swift_client):
            storage_strategy = SwiftStorage(context)

            with MockBackupRunner(filename=backup_id,
                                  user=user,
                                  password=password) as runner:
                (success,
                 note,
                 checksum,
                 location) = storage_strategy.save(runner.manifest, runner)

        self.assertTrue(success, "The backup should have been successful.")
        self.assertIsNotNone(note, "A note should have been returned.")
        self.assertEqual('http://mockswift/v1/database_backups/123.gz.enc',
                         location,
                         "Incorrect swift location was returned.")
Beispiel #12
0
    def test_swift_checksum_etag_mismatch(self):
        """This tests that when etag doesn't match swift checksum False is
            returned and None for checksum and location
        """
        context = TroveContext()
        # this backup_id will trigger fake swift client with calculate_etag
        # enabled to spit out a bad etag when a segment object is uploaded
        backup_id = 'bad_manifest_etag_123'
        user = '******'
        password = '******'

        swift_client = FakeSwiftConnection()
        with patch.object(swift, 'create_swift_client',
                          return_value=swift_client):
            storage_strategy = SwiftStorage(context)

            with MockBackupRunner(filename=backup_id,
                                  user=user,
                                  password=password) as runner:
                (success,
                 note,
                 checksum,
                 location) = storage_strategy.save(runner.manifest, runner)

        self.assertFalse(success, "The backup should have failed!")
        self.assertTrue(note.startswith("Error saving data to Swift!"))
        self.assertIsNone(checksum,
                          "Swift checksum should be None for failed backup.")
        self.assertEqual('http://mockswift/v1/database_backups/'
                         'bad_manifest_etag_123.gz.enc',
                         location,
                         "Incorrect swift location was returned.")
Beispiel #13
0
    def test_run_verify_checksum_mismatch(self):
        """This tests that SwiftDownloadIntegrityError is raised and swift
        download cmd does not run when original backup checksum does not match
        swift object etag"""

        context = TroveContext()
        location = "/backup/location/123"
        is_zipped = False
        backup_checksum = "checksum_different_then_fake_swift_etag"

        swift_client = FakeSwiftConnection()
        when(swift).create_swift_client(context).thenReturn(swift_client)

        storage_strategy = SwiftStorage(context)
        download_stream = storage_strategy.load(context,
                                                location,
                                                is_zipped,
                                                backup_checksum)

        self.assertEqual(download_stream.container, "location")
        self.assertEqual(download_stream.filename, "123")

        self.assertRaises(SwiftDownloadIntegrityError,
                          download_stream.__enter__)

        self.assertEqual(download_stream.process, None,
                         "SwiftDownloadStream process/cmd was not supposed"
                         "to run.")
Beispiel #14
0
    def test_swift_checksum_save(self):
        """This tests that SwiftStorage.save returns the swift checksum"""
        context = TroveContext()
        backup_id = '123'
        user = '******'
        password = '******'
        backup_container = 'database_backups'

        swift_client = FakeSwiftConnectionWithRealEtag()
        when(swift).create_swift_client(context).thenReturn(swift_client)
        storage_strategy = SwiftStorage(context)

        with MockBackupRunner(filename=backup_id,
                              user=user,
                              password=password) as runner:
            (success,
             note,
             checksum,
             location) = storage_strategy.save(backup_container, runner)

        self.assertEqual(success, True,
                         "The backup should have been successful.")
        self.assertIsNotNone(note, "A note should have been returned.")
        self.assertEqual(location, 'http://mockswift/v1/database_backups/123',
                         "Incorrect swift location was returned.")
Beispiel #15
0
class SwiftStorageUtils(trove_testtools.TestCase):

    def setUp(self):
        super(SwiftStorageUtils, self).setUp()
        self.context = TroveContext()
        self.swift_client = FakeSwiftConnection()
        self.create_swift_client_patch = patch.object(
            swift, 'create_swift_client',
            MagicMock(return_value=self.swift_client))
        self.create_swift_client_mock = self.create_swift_client_patch.start()
        self.addCleanup(self.create_swift_client_patch.stop)
        self.swift = SwiftStorage(self.context)

    def tearDown(self):
        super(SwiftStorageUtils, self).tearDown()

    def test_explode_location(self):
        location = 'http://mockswift.com/v1/545433/backups/mybackup.tar'
        url, container, filename = self.swift._explodeLocation(location)
        self.assertEqual('http://mockswift.com/v1/545433', url)
        self.assertEqual('backups', container)
        self.assertEqual('mybackup.tar', filename)

    def test_validate_checksum_good(self):
        match = self.swift._verify_checksum('"my-good-etag"', 'my-good-etag')
        self.assertTrue(match)

    def test_verify_checksum_bad(self):
        self.assertRaises(SwiftDownloadIntegrityError,
                          self.swift._verify_checksum,
                          '"THE-GOOD-THE-BAD"',
                          'AND-THE-UGLY')
Beispiel #16
0
    def test_swift_checksum_etag_mismatch(self):
        """This tests that when etag doesn't match swift checksum False is
            returned and None for checksum and location
        """
        context = TroveContext()
        # this backup_id will trigger fake swift client with calculate_etag
        # enabled to spit out a bad etag when a segment object is uploaded
        backup_id = 'bad_manifest_etag_123'
        user = '******'
        password = '******'

        swift_client = FakeSwiftConnection()
        with patch.object(swift,
                          'create_swift_client',
                          return_value=swift_client):
            storage_strategy = SwiftStorage(context)

            with MockBackupRunner(filename=backup_id,
                                  user=user,
                                  password=password) as runner:
                (success, note, checksum,
                 location) = storage_strategy.save(runner.manifest, runner)

        self.assertFalse(success, "The backup should have failed!")
        self.assertTrue(note.startswith("Error saving data to Swift!"))
        self.assertIsNone(checksum,
                          "Swift checksum should be None for failed backup.")
        self.assertEqual(
            'http://mockswift/v1/database_backups/'
            'bad_manifest_etag_123.gz.enc', location,
            "Incorrect swift location was returned.")
Beispiel #17
0
class SwiftStorageUtils(trove_testtools.TestCase):
    def setUp(self):
        super(SwiftStorageUtils, self).setUp()
        self.context = TroveContext()
        self.swift_client = FakeSwiftConnection()
        self.create_swift_client_patch = patch.object(
            swift, 'create_swift_client',
            MagicMock(return_value=self.swift_client))
        self.create_swift_client_mock = self.create_swift_client_patch.start()
        self.addCleanup(self.create_swift_client_patch.stop)
        self.swift = SwiftStorage(self.context)

    def tearDown(self):
        super(SwiftStorageUtils, self).tearDown()

    def test_explode_location(self):
        location = 'http://mockswift.com/v1/545433/backups/mybackup.tar'
        url, container, filename = self.swift._explodeLocation(location)
        self.assertEqual('http://mockswift.com/v1/545433', url)
        self.assertEqual('backups', container)
        self.assertEqual('mybackup.tar', filename)

    def test_validate_checksum_good(self):
        match = self.swift._verify_checksum('"my-good-etag"', 'my-good-etag')
        self.assertTrue(match)

    @patch('trove.guestagent.strategies.storage.swift.LOG')
    def test_verify_checksum_bad(self, mock_logging):
        self.assertRaises(SwiftDownloadIntegrityError,
                          self.swift._verify_checksum, '"THE-GOOD-THE-BAD"',
                          'AND-THE-UGLY')
Beispiel #18
0
 def setUp(self):
     super(SwiftMetadataTests, self).setUp()
     self.swift_client = FakeSwiftConnection()
     self.context = TroveContext()
     self.create_swift_client_patch = patch.object(
         swift, 'create_swift_client',
         MagicMock(return_value=self.swift_client))
     self.create_swift_client_mock = self.create_swift_client_patch.start()
     self.addCleanup(self.create_swift_client_patch.stop)
     self.swift = SwiftStorage(self.context)
Beispiel #19
0
    def test_run_verify_checksum(self):
        """This tests that swift download cmd runs if original backup checksum
            matches swift object etag
        """

        context = TroveContext()
        location = "/backup/location/123"
        backup_checksum = "fake-md5-sum"

        swift_client = FakeSwiftConnection()
        when(swift).create_swift_client(context).thenReturn(swift_client)

        storage_strategy = SwiftStorage(context)
        download_stream = storage_strategy.load(location, backup_checksum)
        self.assertIsNotNone(download_stream)
Beispiel #20
0
class SwiftMetadataTests(trove_testtools.TestCase):

    def setUp(self):
        super(SwiftMetadataTests, self).setUp()
        self.swift_client = FakeSwiftConnection()
        self.context = TroveContext()
        self.create_swift_client_patch = patch.object(
            swift, 'create_swift_client',
            MagicMock(return_value=self.swift_client))
        self.create_swift_client_mock = self.create_swift_client_patch.start()
        self.addCleanup(self.create_swift_client_patch.stop)
        self.swift = SwiftStorage(self.context)

    def tearDown(self):
        super(SwiftMetadataTests, self).tearDown()

    def test__get_attr(self):
        normal_header = self.swift._get_attr('content-type')
        self.assertEqual('content_type', normal_header)
        meta_header = self.swift._get_attr('x-object-meta-foo')
        self.assertEqual('foo', meta_header)
        meta_header_two = self.swift._get_attr('x-object-meta-foo-bar')
        self.assertEqual('foo_bar', meta_header_two)

    def test__set_attr(self):
        meta_header = self.swift._set_attr('foo')
        self.assertEqual('X-Object-Meta-foo', meta_header)
        meta_header_two = self.swift._set_attr('foo_bar')
        self.assertEqual('X-Object-Meta-foo-bar', meta_header_two)

    def test_load_metadata(self):
        location = 'http://mockswift.com/v1/545433/backups/mybackup.tar'
        headers = {
            'etag': '"fake-md5-sum"',
            'x-object-meta-lsn': '1234567'
        }
        with patch.object(self.swift_client, 'head_object',
                          return_value=headers):
            metadata = self.swift.load_metadata(location, 'fake-md5-sum')
        self.assertEqual({'lsn': '1234567'}, metadata)

    def test_save_metadata(self):
        location = 'http://mockswift.com/v1/545433/backups/mybackup.tar'
        metadata = {'lsn': '1234567'}
        self.swift_client.post_object = Mock()

        self.swift.save_metadata(location, metadata=metadata)

        headers = {
            'X-Object-Meta-lsn': '1234567',
            'X-Object-Manifest': None
        }
        self.swift_client.post_object.assert_called_with(
            'backups', 'mybackup.tar', headers=headers)
Beispiel #21
0
 def setUp(self):
     super(SwiftMetadataTests, self).setUp()
     self.swift_client = FakeSwiftConnection()
     self.context = TroveContext()
     self.create_swift_client_patch = patch.object(
         swift, 'create_swift_client',
         MagicMock(return_value=self.swift_client))
     self.create_swift_client_mock = self.create_swift_client_patch.start()
     self.addCleanup(self.create_swift_client_patch.stop)
     self.swift = SwiftStorage(self.context)
Beispiel #22
0
class SwiftMetadataTests(testtools.TestCase):

    def setUp(self):
        super(SwiftMetadataTests, self).setUp()
        self.swift_client = FakeSwiftConnection()
        self.context = TroveContext()
        when(swift).create_swift_client(self.context).thenReturn(
            self.swift_client)
        self.swift = SwiftStorage(self.context)

    def tearDown(self):
        super(SwiftMetadataTests, self).tearDown()
        unstub()

    def test__get_attr(self):
        normal_header = self.swift._get_attr('content-type')
        self.assertEqual('content_type', normal_header)
        meta_header = self.swift._get_attr('x-object-meta-foo')
        self.assertEqual('foo', meta_header)
        meta_header_two = self.swift._get_attr('x-object-meta-foo-bar')
        self.assertEqual('foo_bar', meta_header_two)

    def test__set_attr(self):
        meta_header = self.swift._set_attr('foo')
        self.assertEqual('X-Object-Meta-foo', meta_header)
        meta_header_two = self.swift._set_attr('foo_bar')
        self.assertEqual('X-Object-Meta-foo-bar', meta_header_two)

    def test_load_metadata(self):
        location = 'http://mockswift.com/v1/545433/backups/mybackup.tar'
        headers = {
            'etag': '"fake-md5-sum"',
            'x-object-meta-lsn': '1234567'
        }
        when(self.swift_client).head_object(any(), any()).thenReturn(
            headers)

        metadata = self.swift.load_metadata(location, 'fake-md5-sum')
        self.assertEqual({'lsn': '1234567'}, metadata)

    def test_save_metadata(self):
        location = 'http://mockswift.com/v1/545433/backups/mybackup.tar'
        metadata = {'lsn': '1234567'}
        self.swift_client.post_object = Mock()

        self.swift.save_metadata(location, metadata=metadata)

        headers = {
            'X-Object-Meta-lsn': '1234567',
            'X-Object-Manifest': None
        }
        self.swift_client.post_object.assert_called_with(
            'backups', 'mybackup.tar', headers=headers)
Beispiel #23
0
    def test_run_verify_checksum_mismatch(self):
        """This tests that SwiftDownloadIntegrityError is raised and swift
            download cmd does not run when original backup checksum
            does not match swift object etag
        """

        context = TroveContext()
        location = "/backup/location/123"
        backup_checksum = "checksum_different_then_fake_swift_etag"

        swift_client = FakeSwiftConnection()
        with patch.object(swift,
                          'create_swift_client',
                          return_value=swift_client):
            storage_strategy = SwiftStorage(context)

        self.assertRaises(SwiftDownloadIntegrityError, storage_strategy.load,
                          location, backup_checksum)
Beispiel #24
0
 def setUp(self):
     super(SwiftMetadataTests, self).setUp()
     self.swift_client = FakeSwiftConnection()
     self.context = TroveContext()
     swift.create_swift_client = MagicMock(return_value=self.swift_client)
     self.swift = SwiftStorage(self.context)
Beispiel #25
0
 def setUp(self):
     super(SwiftMetadataTests, self).setUp()
     self.swift_client = FakeSwiftConnection()
     self.context = TroveContext()
     swift.create_swift_client = MagicMock(return_value=self.swift_client)
     self.swift = SwiftStorage(self.context)