Provisioning uses dynamic configuration 59/34259/3
authorPiotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
Thu, 22 Jan 2015 08:34:38 +0000 (09:34 +0100)
committerPiotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
Thu, 22 Jan 2015 14:29:06 +0000 (15:29 +0100)
[Bug/Feature]   N/A
[Cause]         N/A
[Solution]      N/A
[Verification]  Build, run tests

Change-Id: If1762b8d0993a98048d2f9310981c53f2a913124

32 files changed:
server/configs/templates/template.conf
server/zone-provision-config.hpp
server/zone-provision.cpp
server/zone-provision.hpp
server/zone.cpp
server/zone.hpp
server/zones-manager.cpp
server/zones-manager.hpp
tests/unit_tests/client/configs/ut-client/zones/console1-dbus.conf.in
tests/unit_tests/client/configs/ut-client/zones/console2-dbus.conf.in
tests/unit_tests/client/configs/ut-client/zones/console3-dbus.conf.in
tests/unit_tests/server/configs/CMakeLists.txt
tests/unit_tests/server/configs/ut-server/zones/zone1.conf
tests/unit_tests/server/configs/ut-server/zones/zone2.conf
tests/unit_tests/server/configs/ut-server/zones/zone3.conf
tests/unit_tests/server/configs/ut-zone-admin/zones/buggy.conf
tests/unit_tests/server/configs/ut-zone-admin/zones/missing.conf
tests/unit_tests/server/configs/ut-zone-admin/zones/test-no-shutdown.conf
tests/unit_tests/server/configs/ut-zone-admin/zones/test.conf
tests/unit_tests/server/configs/ut-zone-provision/test.conf [new file with mode: 0644]
tests/unit_tests/server/configs/ut-zone/zones/buggy.conf
tests/unit_tests/server/configs/ut-zone/zones/test-dbus.conf.in
tests/unit_tests/server/configs/ut-zone/zones/test.conf
tests/unit_tests/server/configs/ut-zones-manager/templates/template.conf.in
tests/unit_tests/server/configs/ut-zones-manager/zones/console1-dbus.conf.in
tests/unit_tests/server/configs/ut-zones-manager/zones/console1.conf
tests/unit_tests/server/configs/ut-zones-manager/zones/console2-dbus.conf.in
tests/unit_tests/server/configs/ut-zones-manager/zones/console2.conf
tests/unit_tests/server/configs/ut-zones-manager/zones/console3-dbus.conf.in
tests/unit_tests/server/configs/ut-zones-manager/zones/console3.conf
tests/unit_tests/server/ut-zone-provision.cpp
tests/unit_tests/server/ut-zone.cpp

index e011a64..9690a87 100644 (file)
@@ -11,6 +11,7 @@
     "switchToDefaultAfterTimeout" : true,
     "enableDbusIntegration" : true,
     "runMountPoint" : "~NAME~/run",
+    "provisions" : [],
     "permittedToSend" : [ "/tmp/.*" ],
     "permittedToRecv" : [ "/tmp/.*" ],
     "validLinkPrefixes" : [ "/tmp/",
index 5a9f5ff..93fe1a0 100644 (file)
@@ -34,7 +34,7 @@
 
 namespace vasum {
 
-struct ZoneProvisioning
+struct ZoneProvisioningConfig
 {
 
     struct File
@@ -83,7 +83,7 @@ struct ZoneProvisioning
         )
     };
 
-    struct Unit
+    struct Provision
     {
         CONFIG_DECLARE_UNION
         (
@@ -93,11 +93,11 @@ struct ZoneProvisioning
         )
     };
 
-    std::vector<Unit> units;
+    std::vector<Provision> provisions;
 
     CONFIG_REGISTER
     (
-        units
+        provisions
     )
 };
 
index c04a32a..928d887 100644 (file)
@@ -42,29 +42,17 @@ namespace fs = boost::filesystem;
 
 namespace vasum {
 
-namespace {
-
-const std::string ZONE_PROVISION_FILE = "provision.conf";
-
-void declareUnit(const std::string& file, ZoneProvisioning::Unit&& unit)
-{
-     // TODO: Add to the dynamic configuration
-    ZoneProvisioning config;
-    if (fs::exists(file)) {
-        config::loadFromJsonFile(file, config);
-    }
-    config.units.push_back(std::move(unit));
-    config::saveToJsonFile(file, config);
-}
-
-} // namespace
-
-ZoneProvision::ZoneProvision(const std::string& zonePath,
+ZoneProvision::ZoneProvision(const std::string& rootPath,
+                             const std::string& configPath,
+                             const std::string& dbPath,
+                             const std::string& dbPrefix,
                              const std::vector<std::string>& validLinkPrefixes)
+    : mRootPath(rootPath)
+    , mDbPath(dbPath)
+    , mDbPrefix(dbPrefix)
+    , mValidLinkPrefixes(validLinkPrefixes)
 {
-    mProvisionFile = (fs::path(zonePath) / fs::path(ZONE_PROVISION_FILE)).string();
-    mRootPath = (zonePath / fs::path("rootfs")).string();
-    mValidLinkPrefixes = validLinkPrefixes;
+    config::loadFromKVStoreWithJsonFile(dbPath, configPath, mProvisioningConfig, dbPrefix);
 }
 
 ZoneProvision::~ZoneProvision()
@@ -72,21 +60,26 @@ ZoneProvision::~ZoneProvision()
     stop();
 }
 
-std::string ZoneProvision::getRootPath() const
+void ZoneProvision::saveProvisioningConfig()
 {
-    return mRootPath;
+    config::saveToKVStore(mDbPath, mProvisioningConfig, mDbPrefix);
 }
 
+void ZoneProvision::declareProvision(ZoneProvisioningConfig::Provision&& provision)
+{
+    mProvisioningConfig.provisions.push_back(std::move(provision));
+    saveProvisioningConfig();
+}
 
 void ZoneProvision::declareFile(const int32_t& type,
                                 const std::string& path,
                                 const int32_t& flags,
                                 const int32_t& mode)
 {
-    ZoneProvisioning::Unit unit;
-    unit.set(ZoneProvisioning::File({type, path, flags, mode}));
+    ZoneProvisioningConfig::Provision provision;
+    provision.set(ZoneProvisioningConfig::File({type, path, flags, mode}));
 
-    declareUnit(mProvisionFile, std::move(unit));
+    declareProvision(std::move(provision));
 }
 
 void ZoneProvision::declareMount(const std::string& source,
@@ -95,49 +88,46 @@ void ZoneProvision::declareMount(const std::string& source,
                                  const int64_t& flags,
                                  const std::string& data)
 {
-    ZoneProvisioning::Unit unit;
-    unit.set(ZoneProvisioning::Mount({source, target, type, flags, data}));
+    ZoneProvisioningConfig::Provision provision;
+    provision.set(ZoneProvisioningConfig::Mount({source, target, type, flags, data}));
 
-    declareUnit(mProvisionFile, std::move(unit));
+    declareProvision(std::move(provision));
 }
 
 void ZoneProvision::declareLink(const std::string& source,
                                 const std::string& target)
 {
-    ZoneProvisioning::Unit unit;
-    unit.set(ZoneProvisioning::Link({source, target}));
+    ZoneProvisioningConfig::Provision provision;
+    provision.set(ZoneProvisioningConfig::Link({source, target}));
 
-    declareUnit(mProvisionFile, std::move(unit));
+    declareProvision(std::move(provision));
 }
 
 void ZoneProvision::start() noexcept
 {
-    if (fs::exists(mProvisionFile)) {
-        config::loadFromJsonFile(mProvisionFile, mProvisioningConfig);
-        for (const auto& unit : mProvisioningConfig.units) {
-            try {
-                if (unit.is<ZoneProvisioning::File>()) {
-                    file(unit.as<ZoneProvisioning::File>());
-                } else if (unit.is<ZoneProvisioning::Mount>()) {
-                    mount(unit.as<ZoneProvisioning::Mount>());
-                } else if (unit.is<ZoneProvisioning::Link>()) {
-                    link(unit.as<ZoneProvisioning::Link>());
-                }
-                // mProvisioned must be FILO
-                mProvisioned.push_front(unit);
-            } catch (const std::exception& ex) {
-                LOGE("Provsion error: " << ex.what());
+    for (const auto& provision : mProvisioningConfig.provisions) {
+        try {
+            if (provision.is<ZoneProvisioningConfig::File>()) {
+                file(provision.as<ZoneProvisioningConfig::File>());
+            } else if (provision.is<ZoneProvisioningConfig::Mount>()) {
+                mount(provision.as<ZoneProvisioningConfig::Mount>());
+            } else if (provision.is<ZoneProvisioningConfig::Link>()) {
+                link(provision.as<ZoneProvisioningConfig::Link>());
             }
+            // mProvisioned must be FILO
+            mProvisioned.push_front(provision);
+        } catch (const std::exception& ex) {
+            LOGE("Provsion error: " << ex.what());
         }
     }
 }
 
 void ZoneProvision::stop() noexcept
 {
-    mProvisioned.remove_if([this](const ZoneProvisioning::Unit& unit) -> bool {
+    mProvisioned.remove_if([this](const ZoneProvisioningConfig::Provision& provision) -> bool {
         try {
-            if (unit.is<ZoneProvisioning::Mount>()) {
-                umount(unit.as<ZoneProvisioning::Mount>());
+            if (provision.is<ZoneProvisioningConfig::Mount>()) {
+                umount(provision.as<ZoneProvisioningConfig::Mount>());
             }
             // leaves files, links, fifo, untouched
             return true;
@@ -148,7 +138,7 @@ void ZoneProvision::stop() noexcept
     });
 }
 
-void ZoneProvision::file(const ZoneProvisioning::File& config)
+void ZoneProvision::file(const ZoneProvisioningConfig::File& config)
 {
     bool ret = false;
     const fs::path hostPath = fs::path(mRootPath) / fs::path(config.path);
@@ -183,7 +173,7 @@ void ZoneProvision::file(const ZoneProvisioning::File& config)
     }
 }
 
-void ZoneProvision::mount(const ZoneProvisioning::Mount& config)
+void ZoneProvision::mount(const ZoneProvisioningConfig::Mount& config)
 {
     const fs::path hostPath = fs::path(mRootPath) / fs::path(config.target);
     bool ret = utils::mount(config.source,
@@ -196,7 +186,7 @@ void ZoneProvision::mount(const ZoneProvisioning::Mount& config)
     }
 }
 
-void ZoneProvision::umount(const ZoneProvisioning::Mount& config)
+void ZoneProvision::umount(const ZoneProvisioningConfig::Mount& config)
 {
     const fs::path hostPath = fs::path(mRootPath) / fs::path(config.target);
     bool ret = utils::umount(hostPath.string());
@@ -205,7 +195,7 @@ void ZoneProvision::umount(const ZoneProvisioning::Mount& config)
     }
 }
 
-void ZoneProvision::link(const ZoneProvisioning::Link& config)
+void ZoneProvision::link(const ZoneProvisioningConfig::Link& config)
 {
     const std::string srcHostPath = fs::path(config.source).normalize().string();
     for (const std::string& prefix : mValidLinkPrefixes) {
index 5585c1f..f6933db 100644 (file)
@@ -44,12 +44,23 @@ class ZoneProvision {
 public:
     /**
      * ZoneProvision constructor
-     * @param zonePath directory where zones are defined (lxc configs, rootfs etc)
+     * @param rootPath zone root path
+     * @param configPath path to config with defaults
+     * @param dbPath path to database
+     * @param dbPrefix database prefix
+     * @param validLinkPrefixes valid link prefixes
      */
-    ZoneProvision(const std::string& zonePath,
+    ZoneProvision(const std::string& rootPath,
+                  const std::string& configPath,
+                  const std::string& dbPath,
+                  const std::string& dbPrefix,
                   const std::vector<std::string>& validLinkPrefixes);
     ~ZoneProvision();
 
+    ZoneProvision(const ZoneProvision&) = delete;
+    ZoneProvision& operator=(const ZoneProvision&) = delete;
+    ZoneProvision(ZoneProvision&&) = default;
+
     /**
      * Declare file, directory or pipe that will be created while zone startup
      */
@@ -71,25 +82,24 @@ public:
     void declareLink(const std::string& source,
                      const std::string& target);
 
-   /**
-     * Get zone root path
-     */
-    std::string getRootPath() const;
-
     void start() noexcept;
     void stop() noexcept;
 
 private:
-    ZoneProvisioning mProvisioningConfig;
+    ZoneProvisioningConfig mProvisioningConfig;
     std::string mRootPath;
-    std::string mProvisionFile;
+    std::string mDbPath;
+    std::string mDbPrefix;
     std::vector<std::string> mValidLinkPrefixes;
-    std::list<ZoneProvisioning::Unit> mProvisioned;
+    std::list<ZoneProvisioningConfig::Provision> mProvisioned;
+
+    void saveProvisioningConfig();
+    void declareProvision(ZoneProvisioningConfig::Provision&& provision);
 
-    void mount(const ZoneProvisioning::Mount& config);
-    void umount(const ZoneProvisioning::Mount& config);
-    void file(const ZoneProvisioning::File& config);
-    void link(const ZoneProvisioning::Link& config);
+    void mount(const ZoneProvisioningConfig::Mount& config);
+    void umount(const ZoneProvisioningConfig::Mount& config);
+    void file(const ZoneProvisioningConfig::File& config);
+    void link(const ZoneProvisioningConfig::Link& config);
 };
 
 
index f35a360..fbbd819 100644 (file)
@@ -48,12 +48,14 @@ typedef std::lock_guard<std::recursive_mutex> Lock;
 // TODO: move constants to the config file when default values are implemented there
 const int RECONNECT_RETRIES = 15;
 const int RECONNECT_DELAY = 1 * 1000;
+const std::string DB_PREFIX = "zone.";
 
 } // namespace
 
 Zone::Zone(const utils::Worker::Pointer& worker,
            const std::string& zonesPath,
            const std::string& zoneConfigPath,
+           const std::string& dbPath,
            const std::string& lxcTemplatePrefix,
            const std::string& baseRunMountPointPath)
     : mWorker(worker)
@@ -72,8 +74,12 @@ Zone::Zone(const utils::Worker::Pointer& worker,
     }
 
     mAdmin.reset(new ZoneAdmin(zonesPath, lxcTemplatePrefix, mConfig));
+
     const fs::path zonePath = fs::path(zonesPath) / mAdmin->getId();
-    mProvision.reset(new ZoneProvision(zonePath.string(), mConfig.validLinkPrefixes));
+    mRootPath = (zonePath / fs::path("rootfs")).string();
+    const std::string dbPrefix = DB_PREFIX + mAdmin->getId();
+
+    mProvision.reset(new ZoneProvision(mRootPath, zoneConfigPath, dbPath, dbPrefix, mConfig.validLinkPrefixes));
 }
 
 Zone::~Zone()
@@ -185,10 +191,9 @@ int Zone::getVT() const
 
 std::string Zone::getRootPath() const
 {
-    return mProvision->getRootPath();
+    return mRootPath;
 }
 
-
 bool Zone::activateVT()
 {
     Lock lock(mReconnectMutex);
index 7eb34cf..2eab7f3 100644 (file)
@@ -55,6 +55,7 @@ public:
     Zone(const utils::Worker::Pointer& worker,
          const std::string& zonesPath,
          const std::string& zoneConfigPath,
+         const std::string& dbPath,
          const std::string& lxcTemplatePrefix,
          const std::string& baseRunMountPointPath);
     Zone(Zone&&) = default;
@@ -269,6 +270,7 @@ private:
     DbusStateChangedCallback mDbusStateChangedCallback;
     std::string mDbusAddress;
     std::string mRunMountPoint;
+    std::string mRootPath;
 
     void onNameLostCallback();
     void reconnectHandler();
index f65f9e3..99e87ee 100644 (file)
@@ -204,6 +204,7 @@ void ZonesManager::createZone(const std::string& zoneConfigPath)
     std::unique_ptr<Zone> zone(new Zone(mWorker->createSubWorker(),
                                         mConfig.zonesPath,
                                         zoneConfigPath,
+                                        mConfig.dbPath,
                                         mConfig.lxcTemplatePrefix,
                                         mConfig.runMountPointPrefix));
     const std::string id = zone->getId();
@@ -254,7 +255,7 @@ void ZonesManager::destroyZone(const std::string& zoneId)
     it->second->setDestroyOnExit();
     mZones.erase(it);
 
-    if (mZones.size() == 0) {
+    if (mZones.empty()) {
         if (!utils::removeFile(utils::createFilePath(mConfig.zonesPath, ENABLED_FILE_NAME))) {
             LOGE("Failed to remove enabled file.");
         }
index e074fc9..677b056 100644 (file)
@@ -50,7 +50,7 @@ public:
     /**
      * Create new zone.
      *
-     * @param zoneConfig config of new zone
+     * @param zoneConfigPath config of new zone
      */
     void createZone(const std::string& zoneConfigPath);
 
index 46fb716..097480d 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "/tmp/ut-run/ut-zones-manager-console1-dbus",
+    "provisions" : [],
     "permittedToSend" : [ "/tmp/.*", "/etc/secret2" ],
     "permittedToRecv" : [ "/tmp/.*" ],
     "validLinkPrefixes" : []
index 260d01f..440772b 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "/tmp/ut-run/ut-zones-manager-console2-dbus",
+    "provisions" : [],
     "permittedToSend" : [ "/tmp/.*" ],
     "permittedToRecv" : [ "/tmp/.*", "/etc/secret1" ],
     "validLinkPrefixes" : []
index e20ffe9..2f28ee1 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "/tmp/ut-run/ut-zones-manager-console3-dbus",
+    "provisions" : [],
     "permittedToSend" : [ "/tmp/.*" ],
     "permittedToRecv" : [ "/tmp/.*" ],
     "validLinkPrefixes" : []
index 4a9edbe..596c123 100644 (file)
@@ -28,6 +28,8 @@ FILE(GLOB manager_zone_CONF    ut-zones-manager/zones/*.conf)
 FILE(GLOB zone_CONF            ut-zone/*.conf)
 FILE(GLOB zone_zone_CONF  ut-zone/zones/*.conf)
 
+FILE(GLOB zone_provision_CONF  ut-zone-provision/*.conf)
+
 FILE(GLOB admin_zone_CONF      ut-zone-admin/zones/*.conf)
 
 FILE(GLOB connection_CONF           ut-zone-connection/*.conf)
@@ -97,6 +99,9 @@ INSTALL(FILES        ${zone_zone_CONF}
 INSTALL(FILES        ${zone_zone_CONF_GEN}
         DESTINATION  ${VSM_TEST_CONFIG_INSTALL_DIR}/server/ut-zone/zones)
 
+INSTALL(FILES        ${zone_provision_CONF}
+        DESTINATION  ${VSM_TEST_CONFIG_INSTALL_DIR}/server/ut-zone-provision)
+
 INSTALL(FILES        ${admin_zone_CONF}
         DESTINATION  ${VSM_TEST_CONFIG_INSTALL_DIR}/server/ut-zone-admin/zones)
 
index 2c2ccde..432adc6 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "",
+    "provisions" : [],
     "permittedToSend" : [],
     "permittedToRecv" : [],
     "validLinkPrefixes" : []
index 1d500a7..2126b8f 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "",
+    "provisions" : [],
     "permittedToSend" : [],
     "permittedToRecv" : [],
     "validLinkPrefixes" : []
index a9856c7..dfc4a04 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "",
+    "provisions" : [],
     "permittedToSend" : [],
     "permittedToRecv" : [],
     "validLinkPrefixes" : []
index e96dca8..fa7bd6b 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "",
+    "provisions" : [],
     "permittedToSend" : [],
     "permittedToRecv" : [],
     "validLinkPrefixes" : []
index 9a5ec68..5cb9524 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "",
+    "provisions" : [],
     "permittedToSend" : [],
     "permittedToRecv" : [],
     "validLinkPrefixes" : []
index be05697..7ab5df7 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "",
+    "provisions" : [],
     "permittedToSend" : [],
     "permittedToRecv" : [],
     "validLinkPrefixes" : []
index 45213d7..215fa4f 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "",
+    "provisions" : [],
     "permittedToSend" : [],
     "permittedToRecv" : [],
     "validLinkPrefixes" : []
diff --git a/tests/unit_tests/server/configs/ut-zone-provision/test.conf b/tests/unit_tests/server/configs/ut-zone-provision/test.conf
new file mode 100644 (file)
index 0000000..504b486
--- /dev/null
@@ -0,0 +1,3 @@
+{
+    "provisions" : []
+}
index eaf0811..d29993c 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "",
+    "provisions" : [],
     "permittedToSend" : [],
     "permittedToRecv" : [],
     "validLinkPrefixes" : []
index e4a18fa..77a4180 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "/tmp/ut-run/ut-zone-test-dbus",
+    "provisions" : [],
     "permittedToSend" : [],
     "permittedToRecv" : [],
     "validLinkPrefixes" : []
index 4386c8c..9887e1c 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "",
+    "provisions" : [],
     "permittedToSend" : [],
     "permittedToRecv" : [],
     "validLinkPrefixes" : [ "/tmp" ]
index 7c2f5e4..28879db 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "/tmp/ut-run/~NAME~",
+    "provisions" : [],
     "permittedToSend" : [ "/tmp/.*" ],
     "permittedToRecv" : [ "/tmp/.*" ],
     "validLinkPrefixes" : []
index 46fb716..097480d 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "/tmp/ut-run/ut-zones-manager-console1-dbus",
+    "provisions" : [],
     "permittedToSend" : [ "/tmp/.*", "/etc/secret2" ],
     "permittedToRecv" : [ "/tmp/.*" ],
     "validLinkPrefixes" : []
index 41071ad..1fd4061 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "",
+    "provisions" : [],
     "permittedToSend" : [ "/tmp/.*" ],
     "permittedToRecv" : [ "/tmp/.*" ],
     "validLinkPrefixes" : []
index 260d01f..440772b 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "/tmp/ut-run/ut-zones-manager-console2-dbus",
+    "provisions" : [],
     "permittedToSend" : [ "/tmp/.*" ],
     "permittedToRecv" : [ "/tmp/.*", "/etc/secret1" ],
     "validLinkPrefixes" : []
index f152092..3c37313 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "",
+    "provisions" : [],
     "permittedToSend" : [ "/tmp/.*" ],
     "permittedToRecv" : [ "/tmp/.*" ],
     "validLinkPrefixes" : []
index e20ffe9..2f28ee1 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "/tmp/ut-run/ut-zones-manager-console3-dbus",
+    "provisions" : [],
     "permittedToSend" : [ "/tmp/.*" ],
     "permittedToRecv" : [ "/tmp/.*" ],
     "validLinkPrefixes" : []
index 4979dd2..8e54fe6 100644 (file)
@@ -11,6 +11,7 @@
     "cpuQuotaForeground" : -1,
     "cpuQuotaBackground" : 1000,
     "runMountPoint" : "",
+    "provisions" : [],
     "permittedToSend" : [ "/tmp/.*" ],
     "permittedToRecv" : [ "/tmp/.*" ],
     "validLinkPrefixes" : []
index 8d1738f..c488c09 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "utils/scoped-dir.hpp"
 #include "utils/exception.hpp"
+#include "utils/fs.hpp"
 #include "config/manager.hpp"
 #include "zone-provision.hpp"
 #include "zone-provision-config.hpp"
@@ -47,13 +48,15 @@ namespace fs = boost::filesystem;
 
 namespace {
 
-const std::string PROVISON_CONFIG_FILE = "provision.conf";
+const std::string TEST_CONFIG_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zone-provision/test.conf";
 const std::string ZONE = "ut-zone-provision-test";
 const fs::path ZONES_PATH = "/tmp/ut-zones";
 const fs::path LXC_TEMPLATES_PATH = VSM_TEST_LXC_TEMPLATES_INSTALL_DIR;
 const fs::path ZONE_PATH = ZONES_PATH / fs::path(ZONE);
-const fs::path PROVISION_FILE_PATH = ZONE_PATH / fs::path(PROVISON_CONFIG_FILE);
-const fs::path ROOTFS_PATH = ZONE_PATH / fs::path("rootfs");
+const fs::path SOME_FILE_PATH = ZONE_PATH / "file.txt";
+const fs::path ROOTFS_PATH = ZONE_PATH / "rootfs";
+const fs::path DB_PATH = ZONES_PATH / "vasum.db";
+const std::string DB_PREFIX = "zone";
 
 struct Fixture {
     utils::ScopedDir mZonesPathGuard;
@@ -63,6 +66,26 @@ struct Fixture {
         : mZonesPathGuard(ZONES_PATH.string())
         , mRootfsPath(ROOTFS_PATH.string())
     {
+        BOOST_REQUIRE(utils::saveFileContent(SOME_FILE_PATH.string(), "text"));
+    }
+
+    ZoneProvision create(const std::vector<std::string>& validLinkPrefixes)
+    {
+        return ZoneProvision(ROOTFS_PATH.string(),
+                             TEST_CONFIG_PATH,
+                             DB_PATH.string(),
+                             DB_PREFIX,
+                             validLinkPrefixes);
+    }
+
+    void load(ZoneProvisioningConfig& config)
+    {
+        config::loadFromKVStoreWithJsonFile(DB_PATH.string(), TEST_CONFIG_PATH, config, DB_PREFIX);
+    }
+
+    void save(const ZoneProvisioningConfig& config)
+    {
+        config::saveToKVStore(DB_PATH.string(), config, DB_PREFIX);
     }
 };
 
@@ -78,22 +101,22 @@ BOOST_AUTO_TEST_CASE(DestructorTest)
     {
         utils::ScopedDir provisionfs(mountSource.string());
 
-        ZoneProvisioning config;
-        ZoneProvisioning::Unit unit;
-        unit.set(ZoneProvisioning::File({VSMFILE_DIRECTORY,
+        ZoneProvisioningConfig config;
+        ZoneProvisioningConfig::Provision provision;
+        provision.set(ZoneProvisioningConfig::File({VSMFILE_DIRECTORY,
                                         mountTarget.string(),
                                         0,
                                         0777}));
-        config.units.push_back(unit);
-        unit.set(ZoneProvisioning::Mount({mountSource.string(),
+        config.provisions.push_back(provision);
+        provision.set(ZoneProvisioningConfig::Mount({mountSource.string(),
                                           mountTarget.string(),
                                           "",
                                           MS_BIND,
                                           ""}));
-        config.units.push_back(unit);
+        config.provisions.push_back(provision);
+        save(config);
 
-        config::saveToJsonFile(PROVISION_FILE_PATH.string(), config);
-        ZoneProvision zoneProvision(ZONE_PATH.string(), {});
+        ZoneProvision zoneProvision = create({});
         zoneProvision.start();
     }
     BOOST_CHECK(!fs::exists(mountSource));
@@ -103,35 +126,35 @@ BOOST_AUTO_TEST_CASE(FileTest)
 {
     //TODO: Test Fifo
     const fs::path regularFile = fs::path("/opt/usr/data/ut-regular-file");
-    const fs::path copyFile = PROVISION_FILE_PATH;
+    const fs::path copyFile = SOME_FILE_PATH;
 
-    ZoneProvisioning config;
-    ZoneProvisioning::Unit unit;
-    unit.set(ZoneProvisioning::File({VSMFILE_DIRECTORY,
+    ZoneProvisioningConfig config;
+    ZoneProvisioningConfig::Provision provision;
+    provision.set(ZoneProvisioningConfig::File({VSMFILE_DIRECTORY,
                                     regularFile.parent_path().string(),
                                     0,
                                     0777}));
-    config.units.push_back(unit);
+    config.provisions.push_back(provision);
 
-    unit.set(ZoneProvisioning::File({VSMFILE_REGULAR,
+    provision.set(ZoneProvisioningConfig::File({VSMFILE_REGULAR,
                                     regularFile.string(),
                                     O_CREAT,
                                     0777}));
-    config.units.push_back(unit);
+    config.provisions.push_back(provision);
 
-    unit.set(ZoneProvisioning::File({VSMFILE_DIRECTORY,
+    provision.set(ZoneProvisioningConfig::File({VSMFILE_DIRECTORY,
                                     copyFile.parent_path().string(),
                                     0,
                                     0777}));
-    config.units.push_back(unit);
-    unit.set(ZoneProvisioning::File({VSMFILE_REGULAR,
+    config.provisions.push_back(provision);
+    provision.set(ZoneProvisioningConfig::File({VSMFILE_REGULAR,
                                     copyFile.string(),
                                     0,
                                     0777}));
-    config.units.push_back(unit);
-    config::saveToJsonFile(PROVISION_FILE_PATH.string(), config);
+    config.provisions.push_back(provision);
+    save(config);
 
-    ZoneProvision zoneProvision(ZONE_PATH.string(), {});
+    ZoneProvision zoneProvision = create({});
     zoneProvision.start();
 
     BOOST_CHECK(fs::exists(ROOTFS_PATH / regularFile.parent_path()));
@@ -152,27 +175,27 @@ BOOST_AUTO_TEST_CASE(MountTest)
     utils::ScopedDir provisionfs(mountSource.string());
 
 
-    ZoneProvisioning config;
-    ZoneProvisioning::Unit unit;
-    unit.set(ZoneProvisioning::File({VSMFILE_DIRECTORY,
+    ZoneProvisioningConfig config;
+    ZoneProvisioningConfig::Provision provision;
+    provision.set(ZoneProvisioningConfig::File({VSMFILE_DIRECTORY,
                                     mountTarget.string(),
                                     0,
                                     0777}));
-    config.units.push_back(unit);
-    unit.set(ZoneProvisioning::Mount({mountSource.string(),
+    config.provisions.push_back(provision);
+    provision.set(ZoneProvisioningConfig::Mount({mountSource.string(),
                                       mountTarget.string(),
                                       "",
                                       MS_BIND,
                                       ""}));
-    config.units.push_back(unit);
-    unit.set(ZoneProvisioning::File({VSMFILE_REGULAR,
+    config.provisions.push_back(provision);
+    provision.set(ZoneProvisioningConfig::File({VSMFILE_REGULAR,
                                     (mountTarget / sharedFile).string(),
                                     O_CREAT,
                                     0777}));
-    config.units.push_back(unit);
-    config::saveToJsonFile(PROVISION_FILE_PATH.string(), config);
+    config.provisions.push_back(provision);
+    save(config);
 
-    ZoneProvision zoneProvision(ZONE_PATH.string(), {});
+    ZoneProvision zoneProvision = create({});
     zoneProvision.start();
 
     BOOST_CHECK(fs::exists(ROOTFS_PATH / mountTarget));
@@ -184,17 +207,17 @@ BOOST_AUTO_TEST_CASE(MountTest)
 
 BOOST_AUTO_TEST_CASE(LinkTest)
 {
-    const fs::path linkFile = fs::path("/ut-from-host-provision.conf");
+    const fs::path linkFile = fs::path("/ut-from-host-file.txt");
 
-    ZoneProvisioning config;
-    ZoneProvisioning::Unit unit;
+    ZoneProvisioningConfig config;
+    ZoneProvisioningConfig::Provision provision;
 
-    unit.set(ZoneProvisioning::Link({PROVISION_FILE_PATH.string(),
+    provision.set(ZoneProvisioningConfig::Link({SOME_FILE_PATH.string(),
                                      linkFile.string()}));
-    config.units.push_back(unit);
-    config::saveToJsonFile(PROVISION_FILE_PATH.string(), config);
+    config.provisions.push_back(provision);
+    save(config);
     {
-        ZoneProvision zoneProvision(ZONE_PATH.string(), {});
+        ZoneProvision zoneProvision = create({});
         zoneProvision.start();
 
         BOOST_CHECK(!fs::exists(ROOTFS_PATH / linkFile));
@@ -202,7 +225,7 @@ BOOST_AUTO_TEST_CASE(LinkTest)
         zoneProvision.stop();
     }
     {
-        ZoneProvision zoneProvision(ZONE_PATH.string(), {"/tmp/"});
+        ZoneProvision zoneProvision = create({"/tmp/"});
         zoneProvision.start();
 
         BOOST_CHECK(fs::exists(ROOTFS_PATH / linkFile));
@@ -213,82 +236,81 @@ BOOST_AUTO_TEST_CASE(LinkTest)
 
 BOOST_AUTO_TEST_CASE(DeclareFileTest)
 {
-    ZoneProvision zoneProvision(ZONE_PATH.string(), {});
+    ZoneProvision zoneProvision = create({});
     zoneProvision.declareFile(1, "path", 0747, 0777);
     zoneProvision.declareFile(2, "path", 0747, 0777);
 
-    ZoneProvisioning config;
-    BOOST_REQUIRE_NO_THROW(loadFromJsonFile(PROVISION_FILE_PATH.string(), config));
-    BOOST_REQUIRE_EQUAL(config.units.size(), 2);
-    BOOST_REQUIRE(config.units[0].is<ZoneProvisioning::File>());
-    BOOST_REQUIRE(config.units[1].is<ZoneProvisioning::File>());
-    const ZoneProvisioning::File& unit = config.units[0].as<ZoneProvisioning::File>();
-    BOOST_CHECK_EQUAL(unit.type, 1);
-    BOOST_CHECK_EQUAL(unit.path, "path");
-    BOOST_CHECK_EQUAL(unit.flags, 0747);
-    BOOST_CHECK_EQUAL(unit.mode, 0777);
+    ZoneProvisioningConfig config;
+    load(config);
+    BOOST_REQUIRE_EQUAL(config.provisions.size(), 2);
+    BOOST_REQUIRE(config.provisions[0].is<ZoneProvisioningConfig::File>());
+    BOOST_REQUIRE(config.provisions[1].is<ZoneProvisioningConfig::File>());
+    const ZoneProvisioningConfig::File& provision = config.provisions[0].as<ZoneProvisioningConfig::File>();
+    BOOST_CHECK_EQUAL(provision.type, 1);
+    BOOST_CHECK_EQUAL(provision.path, "path");
+    BOOST_CHECK_EQUAL(provision.flags, 0747);
+    BOOST_CHECK_EQUAL(provision.mode, 0777);
 }
 
 BOOST_AUTO_TEST_CASE(DeclareMountTest)
 {
-    ZoneProvision zoneProvision(ZONE_PATH.string(), {});
+    ZoneProvision zoneProvision = create({});
     zoneProvision.declareMount("/fake/path1", "/fake/path2", "tmpfs", 077, "fake");
     zoneProvision.declareMount("/fake/path2", "/fake/path2", "tmpfs", 077, "fake");
 
-    ZoneProvisioning config;
-    BOOST_REQUIRE_NO_THROW(loadFromJsonFile(PROVISION_FILE_PATH.string(), config));
-    BOOST_REQUIRE_EQUAL(config.units.size(), 2);
-    BOOST_REQUIRE(config.units[0].is<ZoneProvisioning::Mount>());
-    BOOST_REQUIRE(config.units[1].is<ZoneProvisioning::Mount>());
-    const ZoneProvisioning::Mount& unit = config.units[0].as<ZoneProvisioning::Mount>();
-    BOOST_CHECK_EQUAL(unit.source, "/fake/path1");
-    BOOST_CHECK_EQUAL(unit.target, "/fake/path2");
-    BOOST_CHECK_EQUAL(unit.type, "tmpfs");
-    BOOST_CHECK_EQUAL(unit.flags, 077);
-    BOOST_CHECK_EQUAL(unit.data, "fake");
+    ZoneProvisioningConfig config;
+    load(config);
+    BOOST_REQUIRE_EQUAL(config.provisions.size(), 2);
+    BOOST_REQUIRE(config.provisions[0].is<ZoneProvisioningConfig::Mount>());
+    BOOST_REQUIRE(config.provisions[1].is<ZoneProvisioningConfig::Mount>());
+    const ZoneProvisioningConfig::Mount& provision = config.provisions[0].as<ZoneProvisioningConfig::Mount>();
+    BOOST_CHECK_EQUAL(provision.source, "/fake/path1");
+    BOOST_CHECK_EQUAL(provision.target, "/fake/path2");
+    BOOST_CHECK_EQUAL(provision.type, "tmpfs");
+    BOOST_CHECK_EQUAL(provision.flags, 077);
+    BOOST_CHECK_EQUAL(provision.data, "fake");
 }
 
 BOOST_AUTO_TEST_CASE(DeclareLinkTest)
 {
-    ZoneProvision zoneProvision(ZONE_PATH.string(), {});
+    ZoneProvision zoneProvision = create({});
     zoneProvision.declareLink("/fake/path1", "/fake/path2");
     zoneProvision.declareLink("/fake/path2", "/fake/path2");
 
-    ZoneProvisioning config;
-    BOOST_REQUIRE_NO_THROW(loadFromJsonFile(PROVISION_FILE_PATH.string(), config));
-    BOOST_REQUIRE_EQUAL(config.units.size(), 2);
-    BOOST_REQUIRE(config.units[0].is<ZoneProvisioning::Link>());
-    BOOST_REQUIRE(config.units[1].is<ZoneProvisioning::Link>());
-    const ZoneProvisioning::Link& unit = config.units[0].as<ZoneProvisioning::Link>();
-    BOOST_CHECK_EQUAL(unit.source, "/fake/path1");
-    BOOST_CHECK_EQUAL(unit.target, "/fake/path2");
+    ZoneProvisioningConfig config;
+    load(config);
+    BOOST_REQUIRE_EQUAL(config.provisions.size(), 2);
+    BOOST_REQUIRE(config.provisions[0].is<ZoneProvisioningConfig::Link>());
+    BOOST_REQUIRE(config.provisions[1].is<ZoneProvisioningConfig::Link>());
+    const ZoneProvisioningConfig::Link& provision = config.provisions[0].as<ZoneProvisioningConfig::Link>();
+    BOOST_CHECK_EQUAL(provision.source, "/fake/path1");
+    BOOST_CHECK_EQUAL(provision.target, "/fake/path2");
 }
 
 BOOST_AUTO_TEST_CASE(ProvisionedAlreadyTest)
 {
-    const fs::path dir = fs::path("/opt/usr/data/ut-from-host-provision");
-    const fs::path linkFile = fs::path("/ut-from-host-provision.conf");
+    const fs::path dir = fs::path("/opt/usr/data/ut-from-host");
+    const fs::path linkFile = fs::path("/ut-from-host-file.txt");
     const fs::path regularFile = fs::path("/opt/usr/data/ut-regular-file");
 
-    ZoneProvisioning config;
-    ZoneProvisioning::Unit unit;
-    unit.set(ZoneProvisioning::File({VSMFILE_DIRECTORY,
+    ZoneProvisioningConfig config;
+    ZoneProvisioningConfig::Provision provision;
+    provision.set(ZoneProvisioningConfig::File({VSMFILE_DIRECTORY,
                                     dir.string(),
                                     0,
                                     0777}));
-    config.units.push_back(unit);
-    unit.set(ZoneProvisioning::Link({PROVISION_FILE_PATH.string(),
+    config.provisions.push_back(provision);
+    provision.set(ZoneProvisioningConfig::Link({SOME_FILE_PATH.string(),
                                      linkFile.string()}));
-    config.units.push_back(unit);
-    unit.set(ZoneProvisioning::File({VSMFILE_REGULAR,
+    config.provisions.push_back(provision);
+    provision.set(ZoneProvisioningConfig::File({VSMFILE_REGULAR,
                                     regularFile.string(),
                                     O_CREAT,
                                     0777}));
-    config.units.push_back(unit);
-
-    config::saveToJsonFile(PROVISION_FILE_PATH.string(), config);
+    config.provisions.push_back(provision);
+    save(config);
 
-    ZoneProvision zoneProvision(ZONE_PATH.string(), {"/tmp/"});
+    ZoneProvision zoneProvision = create({"/tmp/"});
     zoneProvision.start();
 
     BOOST_CHECK(fs::exists(ROOTFS_PATH / dir));
index 80e73da..783fbf8 100644 (file)
@@ -51,6 +51,7 @@ const std::string BUGGY_CONFIG_PATH = VSM_TEST_CONFIG_INSTALL_DIR "/server/ut-zo
 const std::string MISSING_CONFIG_PATH = "/this/is/a/missing/file/path/config.conf";
 const std::string ZONES_PATH = "/tmp/ut-zones";
 const std::string LXC_TEMPLATES_PATH = VSM_TEST_LXC_TEMPLATES_INSTALL_DIR;
+const std::string DB_PATH = ZONES_PATH + "/vasum.db";
 
 struct Fixture {
     utils::ScopedGlibLoop mLoop;
@@ -66,6 +67,7 @@ struct Fixture {
         return std::unique_ptr<Zone>(new Zone(utils::Worker::create(),
                                               ZONES_PATH,
                                               configPath,
+                                              DB_PATH,
                                               LXC_TEMPLATES_PATH,
                                               ""));
     }