示例#1
0
 def setUp(self):
     """Set up test."""
     yield super(TestDelivery, self).setUp()
     self.mocker = Mocker()
     self.fake_reactor = DummyReactor()
     self.content = self.mocker.mock()
     self.node_owner_id = 1
     self.node_uuid = uuid.uuid4()
     self.node_hash = "hash:blah"
     self.owner_id = 0
     self.free_bytes = 0
     self.node_shard_id = 'shard1'
     self.node_volume_id = uuid.uuid4()
     self.content_node = self.mocker.mock()
     content_class = lambda _: self.content
     MetricsConnector.register_metrics("sli", instance=ExtendedMetrics())
     MetricsConnector.register_metrics("root", instance=ExtendedMetrics())
     MetricsConnector.register_metrics("user", instance=ExtendedMetrics())
     self.factory = StorageServerFactory(s3_host=None,
                                         s3_port=None,
                                         s3_key=None,
                                         s3_ssl=False,
                                         s3_secret=None,
                                         content_class=content_class,
                                         reactor=self.fake_reactor)
示例#2
0
    def setUp(self):
        yield super(NotificationErrorsTestCase, self).setUp()

        self.notifier = FakeNotifier()
        self.patch(notifier, 'get_notifier', lambda: self.notifier)
        self.fake_reactor = DummyReactor()
        self.oops_deferred = defer.Deferred()

        def publish(report):
            self.oops_deferred.callback(report)

        oops_config = configure_oops()
        # Should probably be an option to configure_oops?
        oops_timeline.install_hooks(oops_config)
        oops_config.publishers = [publish]

        self.ssfactory = StorageServerFactory(s3_host=None,
                                              s3_port=None,
                                              s3_key=None,
                                              s3_ssl=False,
                                              s3_secret=None,
                                              oops_config=oops_config,
                                              reactor=self.fake_reactor)

        protocol = StorageServer()
        protocol.factory = self.ssfactory
        protocol.working_caps = ["volumes", "generations"]
        protocol.logger = logging.getLogger("storage.server")
        protocol.session_id = uuid.uuid4()
        self.patch(self.ssfactory.content, 'get_user_by_id',
                   lambda *a: Failure(self.induced_error("Test error")))
 def setUp(self):
     """Set up test."""
     yield super(TestDelivery, self).setUp()
     self.mocker = Mocker()
     self.fake_reactor = DummyReactor()
     self.content = self.mocker.mock()
     self.node_owner_id = 1
     self.node_uuid = uuid.uuid4()
     self.node_hash = "hash:blah"
     self.owner_id = 0
     self.free_bytes = 0
     self.node_volume_id = uuid.uuid4()
     self.content_node = self.mocker.mock()
     content_class = lambda _: self.content
     MetricsConnector.register_metrics("sli", instance=ExtendedMetrics())
     MetricsConnector.register_metrics("root", instance=ExtendedMetrics())
     MetricsConnector.register_metrics("user", instance=ExtendedMetrics())
     self.factory = StorageServerFactory(
         s3_host=None, s3_port=None, s3_key=None, s3_ssl=False,
         s3_secret=None, content_class=content_class,
         reactor=self.fake_reactor)
class TestDelivery(TwistedTestCase):
    """More delivery testing."""
    @inlineCallbacks
    def setUp(self):
        """Set up test."""
        yield super(TestDelivery, self).setUp()
        self.mocker = Mocker()
        self.fake_reactor = DummyReactor()
        self.content = self.mocker.mock()
        self.node_owner_id = 1
        self.node_uuid = uuid.uuid4()
        self.node_hash = "hash:blah"
        self.owner_id = 0
        self.free_bytes = 0
        self.node_volume_id = uuid.uuid4()
        self.content_node = self.mocker.mock()
        content_class = lambda _: self.content
        MetricsConnector.register_metrics("sli", instance=ExtendedMetrics())
        MetricsConnector.register_metrics("root", instance=ExtendedMetrics())
        MetricsConnector.register_metrics("user", instance=ExtendedMetrics())
        self.factory = StorageServerFactory(
            s3_host=None, s3_port=None, s3_key=None, s3_ssl=False,
            s3_secret=None, content_class=content_class,
            reactor=self.fake_reactor)

    @inlineCallbacks
    def tearDown(self):
        """Tear down test."""
        MetricsConnector.unregister_metrics()
        try:
            self.mocker.verify()
        finally:
            yield super(TestDelivery, self).tearDown()
            self.mocker.restore()

    @inlineCallbacks
    def test_new_volume_generation_ok(self):
        """Test new volume generation delivery ok."""
        user = self.mocker.mock()
        expect(self.content.get_user_by_id('user_id')
               ).count(1).result(succeed(user))
        expect(user.broadcast).count(1).result(lambda *a, **kw: None)

        # test
        self.mocker.replay()
        notif = VolumeNewGeneration('user_id', 'vol_id', 23)
        yield self.factory.deliver_volume_new_generation(notif)

    @inlineCallbacks
    def test_new_volume_generation_not_connected(self):
        """Test new volume generation delivery for a not connected user."""
        expect(self.content.get_user_by_id('user_id')
               ).count(1).result(succeed(None))

        # test
        self.mocker.replay()
        notif = VolumeNewGeneration('user_id', 'vol_id', 23)
        yield self.factory.deliver_volume_new_generation(notif)

    @inlineCallbacks
    def test_new_volume_generation_broadcasting_message(self):
        """Test new volume generation delivery with correct message."""
        deferred = defer.Deferred()
        protocol = self.mocker.mock()

        def test(resp, filter):
            """Check that the broadcast message info is ok."""
            self.assertEqual(resp.type,
                             protocol_pb2.Message.VOLUME_NEW_GENERATION)
            self.assertEqual(resp.volume_new_generation.volume, 'vol_id')
            self.assertEqual(resp.volume_new_generation.generation, 66)

            # other session, and generations
            self.mocker.reset()
            expect(protocol.session_id).count(0, 1).result(uuid.uuid4())
            expect(protocol.working_caps).count(0, 1).result(['generations'])
            self.mocker.replay()
            self.assertTrue(filter(protocol))

            # same session, and generations
            self.mocker.reset()
            expect(protocol.session_id).count(0, 1).result(session_id)
            expect(protocol.working_caps).count(0, 1).result(['generations'])
            self.mocker.replay()
            self.assertFalse(filter(protocol))

            deferred.callback(None)

        user = self.mocker.mock()
        expect(self.content.get_user_by_id('user_id')
               ).count(1).result(succeed(user))
        expect(user.broadcast).result(test)

        # test
        self.mocker.replay()
        session_id = uuid.uuid4()
        notif = VolumeNewGeneration('user_id', 'vol_id', 66, session_id)
        yield self.factory.deliver_volume_new_generation(notif)
        yield deferred

    @inlineCallbacks
    def test_share_accepted_broadcasting_message(self):
        """Test that ShareAccepted gets broadcast to both users properly."""
        deferred_from = defer.Deferred()
        deferred_to = defer.Deferred()
        share_id = uuid.uuid4()
        from_user = 1
        to_user = 2
        root_id = uuid.uuid4()

        def test_from(resp, filter):
            """Check that the broadcast message info is ok."""
            self.assertEqual(resp.type,
                             protocol_pb2.Message.SHARE_ACCEPTED)
            self.assertEqual(resp.share_accepted.share_id, str(share_id))
            self.assertEqual(resp.share_accepted.answer,
                             protocol_pb2.ShareAccepted.YES)
            deferred_from.callback(None)

        def test_to(resp, filter):
            """Check that the broadcast message info is ok."""
            self.assertEqual(resp.type,
                             protocol_pb2.Message.VOLUME_CREATED)
            self.assertEqual(resp.volume_created.share.share_id, str(share_id))
            self.assertEqual(resp.volume_created.share.subtree, str(root_id))
            self.assertEqual(resp.volume_created.share.direction,
                             protocol_pb2.Shares.TO_ME)
            deferred_to.callback(None)

        user = self.mocker.mock()
        user2 = self.mocker.mock()

        for i in range(2):
            expect(
                self.content.get_user_by_id(from_user)).result(succeed(user))
            expect(
                self.content.get_user_by_id(to_user)).result(succeed(user2))

        expect(user.id).count(2).result(1)
        expect(user.broadcast).count(1).result(test_from)
        expect(user.username).count(1).result(u"username")
        expect(user.visible_name).count(1).result(u"username")
        expect(user2.id).count(2).result(2)
        expect(user2.broadcast).count(1).result(test_to)

        # test
        self.mocker.replay()
        notif_to = ShareAccepted(share_id, u"name", root_id, from_user,
                                 to_user, Share.VIEW, True)
        notif_from = ShareAccepted(share_id, u"name", root_id, from_user,
                                   to_user, Share.VIEW, True)
        yield self.factory.deliver_share_accepted(notif_to,
                                                  recipient_id=to_user)
        yield self.factory.deliver_share_accepted(notif_from,
                                                  recipient_id=from_user)
        yield deferred_from
        yield deferred_to

    @inlineCallbacks
    def test_share_accepted_broadcasting_no_from(self):
        """Test ShareAccepted when the from user isn't present."""
        deferred_to = defer.Deferred()
        share_id = uuid.uuid4()
        to_user = 1
        from_user = 2
        root_id = uuid.uuid4()

        def test_to(resp, filter):
            """Check that the broadcast message info is ok."""
            self.assertEqual(resp.type,
                             protocol_pb2.Message.VOLUME_CREATED)
            self.assertEqual(resp.volume_created.share.share_id, str(share_id))
            self.assertEqual(resp.volume_created.share.subtree, str(root_id))
            self.assertEqual(resp.volume_created.share.direction,
                             protocol_pb2.Shares.TO_ME)
            deferred_to.callback(None)

        user = self.mocker.mock()
        user2 = self.mocker.mock()
        for i in range(2):
            expect(self.content.get_user_by_id(from_user)
                   ).result(succeed(None))
            expect(self.content.get_user_by_id(to_user)).result(succeed(user2))
        expect(self.content.get_user_by_id(from_user, required=True)
               ).result(succeed(user))
        expect(user.username).count(1).result(u"username")
        expect(user.visible_name).count(1).result(u"username")
        expect(user2.id).count(2).result(2)
        expect(user2.broadcast).count(1).result(test_to)
        # test
        self.mocker.replay()
        notif = ShareAccepted(share_id, u"name", root_id, from_user, to_user,
                              Share.VIEW, True)
        notif2 = ShareAccepted(share_id, u"name", root_id, from_user, to_user,
                               Share.VIEW, True)
        yield self.factory.deliver_share_accepted(notif,
                                                  recipient_id=from_user)
        yield self.factory.deliver_share_accepted(notif2, recipient_id=to_user)
        yield deferred_to
示例#5
0
class TestDelivery(TwistedTestCase):
    """More delivery testing."""
    @inlineCallbacks
    def setUp(self):
        """Set up test."""
        yield super(TestDelivery, self).setUp()
        self.mocker = Mocker()
        self.fake_reactor = DummyReactor()
        self.content = self.mocker.mock()
        self.node_owner_id = 1
        self.node_uuid = uuid.uuid4()
        self.node_hash = "hash:blah"
        self.owner_id = 0
        self.free_bytes = 0
        self.node_shard_id = 'shard1'
        self.node_volume_id = uuid.uuid4()
        self.content_node = self.mocker.mock()
        content_class = lambda _: self.content
        MetricsConnector.register_metrics("sli", instance=ExtendedMetrics())
        MetricsConnector.register_metrics("root", instance=ExtendedMetrics())
        MetricsConnector.register_metrics("user", instance=ExtendedMetrics())
        self.factory = StorageServerFactory(s3_host=None,
                                            s3_port=None,
                                            s3_key=None,
                                            s3_ssl=False,
                                            s3_secret=None,
                                            content_class=content_class,
                                            reactor=self.fake_reactor)

    @inlineCallbacks
    def tearDown(self):
        """Tear down test."""
        MetricsConnector.unregister_metrics()
        try:
            self.mocker.verify()
        finally:
            yield super(TestDelivery, self).tearDown()
            self.mocker.restore()

    @inlineCallbacks
    def test_new_volume_generation_ok(self):
        """Test new volume generation delivery ok."""
        user = self.mocker.mock()
        expect(self.content.get_user_by_id('user_id')).count(1).result(
            succeed(user))
        expect(user.broadcast).count(1).result(lambda *a, **kw: None)

        # test
        self.mocker.replay()
        notif = VolumeNewGeneration('user_id', 'vol_id', 23)
        yield self.factory.deliver_volume_new_generation(notif)

    @inlineCallbacks
    def test_new_volume_generation_not_connected(self):
        """Test new volume generation delivery for a not connected user."""
        expect(self.content.get_user_by_id('user_id')).count(1).result(
            succeed(None))

        # test
        self.mocker.replay()
        notif = VolumeNewGeneration('user_id', 'vol_id', 23)
        yield self.factory.deliver_volume_new_generation(notif)

    @inlineCallbacks
    def test_new_volume_generation_broadcasting_message(self):
        """Test new volume generation delivery with correct message."""
        deferred = defer.Deferred()
        protocol = self.mocker.mock()

        def test(resp, filter):
            """Check that the broadcast message info is ok."""
            self.assertEqual(resp.type,
                             protocol_pb2.Message.VOLUME_NEW_GENERATION)
            self.assertEqual(resp.volume_new_generation.volume, 'vol_id')
            self.assertEqual(resp.volume_new_generation.generation, 66)

            # other session, and generations
            self.mocker.reset()
            expect(protocol.session_id).count(0, 1).result(uuid.uuid4())
            expect(protocol.working_caps).count(0, 1).result(['generations'])
            self.mocker.replay()
            self.assertTrue(filter(protocol))

            # same session, and generations
            self.mocker.reset()
            expect(protocol.session_id).count(0, 1).result(session_id)
            expect(protocol.working_caps).count(0, 1).result(['generations'])
            self.mocker.replay()
            self.assertFalse(filter(protocol))

            deferred.callback(None)

        user = self.mocker.mock()
        expect(self.content.get_user_by_id('user_id')).count(1).result(
            succeed(user))
        expect(user.broadcast).result(test)

        # test
        self.mocker.replay()
        session_id = uuid.uuid4()
        notif = VolumeNewGeneration('user_id', 'vol_id', 66, session_id)
        yield self.factory.deliver_volume_new_generation(notif)
        yield deferred

    @inlineCallbacks
    def test_share_accepted_broadcasting_message(self):
        """Test that ShareAccepted gets broadcast to both users properly."""
        deferred_from = defer.Deferred()
        deferred_to = defer.Deferred()
        share_id = uuid.uuid4()
        from_user = 1
        to_user = 2
        root_id = uuid.uuid4()

        def test_from(resp, filter):
            """Check that the broadcast message info is ok."""
            self.assertEqual(resp.type, protocol_pb2.Message.SHARE_ACCEPTED)
            self.assertEqual(resp.share_accepted.share_id, str(share_id))
            self.assertEqual(resp.share_accepted.answer,
                             protocol_pb2.ShareAccepted.YES)
            deferred_from.callback(None)

        def test_to(resp, filter):
            """Check that the broadcast message info is ok."""
            self.assertEqual(resp.type, protocol_pb2.Message.VOLUME_CREATED)
            self.assertEqual(resp.volume_created.share.share_id, str(share_id))
            self.assertEqual(resp.volume_created.share.subtree, str(root_id))
            self.assertEqual(resp.volume_created.share.direction,
                             protocol_pb2.Shares.TO_ME)
            deferred_to.callback(None)

        user = self.mocker.mock()
        user2 = self.mocker.mock()

        for i in range(2):
            expect(self.content.get_user_by_id(from_user)).result(
                succeed(user))
            expect(self.content.get_user_by_id(to_user)).result(succeed(user2))

        expect(user.id).count(2).result(1)
        expect(user.broadcast).count(1).result(test_from)
        expect(user.username).count(1).result(u"username")
        expect(user.visible_name).count(1).result(u"username")
        expect(user2.id).count(2).result(2)
        expect(user2.broadcast).count(1).result(test_to)

        # test
        self.mocker.replay()
        notif_to = ShareAccepted(share_id, u"name", root_id, from_user,
                                 to_user, "View", True)
        notif_from = ShareAccepted(share_id, u"name", root_id, from_user,
                                   to_user, "View", True)
        yield self.factory.deliver_share_accepted(notif_to,
                                                  recipient_id=to_user)
        yield self.factory.deliver_share_accepted(notif_from,
                                                  recipient_id=from_user)
        yield deferred_from
        yield deferred_to

    @inlineCallbacks
    def test_share_accepted_broadcasting_no_from(self):
        """Test ShareAccepted when the from user isn't present."""
        deferred_to = defer.Deferred()
        share_id = uuid.uuid4()
        to_user = 1
        from_user = 2
        root_id = uuid.uuid4()

        def test_to(resp, filter):
            """Check that the broadcast message info is ok."""
            self.assertEqual(resp.type, protocol_pb2.Message.VOLUME_CREATED)
            self.assertEqual(resp.volume_created.share.share_id, str(share_id))
            self.assertEqual(resp.volume_created.share.subtree, str(root_id))
            self.assertEqual(resp.volume_created.share.direction,
                             protocol_pb2.Shares.TO_ME)
            deferred_to.callback(None)

        user = self.mocker.mock()
        user2 = self.mocker.mock()
        for i in range(2):
            expect(self.content.get_user_by_id(from_user)).result(
                succeed(None))
            expect(self.content.get_user_by_id(to_user)).result(succeed(user2))
        expect(self.content.get_user_by_id(from_user, required=True)).result(
            succeed(user))
        expect(user.username).count(1).result(u"username")
        expect(user.visible_name).count(1).result(u"username")
        expect(user2.id).count(2).result(2)
        expect(user2.broadcast).count(1).result(test_to)
        # test
        self.mocker.replay()
        notif = ShareAccepted(share_id, u"name", root_id, from_user, to_user,
                              "View", True)
        notif2 = ShareAccepted(share_id, u"name", root_id, from_user, to_user,
                               "View", True)
        yield self.factory.deliver_share_accepted(notif,
                                                  recipient_id=from_user)
        yield self.factory.deliver_share_accepted(notif2, recipient_id=to_user)
        yield deferred_to