def test_setListValue_remove_leftovers_tuple(self): cfg = configfile.ConfigFile() cfg.dict = {'fff.size': '5', 'fff.1.value': 'foo', 'fff.1.type': '11', 'fff.1.enabled': 'true', 'fff.2.value': 'bar', 'fff.2.type': '22', 'fff.2.enabled': 'false', 'fff.3.value': 'baz', 'fff.3.type': '33', 'fff.3.enabled': 'true', 'fff.4.value': 'boom', 'fff.4.type': '44', 'fff.4.enabled': 'true', 'fff.5.value': 'bam', 'fff.5.type': '55', 'fff.5.enabled': 'true'} cfg.setListValue('fff', ('str:value', 'int:type', 'bool:enabled'), [('foo', 11, True), ('bar', 22), ('baz',)]) self.assertDictEqual(cfg.dict, {'fff.size': '3', 'fff.1.value': 'foo', 'fff.1.type': '11', 'fff.1.enabled': 'true', 'fff.2.value': 'bar', 'fff.2.type': '22', 'fff.3.value': 'baz'})
def test_setListValue_str(self): cfg = configfile.ConfigFile() cfg.setListValue('bbb', 'str:value', ['foo', 'bar', 'baz']) self.assertDictEqual(cfg.dict, {'bbb.size': '3', 'bbb.1.value': 'foo', 'bbb.2.value': 'bar', 'bbb.3.value': 'baz'})
def test_listValue_zero_count(self): cfg = configfile.ConfigFile() cfg.dict = {'ccc.size': '2', 'ccc.0.value': 'foo', 'ccc.1.value': 'bar', 'ccc.2.value': 'baz'} self.assertListEqual(cfg.listValue('ccc', 'str:value'), ['bar', 'baz'])
def test_boolValue(self): cfg = configfile.ConfigFile() cfg.dict = {'foo': 'true', 'bar': '1', 'baz': 'false', 'bla': '0'} self.assertEqual(cfg.boolValue('foo', False), True) self.assertEqual(cfg.boolValue('bar', False), True) self.assertEqual(cfg.boolValue('baz', True), False) self.assertEqual(cfg.boolValue('bla', True), False)
def test_listValue_str(self): cfg = configfile.ConfigFile() cfg.dict = {'bbb.size': '3', 'bbb.1.value': 'foo', 'bbb.2.value': 'bar', 'bbb.3.value': 'baz'} self.assertListEqual(cfg.listValue('bbb', 'str:value'), ['foo', 'bar', 'baz'])
def test_setListValue_int(self): cfg = configfile.ConfigFile() cfg.setListValue('aaa', 'int:bla', [55, 66, 77]) self.assertDictEqual(cfg.dict, {'aaa.size': '3', 'aaa.1.bla': '55', 'aaa.2.bla': '66', 'aaa.3.bla': '77'})
def test_setListValue_invalid_type_for_type_key(self): cfg = configfile.ConfigFile() with self.assertRaises(TypeError): cfg.setListValue('aaa', 'non_existend_type:value', ['foo',]) with self.assertRaises(TypeError): cfg.setListValue('aaa', {'dict:value'}, ['foo',]) with self.assertRaises(TypeError): cfg.setListValue('aaa', 1, ['foo',])
def test_setListValue_bool(self): cfg = configfile.ConfigFile() cfg.setListValue('ccc', 'bool:foo', [True, False]) self.assertDictEqual(cfg.dict, { 'ccc.size': '2', 'ccc.1.foo': 'true', 'ccc.2.foo': 'false' })
def test_remove_keys_start_with(self): cfg = configfile.ConfigFile() cfg.dict = {'foo': 'true', 'bar': '1', 'baz': 'false', 'bla': '0'} cfg.removeKeysStartsWith('ba') self.assertDictEqual(cfg.dict, {'foo': 'true', 'bla': '0'})
def test_get_list_value_int(self): cfg = configfile.ConfigFile() cfg.dict = { 'aaa.size': '3', 'aaa.1.bla': '55', 'aaa.2.bla': '66', 'aaa.3.bla': '77' } self.assertListEqual(cfg.get_list_value('aaa', 'int:bla'), [55, 66, 77])
def test_remove_keys_start_with_not_matching_prefix(self): cfg = configfile.ConfigFile() cfg.dict = {'foo': 'true', 'bar': '1', 'baz': 'false', 'bla': '0'} cfg.remove_keys_starts_with('not_matching') self.assertDictEqual(cfg.dict, { 'foo': 'true', 'bar': '1', 'baz': 'false', 'bla': '0' })
def test_save(self): """ Saves the config file in the tmp direcory """ with NamedTemporaryFile() as cfgFile: cf = configfile.ConfigFile() self.assertTrue(cf.save(cfgFile.name)) self.assertTrue(os.path.exists(cfgFile.name)) self.assertFalse(cf.save('/foo'))
def test_setListValue_tuple_missing_values(self): cfg = configfile.ConfigFile() cfg.setListValue('ddd', ('str:value', 'int:type', 'bool:enabled'), [('foo', 11, True), ('bar', 22), ('baz',)]) self.assertDictEqual(cfg.dict, {'ddd.size': '3', 'ddd.1.value': 'foo', 'ddd.1.type': '11', 'ddd.1.enabled': 'true', 'ddd.2.value': 'bar', 'ddd.2.type': '22', 'ddd.3.value': 'baz'})
def load_env(f): env = os.environ.copy() env_file = configfile.ConfigFile() env_file.load(f, maxsplit=1) for key in env_file.get_keys(): value = env_file.get_str_value(key) if not value: continue if not key in list(env.keys()): os.environ[key] = value del (env_file)
def test_listValue_wrong_size(self): """ Don't include empty values if size is wrong. Bug #521 https://github.com/bit-team/backintime/issues/521 """ cfg = configfile.ConfigFile() cfg.dict = {'bbb.size': '4', 'bbb.1.value': 'foo', 'bbb.2.value': 'bar', 'bbb.3.value': 'baz'} self.assertListEqual(cfg.listValue('bbb', 'str:value'), ['foo', 'bar', 'baz'])
def test_save(self): """ Saves the config file in the tmp directory """ with NamedTemporaryFile() as cfgFile: cf = configfile.ConfigFile() self.assertTrue(cf.save(cfgFile.name)) self.assertExists(cfgFile.name) #test fail routine in ConfigFile.save with NamedTemporaryFile() as fakeDir: self.assertFalse(cf.save(os.path.join(fakeDir.name, 'foo')))
def test_listValue_invalid_type(self): cfg = configfile.ConfigFile() cfg.dict = {'aaa.size': '3', 'aaa.1.value': '55', 'aaa.2.value': '66', 'aaa.3.value': '77'} with self.assertRaises(TypeError): cfg.listValue('aaa', 'non_existend_type:value') with self.assertRaises(TypeError): cfg.listValue('aaa', {'dict:value'}) with self.assertRaises(TypeError): cfg.listValue('aaa', 1)
def test_listValue_missing_values(self): """ Don't include missing values. Bug #521 https://github.com/bit-team/backintime/issues/521 """ cfg = configfile.ConfigFile() cfg.dict = {'ddd.size': '4', 'ddd.1.value': 'foo', 'ddd.2.value': 'bar', 'ddd.4.value': 'baz'} self.assertListEqual(cfg.listValue('ddd', 'str:value'), ['foo', 'bar', 'baz'])
def test_setListValue_remove_leftovers(self): cfg = configfile.ConfigFile() cfg.dict = {'eee.size': '5', 'eee.1.bla': '55', 'eee.2.bla': '66', 'eee.3.bla': '77', 'eee.4.bla': '88', 'eee.5.bla': '99'} cfg.setListValue('eee', 'int:bla', [55, 66, 77]) self.assertDictEqual(cfg.dict, {'eee.size': '3', 'eee.1.bla': '55', 'eee.2.bla': '66', 'eee.3.bla': '77'})
def test_load(self): """ ConfigFile should be able to load its content from a previously saved ConfigFile object. """ with NamedTemporaryFile() as cfgFile: original_cf = configfile.ConfigFile() key = "config_key" value = "config_value" original_cf.setStrValue(key, value) original_cf.save(cfgFile.name) cf = configfile.ConfigFile() cf.load(cfgFile.name) self.assertEqual(len(cf.keys()), len(original_cf.keys())) for k in original_cf.keys(): with self.subTest(k = k): #workaround for py.test3 2.5.1 doesn't support subTest msg = 'k = %s' %k self.assertTrue(cf.hasKey(k), msg) self.assertEqual(original_cf.strValue(k), cf.strValue(k))
def test_listValue_tuple(self): cfg = configfile.ConfigFile() cfg.dict = {'ddd.size': '3', 'ddd.1.value': 'foo', 'ddd.1.type': '11', 'ddd.1.enabled': 'true', 'ddd.2.value': 'bar', 'ddd.2.type': '22', 'ddd.2.enabled': 'false', 'ddd.3.value': 'baz', 'ddd.3.type': '33', 'ddd.3.enabled': 'true'} self.assertListEqual(cfg.listValue('ddd', ('str:value', 'int:type', 'bool:enabled')), [('foo', 11, True), ('bar', 22, False), ('baz', 33, True)])
def run(self): """ wait for password request on FIFO and answer with password from self.db through FIFO. """ info = configfile.ConfigFile() info.setIntValue('version', self.PW_CACHE_VERSION) info.save(self.config.passwordCacheInfo()) os.chmod(self.config.passwordCacheInfo(), 0o600) logger.debug('Keyring supported: %s' % self.keyringSupported, self) tools.envSave(self.config.cronEnvFile()) if not self.collectPasswords(): logger.debug('Nothing to cache. Quit.', self) sys.exit(0) self.fifo.create() atexit.register(self.fifo.delfifo) signal.signal(signal.SIGHUP, self.reloadHandler) logger.debug('Start loop', self) while True: try: request = self.fifo.read() request = request.split('\n')[0] task, value = request.split(':', 1) if task == 'get_pw': key = value if key in list(self.dbKeyring.keys()): answer = 'pw:' + self.dbKeyring[key] elif key in list(self.dbUsr.keys()): answer = 'pw:' + self.dbUsr[key] else: answer = 'none:' self.fifo.write(answer, 5) elif task == 'set_pw': key, value = value.split(':', 1) self.dbUsr[key] = value except IOError as e: logger.error('Error in writing answer to FIFO: %s' % str(e), self) except KeyboardInterrupt: logger.debug('Quit.', self) break except Timeout: logger.error('FIFO timeout', self) except Exception as e: logger.error('ERROR: %s' % str(e), self)
def test_get_list_value_tuple_missing_values(self): cfg = configfile.ConfigFile() cfg.dict = { 'eee.size': '3', 'eee.1.value': 'foo', 'eee.1.enabled': 'true', 'eee.2.type': '22', 'eee.2.enabled': 'false', 'eee.3.value': 'baz', 'eee.3.type': '33' } self.assertListEqual( cfg.get_list_value('eee', ('str:value', 'int:type', 'bool:enabled')), [('foo', 0, True), ('', 22, False), ('baz', 33, False)])
def test_remapKeyRegex(self): cfg = configfile.ConfigFile() cfg.dict = {'asdf.foo.qwertz': '123', 'foo.qwertz': '456', 'asdf.foo': '789', 'yxcv': 'jkl'} cfg.remapKeyRegex('foo', 'bar') self.assertEqual(cfg.strValue('asdf.bar.qwertz', 'WrongValue'), '123') self.assertEqual(cfg.strValue('bar.qwertz', 'WrongValue'), '456') self.assertEqual(cfg.strValue('asdf.bar', 'WrongValue'), '789') self.assertEqual(cfg.strValue('asdf.foo.qwertz', ''), '') self.assertEqual(cfg.strValue('foo.qwertz', ''), '') self.assertEqual(cfg.strValue('asdf.foo', ''), '') self.assertEqual(cfg.strValue('yxcv', 'WrongValue'), 'jkl')
def save_env(f): """ Save environ variables to file that are needed by cron to connect to keyring. This will only work if the user is logged in. """ env = os.environ.copy() env_file = configfile.ConfigFile() for i in ('GNOME_KEYRING_CONTROL', 'DBUS_SESSION_BUS_ADDRESS', \ 'DBUS_SESSION_BUS_PID', 'DBUS_SESSION_BUS_WINDOWID', \ 'DISPLAY', 'XAUTHORITY', 'GNOME_DESKTOP_SESSION_ID', \ 'KDE_FULL_SESSION'): set_env_key(env, env_file, i) env_file.save(f) del (env_file)
def test_envLoad_without_previous_values(self): test_env = configfile.ConfigFile() test_env.setStrValue('FOO', 'bar') test_env.setStrValue('ASDF', 'qwertz') test_env.save(self.temp_file) #make sure environ is clean self.assertNotIn('FOO', os.environ) self.assertNotIn('ASDF', os.environ) tools.envLoad(self.temp_file) self.assertIn('FOO', os.environ) self.assertIn('ASDF', os.environ) self.assertEqual(os.environ['FOO'], 'bar') self.assertEqual(os.environ['ASDF'], 'qwertz')
def test_envLoad_do_not_overwrite_previous_values(self): test_env = configfile.ConfigFile() test_env.setStrValue('FOO', 'bar') test_env.setStrValue('ASDF', 'qwertz') test_env.save(self.temp_file) #add some environ vars that should not get overwritten os.environ['FOO'] = 'defaultFOO' os.environ['ASDF'] = 'defaultASDF' tools.envLoad(self.temp_file) self.assertIn('FOO', os.environ) self.assertIn('ASDF', os.environ) self.assertEqual(os.environ['FOO'], 'defaultFOO') self.assertEqual(os.environ['ASDF'], 'defaultASDF')
def test_listValue_tuple_missing_values(self): """ Don't include missing values. Bug #521 https://github.com/bit-team/backintime/issues/521 """ cfg = configfile.ConfigFile() cfg.dict = {'eee.size': '3', 'eee.1.value': 'foo', 'eee.1.enabled': 'true', 'eee.2.type': '22', 'eee.2.enabled': 'false', 'eee.3.value': 'baz', 'eee.3.type': '33'} self.assertListEqual(cfg.listValue('eee', ('str:value', 'int:type', 'bool:enabled')), [('foo', 0, True), ('baz', 33, False)])
def getRemoteServers(): retVal = list() try: bslProxy = os.environ['BSLPROXY'] if bslProxy is not None and bslProxy != '': retVal.append(bslProxy) except: pass try: cfg = configfile.ConfigFile("remoteServers.cfg") cfg.load() retVal += cfg.getCfgValueAsList("remoteServers") except: pass return retVal
def test_remapKey(self): cfg = configfile.ConfigFile() cfg.dict = {'foo': '123', 'bar': '456'} #old key not in dict cfg.remapKey('notExistedKey', 'baz') self.assertEqual(cfg.strValue('foo'), '123') self.assertEqual(cfg.strValue('bar'), '456') #valid remap cfg.remapKey('foo', 'baz') self.assertEqual(cfg.strValue('foo'), '') self.assertEqual(cfg.strValue('baz'), '123') self.assertEqual(cfg.strValue('bar'), '456') #do not overwrite existing keys cfg.remapKey('baz', 'bar') self.assertEqual(cfg.strValue('baz'), '') self.assertEqual(cfg.strValue('bar'), '456')