Exemple #1
0
 def setUp(self):
     """Prepare the test fixture."""
     self.__get_pool = iterators.get_pool
     self.clients = {"eggs": MockClient("eggs", ['127.0.0.1:9160']),
                     "tomato": MockClient("tomato", ['127.0.0.1:9160'])}
     iterators.get_pool = lambda pool: self.clients[pool]
     self.client = self.clients["eggs"]
Exemple #2
0
    def test_batch_load(self):
        records, keys = [], []
        for x in range(10):
            record = Record()
            record.key = Key('eggs', 'bacon')
            record['number'] = x
            record['square'] = x * x
            records.append(record)
            keys.append(record.key)

        backing = {}
        for record in records:
            backing[record.key.key] = [ColumnOrSuperColumn(col)
                                       for col in record._columns.values()]

        mock_client = MockClient("Eggs", [])
        mock_client.multiget_slice = \
            lambda keys, parent, pred, clvl: backing
        sets.itr.get_pool = lambda ks: mock_client

        out_records = self.object._batch_load(Record, keys)
        for record in out_records:
            self.assert_(isinstance(record, Record))
            self.assert_(record.key in keys)
            orig = records[records.index(record)]
            self.assert_(orig['number'] == record['number'])
            self.assert_(orig['square'] == record['square'])
Exemple #3
0
 def setUp(self):
     """Prepare the text fixture."""
     obj = view.View()
     obj.key = Key(keyspace='eggs', column_family='bacon',
                   key='dummy_view')
     obj.record_key = Key(keyspace='spam', column_family='tomato')
     self.client = MockClient(['localhost:1234'])
     obj._get_cas = lambda: self.client
     self.object = obj
Exemple #4
0
    def test_iter(self):
        """Make sure FaultTolerantView.__iter__ ignores load errors."""

        class IntermittentFailureRecord(object):

            def load(self, key):
                if key % 2 == 0:
                    raise Exception("Failed to load")
                self.key = key
                return self

        ftv = view.FaultTolerantView()

        client = MockClient(['localhost:1234'])
        client.get_count = lambda *args: 99
        ftv._get_cas = lambda: client

        ftv.record_class = IntermittentFailureRecord
        ftv._keys = lambda: range(10)
        res = tuple(ftv)
        self.assert_(len(res) == 5)
        for record in res:
            self.assert_(record.key % 2 != 0)
Exemple #5
0
    def test_iter(self):
        """Make sure FaultTolerantView.__iter__ ignores load errors."""

        class IntermittentFailureRecord(object):

            def load(self, key):
                if key % 2 == 0:
                    raise Exception("Failed to load")
                self.key = key
                return self

        ftv = view.FaultTolerantView()

        client = MockClient(['localhost:1234'])
        client.get_count = lambda *args: 99
        ftv._get_cas = lambda: client

        ftv.record_class = IntermittentFailureRecord
        ftv._keys = lambda: range(10)
        res = tuple(ftv)
        self.assert_(len(res) == 5)
        for record in res:
            self.assert_(record.key % 2 != 0)
 def setUp(self):
     super(TestClient, self).setUp()
     self.client = MockClient(['localhost:1234', 'localhost:5678'])
class TestClient(ConnectionTest):

    def setUp(self):
        super(TestClient, self).setUp()
        self.client = MockClient(['localhost:1234', 'localhost:5678'])

    def test_init(self):
        pass

    def test_build_server(self):

        class ErrorFailedBuild(Exception):
            pass

        def raise_():
            raise ErrorFailedBuild()


        srv = self.client._build_server('localhost', 1234)
        self.assert_(srv.__class__ is Cassandra.Client)

        # FIXME - test exception handling

    def test_get_server(self):
        # Zero clients
        real = self.client._clients
        bad = (None, [])
        for clts in bad:
            self.client._clients = bad
            self.assert_(ErrorCassandraNoServersConfigured,
                         self.client._get_server)

        # Round-robin
        fake = ['eggs', 'bacon', 'spam']
        self.client._clients = fake
        self.client._current_server = 0
        for exp in fake * 2:
            srv = self.client._get_server()
            self.assert_(srv == exp)

    def test_list_servers(self):
        servers = self.client.list_servers()
        self.assert_(servers.__class__ == list)
        self.assert_(self.client._clients == servers)

    def test_connect(self):

        def raise_(except_):

            def __r():
                raise except_
            return __r

        class _MockTransport(object):

            def __init__(self, *args, **kwargs):
                self.calls = {'open': 0, 'close': 0}

            def open(self):
                self.calls['open'] += 1

            def close(self):
                self.calls['close'] += 1

        # Already connected
        client = self.client._clients[0]
        client.transport = _MockTransport()
        client.transport.isOpen = lambda: True
        self.assert_(self.client._connect(client))

        # Not connected, no error
        client.transport.isOpen = lambda: False
        nopens = client.transport.calls['open']
        self.assert_(self.client._connect(client))
        self.assert_(client.transport.calls['open'] == nopens + 1)

        # Thrift Exception on connect
        client.transport.open = raise_(Thrift.TException)
        self.assertRaises(ErrorThriftMessage,
                          self.client._connect, client,)

        # Other exception on connect
        client.transport.open = raise_(Exception)
        ncloses = client.transport.calls['close']
        self.assert_(self.client._connect(client) == False)
        self.assert_(client.transport.calls['close'] == ncloses + 1)

    def test_getattr(self):
        getter = self.client.__getattr__('get_slice')
        self.assert_(callable(getter))

        self.client._get_server = lambda: None
        self.client._connect = lambda server: False
        getter = self.client.__getattr__('get_slice')
        self.assert_(getter() is None)

        client = Generic()
        client.get_slice = lambda *args, **kwargs: "slice"
        client.transport = Generic()
        client.transport.close = lambda: None
        self.client._connect = lambda server: True
        self.client._get_server = lambda: client
        self.assert_(getter() is "slice")

        def raises(exception_class):

            def raise_(*args, **kwargs):
                raise exception_class(*args, **kwargs)
            return raise_

        client.get_slice = lambda *args, **kwargs: raises(Exception)()
        getter = self.client.__getattr__('get_slice')
        self.assertRaises(Exception, getter)

        client.get_slice = lambda *args, **kwargs: raises(Thrift.TException)()
        getter = self.client.__getattr__('get_slice')
        self.assertRaises(ErrorThriftMessage, getter)

        client.get_slice = lambda *args, **kwargs: \
            raises(Thrift.TException)(message="Test 123")
        getter = self.client.__getattr__('get_slice')
        self.assertRaises(ErrorThriftMessage, getter)
Exemple #8
0
class TestClient(ConnectionTest):

    def setUp(self):
        super(TestClient, self).setUp()
        self.client = MockClient('Keyspace1', ['localhost:1234', 'localhost:5678'])

    def test_init(self):
        pass

    def test_build_server(self):

        exc_classes = (InvalidRequestException, UnavailableException,
                       Thrift.TException)

        cls = Cassandra.Client
        srv = self.client._build_server(cls, 'localhost', 1234)
        self.assert_(isinstance(srv, Cassandra.Client))

        self.client._timeout = 250
        srv = self.client._build_server(cls, 'localhost', 1234)
        self.assert_(srv._iprot.trans._TFramedTransport__trans._timeout ==
                     self.client._timeout * .001)
        self.assert_(isinstance(srv, Cassandra.Client))

        with save(conn.TSocket, ('TSocket',)):
            for exc_class in exc_classes:
                conn.TSocket.TSocket = raises(exc_class)
                self.assert_(self.client._build_server(cls, 'localhost', 1234)
                             is None)

    def test_get_server(self):
        # Zero clients
        real = self.client._clients
        bad = (None, [])
        for clts in bad:
            self.client._clients = clts
            self.assertRaises(ErrorCassandraNoServersConfigured,
                         self.client._get_server)

        # Round-robin
        fake = ['eggs', 'bacon', 'spam']
        self.client._clients = fake
        self.client._current_server = 0
        for exp in 2 * fake:
            srv = self.client._get_server()
            self.assert_(srv == exp)

    def test_list_servers(self):
        servers = self.client.list_servers()
        self.assert_(servers.__class__ == list)
        self.assert_(self.client._clients == servers)

    def test_connect(self):
        client = self.client._get_server()
        self.client._get_server = lambda: client

        # Already connected
        client.transport = _MockTransport()
        client.transport.isOpen = lambda: True
        self.assert_(self.client._connect())

        # Not connected, no error
        nopens = client.transport.calls['open']
        with save(client.transport, ('isOpen',)):
            client.transport.isOpen = lambda: False
            self.assert_(self.client._connect())
            self.assert_(client.transport.calls['open'] == nopens + 1)

        # Thrift Exception on connect - trapped
        ncloses = client.transport.calls['close']
        with save(client.transport, ('isOpen', 'open')):
            client.transport.isOpen = lambda: False
            client.transport.open = raises(TTransportException)
            self.assertRaises(ErrorThriftMessage, self.client._connect)
            self.assert_(client.transport.calls['close'] == ncloses +1)

        # Other exception on connect should be ignored
        with save(client.transport, ('isOpen', 'open')):
            client.transport.isOpen = lambda: False
            client.transport.open = raises(Exception)
            ncloses = client.transport.calls['close']
            self.assertRaises(Exception, self.client._connect)

        # Connection recycling - reuse same connection
        nopens = client.transport.calls['open']
        ncloses = client.transport.calls['close']
        conn = self.client._connect()
        self.client._recycle = 60
        client.connect_time = time.time()
        self.assert_(conn is self.client._connect())
        self.assert_(client.transport.calls['open'] == nopens)
        self.assert_(client.transport.calls['open'] == ncloses)

        # Recycling - reconnect
        nopens = client.transport.calls['open']
        ncloses = client.transport.calls['close']
        conn = self.client._connect()
        self.client._recycle = 60
        client.connect_time = 0
        self.assert_(conn is self.client._connect())
        self.assert_(client.transport.calls['open'] == nopens + 1)
        self.assert_(client.transport.calls['close'] == ncloses + 1)


    def test_methods(self):
        """Test the various client methods."""

        methods = filter(lambda m: m[0] != '_', dir(Cassandra.Iface))

        real_client = Generic()

        @contextmanager
        def get_client():
            yield real_client

        client = self._client('Keyspace1', ['127.0.0.1:9160'])
        client.get_client = get_client
        dummy = lambda *args, **kwargs: (True, args, kwargs)

        for method in methods:
            self.assert_(hasattr(client, method),
                         "Lazyboy client lacks interface method %s" % method)
            self.assert_(callable(getattr(client, method)))
            setattr(real_client, method, dummy)
            res = getattr(client, method)('cleese', gilliam="Terry")
            self.assert_(isinstance(res, tuple),
                         "%s method failed: %s" % (method, res))
            self.assert_(res[0] is True)
            self.assert_(res[1] == ('cleese',))
            self.assert_(res[2] == {'gilliam': "Terry"})

    def test_get_client(self):
        """Test get_client."""
        cass_client = Generic()
        raw_server = Generic()
        self.client._get_server = lambda: raw_server
        self.client._connect = lambda: raw_server
        self.client._servers = [raw_server]
        self.client._current_server = 0

        transport = _MockTransport()
        raw_server.transport = transport

        with self.client.get_client() as clt:
            self.assert_(clt is raw_server)

        # Socket error handling
        ncloses = transport.calls['close']
        try:
            with self.client.get_client() as clt:
                raise socket.error(7, "Test error")
            self.fail_("Exception not raised.")
        except ErrorThriftMessage, exc:
            self.assert_(transport.calls['close'] == ncloses + 1)
            self.assert_(exc.args[1] == "Test error")

        closed = []
        try:
            raw_server.transport = Generic()
            raw_server.transport.close = lambda: closed.append(True)
            with self.client.get_client() as clt:
                raise Thrift.TException("Cleese")
        except ErrorThriftMessage, exc:
            self.assert_(len(closed) == 1)
            self.assert_(exc.args[0] == "Cleese")
 def setUp(self):
     """Prepare the test fixture."""
     self.__get_pool = iterators.get_pool
     self.client = MockClient(['127.0.0.1:9160'])
     iterators.get_pool = lambda pool: self.client
Exemple #10
0
class SliceIteratorTest(unittest.TestCase):

    """Test suite for lazyboy.iterators.slice_iterator."""

    def setUp(self):
        """Prepare the test fixture."""
        self.__get_pool = iterators.get_pool
        self.client = MockClient(['127.0.0.1:9160'])
        iterators.get_pool = lambda pool: self.client

    def tearDown(self):
        """Tear down the test fixture."""
        iterators.get_pool = self.__get_pool

    def test_slice_iterator(self):
        """Test slice_iterator."""

        key = Key(keyspace="eggs", column_family="bacon", key="tomato")
        slice_iterator = iterators.slice_iterator(key, ConsistencyLevel.ONE)
        self.assert_(isinstance(slice_iterator, types.GeneratorType))
        for col in slice_iterator:
            self.assert_(isinstance(col, ttypes.Column))

    def test_slice_iterator_error(self):

        class Client(object):

            def get_slice(*args, **kwargs):
                return None

        key = Key(keyspace="eggs", column_family="bacon", key="tomato")
        real_get_pool = iterators.get_pool
        try:
            iterators.get_pool = lambda pool: Client()
            self.assertRaises(exc.ErrorNoSuchRecord,
                              iterators.slice_iterator,
                              key, ConsistencyLevel.ONE)
        finally:
            iterators.get_pool = real_get_pool

    def test_slice_iterator_supercolumns(self):
        """Test slice_iterator with supercolumns."""
        key = Key(keyspace="eggs", column_family="bacon", key="tomato",
                  super_column="spam")

        cols = [bla.column for bla in self.client.get_slice()]
        scol = ttypes.SuperColumn(name="spam", columns=cols)
        corsc = ttypes.ColumnOrSuperColumn(super_column=scol)

        self.client.get_slice = lambda *args: [corsc]

        slice_iterator = iterators.slice_iterator(key, ConsistencyLevel.ONE)
        self.assert_(isinstance(slice_iterator, types.GeneratorType))
        for col in slice_iterator:
            self.assert_(isinstance(col, ttypes.SuperColumn))

    def test_multigetterator(self):
        """Test multigetterator."""
        keys = [Key("eggs", "bacon", "cleese"),
                Key("eggs", "bacon", "gilliam"),
                Key("eggs", "spam", "jones"),
                Key("eggs", "spam", "idle"),
                Key("tomato", "sausage", "chapman"),
                Key("tomato", "sausage", "palin")]

        def pack(cols):
            return list(iterators.pack(cols))

        data = {'eggs': # Keyspace
                {'bacon': # Column Family
                 {'cleese': pack([Column(name="john", value="cleese")]),
                  'gilliam': pack([Column(name="terry", value="gilliam")])},
                 'spam': # Column Family
                     {'jones': pack([Column(name="terry", value="jones")]),
                      'idle': pack([Column(name="eric", value="idle")])}},
                'tomato': # Keyspace
                {'sausage': # Column Family
                 {'chapman':
                      pack([SuperColumn(name="chap_scol", columns=[])]),
                  'palin':
                      pack([SuperColumn(name="pal_scol", columns=[])])}}}

        def multiget_slice(keyspace, keys, column_parent, predicate,
                           consistencylevel):
            return data[keyspace][column_parent.column_family]
        self.client.multiget_slice = multiget_slice

        res = iterators.multigetterator(keys, ConsistencyLevel.ONE)
        self.assert_(isinstance(res, dict))
        for (keyspace, col_fams) in res.iteritems():
            self.assert_(keyspace in res)
            self.assert_(isinstance(col_fams, dict))
            for (col_fam, rows) in col_fams.iteritems():
                self.assert_(col_fam in data[keyspace])
                self.assert_(isinstance(rows, dict))
                for (row_key, columns) in rows.iteritems():
                    self.assert_(row_key in data[keyspace][col_fam])
                    for column in columns:
                        if keyspace == 'tomato':
                            self.assert_(isinstance(column, SuperColumn))
                        else:
                            self.assert_(isinstance(column, Column))

    def test_sparse_get(self):
        """Test sparse_get."""
        key = Key(keyspace="eggs", column_family="bacon", key="tomato")

        getter = iterators.sparse_get(key, ['eggs', 'bacon'])
        self.assert_(isinstance(getter, types.GeneratorType))
        for col in getter:
            self.assert_(isinstance(col, ttypes.Column))

    def test_sparse_multiget(self):
        """Test sparse_multiget."""
        key = Key(keyspace="eggs", column_family="bacon", key="tomato")

        row_keys = ['cleese', 'jones', 'gilliam', 'chapman', 'idle', 'palin']

        keys = (key.clone(key=row_key) for row_key in row_keys)


        cols = self.client.get_slice()
        res = dict((row_key, cols) for row_key in row_keys)

        self.client.multiget_slice = lambda *args: res
        getter = iterators.sparse_multiget(keys, ['eggs', 'bacon'])
        for (row_key, cols) in getter.iteritems():
            self.assert_(isinstance(row_key, str))
            self.assert_(row_key in row_keys)
            self.assert_(isinstance(cols, list))
            for col in cols:
                self.assert_(isinstance(col, ttypes.Column))

    def test_key_range(self):
        """Test key_range."""
        key = Key(keyspace="eggs", column_family="bacon", key="tomato")
        keys = ['spam', 'eggs', 'sausage', 'tomato', 'spam']
        self.client.get_key_range = lambda *args: keys
        self.assert_(iterators.key_range(key) == keys)

    def test_key_range_iterator(self):
        """Test key_range_iterator."""
        key = Key(keyspace="eggs", column_family="bacon", key="tomato")
        keys = ['spam', 'eggs', 'sausage', 'tomato', 'spam']

        real_key_range = iterators.key_range
        iterators.key_range = lambda *args: keys
        try:
            key_iter = iterators.key_range_iterator(key)
            self.assert_(isinstance(key_iter, types.GeneratorType))
            for key in key_iter:
                self.assert_(isinstance(key, Key))
        finally:
            iterators.key_range = real_key_range

    def test_pack(self):
        """Test pack."""
        columns = self.client.get_slice()
        packed = iterators.pack(columns)
        self.assert_(isinstance(packed, types.GeneratorType))
        for obj in packed:
            self.assert_(isinstance(obj, ttypes.ColumnOrSuperColumn))

    def test_unpack(self):
        """Test unpack."""
        columns = self.client.get_slice()
        unpacked = iterators.unpack(columns)
        self.assert_(isinstance(unpacked, types.GeneratorType))

        for obj in unpacked:
            self.assert_(isinstance(obj, ttypes.Column))
Exemple #11
0
 def setUp(self):
     super(TestClient, self).setUp()
     self.client = MockClient(["localhost:1234", "localhost:5678"])
Exemple #12
0
 def setUp(self):
     super(TestClient, self).setUp()
     self.client = MockClient('Keyspace1',
                              ['localhost:1234', 'localhost:5678'])
Exemple #13
0
class TestClient(ConnectionTest):
    def setUp(self):
        super(TestClient, self).setUp()
        self.client = MockClient('Keyspace1',
                                 ['localhost:1234', 'localhost:5678'])

    def test_init(self):
        pass

    def test_build_server(self):

        exc_classes = (InvalidRequestException, UnavailableException,
                       Thrift.TException)

        cls = Cassandra.Client
        srv = self.client._build_server(cls, 'localhost', 1234)
        self.assert_(isinstance(srv, Cassandra.Client))

        self.client._timeout = 250
        srv = self.client._build_server(cls, 'localhost', 1234)
        self.assert_(srv._iprot.trans._TFramedTransport__trans._timeout ==
                     self.client._timeout * .001)
        self.assert_(isinstance(srv, Cassandra.Client))

        with save(conn.TSocket, ('TSocket', )):
            for exc_class in exc_classes:
                conn.TSocket.TSocket = raises(exc_class)
                self.assert_(
                    self.client._build_server(cls, 'localhost', 1234) is None)

    def test_get_server(self):
        # Zero clients
        real = self.client._clients
        bad = (None, [])
        for clts in bad:
            self.client._clients = clts
            self.assertRaises(ErrorCassandraNoServersConfigured,
                              self.client._get_server)

        # Round-robin
        fake = ['eggs', 'bacon', 'spam']
        self.client._clients = fake
        self.client._current_server = 0
        for exp in 2 * fake:
            srv = self.client._get_server()
            self.assert_(srv == exp)

    def test_list_servers(self):
        servers = self.client.list_servers()
        self.assert_(servers.__class__ == list)
        self.assert_(self.client._clients == servers)

    def test_connect(self):
        client = self.client._get_server()
        self.client._get_server = lambda: client

        # Already connected
        client.transport = _MockTransport()
        client.transport.isOpen = lambda: True
        self.assert_(self.client._connect())

        # Not connected, no error
        nopens = client.transport.calls['open']
        with save(client.transport, ('isOpen', )):
            client.transport.isOpen = lambda: False
            self.assert_(self.client._connect())
            self.assert_(client.transport.calls['open'] == nopens + 1)

        # Thrift Exception on connect - trapped
        ncloses = client.transport.calls['close']
        with save(client.transport, ('isOpen', 'open')):
            client.transport.isOpen = lambda: False
            client.transport.open = raises(TTransportException)
            self.assertRaises(ErrorThriftMessage, self.client._connect)
            self.assert_(client.transport.calls['close'] == ncloses + 1)

        # Other exception on connect should be ignored
        with save(client.transport, ('isOpen', 'open')):
            client.transport.isOpen = lambda: False
            client.transport.open = raises(Exception)
            ncloses = client.transport.calls['close']
            self.assertRaises(Exception, self.client._connect)

        # Connection recycling - reuse same connection
        nopens = client.transport.calls['open']
        ncloses = client.transport.calls['close']
        conn = self.client._connect()
        self.client._recycle = 60
        client.connect_time = time.time()
        self.assert_(conn is self.client._connect())
        self.assert_(client.transport.calls['open'] == nopens)
        self.assert_(client.transport.calls['open'] == ncloses)

        # Recycling - reconnect
        nopens = client.transport.calls['open']
        ncloses = client.transport.calls['close']
        conn = self.client._connect()
        self.client._recycle = 60
        client.connect_time = 0
        self.assert_(conn is self.client._connect())
        self.assert_(client.transport.calls['open'] == nopens + 1)
        self.assert_(client.transport.calls['close'] == ncloses + 1)

    def test_methods(self):
        """Test the various client methods."""

        methods = filter(lambda m: m[0] != '_', dir(Cassandra.Iface))

        real_client = Generic()

        @contextmanager
        def get_client():
            yield real_client

        client = self._client('Keyspace1', ['127.0.0.1:9160'])
        client.get_client = get_client
        dummy = lambda *args, **kwargs: (True, args, kwargs)

        for method in methods:
            self.assert_(hasattr(client, method),
                         "Lazyboy client lacks interface method %s" % method)
            self.assert_(callable(getattr(client, method)))
            setattr(real_client, method, dummy)
            res = getattr(client, method)('cleese', gilliam="Terry")
            self.assert_(isinstance(res, tuple),
                         "%s method failed: %s" % (method, res))
            self.assert_(res[0] is True)
            self.assert_(res[1] == ('cleese', ))
            self.assert_(res[2] == {'gilliam': "Terry"})

    def test_get_client(self):
        """Test get_client."""
        cass_client = Generic()
        raw_server = Generic()
        self.client._get_server = lambda: raw_server
        self.client._connect = lambda: raw_server
        self.client._servers = [raw_server]
        self.client._current_server = 0

        transport = _MockTransport()
        raw_server.transport = transport

        with self.client.get_client() as clt:
            self.assert_(clt is raw_server)

        # Socket error handling
        ncloses = transport.calls['close']
        try:
            with self.client.get_client() as clt:
                raise socket.error(7, "Test error")
            self.fail_("Exception not raised.")
        except ErrorThriftMessage, exc:
            self.assert_(transport.calls['close'] == ncloses + 1)
            self.assert_(exc.args[1] == "Test error")

        closed = []
        try:
            raw_server.transport = Generic()
            raw_server.transport.close = lambda: closed.append(True)
            with self.client.get_client() as clt:
                raise Thrift.TException("Cleese")
        except ErrorThriftMessage, exc:
            self.assert_(len(closed) == 1)
            self.assert_(exc.args[0] == "Cleese")