def test_properties(self): config = kdb.KeySet(1, kdb.Key("system/module", "without config")) spec = kdb.tools.PluginSpec("somename", config) self.assertEqual(spec.name, "somename") self.assertEqual(spec.fullname, "somename#somename") self.assertEqual(spec.refName, "somename") self.assertEqual(spec.config, config) self.assertFalse(spec.refNumber) spec.name = "other" self.assertEqual(spec.name, "other") spec.refName = "myref" self.assertEqual(spec.refName, "myref") spec.fullname = "fullname" self.assertEqual(spec.fullname, "fullname#myref") spec.refNumber = 5 self.assertTrue(spec.refNumber) config.append(kdb.Key("system/modules/2")) self.assertNotEqual(spec.config, config) spec.config = config self.assertEqual(spec.config, config) with self.assertRaises(kdb.tools.BadPluginName): spec.fullname = "this is invalid"
def test_helpers(self): with self.assertRaises(ValueError): kdb.Key("user:/noarray").array_elements() parts = kdb.Key("user:/some/array/#_12").array_elements() self.assertEqual(parts.index, 12) self.assertEqual(parts.name, "user:/some/array") self.assertEqual(parts.basename, "array")
def test_properties(self): self.assertEqual(self.key.name, "user/foo/bar") self.assertEqual(self.key.value, "value") self.assertEqual(self.key.basename, "bar") self.assertEqual(self.key.fullname, "user:myowner/foo/bar") self.assertEqual(self.bkey.name, "system/bkey") self.assertEqual(self.bkey.value, b"bvalue\0\0") self.assertEqual(self.bkey.basename, "bkey") self.assertEqual(self.bkey.fullname, "system/bkey") k = kdb.Key("user/key1", kdb.KEY_VALUE, "value") self.assertFalse(k.isBinary()) self.assertIsNone(k.getMeta("binary")) k.name = "system/key2" k.basename = "key3" k.value = b"bvalue\0\0" self.assertEqual(k.name, "system/key3") self.assertEqual(k.value, b"bvalue\0\0") self.assertTrue(k.isBinary()) self.assertIsInstance(self.bkey.getMeta("binary"), kdb.Key) k = kdb.Key("user/key2") with self.assertRaises(kdb.KeyInvalidName): k.name = "foo"
def setUp(self): self.key = kdb.Key("user/foo/bar", kdb.KEY_VALUE, "value", kdb.KEY_META, "by", "manuel", kdb.KEY_META, "owner", "myowner") self.bkey = kdb.Key("system/bkey", kdb.KEY_VALUE, b"bvalue\0\0", kdb.KEY_END, kdb.KEY_META, "lost", "lost")
def get(self, returned, parentKey): mod = "system/elektra/modules/python" if parentKey.name == mod: returned.append(kdb.Key(mod, kdb.KEY_VALUE, "contract below")) returned.append(kdb.Key(mod+"/infos", kdb.KEY_VALUE, "contract below")) returned.append(kdb.Key(mod+"/infos/placements", kdb.KEY_VALUE, "postgetstorage presetstorage")) return 1 return 1
def test_python_head(self): ks = kdb.KeySet(0) ks.append(kdb.Key("user:/foo")) self.assertEqual(ks.head(), ks[0]) ks.append(kdb.Key("user:/bar")) self.assertEqual(ks.head(), ks[0]) ks.remove("user:/foo") self.assertEqual(ks.head(), ks[0])
def test_ensure(self): with kdb.KDB() as db: ks = kdb.KeySet() k = kdb.Key("system:/elektra/ensure/plugins/global/gopts", kdb.KEY_VALUE, "mounted") parent = kdb.Key("system:/elektra") rc = db.ensure(ks, parent) self.assertEqual(rc, 0) db.get(ks, parent)
def test_functions(self): self.assertTrue(self.key.isUser()) self.assertTrue(self.bkey.isSystem()) self.assertTrue(self.key.isString()) self.assertTrue(self.bkey.isBinary()) self.assertTrue(self.key.isBelow(kdb.Key("user/foo"))) k = kdb.Key("user/key1", kdb.KEY_VALUE, "value") self.assertEqual(k.get(), "value")
def setUp(self): self.ks = kdb.KeySet(100, kdb.Key("system:/key1"), kdb.Key("system:/key2"), kdb.Key("user:/key3"), kdb.Key("user:/key4"), kdb.KS_END, kdb.Key("user:/lost") )
def test_functions(self): self.assertTrue(self.key.isUser()) self.assertTrue(self.bkey.isSystem()) self.assertTrue(self.key.isString()) self.assertTrue(self.bkey.isBinary()) self.assertTrue(self.key.isBelow(kdb.Key("user:/foo"))) self.assertFalse(self.key.isNameLocked()) self.assertFalse(self.key.isValueLocked()) self.assertFalse(self.key.isMetaLocked()) k = kdb.Key("user:/key1", kdb.KEY_VALUE, "value") self.assertEqual(k.get(), "value") k.set(b"bvalue\0\0") self.assertEqual(k.get(), b"bvalue\0\0")
def test_operator(self): self.assertFalse(self.ks == kdb.KeySet(0)) self.assertEqual(len(self.ks), 4) self.assertEqual(len(kdb.KeySet(0)), 0) self.assertTrue(kdb.Key("user:/key3") in self.ks) self.assertFalse(kdb.Key("user:/foo") in self.ks) self.assertEqual(self.ks[0], kdb.Key("user:/key3")) self.assertEqual(self.ks[-1], kdb.Key("system:/key2")) with self.assertRaises(IndexError): self.assertIsNone(self.ks[100]) with self.assertRaises(IndexError): self.assertIsNone(self.ks[-100]) self.assertEqual(self.ks[1:3], [ self.ks[1], self.ks[2] ]) self.assertEqual(self.ks["user:/key3"], kdb.Key("user:/key3")) with self.assertRaises(KeyError): self.assertIsNone(self.ks["user:/doesnt_exist"]) self.assertEqual(self.ks[kdb.Key("system:/key2")], kdb.Key("system:/key2")) with self.assertRaises(KeyError): self.ks[kdb.Key("user:/doesnt_exist")] self.assertEqual(str(self.ks), "['user:/key3', 'user:/key4', 'system:/key1', 'system:/key2']") self.assertEqual(repr(self.ks), "kdb.KeySet(4, kdb.Key('user:/key3'), kdb.Key('user:/key4'), kdb.Key('system:/key1'), kdb.Key('system:/key2'))") self.assertIsInstance(hash(self.ks[0]), int) self.assertTrue(self.ks[0].isNameLocked()) self.assertFalse(self.ks[0].isValueLocked()) self.assertFalse(self.ks[0].isMetaLocked())
def ls(os_path): path = os_path_to_elektra_path(os_path) root = kdb.Key(path) is_root_level = len(path) > 1 and path.endswith("/") # special case with _get_kdb_instance() as db: ks = kdb.KeySet() db.get(ks, root) #only retain keys that are below the root (kdb.get does not gurantee this property) ks_filtered = kdb.KeySet() for key in ks: if key.isBelowOrSame(root): ks_filtered.append(key) path_without_namespace = _remove_namespace_prefix(path) result_keys_without_namespace = map(_remove_namespace_prefix, ks_filtered.unpack_names()) below = {name.split(path_without_namespace)[1] for name in result_keys_without_namespace if is_path_prefix(path_without_namespace, name)} dirs = {name.split("/")[0 if is_root_level else 1] for name in below if "/" in name} files = {name for name in below if not "/" in name}.difference(dirs) if '' in files: files.remove('') files.add(dir_file_special_name) return (dirs, files)
def tearDownClass(cls): # cleanup with kdb.KDB() as db: ks = kdb.KeySet(100) db.get(ks, TEST_NS) ks.cut(kdb.Key(TEST_NS)) db.set(ks, TEST_NS)
def patched_get(ks, orig_root): justified_root = re.sub("^proc:/", "/", str(orig_root)) status = orig_get(ks, justified_root) if kdb.Key(orig_root).isCascading(): for key_to_remove in ks.filter(lambda key: key.isSpec()): ks.remove(key_to_remove) return status
def test_helpers(self): self.assertEqual( self.ks.unpack_names(), set(['system:/key1', 'system:/key2', 'user:/key3', 'user:/key4'])) self.assertEqual(self.ks.unpack_basenames(), set(['key1', 'key2', 'key3', 'key4'])) self.assertEqual(self.ks.filter_below(kdb.Key('user:/')), kdb.KeySet(2, self.ks[0], self.ks[1]))
def setUp(self): self.parent_key = kdb.Key("user:/python") self.plugin = dns_plugin.ElektraPlugin() self.localhost_key_with_plugin = kdb.Key("user:/python/hostname", kdb.KEY_VALUE, "localhost", kdb.KEY_META, "check/dns", "") self.valid_key_with_plugin = kdb.Key("user:/python/hostname", kdb.KEY_VALUE, VALID_DOMAIN, kdb.KEY_META, "check/dns", "") self.invalid_key_with_plugin = kdb.Key("user:/python/hostname", kdb.KEY_VALUE, INVALID_DOMAIN, kdb.KEY_META, "check/dns", "") self.valid_key_without_plugin = kdb.Key("user:/foo/bar", kdb.KEY_VALUE, "val") self.invalid_ks = kdb.KeySet(10, self.invalid_key_with_plugin, self.valid_key_without_plugin, kdb.KS_END) self.valid_ks = kdb.KeySet(10, self.valid_key_with_plugin, self.valid_key_without_plugin, kdb.KS_END) self.localhost_ks = kdb.KeySet(10, self.localhost_key_with_plugin, kdb.KS_END)
def test_ctor(self): self.assertIsInstance(self.key, kdb.Key) self.assertIsInstance(self.bkey, kdb.Key) k = kdb.Key("/cascading/key") self.assertIsInstance(k, kdb.Key) self.assertTrue(k.isValid()) k = kdb.Key("spec/key") self.assertIsInstance(k, kdb.Key) self.assertTrue(k.isValid()) k = kdb.Key("proc/key") self.assertIsInstance(k, kdb.Key) self.assertTrue(k.isValid()) k = kdb.Key("dir/key") self.assertIsInstance(k, kdb.Key) self.assertTrue(k.isValid()) k = kdb.Key("user/key") self.assertIsInstance(k, kdb.Key) self.assertTrue(k.isValid()) k = kdb.Key("system/key") self.assertIsInstance(k, kdb.Key) self.assertTrue(k.isValid()) k = kdb.Key() self.assertIsInstance(k, kdb.Key) self.assertFalse(k.isValid()) k = kdb.Key("wrongname") self.assertIsInstance(k, kdb.Key) self.assertFalse(k.isValid()) k = kdb.Key("user/foo") self.assertIsInstance(k, kdb.Key) self.assertTrue(k.isValid()) k = kdb.Key(self.key) self.assertIsInstance(k, kdb.Key) self.assertTrue(k.isValid())
def setupSpec(): with kdb.KDB() as db: ks = kdb.KeySet(10) db.get(ks, spec_base_key) if len(ks.cut(kdb.Key(spec_base_key))) > 0: print("ERROR: Couldn't setup spec, keys exist!", file=sys.stderr) exit(1) ks.extend(spec) db.set(ks, spec_base_key)
def create_key(os_path): path = os_path_to_elektra_path(os_path) with _get_kdb_instance() as db: ks = kdb.KeySet() db.get(ks, path) if not path in ks: key = kdb.Key(path) ks.append(key) keys_modified = db.set(ks, path) if keys_modified != 1: raise OSError(errno.EIO)
def test_load(self): modules = kdb.tools.Modules() config = kdb.KeySet(1, kdb.Key("system:/module", "without config")) spec = kdb.tools.PluginSpec("dump", config) plugin = modules.load(spec) self.assertIsInstance(plugin, kdb.tools.Plugin) self.assertEqual(plugin.name, "dump") self.assertEqual(config, plugin.getConfig()) with self.assertRaises(kdb.tools.NoPlugin): modules.load(kdb.tools.PluginSpec("does_not_exist"))
def get(self, returned, parentKey): print("[CLASS-PYTHON-C] get") mod = "system:/elektra/modules/python" if parentKey.name == mod: returned.append(kdb.Key(mod, kdb.KEY_VALUE, "contract below")) returned.append( kdb.Key(mod + "/infos", kdb.KEY_VALUE, "contract below")) returned.append( kdb.Key(mod + "/infos/provides", kdb.KEY_VALUE, "storage")) returned.append( kdb.Key(mod + "/infos/placements", kdb.KEY_VALUE, "getstorage setstorage")) return 1 config = configparser.ConfigParser() config.readfp(open(parentKey.value)) for s in config.sections(): for o in config.options(s): returned.append( kdb.Key(parentKey.name + "/" + s + "/" + o, kdb.KEY_VALUE, config.get(s, o))) return 1
def test_spec_reader(self): spec = kdb.KeySet( 1, kdb.Key("spec:/example/xx", {"mountpoint": "/example/xx"})) sr = kdb.tools.SpecReader() sr.readSpecification(spec) backends = sr.getBackends() self.assertIsInstance(backends, kdb.tools.SpecBackendBuilderMap) self.assertEqual(len(backends), 1) self.assertIsInstance(backends[spec[0]], kdb.tools.SpecBackendBuilder) self.assertEqual(backends.keys(), [k for k in spec]) self.assertIsInstance(backends.values()[0], kdb.tools.SpecBackendBuilder)
def main(): parser = argparse.ArgumentParser() parser.add_argument('mountpoint') parser.add_argument('-f', '--foreground', default=False) parser.add_argument('-p', '--parent_key', default="/") parser.add_argument('-l', '--logger', default="stdout", choices=["syslog", "stdout", "none"]) parser.add_argument( '-ll', '--loglevel', default="DEBUG", choices=["INFO", "DEBUG", "ERROR", "CRITICAL", "FATAL", "WARN"]) parser.add_argument('-a', '--allow-other', default=True) parser.add_argument('-nt', '--nothreads', default=True) args = parser.parse_args() #validate parent_key try: parent_key = kdb.Key(args.parent_key) if not parent_key.isValid() or not parent_key.isCascading(): raise NameError elektra_util.parent_key = parent_key except kdb.kdb.KeyInvalidName: raise NameError except NameError: print("parent_key needs to be a valid key in the cascading namespace", file=sys.stderr) sys.exit("1") #configure logging logging.basicConfig(level=getattr(logging, args.loglevel)) logger = logging.getLogger() if args.logger == "syslog": logger.addHandler(logging.handlers.SysLogHandler(address='/dev/log')) elif args.logger == "none": logger.propagate = False elif args.logger == "stdout": pass FUSE(RootlevelResolver(args.mountpoint), args.mountpoint, foreground=args.foreground, allow_other=args.allow_other, nothreads=args.nothreads)
def test_meta(self): self.assertIsInstance(self.key.getMeta("owner"), kdb.Key) self.assertEqual(self.key.getMeta("owner").name, "meta:/owner") self.assertEqual(self.key.getMeta("owner").value, "myowner") self.assertEqual(self.key.getMeta("by").value, "manuel") self.assertTrue(self.key.getMeta("by").isNameLocked()) self.assertTrue(self.key.getMeta("by").isValueLocked()) self.assertTrue(self.key.getMeta("by").isMetaLocked()) self.assertFalse(self.key.hasMeta("doesnt_exist")) self.assertIsNone(self.key.getMeta("doesnt_exist")) self.assertTrue(bool(self.bkey.getMeta("binary"))) self.assertIsNone(self.bkey.getMeta("owner")) k = kdb.Key("user:/key1") k.setMeta("foo", "bar") self.assertEqual(k.getMeta("foo").value, "bar") k = kdb.Key("user:/key1", { "foo2": "bar2", "foo3": "bar3" }) self.assertEqual(k.getMeta("foo2").value, "bar2") self.assertEqual(k.getMeta("foo3").value, "bar3") self.assertEqual(sum(1 for _ in self.key.getMeta()), 2) self.assertEqual(sum(1 for _ in self.bkey.getMeta()), 1)
def elektraSet(mountpoint, keyset, keeporder): with kdb.KDB() as db: ks = kdb.KeySet(0) rc = 0 try: rc = db.get(ks, mountpoint) except kdb.KDBException as e: raise ElektraReadException("KDB.get failed: {}".format(e)) if rc == -1: raise ElektraReadException( "Failed to read keyset below {}".format(mountpoint)) flattenedKeyset = flatten_json(keyset) i = 0 for name, value in flattenedKeyset.items(): key = None kname = None try: key = ks[mountpoint + "/" + name] if keeporder and key.hasMeta("order"): i = int((key.getMeta("order").value)) + 1 if keeporder: key.setMeta("order", str(i)) i += 1 except KeyError: key = kdb.Key(mountpoint + "/" + name) if keeporder: key.setMeta("order", str(i)) i += 1 ks.append(key) if isinstance(value, dict): for sname, svalue in value.items(): if sname == 'value': key.value = svalue elif sname == 'meta': for mname, mvalue in svalue.items(): key.setMeta(mname, str(mvalue)) else: key.value = value try: rc = db.set(ks, mountpoint) except kdb.KDBException as e: raise ElektraWriteException("KDB.set failed: {}".format(e)) if rc == -1: raise ElektraWriteException( "Failed to write keyset to {}".format(mountpoint)) return rc
def test_meta(self): self.assertIsInstance(self.key.getMeta("owner"), kdb.Key) self.assertEqual(self.key.getMeta("owner").name, "owner") self.assertEqual(self.key.getMeta("owner").value, "myowner") self.assertEqual(self.key.getMeta("by").value, "manuel") self.assertFalse(self.key.hasMeta("doesnt_exist")) self.assertIsNone(self.key.getMeta("doesnt_exist")) self.assertTrue(bool(self.bkey.getMeta("binary"))) self.assertIsNone(self.bkey.getMeta("owner")) k = kdb.Key("user/key1") k.setMeta("foo", "bar") self.assertEqual(k.getMeta("foo").value, "bar") self.assertEqual(sum(1 for _ in self.key.getMeta()), 2) self.assertEqual(sum(1 for _ in self.bkey.getMeta()), 1)
def test_set(self): with kdb.KDB() as db: ks = kdb.KeySet(100) db.get(ks, TEST_NS) try: key = ks[TEST_NS + "/mykey"] except KeyError: key = kdb.Key(TEST_NS + "/mykey") ks.append(key) key.value = "new_value" db.set(ks, TEST_NS) with kdb.KDB() as db: ks = kdb.KeySet(100) db.get(ks, TEST_NS) self.assertEqual(ks[TEST_NS + "/mykey"].value, "new_value")
def test_operator(self): self.assertEqual(len(self.ks), 4) self.assertEqual(len(kdb.KeySet(0)), 0) self.assertTrue(kdb.Key("user/key3") in self.ks) self.assertFalse(kdb.Key("user/foo") in self.ks) self.assertEqual(self.ks[0], kdb.Key("system/key1")) self.assertEqual(self.ks[-1], kdb.Key("user/key4")) with self.assertRaises(IndexError): self.assertIsNone(self.ks[100]) with self.assertRaises(IndexError): self.assertIsNone(self.ks[-100]) self.assertEqual(self.ks[1:3], [ self.ks[1], self.ks[2] ]) self.assertEqual(self.ks["user/key3"], kdb.Key("user/key3")) with self.assertRaises(KeyError): self.assertIsNone(self.ks["user/doesnt_exist"]) self.assertEqual(self.ks[kdb.Key("system/key2")], kdb.Key("system/key2")) with self.assertRaises(KeyError): self.ks[kdb.Key("user/doesnt_exist")]
def elektraUmount(mountpoint): with kdb.KDB() as db: ks = kdb.KeySet(0) mountpoints = "system/elektra/mountpoints" rc = 0 try: rc = db.get(ks, mountpoints) except kdb.KDBException as e: raise ElektraReadException("KDB.get failed: {}".format(e)) if rc != 1: raise ElektraUmountException( "Failed to fetch elektra facts: failed to read system/elektra/mountpoints." ) key = kdb.Key() key.name = mountpoints + '/' + mountpoint.replace('/', '\/') ks.cut(key) try: rc = db.set(ks, mountpoints) except kdb.KDBException as e: raise ElektraWriteException("KDB.set failed: {}".format(e)) if rc != 1: raise ElektraUmountException("Failed to umount " + key.name)
def test_gopts(self): try: setupSpec() custom_argv = ["test", "get", "-v", "user:/"] custom_envp = [] config = kdb.KeySet(0) contract = kdb.KeySet(0) kdb.goptsContract(contract, custom_argv, custom_envp, kdb.Key(base_key), config) with kdb.KDB(contract) as db: ks = kdb.KeySet(0) db.get(ks, base_key) self.assertTrue((base_key in ks)) self.assertEqual(ks[base_key].value, "getter") self.assertEqual(ks[base_key + "/getter/keyname"].value, "user:/") self.assertTrue((base_key + "/getter/verbose" in ks)) self.assertEqual(ks[base_key + "/getter/verbose"].value, "1") finally: removeSpec()