TEST_F (Simple, RemoveFile) { using namespace kdb; KDB kdb; KeySet ks; kdb.get (ks, testRoot); ks.append (Key ("system" + testRoot + "remove", KEY_END)); ASSERT_EQ (ks.size (), 1) << "could not append key\n" << ks; kdb.set (ks, testRoot); ASSERT_EQ (ks.size (), 1) << "key gone after kdb.set?\n" << ks; struct stat buf; ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), 0) << "found no file"; Key parentKey; kdb.close (parentKey); kdb.open (parentKey); kdb.get (ks, testRoot); ks.clear (); ASSERT_EQ (ks.size (), 0) << "keyset should be empty after clearing it\n" << ks; kdb.set (ks, testRoot); ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), -1) << "found wrong file"; }
TEST_F (Simple, SetNothing) { using namespace kdb; KDB kdb; KeySet ks; kdb.get (ks, testRoot); ASSERT_EQ (ks.size (), 0) << "got keys from freshly mounted backends" << ks; kdb.set (ks, testRoot); ASSERT_EQ (ks.size (), 0) << "got keys from freshly mounted backends" << ks; struct stat buf; ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), -1) << "found wrong file"; }
/** * @brief Read a YAML file and add the resulting data to a given key set * * @param mappings The key set where the YAML data will be stored * @param parent This key stores the path to the YAML data file that should be read */ void yamlcpp::yamlRead (KeySet & mappings, Key & parent) { YAML::Node config = YAML::LoadFile (parent.getString ()); #ifdef HAVE_LOGGER ostringstream data; data << config; ELEKTRA_LOG_DEBUG ("Read data “%s”", data.str ().c_str ()); #endif convertNodeToKeySet (config, mappings, parent); ELEKTRA_LOG_DEBUG ("Added %zd key%s", mappings.size (), mappings.size () == 1 ? "" : "s"); }
TEST_F(ThreeWayMergeTest, DeleteModifyConflict) { ours.lookup ("user/parento/config/key1", KDB_O_POP); theirs.lookup ("user/parentt/config/key1").setString ("modifiedvalue"); MergeResult result = merger.mergeKeySet (base, ours, theirs, mergeParent); EXPECT_TRUE(result.hasConflicts()) << "No conflict detected although conflicts should exist"; KeySet conflicts = result.getConflictSet (); ASSERT_EQ(1, conflicts.size())<< "Wrong number of conflicts"; testConflictMeta (conflicts.at (0), CONFLICT_DELETE, CONFLICT_MODIFY); KeySet merged = result.getMergedKeys (); EXPECT_EQ(4, merged.size ()); compareAllExceptKey1 (merged); }
TEST_F(ThreeWayMergeTest, SameMetaKeyModifyConflict) { ours.lookup ("user/parento/config/key1").setMeta<std::string> ("testmeta", "ourvalue"); theirs.lookup ("user/parentt/config/key1").setMeta<std::string> ("testmeta", "theirvalue"); MergeResult result = merger.mergeKeySet (base, ours, theirs, mergeParent); ASSERT_TRUE (result.hasConflicts())<< "No conflict detected although conflicts should exist"; KeySet conflicts = result.getConflictSet (); EXPECT_EQ(1, conflicts.size ()); testConflictMeta (conflicts.at (0), CONFLICT_META, CONFLICT_META); KeySet merged = result.getMergedKeys (); EXPECT_EQ(4, merged.size ()); compareAllExceptKey1 (merged); }
int LsCommand::execute (Cmdline const & cl) { checkArguments (cl); printWarnings (cerr, root); root = cl.createKey (0); kdb.get (ks, root); if (cl.verbose) cout << "size of all keys in mountpoint: " << ks.size () << endl; KeySet part (ks.cut (root)); if (cl.verbose) cout << "size of requested keys: " << part.size () << endl; cout.setf (std::ios_base::unitbuf); if (cl.null) { cout.unsetf (std::ios_base::skipws); } printResults (part, getDepth (root), cl); printWarnings (cerr, root); return 0; }
int LsCommand::execute (Cmdline const & cl) { if (cl.arguments.size () != 1) { throw invalid_argument ("1 argument required"); } printWarnings (cerr, root); root = cl.createKey (0); kdb.get (ks, root); if (cl.verbose) cout << "size of all keys in mountpoint: " << ks.size () << endl; KeySet part (ks.cut (root)); if (cl.verbose) cout << "size of requested keys: " << part.size () << endl; cout.setf (std::ios_base::unitbuf); if (cl.null) { cout.unsetf (std::ios_base::skipws); } cout << part; printWarnings (cerr, root); return 0; }
TEST_F (Simple, GetAppendMeta) { using namespace kdb; KDB kdb; KeySet ks; ks.append (Key ("meta/key", KEY_META_NAME, KEY_END)); Key parentKey (testRoot, KEY_END); kdb.get (ks, parentKey); ASSERT_EQ (ks.size (), 1) << "no key stayed"; ks.rewind (); ks.next (); EXPECT_EQ (ks.current ().getName (), "meta/key") << "name of element in keyset wrong"; EXPECT_EQ (ks.current ().getString (), "") << "string of element in keyset wrong"; kdb.set (ks, parentKey); ks.rewind (); ks.next (); EXPECT_EQ (ks.current ().getName (), "meta/key") << "name of element in keyset wrong"; EXPECT_EQ (ks.current ().getString (), "") << "string of element in keyset wrong"; kdb.close (parentKey); KeySet ks2; kdb.open (parentKey); kdb.get (ks2, parentKey); ASSERT_EQ (ks2.size (), 0) << "got keys from freshly mounted backends"; }
TEST_F (Simple, SetSystemGetAppend2) { using namespace kdb; KDB kdb; KeySet ks; Key parentKey (testRoot, KEY_END); ks.append (Key ("system" + testRoot + "key", KEY_VALUE, "value1", KEY_END)); kdb.get (ks, parentKey); ASSERT_EQ (ks.size (), 1) << "got keys from freshly mounted backends"; ks.rewind (); ks.next (); EXPECT_EQ (ks.current ().getName (), "system/tests/kdb/key") << "name of element in keyset wrong"; EXPECT_EQ (ks.current ().getString (), "value1") << "string of element in keyset wrong"; kdb.set (ks, parentKey); kdb.close (parentKey); KeySet ks2; ks2.append (Key ("system" + testRoot + "key2", KEY_VALUE, "value2", KEY_END)); kdb.open (parentKey); kdb.get (ks2, parentKey); ks2.rewind (); ks2.next (); ASSERT_EQ (ks2.size (), 1) << "wrong size"; EXPECT_EQ (ks2.current ().getName (), "system/tests/kdb/key") << "name of element in keyset wrong"; EXPECT_EQ (ks2.current ().getString (), "value1") << "string of element in keyset wrong"; }
int GetCommand::execute (Cmdline const & cl) { if (cl.arguments.size () != 1) throw invalid_argument ("Need one argument"); KeySet conf; kdb::Key root = cl.createKey (0); kdb::KDB kdb (root); std::string n; if (cl.all) { n = root.getName (); root.setName ("/"); } kdb.get (conf, root); if (cl.all) { root.setName (n); } // do a lookup without tracer to warm up default cache conf.lookup (root); root.setCallback (warnOnMeta); if (cl.verbose) { cout << "got " << conf.size () << " keys" << std::endl; root.setCallback (printTrace); } Key k = conf.lookup (root); int ret = 0; if (k) { if (cl.verbose) { cout << "The resulting keyname is " << k.getName () << std::endl; } cout << k.getString (); } else { cerr << "Did not find key"; ret = 1; } if (!cl.noNewline) { cout << endl; } printWarnings (cerr, root); printError (cerr, root); return ret; }
TEST_F (Simple, GetSystem) { using namespace kdb; KDB kdb; KeySet ks; Key parentKey ("system" + testRoot, KEY_END); ks.append (Key (parentKey.getName () + "/key", KEY_END)); EXPECT_NE (kdb.get (ks, parentKey), -1); ASSERT_EQ (ks.size (), 1) << "no key stayed" << ks; ks.rewind (); ks.next (); EXPECT_EQ (ks.current ().getName (), "system/tests/kdb/key") << "name of element in keyset wrong"; EXPECT_EQ (ks.current ().getString (), "") << "string of element in keyset wrong"; ASSERT_NE (kdb.set (ks, parentKey), -1); ks.rewind (); ks.next (); EXPECT_EQ (ks.current ().getName (), "system/tests/kdb/key") << "name of element in keyset wrong"; EXPECT_EQ (ks.current ().getString (), "") << "string of element in keyset wrong"; kdb.close (parentKey); KeySet ks2; kdb.open (parentKey); kdb.get (ks2, parentKey); ks.rewind (); ks.next (); EXPECT_EQ (ks.current ().getName (), "system/tests/kdb/key") << "name of element in keyset wrong"; EXPECT_EQ (ks.current ().getString (), "") << "string of element in keyset wrong"; }
void RemountCommand::cloneMountpoint(Cmdline const & cl) { Key existingParent (Backends::getBasePath(existingName), KEY_END); Key newParent (Backends::getBasePath(mp), KEY_END); KeySet existingBackend = mountConf.cut(existingParent); mountConf.append(existingBackend); KeySet newBackend(existingBackend.size(), KS_END); string configPath = newParent.getName() + "/config/path"; string mpPath = newParent.getName() + "/mountpoint"; existingBackend.rewind(); while (Key current = existingBackend.next()) { Key newKey = rebaseKey (current, existingParent, newParent); newBackend.append(newKey); if (newKey.getName() == mpPath) { newKey.setString(mp); } if (newKey.getName() == configPath) { newKey.setString(cl.arguments[0]); } } mountConf.append(newBackend); }
std::vector<PluginSpec> PluginVariantDatabase::getPluginVariantsFromGenconf (PluginSpec const & whichplugin, KeySet const & genconf, KeySet const & sysconf) const { std::vector<PluginSpec> result; KeySet ksToIterate (genconf); for (auto kCurrent : ksToIterate) { Key kCurrentTest (kCurrent.getNamespace () + "/", KEY_END); kCurrentTest.addBaseName (kCurrent.getBaseName ()); // e.g. system/space if (kCurrentTest == kCurrent) { PluginSpec variant (whichplugin); KeySet ksVariantConfToAdd; // new base for plugin conf Key kVariantPluginConf ("system/", KEY_END); // take variant config from genconf and transform it to proper plugin conf, // e.g. system/space/config/format -> system/format Key kVariantConf (kCurrentTest); kVariantConf.addBaseName ("config"); // e.g. system/space/config this->addKeysBelowKeyToConf (kVariantConf, genconf, kVariantPluginConf, ksVariantConfToAdd); // TODO plugin infos // check if the variant was disabled : system/elektra/plugins/simpleini/variants/space/disable Key kDisable = sysconf.lookup (this->buildVariantSysconfKey (whichplugin, kCurrent.getBaseName (), "disable")); if (kDisable && kDisable.getString () == "1") { continue; // skip this variant } // check if an override is available : system/elektra/plugins/simpleini/variants/space/override Key kOverride = sysconf.lookup (this->buildVariantSysconfKey (whichplugin, kCurrent.getBaseName (), "override")); if (kOverride && kOverride.getString () == "1") { // first delete config from genconf entirely ksVariantConfToAdd.clear (); Key kVariantSysconf (this->buildVariantSysconfKey (whichplugin, kCurrent.getBaseName (), "config")); this->addKeysBelowKeyToConf (kVariantSysconf, sysconf, kVariantPluginConf, ksVariantConfToAdd); } if (ksVariantConfToAdd.size () == 0) { continue; // no config means no variant } variant.appendConfig (ksVariantConfToAdd); result.push_back (variant); } } std::vector<PluginSpec> resFromSysconf (this->getPluginVariantsFromSysconf (whichplugin, sysconf, genconf)); result.insert (result.end (), resFromSysconf.begin (), resFromSysconf.end ()); return result; }
std::vector<PluginSpec> PluginVariantDatabase::getPluginVariantsFromSysconf (PluginSpec const & whichplugin, KeySet const & sysconf, KeySet const & genconfToIgnore) const { std::vector<PluginSpec> result; KeySet ksSysconf (sysconf); // first find possible variants Key kVariantBase ("system/elektra/plugins", KEY_END); kVariantBase.addBaseName (whichplugin.getName ()); kVariantBase.addBaseName ("variants"); KeySet ksPluginVariantSysconf (ksSysconf.cut (kVariantBase)); KeySet ksToIterate (ksPluginVariantSysconf); for (auto kCurrent : ksToIterate) { Key kCurrentTest (kVariantBase); kCurrentTest.addBaseName (kCurrent.getBaseName ()); if (kCurrentTest == kCurrent) { PluginSpec variant (whichplugin); KeySet ksVariantConfToAdd; // new base for plugin conf Key kVariantPluginConf ("system/", KEY_END); // add system conf for plugin variant Key kVariantSysconf (this->buildVariantSysconfKey (whichplugin, kCurrent.getBaseName (), "config")); this->addKeysBelowKeyToConf (kVariantSysconf, ksPluginVariantSysconf, kVariantPluginConf, ksVariantConfToAdd); // check if the variant was disabled : system/elektra/plugins/simpleini/variants/space/disable Key kDisable = sysconf.lookup (this->buildVariantSysconfKey (whichplugin, kCurrent.getBaseName (), "disable")); if (kDisable && kDisable.getString () == "1") { continue; // skip this variant } // check if the variant is in the genconfToIgnore list Key kGenconfVariant (kVariantPluginConf); kGenconfVariant.addBaseName (kCurrent.getBaseName ()); Key kIgnore = genconfToIgnore.lookup (kGenconfVariant); if (kIgnore) { continue; // this variant was added by genconf already } if (ksVariantConfToAdd.size () == 0) { continue; // no config means no variant } variant.appendConfig (ksVariantConfToAdd); result.push_back (variant); } } return result; }
TEST_F (Simple, WrongStateCascading) { using namespace kdb; KDB kdb; KeySet ks; Key parentKey (testRoot, KEY_END); EXPECT_THROW (kdb.set (ks, parentKey), kdb::KDBException) << "kdb set without prior kdb get should have 107 Wrong State"; ASSERT_EQ (ks.size (), 0) << "got keys from freshly mounted backends" << ks; }
TEST_F (Simple, WrongParent) { using namespace kdb; KDB kdb; Key parent ("meta", KEY_META_NAME, KEY_END); KeySet ks; EXPECT_THROW (kdb.set (ks, parent), kdb::KDBException); ASSERT_EQ (ks.size (), 0) << "got keys from freshly mounted backends" << ks; }
MojErr MojDbQueryPlan::rangesFromKeySets(const KeySet& lowerKeys, const KeySet& upperKeys, const KeySet& prefixKeys, const MojDbQuery::WhereClause* clause) { MojAssert(lowerKeys.size() == upperKeys.size() && lowerKeys.size() == prefixKeys.size()); MojUInt32 index = 0; KeySet::ConstIterator lowerIter = lowerKeys.begin(); KeySet::ConstIterator upperIter = upperKeys.begin(); KeySet::ConstIterator prefixIter = prefixKeys.begin(); while (lowerIter != lowerKeys.end()) { MojErr err = rangesFromKeys(*lowerIter, *upperIter, *prefixIter, index, clause); MojErrCheck(err); ++index; ++lowerIter; ++upperIter; ++prefixIter; } return MojErrNone; }
TEST_F (Simple, MetaInSet) { using namespace kdb; KDB kdb; KeySet ks; Key parent (testRoot, KEY_END); kdb.get (ks, parent); ASSERT_EQ (ks.size (), 0) << "got keys from freshly mounted backend" << ks; ks.append (Key ("meta" + testRoot + "wrong_meta_key", KEY_META_NAME, KEY_END)); ASSERT_EQ (ks.size (), 1) << "key not inserted:\n" << ks; kdb.set (ks, parent); printError (std::cout, parent); printWarnings (std::cout, parent); ASSERT_EQ (ks.size (), 1) << "got wrong keys:\n" << ks; struct stat buf; ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), -1) << "did find config file"; }
TEST_F(ThreeWayMergeTest, SameAddedDifferentValueConflict) { ours.append (Key ("user/parento/config/key5", KEY_VALUE, "newvalueours", KEY_END)); theirs.append (Key ("user/parentt/config/key5", KEY_VALUE, "newvaluetheirs", KEY_END)); MergeResult result = merger.mergeKeySet (base, ours, theirs, mergeParent); ASSERT_TRUE (result.hasConflicts())<< "No conflict detected although conflicts should exist"; KeySet conflicts = result.getConflictSet (); EXPECT_EQ(1, conflicts.size ()); testConflictMeta (conflicts.at (0), CONFLICT_ADD, CONFLICT_ADD); KeySet merged = result.getMergedKeys (); EXPECT_EQ(5, merged.size ()); compareAllKeys (merged); }
TEST_F(ThreeWayMergeTest, EqualKeySetsMerge) { MergeResult result = merger.mergeKeySet (base, ours, theirs, mergeParent); EXPECT_FALSE(result.hasConflicts()) << "Invalid conflict detected"; KeySet merged = result.getMergedKeys (); EXPECT_EQ(5, merged.size ()); compareAllKeys (merged); }
TEST_F (Simple, TryChangeAfterSet) { using namespace kdb; KDB kdb; KeySet ks; std::string name = "system" + testRoot + "try_change"; Key k (name, KEY_END); EXPECT_EQ (k.getName (), name); ks.append (k); EXPECT_THROW (k.setName ("user/x"), kdb::KeyInvalidName); EXPECT_EQ (k.getName (), name); kdb.get (ks, testRoot); ASSERT_EQ (ks.size (), 1) << "lost keys in get\n" << ks; kdb.set (ks, testRoot); EXPECT_THROW (k.setName ("user/x"), kdb::KeyInvalidName); EXPECT_EQ (k.getName (), name); ASSERT_EQ (ks.size (), 1) << "got no keys\n" << ks; struct stat buf; ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), 0) << "did not find config file"; }
TEST_F (Simple, EverythingInGetSet) { using namespace kdb; KDB kdb; KeySet ks = getAll (); Key parent (testRoot, KEY_END); kdb.get (ks, parent); ASSERT_EQ (ks.size (), 714) << "did not keep" << ks; kdb.set (ks, parent); printError (std::cout, parent); printWarnings (std::cout, parent); ASSERT_EQ (ks.size (), 714) << "got wrong keys:\n" << ks; // KeySet cmp = getAll(); // ASSERT_EQ(ks, cmp); // ASSERT_THAT(ks, ContainerEq(cmp)); compareKeySet (ks, getAll ()); struct stat buf; ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), -1) << "did find config file"; }
TEST_F(ThreeWayMergeTest, SameDeletedKeyMerge) { ours.lookup ("user/parento/config/key1", KDB_O_POP); theirs.lookup ("user/parentt/config/key1", KDB_O_POP); MergeResult result = merger.mergeKeySet (base, ours, theirs, mergeParent); EXPECT_FALSE(result.hasConflicts()) << "Invalid conflict detected"; KeySet merged = result.getMergedKeys (); EXPECT_EQ(4, merged.size ()); compareAllExceptKey1 (merged); }
TEST_F (Simple, GetCascading) { using namespace kdb; KDB kdb; KeySet ks; Key parentKey (testRoot, KEY_END); kdb.get (ks, parentKey); ASSERT_EQ (ks.size (), 0) << "got keys from freshly mounted backends" << ks; Key setParentKey ("system" + testRoot, KEY_END); kdb.set (ks, setParentKey); kdb.close (parentKey); }
TEST_F(ThreeWayMergeTest, SameAddedEqualValueMerges) { ours.append (Key ("user/parento/config/key5", KEY_VALUE, "newvalue", KEY_END)); theirs.append (Key ("user/parentt/config/key5", KEY_VALUE, "newvalue", KEY_END)); MergeResult result = merger.mergeKeySet (base, ours, theirs, mergeParent); EXPECT_FALSE(result.hasConflicts()) << "Invalid conflict detected"; KeySet merged = result.getMergedKeys (); EXPECT_EQ(6, merged.size ()); compareAllKeys (merged); compareKeys (Key ("user/parentm/config/key5", KEY_VALUE, "newvalue", KEY_END), merged.lookup (mk5)); }
TEST_F (AutoMergeStrategyTest, EqualsDeleteMerges) { task.theirs.lookup ("user/parentt/config/key1", KDB_O_POP); Key conflictKey = mergeKeys.lookup (mk1); result.addConflict (conflictKey, CONFLICT_SAME, CONFLICT_DELETE); conflictKey = result.getConflictSet ().at (0); strategy.resolveConflict (task, conflictKey, result); EXPECT_FALSE (result.hasConflicts ()) << "Invalid conflict detected"; KeySet merged = result.getMergedKeys (); EXPECT_EQ (3, merged.size ()); /* key with index 1 should be deleted */ compareAllExceptKey1 (merged); }
TEST_F(NewKeyStrategyTest, EqualsAddKeyMerge) { Key addedKey = Key ("user/parentt/config/key5", KEY_VALUE, "value5", KEY_END); task.theirs.append (addedKey); mergeKeys.append (mk5); Key conflictKey = mergeKeys.lookup (mk5); result.addConflict (conflictKey, CONFLICT_SAME, CONFLICT_ADD); conflictKey = result.getConflictSet ().at (0); strategy.resolveConflict (task, conflictKey, result); EXPECT_FALSE(result.hasConflicts()) << "Invalid conflict detected"; KeySet merged = result.getMergedKeys (); EXPECT_EQ(5, merged.size ()); compareAllKeys (merged); }
TEST_F (Simple, TriggerError) { using namespace kdb; KDB kdb; KeySet ks; EXPECT_EQ (kdb.get (ks, testRoot), 0) << "nothing to do in get"; ks.append (Key ("system" + testRoot + "a", KEY_END)); ks.append (Key ("system" + testRoot + "k", KEY_META, "trigger/error", "10", KEY_END)); ks.append (Key ("system" + testRoot + "z", KEY_END)); struct stat buf; ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), -1) << "found wrong file"; EXPECT_THROW (kdb.set (ks, testRoot), kdb::KDBException) << "could not trigger error"; ASSERT_EQ (ks.size (), 3) << "key suddenly missing"; EXPECT_EQ (ks.current ().getName (), "system" + testRoot + "k") << "ks should point to error key"; ASSERT_EQ (stat (mp->systemConfigFile.c_str (), &buf), -1) << "created file even though error triggered"; }
TEST_F (Simple, GetAppendNamespaces) { using namespace kdb; for (size_t i = 0; i < namespaces.size (); ++i) { KDB kdb; KeySet ks; ks.append (Key (namespaces[i].name + testRoot + "key", KEY_END)); kdb.get (ks, testRoot); ASSERT_EQ (ks.size (), 1) << "did not got key appended first with namespace " << namespaces[i].name; ks.rewind (); ks.next (); EXPECT_EQ (ks.current ().getName (), namespaces[i].name + "/tests/kdb/key") << "name of element in keyset wrong"; EXPECT_EQ (ks.current ().getString (), "") << "string of element in keyset wrong"; } }
void object::test<4>() { Unkeyed one, two, three; typedef std::set<Unkeyed*> KeySet; KeySet instances; instances.insert(&one); instances.insert(&two); instances.insert(&three); for (Unkeyed::instance_iter ii(Unkeyed::beginInstances()), iend(Unkeyed::endInstances()); ii != iend; ++ii) { Unkeyed& ref = *ii; ensure_equals("spurious instance", instances.erase(&ref), 1); } ensure_equals("unreported instance", instances.size(), 0); }