Libvirt network wrapper 83/22283/2
authorJan Olszak <j.olszak@samsung.com>
Fri, 30 May 2014 16:14:51 +0000 (18:14 +0200)
committerGerrit Code Review <gerrit@review.vlan103.tizen.org>
Mon, 2 Jun 2014 13:15:55 +0000 (06:15 -0700)
    [Bug/Feature]   A wrapper for libvirt' network
    [Cause]         N/A
    [Solution]      N/A
    [Verification]  Build, install, run tests.

Change-Id: I0997f846132cc29035b144705ff4a4835a3dad01

common/libvirt/domain.cpp
common/libvirt/network.cpp [new file with mode: 0644]
common/libvirt/network.hpp [new file with mode: 0644]
tests/unit_tests/libvirt/domain.cpp
tests/unit_tests/libvirt/network.cpp [new file with mode: 0644]

index 1c6c52d..ae25a02 100644 (file)
@@ -27,8 +27,6 @@
 #include "libvirt/helpers.hpp"
 #include "libvirt/exception.hpp"
 
-#include <cassert>
-
 
 namespace security_containers {
 namespace libvirt {
diff --git a/common/libvirt/network.cpp b/common/libvirt/network.cpp
new file mode 100644 (file)
index 0000000..fdb9126
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Jan Olszak <j.olszak@samsung.com>
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+/**
+ * @file
+ * @author  Jan Olszak (j.olszak@samsung.com)
+ * @brief   Implementation of the class wrapping libvirt network
+ */
+
+#include "log/logger.hpp"
+#include "libvirt/network.hpp"
+#include "libvirt/helpers.hpp"
+#include "libvirt/exception.hpp"
+
+
+namespace security_containers {
+namespace libvirt {
+
+
+LibvirtNetwork::LibvirtNetwork(const std::string& configXML)
+    : mCon(LIBVIRT_LXC_ADDRESS)
+{
+    mNet = virNetworkDefineXML(mCon.get(), configXML.c_str());
+
+    if (mNet == NULL) {
+        LOGE("Error while defining a network:\n"
+             << libvirtFormatError());
+        throw LibvirtOperationException();
+    }
+}
+
+LibvirtNetwork::~LibvirtNetwork()
+{
+    if (virNetworkUndefine(mNet) < 0) {
+        LOGE("Error while undefining the network:\n"
+             << libvirtFormatError());
+    }
+
+    if (virNetworkFree(mNet) < 0) {
+        LOGE("Error while destroying the network object:\n"
+             << libvirtFormatError());
+    }
+}
+
+virNetworkPtr LibvirtNetwork::get()
+{
+    return mNet;
+}
+
+LibvirtNetwork::operator bool() const
+{
+    return mNet != NULL;
+}
+
+} // namespace libvirt
+} // namespace security_containers
diff --git a/common/libvirt/network.hpp b/common/libvirt/network.hpp
new file mode 100644 (file)
index 0000000..5c5626e
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Jan Olszak <j.olszak@samsung.com>
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+/**
+ * @file
+ * @author  Jan Olszak (j.olszak@samsung.com)
+ * @brief   Declaration of the class wrapping libvirt network
+ */
+
+#ifndef COMMON_LIBVIRT_NETWORK_HPP
+#define COMMON_LIBVIRT_NETWORK_HPP
+
+#include "libvirt/connection.hpp"
+
+#include <libvirt/libvirt.h>
+
+
+namespace security_containers {
+namespace libvirt {
+
+
+class LibvirtNetwork {
+
+public:
+    LibvirtNetwork(const std::string& configXML);
+    ~LibvirtNetwork();
+
+    /**
+     * @return The libvirt network pointer
+     */
+    virNetworkPtr get();
+
+    /**
+     * @return libvirt network pointer is not NULL
+     */
+    operator bool() const;
+
+private:
+    LibvirtConnection mCon;
+    virNetworkPtr mNet = NULL;
+};
+
+
+} // namespace libvirt
+} // namespace security_containers
+
+
+#endif // COMMON_LIBVIRT_NETWORK_HPP
index b05fa5a..2ecbcc3 100644 (file)
@@ -20,7 +20,7 @@
 /**
  * @file
  * @author  Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
- * @brief   Unit tests of the ContainerAdmin class
+ * @brief   Unit tests of the LibvirtDomain class
  */
 
 #include "ut.hpp"
diff --git a/tests/unit_tests/libvirt/network.cpp b/tests/unit_tests/libvirt/network.cpp
new file mode 100644 (file)
index 0000000..7f862f7
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Jan Olszak <j.olszak@samsung.com>
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+
+/**
+ * @file
+ * @author  Jan Olszak (j.olszak@samsung.com)
+ * @brief   Unit tests of the LibvirtNetwork class
+ */
+
+#include "ut.hpp"
+
+#include "libvirt/network.hpp"
+#include "libvirt/exception.hpp"
+
+#include <memory>
+
+BOOST_AUTO_TEST_SUITE(LibvirtNetworkSuite)
+
+
+using namespace security_containers;
+using namespace security_containers::libvirt;
+
+
+namespace {
+
+const std::string CORRECT_CONFIG_XML =  "<network>"
+                                        "    <name>test</name>"
+                                        "    <forward mode='nat'>"
+                                        "        <nat>"
+                                        "          <port start='1024' end='65535'/>"
+                                        "        </nat>"
+                                        "    </forward>"
+                                        "   <bridge name='test-virbr0' stp='on' delay='0'/>"
+                                        "   <ip address='192.168.122.1' netmask='255.255.255.0'>"
+                                        "       <dhcp>"
+                                        "          <range start='192.168.122.2' end='192.168.122.254'/>"
+                                        "       </dhcp>"
+                                        "   </ip>"
+                                        "</network>";
+
+const std::string BUGGY_CONFIG_XML = "<><TRASH>";
+
+} // namespace
+
+BOOST_AUTO_TEST_CASE(ConstructorDestructorTest)
+{
+    std::unique_ptr<LibvirtNetwork> netPtr;
+    BOOST_REQUIRE_NO_THROW(netPtr.reset(new LibvirtNetwork(CORRECT_CONFIG_XML)));
+    BOOST_REQUIRE_NO_THROW(netPtr.reset());
+}
+
+BOOST_AUTO_TEST_CASE(BuggyConfigTest)
+{
+    BOOST_REQUIRE_THROW(LibvirtNetwork net(BUGGY_CONFIG_XML), LibvirtOperationException);
+}
+
+BOOST_AUTO_TEST_CASE(DefinitionTest)
+{
+    LibvirtNetwork net(CORRECT_CONFIG_XML);
+    BOOST_CHECK(net.get() != NULL);
+}
+
+BOOST_AUTO_TEST_CASE(BoolTest)
+{
+    LibvirtNetwork net(CORRECT_CONFIG_XML);
+    BOOST_CHECK(net);
+}
+
+BOOST_AUTO_TEST_SUITE_END()