def _load_config(self, configfile): config = Config() if not configfile: return config configpath = Path(configfile) try: if not configpath.expanduser().resolve().exists(): logger.debug('Ignoring non existing config file %s', configfile) return config except FileNotFoundError: # we are on python 3.5 and Path.resolve raised a FileNotFoundError logger.debug('Ignoring non existing config file %s', configfile) return config try: config.load(configpath) logger.debug('Loaded config %s', configfile) except Exception as e: # pylint: disable=broad-except raise RuntimeError( 'Error while parsing config file {config}. Error was ' '{message}'.format(config=configfile, message=e)) from None return config
def test_load_with_non_existing_configfile(self): test_config_path = __here__ / 'foo.cfg' self.assertFalse(test_config_path.is_file()) config = Config() with self.assertRaises(FileNotFoundError): config.load(test_config_path)
def test_load_auth(self): root = logging.getLogger(name) root.disabled = True test_config_path = __here__ / 'test_auth.cfg' self.assertTrue(test_config_path.is_file()) config = Config() config.load(test_config_path) self.assertEqual(config.get('gmp', 'username'), 'foo') self.assertEqual(config.get('gmp', 'password'), 'bar') root.disabled = False
def test_config_defaults(self): config = Config() self.assertEqual(config.get('gmp', 'username'), '') self.assertEqual(config.get('gmp', 'password'), '') self.assertEqual(config.get('ssh', 'username'), 'gmp') self.assertEqual(config.get('ssh', 'password'), 'gmp') self.assertEqual(config.get('ssh', 'port'), DEFAULT_SSH_PORT) self.assertEqual(config.get('unixsocket', 'socketpath'), DEFAULT_UNIX_SOCKET_PATH) self.assertEqual(config.get('tls', 'port'), DEFAULT_GVM_PORT)
def test_load(self): test_config_path = __here__ / 'test.cfg' self.assertTrue(test_config_path.is_file()) config = Config() config.load(test_config_path) self.assertEqual(config.get('gmp', 'username'), 'bar') self.assertEqual(config.get('gmp', 'password'), 'bar') self.assertEqual(config.get('ssh', 'username'), 'ipsum') self.assertEqual(config.get('ssh', 'password'), 'lorem') self.assertEqual(config.get('ssh', 'port'), '123') self.assertEqual(config.get('unixsocket', 'socketpath'), '/foo/bar.sock') self.assertEqual(config.get('tls', 'port'), '123') self.assertEqual(config.get('tls', 'certfile'), 'foo.cert') self.assertEqual(config.get('tls', 'keyfile'), 'foo.key') self.assertEqual(config.get('tls', 'cafile'), 'foo.ca') self.assertDictEqual(config.defaults(), dict(timeout='1000', foo='bar', username='******'))
def test_get_unknown_setting(self): config = Config() self.assertIsNone(config.get('foo', 'bar'))