Exemplo n.º 1
0
	def test_file_wrong(self):
		"""Check reading config file with wrong syntax"""

		conf_old = json.dumps(config.read())
		with self.assertRaises(SyntaxError):
			conf = config.read('./test_config2.conf', force=True)

		config.read(inline = conf_old, force=True)
Exemplo n.º 2
0
	def test_inline_wrong(self):
		"""Check wrong syntax in inline configuration"""

		confstr = """
		{
			"database": {
				"name": "testing
			}
		}
		"""
		conf_old = json.dumps(config.read())
		with self.assertRaises(SyntaxError):
			conf = config.read(inline = confstr, force=True)

		config.read(inline = conf_old, force=True)
Exemplo n.º 3
0
	def get_connection(self, dbhost=None, dbname=None, dbuser=None, dbpass=None, dbtype='global', primarykey=None):
		"""Returns free connection"""

		conf = config.read()

		conn = {}
		conn['dbuser'] = dbuser or conf['database']['user']
		conn['dbpass'] = dbpass or conf['database']['pass']
		conn['dbname'] = dbname or conf['database']['name']
		conn['dbhost'] = dbhost or get_host(dbtype, primarykey)

		if not conn['dbhost']:
			raise DBError("Database host name is not specified")

		conn['key'] = "{0}@{1}/{2}".format(conn['dbuser'], conn['dbhost'], conn['dbname'])

		if conn['key'] not in self._CONNECTIONS:
			self._CONNECTIONS[conn['key']] = pool.ThreadedConnectionPool(

				# Pool params
				MIN_OPEN_CONNECTIONS,
				MAX_OPEN_CONNECTIONS,

				# Connection params
				host     = conn['dbhost'],
				database = conn['dbname'],
				user     = conn['dbuser'],
				password = conn['dbpass']
			)
		conn['socket'] = self._CONNECTIONS[conn['key']].getconn()

		return conn
Exemplo n.º 4
0
	def test_incomplete_config(self):
		"""Check reading incomplete configuration"""

		confstr = """
		{
			"database": {
				"name": "testing",
				"server": "127.0.0.1"
			}
		}
		"""
		conf_old = json.dumps(config.read())
		with self.assertNotRaises(SyntaxError):
			conf = config.read(inline = confstr, force=True)
		self.assertEqual(conf['logging']['level'], 'error')

		config.read(inline = conf_old, force=True)
Exemplo n.º 5
0
	def test_inline_config(self):
		"""Check reading configuration from string"""

		confstr = """
		{
			"database": {
				"name": "testing",
				"server": "127.0.0.1",
				"shards": ["127.0.0.10", "127.0.0.11", "127.0.0.12", "127.0.0.13", "127.0.0.14"]
			}
		}
		"""

		conf_old = json.dumps(config.read())

		with self.assertNotRaises(SyntaxError):
			conf_new = config.read(inline = confstr, force=True)

		config.read(inline = conf_old, force=True)
Exemplo n.º 6
0
def get_host(dbtype, key=None):
	"""Returns database server"""

	conf = config.read()

	if key != None:
		data = config.subdict(conf['database']['shards'], field='weight')
		ring = hashing.HashRing(data.keys(), data)
		return conf['database']['shards'][ring.get_node(key)]['server']
	elif dbtype == 'local':
		for shard in conf['database']['shards'].itervalues():
			if shard['local'] == True:
				return shard['server']
	elif dbtype == 'global':
		return conf['database']['server']
Exemplo n.º 7
0
def logger(subname, **kwargs):
    global LOGGER, HANDLER

    if not kwargs.get("init", False):
        return LOGGER

    if HANDLER != None:
        LOGGER.removeHandler(HANDLER)

    conf = config.read()
    log_type = kwargs.get("type", conf["logging"]["type"]) or "syslog"
    log_level = log_level_mapping[kwargs.get("level", conf["logging"]["level"]).lower()]

    if log_type == "file":
        name = kwargs.get("name", "billing")
        log_file = kwargs.get("log_file", conf["logging"]["logdir"]) + "/" + name + ".log"
        log_max_bytes = unitconvert.convert_from(conf["logging"]["logsize"])
        log_count = conf["logging"]["backcount"]

        HANDLER = logging.handlers.RotatingFileHandler(log_file, maxBytes=log_max_bytes, backupCount=log_count)

    elif log_type == "syslog":
        log_address = kwargs.get("address", str(conf["logging"]["address"]))
        log_facility = kwargs.get("facility", conf["logging"]["facility"])

        HANDLER = SysLogHandler(address=log_address, facility=SysLogHandler.facility_names[log_facility])

    elif log_type == "stderr":
        HANDLER = logging.StreamHandler(sys.stderr)

        # LOGGER = logging.getLogger()
    LOGGER.setLevel(log_level)

    HANDLER.setFormatter(
        logging.Formatter(kwargs.get("log_format", default_log_format), kwargs.get("date_format", default_date_format))
    )

    LOGGER.addHandler(HANDLER)

    return LOGGER
Exemplo n.º 8
0
def write_zone(key):
	conf = config.read()
	data = config.subdict(conf['zones'], field='weight')
	ring = hashing.HashRing(data.keys(), data)
	res  = ring.get_node(key)
	return conf['zones'][res]
Exemplo n.º 9
0
confstr = """
{
	"database": {
		"name": "testing",
		"server": "127.0.0.1",
		"user": "******",
		"pass": "******",
		"shards": {
			"shard0": { "server":"127.0.0.1", "weight":3, "local":true }
		}
	}
}
"""

config.read(inline = confstr, force=True)


class _AssertNotRaisesContext(object):
	"""A context manager used to implement TestCase.assertNotRaises method."""

	def __init__(self, expected, test_case):
		self.expected = expected
		self.failureException = test_case.failureException


	def __enter__(self):
		return self


	def __exit__(self, exc_type, exc_value, tb):
Exemplo n.º 10
0
		for s in self.sql_drop_table():
			yield s


	def create(self, conn):
		connect = conn or self.conn
		for s in self.sql_create():
			connect.execute(s)


	def drop(self, conn):
		connect = conn or self.conn
		for s in self.sql_drop():
			connect.execute(s)

conf = config.read()
GLOBAL = [conf['database']['server']]
LOCAL  = [i['server'] for i in conf['database']['shards'].itervalues()]

SCHEMA = [
	(GLOBAL, DBTable("metrics",
			columns = [
				("id",        "varchar(128)", "NOT NULL PRIMARY KEY"),
				("type",      "varchar(32)",  "NOT NULL"),
				("formula",   "varchar(32)",  "NOT NULL"),
				("aggregate", "int",          "NOT NULL"),
				("sync",      "int",          "NOT NULL DEFAULT '0'"),
			]
		)),
	(LOCAL, DBTable("queue",
			columns = [