Пример #1
0
    def test_replication(self):
        # First, create a few records on the first kinto collection.
        with self.client.batch(bucket='origin', collection='coll') as batch:
            batch.create_bucket()
            batch.create_collection()

            for n in range(10):
                batch.create_record(data={'foo': 'bar', 'n': n})

        origin = Client(
            server_url=self.server_url,
            auth=self.auth,
            bucket='origin',
            collection='coll'
        )
        destination = Client(
            server_url=self.server_url,
            auth=self.auth,
            bucket='destination',
            collection='coll')

        replication.replicate(origin, destination)
        records = self.client.get_records(bucket='destination',
                                          collection='coll')
        assert len(records) == 10
Пример #2
0
    def test_logger_outputs_replication_information(self, logger):
        origin_session = mock.MagicMock()
        origin_session.server_url = "http://origin/v1"
        destination_session = mock.MagicMock()
        destination_session.server_url = "http://destination/v1"
        mock_response(origin_session)
        mock_response(destination_session)

        origin = Client(
            session=origin_session,
            bucket="buck",
            collection="coll"
        )
        destination = Client(
            session=destination_session,
            bucket="buck",
            collection="coll"
        )
        destination._server_settings = {'batch_max_requests': 15}
        replicate(origin, destination)
        msg = ("Replication from <KintoClient http://origin/v1/buckets/buck/"
               "collections/coll> to <KintoClient http://destination/v1/"
               "buckets/buck/collections/coll>")
        logger.info.assert_any_call(msg)
        logger.info.assert_any_call("replication of 0 records")
Пример #3
0
    def test_logger_outputs_replication_information(self, logger):
        origin_session = mock.MagicMock()
        origin_session.server_url = "http://origin/v1"
        destination_session = mock.MagicMock()
        destination_session.server_url = "http://destination/v1"
        mock_response(origin_session)
        mock_response(destination_session)

        origin = Client(
            session=origin_session,
            bucket="buck",
            collection="coll"
        )
        destination = Client(
            session=destination_session,
            bucket="buck",
            collection="coll"
        )
        destination._server_settings = {'batch_max_requests': 15}
        replicate(origin, destination)
        msg = ("Replication from <KintoClient http://origin/v1/buckets/buck/"
               "collections/coll> to <KintoClient http://destination/v1/"
               "buckets/buck/collections/coll>")
        logger.info.assert_any_call(msg)
        logger.info.assert_any_call("replication of 0 records")
Пример #4
0
    def test_update(self):
        # Create ten records
        collection = self.get_timestamp()
        self.master.create_collection(collection, bucket=self.bucket)
        self.read_only.create_collection(collection, bucket=self.bucket)

        # Insert them into master
        for x in range(10):
            record = self.master.create_record(data=self.generate_record(),
                                               collection=collection,
                                               bucket=self.bucket)

        # Replicate them over to the read-only instance
        origin = dict(server_url=self.master_url,
                      auth=self.credentials,
                      bucket=self.bucket,
                      collection=collection)

        destination = dict(server_url=self.read_only_url,
                           auth=self.credentials,
                           bucket=self.bucket,
                           collection=collection)

        replication.replicate(origin, destination)
        records = self.read_only.get_records(bucket=self.bucket,
                                             collection=collection)
        self.assertEquals(10, len(records))

        # Change one record and update master
        records = self.master.get_records(bucket=self.bucket,
                                          collection=collection)
        record = records[1]
        record['title'] = 'Updated record'
        updated_id = record['id']
        self.master.update_record(record,
                                  bucket=self.bucket,
                                  collection=collection)
        replication.replicate(origin, destination)

        # Verify that the matched record was changed
        record = self.read_only.get_record(updated_id,
                                           bucket=self.bucket,
                                           collection=collection)
        self.assertEquals(updated_id, record['data']['id'])

        # Verify that we have ten records in each system
        self.assertEquals(
            len(
                self.master.get_records(bucket=self.bucket,
                                        collection=collection)),
            len(
                self.read_only.get_records(bucket=self.bucket,
                                           collection=collection)))

        self.master.delete_collection(collection, bucket=self.bucket)
        self.read_only.delete_collection(collection, bucket=self.bucket)
Пример #5
0
    def test_removed_records_are_deleted_on_the_destination(self):
        self.origin.get_records.return_value = [
            {'id': '1234', 'deleted': True, 'last_modified': '1234'},
            {'id': '4567', 'deleted': True, 'last_modified': '4567'}
        ]
        batch = mock.MagicMock()
        batched = batch().__enter__()
        self.destination.batch = batch

        replicate(self.origin, self.destination)
        batched.delete_record.assert_any_call('1234', last_modified='1234')
        batched.delete_record.assert_any_call('4567', last_modified='4567')
Пример #6
0
 def test_destination_collection_is_created_if_not_exist(self):
     self.destination.get_collection.side_effect = exceptions.KintoException
     self.origin.get_collection.return_value = {
         'data': mock.sentinel.data,
         'permissions': mock.sentinel.permissions
     }
     replicate(self.origin, self.destination)
     self.destination.create_collection.assert_called_with(
         data=mock.sentinel.data,
         permissions=mock.sentinel.permissions,
         safe=False
     )
Пример #7
0
    def test_removed_records_are_deleted_on_the_destination(self):
        self.origin.get_records.return_value = [
            {'id': '1234', 'deleted': True, 'last_modified': '1234'},
            {'id': '4567', 'deleted': True, 'last_modified': '4567'}
        ]
        batch = mock.MagicMock()
        batched = batch().__enter__()
        self.destination.batch = batch

        replicate(self.origin, self.destination)
        batched.delete_record.assert_any_call('1234', last_modified='1234')
        batched.delete_record.assert_any_call('4567', last_modified='4567')
Пример #8
0
 def test_destination_collection_is_created_if_not_exist(self):
     self.destination.get_collection.side_effect = exceptions.KintoException
     self.origin.get_collection.return_value = {
         'data': mock.sentinel.data,
         'permissions': mock.sentinel.permissions
     }
     replicate(self.origin, self.destination)
     self.destination.create_collection.assert_called_with(
         data=mock.sentinel.data,
         permissions=mock.sentinel.permissions,
         if_not_exists=True
     )
Пример #9
0
    def test_replication(self):
        # First, create a few records on the first kinto collection.
        with self.client.batch(bucket="origin", collection="coll") as batch:
            batch.create_bucket()
            batch.create_collection()

            for n in range(10):
                batch.create_record(data={"foo": "bar", "n": n})

        origin = Client(server_url=self.server_url, auth=self.auth, bucket="origin", collection="coll")
        destination = Client(server_url=self.server_url, auth=self.auth, bucket="destination", collection="coll")

        replication.replicate(origin, destination)
        records = self.client.get_records(bucket="destination", collection="coll")
        assert len(records) == 10
Пример #10
0
    def test_new_records_are_sent_to_the_destination(self):
        self.origin.get_records.return_value = [
            {'id': '1234', 'foo': 'bar', 'last_modified': 1234},
            {'id': '4567', 'bar': 'baz', 'last_modified': 4567}
        ]
        batch = mock.MagicMock()
        batched = batch().__enter__()
        self.destination.batch = batch

        replicate(self.origin, self.destination)
        batched.update_record.assert_any_call(
            data={'id': '4567', 'bar': 'baz', 'last_modified': 4567},
            safe=False
        )
        batched.update_record.assert_any_call(
            data={'id': '1234', 'foo': 'bar', 'last_modified': 1234},
            safe=False
        )
Пример #11
0
    def test_new_records_are_sent_to_the_destination(self):
        self.origin.get_records.return_value = [
            {'id': '1234', 'foo': 'bar', 'last_modified': 1234},
            {'id': '4567', 'bar': 'baz', 'last_modified': 4567}
        ]
        batch = mock.MagicMock()
        batched = batch().__enter__()
        self.destination.batch = batch

        replicate(self.origin, self.destination)
        batched.update_record.assert_any_call(
            data={'id': '4567', 'bar': 'baz', 'last_modified': 4567},
            safe=False
        )
        batched.update_record.assert_any_call(
            data={'id': '1234', 'foo': 'bar', 'last_modified': 1234},
            safe=False
        )
Пример #12
0
    def test_replication(self):
        # First, create a few records on the first kinto collection.
        with self.client.batch(bucket='origin', collection='coll') as batch:
            batch.create_bucket()
            batch.create_collection()

            for n in range(10):
                batch.create_record(data={'foo': 'bar', 'n': n})

        origin = Client(server_url=self.server_url,
                        auth=self.auth,
                        bucket='origin',
                        collection='coll')
        destination = Client(server_url=self.server_url,
                             auth=self.auth,
                             bucket='destination',
                             collection='coll')

        replication.replicate(origin, destination)
        records = self.client.get_records(bucket='destination',
                                          collection='coll')
        assert len(records) == 10
Пример #13
0
 def test_destination_bucket_is_created_if_not_exist(self):
     self.destination.get_bucket.side_effect = exceptions.BucketNotFound
     replicate(self.origin, self.destination)
     self.destination.create_bucket.assert_called_with()
Пример #14
0
 def test_destination_bucket_is_created_if_not_exist(self):
     self.destination.get_bucket.side_effect = exceptions.BucketNotFound
     replicate(self.origin, self.destination)
     self.destination.create_bucket.assert_called_with(if_not_exists=True)