LCOV - code coverage report
Current view: top level - tests/kdb - testkdb_nested.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 135 135 100.0 %
Date: 2019-09-12 12:28:41 Functions: 19 26 73.1 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for KDB
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include <keysetio.hpp>
      11             : 
      12             : #include <gtest/gtest-elektra.h>
      13             : 
      14             : 
      15          24 : class Nested : public ::testing::Test
      16             : {
      17             : protected:
      18             :         static const std::string testRoot;
      19             :         static const std::string testBelow;
      20             :         static const std::string configFileRoot;
      21             :         static const std::string configFileBelow;
      22             : 
      23             : 
      24             :         testing::Namespaces namespaces;
      25             :         testing::MountpointPtr mpRoot;
      26             :         testing::MountpointPtr mpBelow;
      27             : 
      28          36 :         Nested () : namespaces ()
      29             :         {
      30          12 :         }
      31             : 
      32          12 :         virtual void SetUp () override
      33             :         {
      34          60 :                 mpRoot.reset (new testing::Mountpoint (testRoot, configFileRoot));
      35          60 :                 mpBelow.reset (new testing::Mountpoint (testBelow, configFileBelow));
      36          12 :         }
      37             : 
      38          12 :         virtual void TearDown () override
      39             :         {
      40          24 :                 mpBelow.reset ();
      41          24 :                 mpRoot.reset ();
      42          12 :         }
      43             : };
      44             : 
      45           6 : const std::string Nested::testRoot = "/tests/kdb/";
      46           6 : const std::string Nested::testBelow = "/tests/kdb/below/";
      47           6 : const std::string Nested::configFileRoot = "kdbFileRoot.dump";
      48           6 : const std::string Nested::configFileBelow = "kdbFileBelow.dump";
      49             : 
      50             : 
      51          20 : TEST_F (Nested, GetSetNothing)
      52             : {
      53             :         using namespace kdb;
      54           4 :         KDB kdb;
      55           4 :         KeySet ks;
      56           6 :         ASSERT_EQ (kdb.get (ks, testRoot), 0) << "should be nothing to update";
      57           8 :         ASSERT_EQ (ks.size (), 0) << "got keys from freshly mounted backends" << ks;
      58             :         struct stat buf;
      59          12 :         ASSERT_EQ (stat (mpRoot->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
      60          12 :         ASSERT_EQ (stat (mpBelow->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
      61           6 :         ASSERT_EQ (kdb.set (ks, testRoot), 0) << "should be nothing to set";
      62          12 :         ASSERT_EQ (stat (mpRoot->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
      63          12 :         ASSERT_EQ (stat (mpBelow->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
      64             : }
      65             : 
      66             : 
      67          20 : TEST_F (Nested, GetSetRoot)
      68             : {
      69             :         using namespace kdb;
      70           4 :         KDB kdb;
      71           4 :         KeySet ks;
      72             : 
      73           6 :         std::string name = "system" + testRoot + "key";
      74           6 :         Key k (name, KEY_END);
      75           8 :         EXPECT_EQ (k.getName (), name);
      76           2 :         ks.append (k);
      77             : 
      78           6 :         ASSERT_EQ (kdb.get (ks, testRoot), 0) << "should be nothing to update";
      79           8 :         ASSERT_EQ (ks.size (), 1) << "did not keep key at get" << ks;
      80             :         struct stat buf;
      81          12 :         ASSERT_EQ (stat (mpRoot->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
      82          12 :         ASSERT_EQ (stat (mpBelow->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
      83           6 :         ASSERT_EQ (kdb.set (ks, testRoot), 1);
      84           8 :         ASSERT_EQ (ks.size (), 1) << "did not keep key at set" << ks;
      85          12 :         ASSERT_EQ (stat (mpRoot->systemConfigFile.c_str (), &buf), 0) << "root file not created";
      86          12 :         ASSERT_EQ (stat (mpBelow->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
      87             : 
      88           6 :         Key parent (testRoot, KEY_END);
      89           2 :         kdb.close (parent);
      90           2 :         kdb.open (parent);
      91           4 :         KeySet ks2;
      92           6 :         ASSERT_EQ (kdb.get (ks2, testRoot), 1);
      93           8 :         ASSERT_EQ (ks2.size (), 1) << "did not get key stored before" << ks;
      94             : }
      95             : 
      96           6 : kdb::KeySet getAll ()
      97             : {
      98             :         using namespace ckdb;
      99             :         return
     100             : #include <data_allns.c>
     101             : }
     102             : 
     103             : 
     104          20 : TEST_F (Nested, GetSetBelow)
     105             : {
     106             :         using namespace kdb;
     107           4 :         KDB kdb;
     108           4 :         KeySet ks;
     109             : 
     110           6 :         std::string name = "system" + testRoot + "below/key";
     111           6 :         Key k (name, KEY_END);
     112           8 :         EXPECT_EQ (k.getName (), name);
     113           2 :         ks.append (k);
     114           6 :         ks.append (getAll ());
     115             : 
     116           6 :         ASSERT_EQ (kdb.get (ks, testRoot), 0) << "should be nothing to update";
     117           8 :         ASSERT_EQ (ks.size (), 715) << "did not keep key at get" << ks;
     118             :         struct stat buf;
     119          12 :         ASSERT_EQ (stat (mpRoot->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
     120          12 :         ASSERT_EQ (stat (mpBelow->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
     121           6 :         ASSERT_EQ (kdb.set (ks, testRoot), 1);
     122           8 :         ASSERT_EQ (ks.size (), 715) << "did not keep key at set" << ks;
     123          12 :         ASSERT_EQ (stat (mpRoot->systemConfigFile.c_str (), &buf), -1) << "root file created?";
     124          12 :         ASSERT_EQ (stat (mpBelow->systemConfigFile.c_str (), &buf), 0) << "below file not created";
     125             : 
     126           6 :         Key parent (testRoot, KEY_END);
     127           2 :         kdb.close (parent);
     128           2 :         kdb.open (parent);
     129           4 :         KeySet ks2;
     130           6 :         ASSERT_EQ (kdb.get (ks2, testRoot), 1);
     131           8 :         ASSERT_EQ (ks2.size (), 1) << "did not get key stored before" << ks;
     132             : }
     133             : 
     134             : 
     135          20 : TEST_F (Nested, RemoveFiles)
     136             : {
     137             :         using namespace kdb;
     138           4 :         KDB kdb;
     139           4 :         KeySet ks;
     140             : 
     141          10 :         ks.append (Key ("system" + testRoot + "key", KEY_END));
     142          10 :         ks.append (Key ("system" + testRoot + "below/key", KEY_END));
     143             : 
     144           6 :         ASSERT_EQ (kdb.get (ks, testRoot), 0) << "should be nothing to update";
     145           8 :         ASSERT_EQ (ks.size (), 2) << "did not keep key at get" << ks;
     146           6 :         ASSERT_EQ (kdb.set (ks, testRoot), 1);
     147           8 :         ASSERT_EQ (ks.size (), 2) << "did not keep key at set" << ks;
     148             :         struct stat buf;
     149          12 :         ASSERT_EQ (stat (mpRoot->systemConfigFile.c_str (), &buf), 0) << "root file not created";
     150          12 :         ASSERT_EQ (stat (mpBelow->systemConfigFile.c_str (), &buf), 0) << "below file not created";
     151             : 
     152           6 :         Key parent (testRoot, KEY_END);
     153           2 :         kdb.close (parent);
     154           2 :         kdb.open (parent);
     155           4 :         KeySet ks2;
     156           6 :         ASSERT_EQ (kdb.get (ks2, testRoot), 1);
     157           8 :         ASSERT_EQ (ks2.size (), 2) << "did not get key stored before" << ks;
     158           2 :         ks2.clear ();
     159           6 :         ASSERT_EQ (kdb.set (ks2, testRoot), 1); // remove files
     160          12 :         ASSERT_EQ (stat (mpRoot->systemConfigFile.c_str (), &buf), -1) << "found wrong file, file not removed";
     161          12 :         ASSERT_EQ (stat (mpBelow->systemConfigFile.c_str (), &buf), -1) << "found wrong file, file not removed";
     162             : }
     163             : 
     164             : 
     165          20 : TEST_F (Nested, GetSetRemoveBoth)
     166             : {
     167             :         using namespace kdb;
     168           4 :         KDB kdb;
     169           4 :         KeySet ks;
     170             : 
     171          10 :         ks.append (Key ("system" + testRoot + "key", KEY_END));
     172          10 :         ks.append (Key ("system" + testRoot + "key/subkey", KEY_END));
     173          10 :         ks.append (Key ("system" + testRoot + "below/key", KEY_END));
     174          10 :         ks.append (Key ("system" + testRoot + "below/key/subkey", KEY_END));
     175           6 :         ks.append (getAll ());
     176             : 
     177           6 :         ASSERT_EQ (kdb.get (ks, testRoot), 0) << "should be nothing to update";
     178           8 :         ASSERT_EQ (ks.size (), 718) << "did not keep key at get" << ks;
     179             :         struct stat buf;
     180          12 :         ASSERT_EQ (stat (mpRoot->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
     181          12 :         ASSERT_EQ (stat (mpBelow->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
     182           6 :         ASSERT_EQ (kdb.set (ks, testRoot), 1);
     183           8 :         ASSERT_EQ (ks.size (), 718) << "did not keep key at set" << ks;
     184          12 :         ASSERT_EQ (stat (mpRoot->systemConfigFile.c_str (), &buf), 0) << "root file not created";
     185          12 :         ASSERT_EQ (stat (mpBelow->systemConfigFile.c_str (), &buf), 0) << "below file not created";
     186             : 
     187           6 :         Key parent (testRoot, KEY_END);
     188           2 :         kdb.close (parent);
     189           2 :         kdb.open (parent);
     190           4 :         KeySet ks2;
     191           6 :         ASSERT_EQ (kdb.get (ks2, testRoot), 1);
     192           8 :         ASSERT_EQ (ks2.size (), 4) << "did not get key stored before" << ks;
     193             : 
     194           4 :         KeySet ks3;
     195           6 :         ks3.append (getAll ());
     196           6 :         ASSERT_EQ (kdb.set (ks3, testRoot), 1); // remove all keys
     197          12 :         ASSERT_EQ (stat (mpRoot->systemConfigFile.c_str (), &buf), -1) << "found wrong file (not removed)";
     198          12 :         ASSERT_EQ (stat (mpBelow->systemConfigFile.c_str (), &buf), -1) << "found wrong file (not removed)";
     199             : }
     200             : 
     201             : 
     202          20 : TEST_F (Nested, ErrorBelow)
     203             : {
     204             :         using namespace kdb;
     205           4 :         KDB kdb;
     206           4 :         KeySet ks;
     207             : 
     208          10 :         ks.append (Key ("system" + testRoot + "a", KEY_END));
     209          10 :         ks.append (Key ("system" + testRoot + "k", KEY_END));
     210          10 :         ks.append (Key ("system" + testRoot + "7", KEY_END));
     211          10 :         ks.append (Key ("system" + testBelow + "a", KEY_END));
     212          10 :         ks.append (Key ("system" + testBelow + "k", KEY_META, "trigger/error", "10", KEY_END));
     213          10 :         ks.append (Key ("system" + testBelow + "z", KEY_END));
     214             : 
     215           6 :         ASSERT_EQ (kdb.get (ks, testRoot), 0) << "should be nothing to update";
     216           8 :         ASSERT_EQ (ks.size (), 6) << "did not keep key at get" << ks;
     217             :         struct stat buf;
     218          12 :         ASSERT_EQ (stat (mpRoot->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
     219          12 :         ASSERT_EQ (stat (mpBelow->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
     220           4 :         EXPECT_THROW (kdb.set (ks, testRoot), kdb::KDBException) << "could not trigger error";
     221           8 :         ASSERT_EQ (ks.size (), 6) << "did not keep key at set" << ks;
     222          12 :         ASSERT_EQ (stat (mpRoot->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
     223          12 :         ASSERT_EQ (stat (mpBelow->systemConfigFile.c_str (), &buf), -1) << "found wrong file";
     224           6 : }

Generated by: LCOV version 1.13