91 lines
3.8 KiB
Python
91 lines
3.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import unittest
|
|
import json
|
|
from pgbouncemgr.config import *
|
|
|
|
def load_test_config(name):
|
|
path = os.path.join(
|
|
os.path.dirname(os.path.realpath(__file__)),
|
|
"testfiles", name)
|
|
return Config(path)
|
|
|
|
class ConfigTests(unittest.TestCase):
|
|
def test_ConfigThatDoesNotExist_RaisesException(self):
|
|
with self.assertRaises(ConfigNotFound) as context:
|
|
load_test_config("/path/doesnotexist.yaml")
|
|
self.assertIn("path=/path/doesnotexist.yaml", str(context.exception))
|
|
|
|
def test_ConfigContainingInvalidSection_RaisesException(self):
|
|
with self.assertRaises(InvalidConfigSection) as context:
|
|
load_test_config("invalid_section.yaml")
|
|
self.assertIn("section=unknown_section", str(context.exception))
|
|
|
|
def test_ConfigContainingInvalidKey_RaisesException(self):
|
|
with self.assertRaises(InvalidConfigKey) as context:
|
|
load_test_config("invalid_key.yaml")
|
|
self.assertIn("key=main.improbability_drive_level", str(context.exception))
|
|
|
|
def test_ConfigContainingInvalidIntegerValue_RaisesException(self):
|
|
with self.assertRaises(InvalidConfigValue) as context:
|
|
load_test_config("invalid_main_interval_in_sec.yaml")
|
|
self.assertIn("it must be an integer", str(context.exception))
|
|
self.assertIn("key=main.poll_interval_in_sec, value=ten seconds", str(context.exception))
|
|
|
|
def test_ConfigContainingEmptyValue_RaisesException(self):
|
|
with self.assertRaises(InvalidConfigValue) as context:
|
|
load_test_config("invalid_node_empty_host.yaml")
|
|
self.assertIn("it must not be an empty value", str(context.exception))
|
|
self.assertIn(r"key=node[nodeA].host, value=' \t\r\n '", str(context.exception))
|
|
|
|
def test_ConfigFromEmptyConfigurationFile_UsesDefaultValues(self):
|
|
config = load_test_config("empty.yaml")
|
|
# main
|
|
self.assertEqual("postgres", config.run_user)
|
|
self.assertEqual("postgres", config.run_group)
|
|
self.assertEqual("/var/lib/pgbouncemgr/state.json", config.state_file)
|
|
self.assertEqual(2, config.poll_interval_in_sec)
|
|
# db connection defaults
|
|
self.assertEqual("0.0.0.0", config.db_connection_defaults["host"])
|
|
self.assertEqual(5432, config.db_connection_defaults["port"])
|
|
self.assertEqual(1, config.db_connection_defaults["connect_timeout"])
|
|
self.assertEqual("pgbouncemgr", config.db_connection_defaults["user"])
|
|
self.assertEqual(None, config.db_connection_defaults["password"])
|
|
self.assertEqual("template1", config.db_connection_defaults["database"])
|
|
# pgbouncer
|
|
self.assertEqual("/etc/pgbouncer/pgbouncer.ini", config.pgbouncer["pgbouncer_config"])
|
|
self.assertEqual(6432, config.pgbouncer["port"])
|
|
# nodes
|
|
self.assertEqual({}, config.nodes)
|
|
|
|
def test_ConfigFromBasicConfigurationFile_DbConnectionDefaultsAreApplied(self):
|
|
config = load_test_config("basic.yaml")
|
|
self.assertEqual({
|
|
"connect_timeout": 1,
|
|
"host": "0.0.0.0",
|
|
"database": "template1",
|
|
"password": "Wilmaaaaa!!!",
|
|
"pgbouncer_config": "/etc/pgbouncer/pgbouncer.ini",
|
|
"port": 6432,
|
|
"user": "pgbouncemgr"
|
|
}, config.pgbouncer)
|
|
self.assertEqual({
|
|
"nodeA": {
|
|
"connect_timeout": 1,
|
|
"host": "1.2.3.4",
|
|
"database": "template1",
|
|
"password": "Wilmaaaaa!!!",
|
|
"port": 8888,
|
|
"user": "pgbouncemgr"
|
|
},
|
|
"nodeB": {
|
|
"connect_timeout": 1,
|
|
"host": "2.3.4.5",
|
|
"database": "template1",
|
|
"password": "Wilmaaaaa!!!",
|
|
"port": 7777,
|
|
"user": "pgbouncemgr"
|
|
}
|
|
}, config.nodes)
|