def testMultiHost(self): info = self.cluster_info cs = ConnectionString(self.make_connargs()['connection_string']) cs.hosts = [ info.host + ':' + '10', info.host + ':' + str(info.port) ] cb = self.make_connection(connection_string=cs.encode()) d = cb.connect() d.addCallback(lambda x: self.assertTrue(cb.connected)) return d
def testMultiHost(self): info = self.cluster_info cs = ConnectionString(self.make_connargs()['connection_string']) cs.hosts = [info.host + ':' + '10', info.host + ':' + str(info.port)] cb = self.make_connection(connection_string=cs.encode()) d = cb.connect() d.addCallback(lambda x: self.assertTrue(cb.connected)) return d
def test_server_not_found(self): connargs = self.make_connargs() cs = ConnectionString(connargs['connection_string']) cs.hosts = [ 'example.com' ] connargs['connection_string'] = cs.encode() self.assertRaises((CouchbaseNetworkError, TimeoutError), self.factory, **connargs) cs.hosts = [ self.cluster_info.host + ':' + str(34567)] self.assertRaises(CouchbaseNetworkError, self.factory, **connargs)
def open_bucket(self, bucket_name, **kwargs): """ Open a new connection to a Couchbase bucket :param bucket_name: The name of the bucket to open :param kwargs: Additional arguments to provide to the constructor :return: An instance of the `bucket_class` object provided to :meth:`__init__` """ if self.authenticator: username, password = self.authenticator.get_credentials( bucket_name) else: username, password = None, None connstr = ConnectionString.parse(str(self.connstr)) connstr.bucket = bucket_name if username: connstr.set_option('username', username) if 'password' in kwargs: if isinstance(self.authenticator, PasswordAuthenticator): raise MixedAuthError("Cannot override " "PasswordAuthenticators password") else: kwargs['password'] = password rv = self.bucket_class(str(connstr), **kwargs) self._buckets[bucket_name] = weakref.ref(rv) if isinstance(self.authenticator, ClassicAuthenticator): for bucket, passwd in self.authenticator.buckets.items(): if passwd: rv.add_bucket_creds(bucket, passwd) return rv
def open_bucket(self, bucket_name, **kwargs): # type: (str, str) -> Bucket """ Open a new connection to a Couchbase bucket :param bucket_name: The name of the bucket to open :param kwargs: Additional arguments to provide to the constructor :return: An instance of the `bucket_class` object provided to :meth:`__init__` """ if self.authenticator: auth_credentials_full = self.authenticator.get_auto_credentials(bucket_name) else: auth_credentials_full = {'options': {}} auth_credentials = auth_credentials_full['options'] connstr = ConnectionString.parse(str(self.connstr)) connstr.bucket = bucket_name for attrib in set(auth_credentials) - {'password'}: connstr.set_option(attrib, auth_credentials[attrib]) # Check if there are conflicting authentication types in any of the parameters # Also sets its own 'auth_type' field to the type of authentication it # thinks is being specified normalizer = Cluster.ParamNormaliser(self.authenticator, connstr, **kwargs) # we don't do anything with this information unless the Normaliser thinks # Cert Auth is involved as this is outside the remit of PYCBC-487/488/489 if issubclass(normalizer.auth_type, CertAuthenticator) or issubclass(type(self.authenticator), CertAuthenticator): # TODO: check_clash_free_params/check_no_unwanted_keys including connstr options # should probably apply to PasswordAuthenticator as well, # but outside remit of PYCBC-487 # TODO: do we accept clashing/unwanted options in connstr/kwargs e.g. password? # Here, we throw an exception # in the case of any clash/unwanted options, but we could scrub # clashing/unwanted options from connstr/kwargs normalizer.check_no_unwanted_keys() normalizer.check_clash_free_params() normalizer.assert_no_critical_complaints() if 'password' in kwargs: if isinstance(self.authenticator, PasswordAuthenticator): raise MixedAuthError("Cannot override " "PasswordAuthenticators password") else: kwargs['password'] = auth_credentials.get('password', None) connstr.scheme = auth_credentials_full.get('scheme', connstr.scheme) rv = self.bucket_class(str(connstr), **kwargs) self._buckets[bucket_name] = weakref.ref(rv) if isinstance(self.authenticator, ClassicAuthenticator): for bucket, passwd in self.authenticator.buckets.items(): if passwd: rv.add_bucket_creds(bucket, passwd) return rv
def test_does_not_encode_slashes(self): connstr = ConnectionString.parse('couchbases://10.112.170.101?certpath=/var/rootcert.pem') self.assertTrue('certpath' in connstr.options) self.assertEqual('/var/rootcert.pem', connstr.options.get('certpath')[0]) encoded = connstr.encode() self.assertEqual('couchbases://10.112.170.101?certpath=/var/rootcert.pem', encoded)
def test_multi_hosts(self): passwd = self.cluster_info.bucket_password cs = ConnectionString(bucket=self.cluster_info.bucket_name, hosts=[self.cluster_info.host]) if not self.mock: cb = self.factory(str(cs), password=passwd) self.assertTrue(cb.upsert("foo", "bar").success) cs.hosts = [self.cluster_info.host + ':' + str(self.cluster_info.port)] cs.scheme = 'http' cb = self.factory(str(cs)) self.assertTrue(cb.upsert("foo", "bar").success) cs.hosts.insert(0, 'localhost:1') cb = self.factory(str(cs)) self.assertTrue(cb.upsert("foo", "bar").success)
def test_multi_hosts(self): passwd = self.cluster_info.bucket_password cs = ConnectionString(bucket=self.cluster_info.bucket_name, hosts=[self.cluster_info.host]) if not self.mock: cb = self.factory(str(cs), password=passwd) self.assertTrue(cb.upsert("foo", "bar").success) cs.hosts = [ self.cluster_info.host + ':' + str(self.cluster_info.port) ] cs.scheme = 'http' cb = self.factory(str(cs)) self.assertTrue(cb.upsert("foo", "bar").success) cs.hosts.insert(0, 'localhost:1') cb = self.factory(str(cs)) self.assertTrue(cb.upsert("foo", "bar").success)
def make_connection(self, **overrides): no_mutinfo = overrides.pop('no_mutinfo', False) kwargs = self.make_connargs(**overrides) connstr = kwargs.get('connection_string', '') connstr = ConnectionString.parse(connstr) if not no_mutinfo: connstr.options['fetch_mutation_tokens'] = '1' kwargs['connection_string'] = connstr.encode() return super(MutationTokensTest, self).make_connection(**kwargs)
def open_bucket(self, bucket_name, **kwargs): """ Open a new connection to a Couchbase bucket :param bucket_name: The name of the bucket to open :param kwargs: Additional arguments to provide to the constructor :return: An instance of the `bucket_class` object provided to :meth:`__init__` """ if self.authenticator: auth_credentials_full = self.authenticator.get_credentials( bucket_name) else: auth_credentials_full = {'options': {}} auth_credentials = auth_credentials_full['options'] connstr = ConnectionString.parse(str(self.connstr)) connstr.bucket = bucket_name for attrib in set(auth_credentials) - {'password'}: connstr.set_option(attrib, auth_credentials[attrib]) if isinstance(self.authenticator, CertAuthenticator): # TODO: _assert_clash_free_params/_assert_no_unwanted_keys including connstr options # should probably apply to PasswordAuthenticator as well, # but outside remit of PYCBC-487 # TODO: do we accept clashing/unwanted options in connstr/kwargs e.g. password? # Here, we throw an exception # in the case of any clash/unwanted options, but we could scrub # clashing/unwanted options from connstr/kwargs param_dicts = dict(kwargs=OverrideDict(kwargs), connstr=connstr.options, auth_credential=auth_credentials) normalizer = Cluster.ParamNormaliser(param_dicts, self.authenticator) normalizer.handle_unwanted_keys() normalizer.assert_clash_free_params() if 'password' in kwargs: if isinstance(self.authenticator, PasswordAuthenticator): raise MixedAuthError("Cannot override " "PasswordAuthenticators password") else: kwargs['password'] = auth_credentials.get('password', None) connstr.scheme = auth_credentials_full.get('scheme', connstr.scheme) rv = self.bucket_class(str(connstr), **kwargs) self._buckets[bucket_name] = weakref.ref(rv) if isinstance(self.authenticator, ClassicAuthenticator): for bucket, passwd in self.authenticator.buckets.items(): if passwd: rv.add_bucket_creds(bucket, passwd) return rv
def _create_cluster(self): connargs = self.make_connargs() connstr = ConnectionString.parse(str(connargs.pop('connection_string'))) connstr.clear_option('username') bucket = connstr.bucket connstr.bucket = None password = connargs.get('password', '') # Can I open a new bucket via open_bucket? cluster = Cluster(connstr, bucket_class=self.factory) cluster.authenticate(ClassicAuthenticator(buckets={bucket: password})) return cluster, bucket
def _create_cluster(self): connargs = self.make_connargs() connstr = ConnectionString.parse(str(connargs.pop('connection_string'))) connstr.clear_option('username') bucket = connstr.bucket connstr.bucket = None password = connargs.get('password', '') # Can I open a new bucket via open_bucket? cluster = Cluster(connstr, bucket_class=self.factory) cluster.authenticate(ClassicAuthenticator(buckets={bucket: password},cluster_password=self.cluster_info.admin_password, cluster_username=self.cluster_info.admin_username)) return cluster, bucket
def _create_cluster_clean(self, authenticator): connargs = self.make_connargs() connstr = ConnectionString.parse(str(connargs.pop('connection_string'))) connstr.clear_option('username') bucket = connstr.bucket connstr.bucket = None password = connargs.get('password', None) keys_to_skip = authenticator.get_credentials(bucket)['options'].keys() for entry in keys_to_skip: connstr.clear_option(entry) cluster = Cluster(connstr, bucket_class=self.factory) cluster.authenticate(ClassicAuthenticator(buckets={bucket: password})) return cluster, bucket
def _create_cluster_clean(self, authenticator): connargs = self.make_connargs() connstr = ConnectionString.parse(str( connargs.pop('connection_string'))) connstr.clear_option('username') bucket = connstr.bucket connstr.bucket = None password = connargs.get('password', None) keys_to_skip = authenticator.get_credentials(bucket)['options'].keys() for entry in keys_to_skip: connstr.clear_option(entry) cluster = Cluster(connstr, bucket_class=self.factory) cluster.authenticate(ClassicAuthenticator(buckets={bucket: password})) return cluster, bucket
def get_new_connection(self, conn_params): name = self.settings_dict['NAME'] if not name: name = 'default' try: bucket = self._buckets[name] except KeyError: print "Connecting to bucket", name cstr = ConnectionString.parse(self.settings_dict['CONNECTION_STRING']) cstr.options['fetch_mutation_tokens'] = '1' cstr.bucket = name bucket = Bucket(str(cstr)) self._buckets[name] = bucket return Connection(self, {}, bucket)
def get_new_connection(self, conn_params): name = self.settings_dict['NAME'] if not name: name = 'default' try: bucket = self._buckets[name] except KeyError: print "Connecting to bucket", name cstr = ConnectionString.parse( self.settings_dict['CONNECTION_STRING']) cstr.options['fetch_mutation_tokens'] = '1' cstr.bucket = name bucket = Bucket(str(cstr)) self._buckets[name] = bucket return Connection(self, {}, bucket)
def __init__(self, connection_string='couchbase://localhost', bucket_class=Bucket): """ Creates a new Cluster object :param connection_string: Base connection string. It is an error to specify a bucket in the string. :param bucket_class: :class:`couchbase.bucket.Bucket` implementation to use. """ self.connstr = ConnectionString.parse(str(connection_string)) self.bucket_class = bucket_class self.authenticator = None self._buckets = {} if self.connstr.bucket: raise ValueError('Cannot pass bucket to connection string: ' + self.connstr.bucket) if 'username' in self.connstr.options: raise ValueError('username must be specified in the authenticator, ' 'not the connection string')
def __init__(self, connection_string='couchbase://localhost', bucket_class=Bucket): """ Creates a new Cluster object :param connection_string: Base connection string. It is an error to specify a bucket in the string. :param bucket_class: :class:`couchbase.bucket.Bucket` implementation to use. """ self.connstr = ConnectionString.parse(str(connection_string)) self.bucket_class = bucket_class self.authenticator = None self._buckets = {} if self.connstr.bucket: raise ValueError('Cannot pass bucket to connection string: ' + self.connstr.bucket) if 'username' in self.connstr.options: raise ValueError( 'username must be specified in the authenticator, ' 'not the connection string')
def test_actions(self): if not self.is_realserver: raise SkipTest('Real server must be used for admin tests') if not os.environ.get('PYCBC_TEST_ADMIN'): raise SkipTest('PYCBC_TEST_ADMIN must be set in the environment') try: # Remove the bucket, if it exists self.admin.bucket_remove('dummy') except CouchbaseError: pass # Need to explicitly enable admin tests.. # Create the bucket self.admin.bucket_create(name='dummy', ram_quota=100, bucket_password='******') self.admin.wait_ready('dummy', timeout=15.0) # All should be OK, ensure we can connect: connstr = ConnectionString.parse( self.make_connargs()['connection_string']) connstr.bucket = 'dummy' connstr = connstr.encode() self.factory(connstr, password='******') # OK, it exists self.assertRaises(CouchbaseError, self.factory, connstr) # Change the password self.admin.bucket_update('dummy', self.admin.bucket_info('dummy'), bucket_password='') self.factory(connstr) # No password # Remove the bucket self.admin.bucket_remove('dummy') self.assertRaises(CouchbaseError, self.factory, connstr)
def test_pathless_connstr(self): # Not strictly a cluster test, but relevant connstr = ConnectionString.parse('couchbase://localhost?opt1=val1&opt2=val2') self.assertTrue('opt1' in connstr.options) self.assertTrue('opt2' in connstr.options)
def test_pathless_connstr(self): connstr = ConnectionString.parse('couchbase://localhost?opt1=val1&opt2=val2') self.assertTrue('opt1' in connstr.options) self.assertTrue('opt2' in connstr.options)
def test_pathless_connstr(self): # Not strictly a cluster test, but relevant connstr = ConnectionString.parse( 'couchbase://localhost?opt1=val1&opt2=val2') self.assertTrue('opt1' in connstr.options) self.assertTrue('opt2' in connstr.options)
def open_bucket(self, bucket_name, **kwargs): # type: (str, str) -> Bucket """ Open a new connection to a Couchbase bucket :param bucket_name: The name of the bucket to open :param kwargs: Additional arguments to provide to the constructor :return: An instance of the `bucket_class` object provided to :meth:`__init__` """ if self.authenticator: auth_credentials_full = self.authenticator.get_auto_credentials( bucket_name) else: auth_credentials_full = {'options': {}} auth_credentials = auth_credentials_full['options'] connstr = ConnectionString.parse(str(self.connstr)) connstr.bucket = bucket_name for attrib in set(auth_credentials) - {'password'}: connstr.set_option(attrib, auth_credentials[attrib]) # Check if there are conflicting authentication types in any of the parameters # Also sets its own 'auth_type' field to the type of authentication it # thinks is being specified normalizer = Cluster.ParamNormaliser(self.authenticator, connstr, **kwargs) # we don't do anything with this information unless the Normaliser thinks # Cert Auth is involved as this is outside the remit of PYCBC-487/488/489 if issubclass(normalizer.auth_type, CertAuthenticator) or issubclass( type(self.authenticator), CertAuthenticator): # TODO: check_clash_free_params/check_no_unwanted_keys including connstr options # should probably apply to PasswordAuthenticator as well, # but outside remit of PYCBC-487 # TODO: do we accept clashing/unwanted options in connstr/kwargs e.g. password? # Here, we throw an exception # in the case of any clash/unwanted options, but we could scrub # clashing/unwanted options from connstr/kwargs normalizer.check_no_unwanted_keys() normalizer.check_clash_free_params() normalizer.assert_no_critical_complaints() if 'password' in kwargs: if isinstance(self.authenticator, PasswordAuthenticator): raise MixedAuthError("Cannot override " "PasswordAuthenticators password") else: kwargs['password'] = auth_credentials.get('password', None) connstr.scheme = auth_credentials_full.get('scheme', connstr.scheme) rv = self.bucket_class(str(connstr), **kwargs) self._buckets[bucket_name] = weakref.ref(rv) if isinstance(self.authenticator, ClassicAuthenticator): for bucket, passwd in self.authenticator.buckets.items(): if passwd: rv.add_bucket_creds(bucket, passwd) return rv