def test_read_charset(self): if self.verbose == 1: print() LOG.info( "Testing reading of configuration files with different charcter sets." ) from fb_tools.multi_config import BaseMultiConfig test_stems = ('test_multicfg-latin1', 'test_multicfg-utf-16', 'test_multicfg-utf-32', 'test_multicfg-utf8') for stem in test_stems: if self.verbose: print() LOG.info("Testing for file stem {!r} ...".format(stem)) cfg = BaseMultiConfig(appname=self.appname, config_dir=self.test_cfg_dir.name, additional_cfgdirs=self.test_cfg_dir, verbose=self.verbose, append_appname_to_stems=False, additional_stems=stem) cfg.read() LOG.info('Read config:\n' + pp(cfg.cfg))
def test_call_sync(self): LOG.info("Testing synchronous execution of a shell script.") from fb_tools.common import pp from fb_tools.errors import CommandNotFoundError from fb_tools.handling_obj import CompletedProcess import fb_tools.handler # noqa from fb_tools.handler import BaseHandler curdir = os.path.dirname(os.path.abspath(__file__)) call_script = os.path.join(curdir, 'call_script.sh') if not os.path.exists(call_script): raise CommandNotFoundError(call_script) LOG.debug("Trying to execute {!r} synchronous ...".format(call_script)) hdlr = BaseHandler( appname=self.appname, verbose=self.verbose, ) proc = hdlr.call([call_script]) LOG.debug("Got back a {} object.".format(proc.__class__.__name__)) self.assertIsInstance(proc, CompletedProcess) LOG.debug("Got return value: {}.".format(proc.returncode)) LOG.debug("Got proc args:\n{}.".format(pp(proc.args))) LOG.debug("Got STDOUT: {!r}".format(proc.stdout)) LOG.debug("Got STDERR: {!r}".format(proc.stderr)) self.assertEqual(proc.returncode, 0) self.assertIsNotNone(proc.stdout) self.assertIsNotNone(proc.stderr)
def test_collect_cfg_files(self): if self.verbose == 1: print() LOG.info("Testing collecting of configuration files.") exts = ('.ini', '.js', '.yaml') ext_methods = { '.ini': 'load_ini', '.js': 'load_json', '.yaml': 'load_yaml', } from fb_tools.multi_config import BaseMultiConfig cfg = BaseMultiConfig(appname=self.appname, config_dir=self.test_cfg_dir.name, additional_cfgdirs=self.test_cfg_dir, verbose=self.verbose) if self.verbose >= 2: LOG.debug("Current configuration directories:\n{}".format( pp(cfg.config_dirs))) LOG.debug("Initialized stems:\n{}".format(pp(cfg.stems))) cfg.collect_config_files() if self.verbose >= 2: LOG.debug("Found configuration files:\n{}".format( pp(cfg.config_files))) LOG.debug("Found read methods:\n{}".format( pp(cfg.config_file_methods))) for ext in exts: path = self.test_cfg_dir / (self.appname + ext) exp_method = ext_methods[ext] LOG.debug( "Checking for existence of detected cfg file {!r}.".format( str(path))) self.assertIn(path, cfg.config_files) LOG.debug("Checking method {m!r} of cfg file {f!r}.".format( m=exp_method, f=str(path))) found_method = cfg.config_file_methods[path] LOG.debug("Found method: {!r}".format(found_method)) self.assertEqual(exp_method, found_method)
def test_init_cfg_dirs(self): if self.verbose == 1: print() LOG.info("Testing init of configuration directories.") from fb_tools.multi_config import BaseMultiConfig cfg = BaseMultiConfig( appname=self.appname, base_dir=self.base_dir, config_dir='test', additional_stems='test', additional_cfgdirs=self.test_cfg_dir, verbose=self.verbose, ) if self.verbose >= 2: LOG.debug("Current configuration directories:\n{}".format( pp(cfg.config_dirs))) system_path = Path('/etc', 'test') LOG.debug("Testing existence of system config path {!r}.".format( system_path)) self.assertIn(system_path, cfg.config_dirs) user_path = Path(os.path.expanduser('~')) / '.config' / 'test' LOG.debug( "Testing existence of user config path {!r}.".format(user_path)) self.assertIn(user_path, cfg.config_dirs) cwd_etc_dir = Path.cwd() / 'etc' LOG.debug( "Testing existence of config path in current dir {!r}.".format( cwd_etc_dir)) self.assertIn(cwd_etc_dir, cfg.config_dirs) base_etc_dir = self.base_dir / 'etc' LOG.debug("Testing existence of basedir config path {!r}.".format( base_etc_dir)) self.assertIn(base_etc_dir, cfg.config_dirs) LOG.debug("Testing existence of basedir {!r}.".format(self.base_dir)) self.assertIn(self.base_dir, cfg.config_dirs) cur_dir = Path.cwd() LOG.debug("Testing existence of current dir {!r}.".format(cur_dir)) self.assertIn(cur_dir, cfg.config_dirs) LOG.debug("Testing existence of config dir {!r}.".format( str(self.test_cfg_dir))) self.assertIn(self.test_cfg_dir, cfg.config_dirs)
def test_read_cfg_files(self): if self.verbose == 1: print() LOG.info("Testing reading of configuration files.") from fb_tools.multi_config import BaseMultiConfig cfg = BaseMultiConfig(appname=self.appname, config_dir=self.test_cfg_dir.name, additional_cfgdirs=self.test_cfg_dir, verbose=self.verbose) if self.verbose >= 2: LOG.debug("Current configuration directories:\n{}".format( pp(cfg.config_dirs))) cfg.read() if self.verbose > 1: LOG.debug("Read raw configs:\n" + pp(cfg.configs_raw))
def test_refresh_lockobject(self): LOG.info("Testing refreshing of a lock object.") from fb_tools.handler.lock import LockHandler locker = LockHandler( appname='test_lock', verbose=self.verbose, lockdir=self.lock_dir, ) try: LOG.debug("Creating lockfile %r ...", self.lock_file) lock = locker.create_lockfile(self.lock_basename) LOG.debug("Current ctime: %s" % (lock.ctime.isoformat(' '))) LOG.debug("Current mtime: %s" % (lock.mtime.isoformat(' '))) if self.verbose > 2: LOG.debug("Current lock object before refreshing:\n{}".format( pp(lock.as_dict()))) mtime1 = lock.stat().st_mtime LOG.debug("Sleeping two seconds ...") time.sleep(2) lock.refresh() LOG.debug("New mtime: %s" % (lock.mtime.isoformat(' '))) if self.verbose > 2: LOG.debug("Current lock object after refreshing:\n{}".format( pp(lock.as_dict()))) mtime2 = lock.stat().st_mtime tdiff = mtime2 - mtime1 LOG.debug( "Got a time difference between mtimes of %0.3f seconds." % (tdiff)) self.assertGreater(mtime2, mtime1) lock = None finally: LOG.debug("Removing lockfile %r ...", self.lock_file) locker.remove_lockfile(self.lock_basename)
def test_evaluation(self): if self.verbose == 1: print() LOG.info("Testing evaluation configuration.") from fb_tools.multi_config import BaseMultiConfig test_stem = 'test_multicfg-verbose' test_logfile = Path('/var/log/test-multiconfig.log') used_verbose = self.verbose if self.verbose > 3: used_verbose = 3 cfg = BaseMultiConfig(appname=self.appname, config_dir=self.test_cfg_dir.name, additional_cfgdirs=self.test_cfg_dir, verbose=used_verbose, append_appname_to_stems=False, additional_stems=test_stem) LOG.debug("Testing raising RuntimeError on unread configuration ...") with self.assertRaises(RuntimeError) as cm: cfg.eval() e = cm.exception LOG.info("{c} raised on unread configuration: {e}".format( c=e.__class__.__name__, e=e)) LOG.debug("Reading verbose level from configuration.") cfg.read() LOG.info('Read config:\n' + pp(cfg.cfg)) cfg.eval() LOG.debug("New debug level: {!r}.".format(cfg.verbose)) LOG.debug("Evaluated logfile: {!r}.".format(cfg.logfile)) self.assertEqual(cfg.verbose, 7) self.assertEqual(cfg.logfile, test_logfile)
def test_read_broken(self): if self.verbose == 1: print() LOG.info("Testing reading of broken configuration files.") from fb_tools.multi_config import BaseMultiConfig, MultiCfgParseError test_stems = ( 'test_multicfg-broken-ini', 'test_multicfg-broken-json', 'test_multicfg-broken-hjson', 'test_multicfg-broken-yaml', 'test_multicfg-broken-toml', ) for stem in test_stems: if self.verbose: print() LOG.info("Testing for file stem {!r} ...".format(stem)) with self.assertRaises(MultiCfgParseError) as cm: cfg = BaseMultiConfig(appname=self.appname, config_dir=self.test_cfg_dir.name, additional_cfgdirs=self.test_cfg_dir, verbose=self.verbose, append_appname_to_stems=False, additional_stems=stem) cfg.read() LOG.info('Read config:\n' + pp(cfg.cfg)) e = cm.exception LOG.info("{c} raised on stem {s!r}: {e}".format( c=e.__class__.__name__, s=stem, e=e))
def test_init_stems(self): if self.verbose == 1: print() LOG.info("Testing init of configuration file stems.") valid_stems = [ 'uhu', ('bla', 'blub'), b'banane', ['item0', 'item1'], Path('p0'), ] valid_stems.append(('a', b'b', Path('p1'))) invalid_stems = ( 1, 2.3, { 'uhu': 'banane' }, os.sep, str(Path('p0') / 'p1'), Path('uhu') / 'banane', ) from fb_tools.multi_config import BaseMultiConfig LOG.debug("Testing, whether appname is in file stems ...") cfg = BaseMultiConfig(appname=self.appname, config_dir='test', verbose=self.verbose) if self.verbose >= 2: LOG.debug("Initialized stems:\n{}".format(pp(cfg.stems))) if self.verbose > 1: LOG.debug("Checking for existence of stem {!r}.".format( self.appname)) self.assertIn(self.appname, cfg.stems) LOG.debug("Testing for valid stems ...") for stem in valid_stems: LOG.debug("Testing valid stem {s!r} ({c}).".format( s=stem, c=stem.__class__.__name__)) cfg = BaseMultiConfig( appname=self.appname, config_dir='test', additional_stems=stem, verbose=self.verbose, ) if self.verbose >= 2: LOG.debug("Initialized stems:\n{}".format(pp(cfg.stems))) if is_sequence(stem): for st in stem: item = str(to_str(st)) if self.verbose > 1: LOG.debug( "Checking for existence of stem {!r}.".format( item)) self.assertIn(item, cfg.stems) else: item = str(to_str(stem)) if self.verbose > 1: LOG.debug( "Checking for existence of stem {!r}.".format(item)) self.assertIn(item, cfg.stems) for stem in invalid_stems: LOG.debug("Testing invalid stem {s!r} ({c}).".format( s=stem, c=stem.__class__.__name__)) with self.assertRaises((TypeError, ValueError)) as cm: cfg = BaseMultiConfig( appname=self.appname, config_dir='test', additional_stems=stem, verbose=self.verbose, ) e = cm.exception LOG.debug("{c} raised on stem {s!r}: {e}".format( c=e.__class__.__name__, s=stem, e=e))