Example #1
0
 def modify_columns_multikeys(cls, keys, mapping, ttl=None, timestamp=None):
     """Updates a set of rows to give the columns specified by the keys of
     `mapping` their respective values."""
     row = map(lambda x: Column(x, mapping[x], timestamp, ttl), mapping)
     row.append(Column(cls.EXISTS_COLUMN, "", timestamp, ttl))
     mutmap = {key: {cls.cass_table: row} for key in keys}
     yield cls.ha_batch_mutate(mutmap)
Example #2
0
def dostuff(client):
    yield client.insert(key='test', column_family=CF, value='testval', column=colname)

    res = yield client.get(key='test', column_family=CF, column=colname)
    print 'get', res

    res = yield client.get_slice(key='test', column_family=CF)
    print 'get_slice', res

    res = yield client.multiget(keys=['test', 'test2'], column_family=CF, column=colname)
    print 'multiget', res

    res = yield client.multiget_slice(keys=['test', 'test2'], column_family=CF)
    print 'multiget_slice', res

    res = yield client.get_count(key='test', column_family=CF)
    print 'get_count', res

    # batch insert will figure out if you're trying a CF or SCF
    # from the data structure
    res = yield client.batch_insert(key='test', column_family=CF, mapping={colname: 'bar'})
    print "batch_insert", res

    # with ttypes, you pass a list as you would for raw thrift
    # this way you can set custom timestamps
    cols = [Column(colname, 'bar', 1234), Column('bar', 'baz', 54321)]
    res = yield client.batch_insert(key='test', column_family=CF, mapping=cols)
    print "batch_insert", res

    client.system_drop_keyspace('Keyspace1')
Example #3
0
 def side_effect_slice(column_family, key):
     if (column_family == config.PUBLIC_IDS_TABLE and key == 'priv2'):
         return ([
             ColumnOrSuperColumn(column=Column(
                 timestamp=1371131096949743,
                 name='sip:pub',
                 value='sip:pub',
                 ttl=None),
                                 counter_super_column=None,
                                 super_column=None,
                                 counter_column=None)
         ])
     elif (column_family == config.PRIVATE_IDS_TABLE
           and key == 'sip:pub'):
         return ([
             ColumnOrSuperColumn(column=Column(
                 timestamp=1371131096949743,
                 name='priv',
                 value='priv',
                 ttl=None),
                                 counter_super_column=None,
                                 super_column=None,
                                 counter_column=None)
         ])
     else:
         raise Exception('FAIL')
Example #4
0
    def test_get_many_results(self):
        self.mock_cass.get_slice.return_value = defer.Deferred()
        self.mock_cass.get.return_value = defer.Deferred()
        self.handler.finish = mock.MagicMock()
        self.request.arguments = {}
        self.handler.get("sip:pub")
        self.mock_cass.get_slice.assert_called_once_with(
            column_family=config.PRIVATE_IDS_TABLE, key='sip:pub')
        result_list = [
            ColumnOrSuperColumn(column=Column(timestamp=1371131096949743,
                                              name='priv',
                                              value='priv',
                                              ttl=None),
                                counter_super_column=None,
                                super_column=None,
                                counter_column=None),
            ColumnOrSuperColumn(column=Column(timestamp=1371131096949743,
                                              name='priv2',
                                              value='priv2',
                                              ttl=None),
                                counter_super_column=None,
                                super_column=None,
                                counter_column=None)
        ]

        self.mock_cass.get_slice.return_value.callback(result_list)
        self.assertTrue(self.handler.finish.called)
        self.assertEquals(self.handler.get_status(), httplib.OK)
        self.assertEquals(self.handler.finish.call_args[0][0], {
            "public_id": "sip:pub",
            "private_ids": ["priv", "priv2"]
        })
Example #5
0
def dostuff(client):
    yield client.insert(key='test', column_family=CF, value='testval', column=colname)
    yield client.insert(key='test', column_family=SCF, value='testval', column=colname, super_column=scname)

    res = yield client.get(key='test', column_family=CF, column=colname)
    print 'get', res

    res = yield client.get(key='test', column_family=SCF, column=colname, super_column=scname)
    print 'get (super)', res

    res = yield client.get_slice(key='test', column_family=CF)
    print 'get_slice', res

    res = yield client.multiget(keys=['test', 'test2'], column_family=CF, column=colname)
    print 'multiget', res

    res = yield client.multiget_slice(keys=['test', 'test2'], column_family=CF)
    print 'multiget_slice', res

    res = yield client.get_count(key='test', column_family=CF)
    print 'get_count', res

    yield client.add(key='test', column_family=COUNT_CF, value=1, column='testcounter')
    res = yield client.get(key='test', column_family=COUNT_CF, column='testcounter')
    print 'get counter value', res

    yield client.add(key='test', column_family=SUPERCOUNT_CF, value=1,
                     column='testcounter', super_column='testsuper')
    res = yield client.get(key='test', column_family=SUPERCOUNT_CF,
                           column='testcounter', super_column='testsuper')
    print 'get super counter value', res

    # batch insert will figure out if you're trying a CF or SCF
    # from the data structure
    res = yield client.batch_insert(key='test', column_family=CF, mapping={colname: 'bar'})
    print "batch_insert", res
    res = yield client.batch_insert(key='test', column_family=SCF, mapping={'foo': {colname: 'bar'}})
    print "batch_insert", res

    # with ttypes, you pass a list as you would for raw thrift
    # this way you can set custom timestamps
    cols = [Column(colname, 'bar', 1234), Column('bar', 'baz', 54321)]
    res = yield client.batch_insert(key='test', column_family=CF, mapping=cols)
    print "batch_insert", res
    cols = [SuperColumn(name=colname, columns=cols)]

    # of course you don't have to use kwargs if the order is correct
    res = yield client.batch_insert('test', SCF, cols)
    print "batch_insert", res
Example #6
0
    def test_post_exists(self):
        self.mock_cass.get_slice.return_value = defer.Deferred()
        self.handler.finish = mock.MagicMock()
        self.request.arguments = {'private_id': 'priv'}

        self.handler.post("sip:pub")
        # get_slice to check if it exists
        self.mock_cass.get_slice.assert_called_once_with(
            column_family=config.PUBLIC_IDS_TABLE,
            key='priv',
            start='sip:pub',
            finish='sip:pub')

        result_list = [
            ColumnOrSuperColumn(column=Column(timestamp=1371131096949743,
                                              name='sip:pub',
                                              value='sip:pub',
                                              ttl=None),
                                counter_super_column=None,
                                super_column=None,
                                counter_column=None)
        ]

        rv = self.mock_cass.get_slice.return_value
        self.mock_cass.reset_mock()
        self.mock_cass.get_slice.return_value = defer.Deferred()
        rv.callback(result_list)

        # get_slice public IDs, key=priv to get returned data
        self.mock_cass.get_slice.assert_called_once_with(
            column_family=config.PRIVATE_IDS_TABLE, key='sip:pub')
        result_list = [
            ColumnOrSuperColumn(column=Column(timestamp=1371131096949743,
                                              name='priv',
                                              value='priv',
                                              ttl=None),
                                counter_super_column=None,
                                super_column=None,
                                counter_column=None)
        ]
        self.mock_cass.get_slice.return_value.callback(result_list)

        self.assertTrue(self.handler.finish.called)
        self.assertEquals(self.handler.get_status(), httplib.OK)
        self.assertEquals(self.handler.finish.call_args[0][0], {
            "public_id": "sip:pub",
            "private_ids": ["priv"]
        })
Example #7
0
    def test_get_mainline(self, decrypt_password):
        self.mock_cass.get_slice.return_value = defer.Deferred()
        self.mock_cass.get.return_value = defer.Deferred()
        self.handler.finish = mock.MagicMock()
        decrypt_password.return_value = "dec_pw"

        # Make the call
        self.handler.get("priv", "pub")

        # Expect a call to validate the pub/priv
        self.mock_cass.get_slice.assert_called_once_with(column_family=config.PUBLIC_IDS_TABLE, key='priv', start='pub', finish='pub')
        result = mock.MagicMock()
        result_list = [
             ColumnOrSuperColumn(column=Column(timestamp=1371131096949743,
            name='pub', value='pub', ttl=None), counter_super_column=None,
            super_column=None, counter_column=None)]
        self.mock_cass.get_slice.return_value.callback(result_list)

        # Now the database query to retrieve the digest
        self.mock_cass.get.assert_called_once_with(column='col', column_family='table', key='priv')
        result = mock.MagicMock()
        result.column.value = "enc_pw"
        self.mock_cass.get.return_value.callback(result)
        decrypt_password.assert_called_once_with("enc_pw", settings.PASSWORD_ENCRYPTION_KEY)
        self.assertEquals(self.handler.finish.call_args[0][0], {"digest": "dec_pw"})
Example #8
0
    def test_delete_wildcard_mainline(self):
        self.mock_cass.remove.return_value = defer.Deferred()
        self.mock_cass.get_slice.return_value = defer.Deferred()
        self.handler.finish = mock.MagicMock()
        self.request.arguments = {}
        self.handler.delete("sip:pub")

        # Expect a call to query the set of IDs to delete
        self.mock_cass.get_slice.assert_called_once_with(
            column_family=config.PRIVATE_IDS_TABLE, key='sip:pub')
        result_list = [
            ColumnOrSuperColumn(column=Column(timestamp=1371131096949743,
                                              name='priv',
                                              value='priv',
                                              ttl=None),
                                counter_super_column=None,
                                super_column=None,
                                counter_column=None)
        ]
        self.mock_cass.get_slice.return_value.callback(result_list)

        self.mock_cass.remove.return_value.callback(mock.MagicMock())
        self.mock_cass.remove.assert_has_calls([
            call(column_family=config.PUBLIC_IDS_TABLE,
                 key='priv',
                 column='sip:pub'),
            call(column_family=config.PRIVATE_IDS_TABLE,
                 key='sip:pub',
                 column='priv')
        ],
                                               any_order=True)

        self.assertTrue(self.handler.finish.called)
        self.assertEquals(self.handler.get_status(), httplib.NO_CONTENT)
Example #9
0
 def test_put_multi_ims_subscription(self):
     """Test multiple IMS subscriptions can be put into the cache"""
     self.cass_client.batch_mutate.return_value = batch_mutate = defer.Deferred(
     )
     res = Result(
         self.cache.put_multi_ims_subscription(["pub1", "pub2"],
                                               "xml",
                                               ttl=self.ttl,
                                               timestamp=self.timestamp))
     row = {
         "impu": [
             Column("ims_subscription_xml", "xml", self.timestamp,
                    self.ttl),
             Column("_exists", "", self.timestamp, self.ttl)
         ]
     }
     self.cass_client.batch_mutate.assert_called_once_with(
         {
             "pub1": row,
             "pub2": row
         },
         consistency=ConsistencyLevel.LOCAL_QUORUM)
     batch_mutate.callback(None)
     self.assertEquals(res.value(), None)
Example #10
0
    def test_user_wrong_association(self):
        self.mock_cass.get_slice.return_value = defer.Deferred()
        self.mock_cass.get.return_value = defer.Deferred()
        self.handler.finish = mock.MagicMock()
        get_deferred = self.handler.get("priv", "pub")
        get_errback = mock.MagicMock()
        get_deferred.addErrback(get_errback)

        # Expect a call to validate the pub/priv
        self.mock_cass.get_slice.assert_called_once_with(column_family=config.PUBLIC_IDS_TABLE, key='priv', start='pub', finish='pub')
        result_list = [
             ColumnOrSuperColumn(column=Column(timestamp=1371131096949743,
            name='pub2', value='pub2', ttl=None), counter_super_column=None,
            super_column=None, counter_column=None)]
        self.mock_cass.get_slice.return_value.callback(result_list)

        self.assertEquals(get_errback.call_args[0][0].getErrorMessage(), 'HTTP 404: Not Found')
Example #11
0
    def test_post_add_first_entry(self):
        self.mock_cass.get_slice.return_value = defer.Deferred()
        self.handler.finish = mock.MagicMock()
        self.request.arguments = {'private_id': 'priv'}

        self.handler.post("sip:pub")
        # get_slice to check if it exists
        self.mock_cass.get_slice.assert_called_once_with(
            column_family=config.PUBLIC_IDS_TABLE,
            key='priv',
            start='sip:pub',
            finish='sip:pub')

        rv = self.mock_cass.get_slice.return_value
        self.mock_cass.reset_mock()

        # Prepare for 2x get-slice calls to check for limits
        def side_effect_slice(column_family, key):
            if (column_family == config.PUBLIC_IDS_TABLE and key == 'priv'):
                return []
            elif (column_family == config.PRIVATE_IDS_TABLE
                  and key == 'sip:pub'):
                return []
            else:
                raise Exception('FAIL')

        self.mock_cass.get_slice.side_effect = side_effect_slice
        self.mock_cass.insert.return_value = defer.Deferred()

        rv.callback([])  # The callback on the original get_slice
        # This will trigger the 2 get_slices, and then
        # the two inserts

        # Restore the get_slice mock in in advance of the final call on it.
        self.mock_cass.get_slice.return_value = defer.Deferred()
        self.mock_cass.get_slice.side_effect = None

        self.mock_cass.insert.assert_has_calls([
            call(column_family=config.PUBLIC_IDS_TABLE,
                 key='priv',
                 column='sip:pub',
                 value='sip:pub'),
            call(column_family=config.PRIVATE_IDS_TABLE,
                 key='sip:pub',
                 column='priv',
                 value='priv')
        ],
                                               any_order=True)

        self.mock_cass.reset_mock()
        self.mock_cass.insert.return_value.callback(mock.MagicMock())

        # get_slice public IDs, key=priv to get returned data
        self.mock_cass.get_slice.assert_called_once_with(
            column_family=config.PRIVATE_IDS_TABLE, key='sip:pub')

        result_list = [
            ColumnOrSuperColumn(column=Column(timestamp=1371131096949743,
                                              name='priv',
                                              value='priv',
                                              ttl=None),
                                counter_super_column=None,
                                super_column=None,
                                counter_column=None)
        ]
        self.mock_cass.get_slice.return_value.callback(result_list)

        self.assertTrue(self.handler.finish.called)
        self.assertEquals(self.handler.get_status(), httplib.CREATED)
        self.assertEquals(self.handler.finish.call_args[0][0], {
            "public_id": "sip:pub",
            "private_ids": ["priv"]
        })