2 * Copyright (C) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License version 2.1 as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * @author Maciej Karpiuk (m.karpiuk2@samsung.com)
21 * @brief Unit tests of lxcpp provisioning
25 #include "config/manager.hpp"
26 #include "lxcpp/lxcpp.hpp"
27 #include "lxcpp/container.hpp"
28 #include "lxcpp/container-config.hpp"
29 #include "lxcpp/provision-config.hpp"
31 #include "utils/scoped-dir.hpp"
36 using namespace lxcpp;
37 using namespace config;
38 using namespace provision;
40 const std::string TEST_DIR = "/tmp/ut-provisioning";
41 const std::string ROOT_DIR = TEST_DIR + "/root";
44 Fixture() : mTestPath(ROOT_DIR) {
45 container = std::unique_ptr<Container>(createContainer("ProvisioningTester", ROOT_DIR));
49 std::unique_ptr<Container> container;
50 utils::ScopedDir mTestPath;
56 BOOST_FIXTURE_TEST_SUITE(LxcppProvisioningSuite, Fixture)
58 BOOST_AUTO_TEST_CASE(ListProvisionsEmptyContainer)
60 BOOST_REQUIRE(container->getFiles().size() == 0);
61 BOOST_REQUIRE(container->getMounts().size() == 0);
62 BOOST_REQUIRE(container->getLinks().size() == 0);
65 BOOST_AUTO_TEST_CASE(AddDeclareFile)
67 container->declareFile(File::Type::FIFO, "path", 0747, 0777);
68 container->declareFile(File::Type::REGULAR, "path", 0747, 0777);
70 std::vector<File> fileList = container->getFiles();
71 BOOST_REQUIRE_EQUAL(fileList.size(), 2);
73 BOOST_REQUIRE(fileList[0].type == File::Type::FIFO);
74 BOOST_REQUIRE(fileList[0].path == "path");
75 BOOST_REQUIRE(fileList[0].flags == 0747);
76 BOOST_REQUIRE(fileList[0].mode == 0777);
77 BOOST_REQUIRE(fileList[1].type == File::Type::REGULAR);
79 BOOST_REQUIRE_NO_THROW(container->removeFile(fileList[0]));
80 BOOST_REQUIRE_EQUAL(container->getFiles().size(), 1);
81 File dummyFile({File::Type::FIFO, "dummy", 1, 2});
82 BOOST_REQUIRE_THROW(container->removeFile(dummyFile), ProvisionException);
83 BOOST_REQUIRE_NO_THROW(container->removeFile(fileList[1]));
84 BOOST_REQUIRE_EQUAL(container->getFiles().size(), 0);
87 BOOST_AUTO_TEST_CASE(AddDeclareMount)
89 container->declareMount("/fake/path1", "/fake/path2", "tmpfs", 077, "fake");
90 container->declareMount("/fake/path2", "/fake/path2", "tmpfs", 077, "fake");
91 BOOST_CHECK_THROW(container->declareMount("/fake/path2", "/fake/path2", "tmpfs", 077, "fake"),
94 std::vector<Mount> mountList = container->getMounts();
95 BOOST_REQUIRE_EQUAL(mountList.size(), 2);
97 BOOST_REQUIRE(mountList[0].source == "/fake/path1");
98 BOOST_REQUIRE(mountList[0].target == "/fake/path2");
99 BOOST_REQUIRE(mountList[0].type == "tmpfs");
100 BOOST_REQUIRE(mountList[0].flags == 077);
101 BOOST_REQUIRE(mountList[0].data == "fake");
103 BOOST_REQUIRE(mountList[1].source == "/fake/path2");
104 BOOST_REQUIRE(mountList[1].target == "/fake/path2");
105 BOOST_REQUIRE(mountList[1].type == "tmpfs");
106 BOOST_REQUIRE(mountList[1].flags == 077);
107 BOOST_REQUIRE(mountList[1].data == "fake");
109 BOOST_REQUIRE_NO_THROW(container->removeMount(mountList[0]));
110 BOOST_REQUIRE_EQUAL(container->getMounts().size(), 1);
111 Mount dummyMount({"a", "b", "c", 1, "d"});
112 BOOST_REQUIRE_THROW(container->removeMount(dummyMount), ProvisionException);
113 BOOST_REQUIRE_NO_THROW(container->removeMount(mountList[1]));
114 BOOST_REQUIRE_EQUAL(container->getMounts().size(), 0);
117 BOOST_AUTO_TEST_CASE(AddDeclareLink)
119 container->declareLink("/fake/path1", "/fake/path2");
120 container->declareLink("/fake/path2", "/fake/path2");
121 BOOST_CHECK_THROW(container->declareLink("/fake/path2", "/fake/path2"),
124 std::vector<Link> linkList = container->getLinks();
125 BOOST_REQUIRE_EQUAL(linkList.size(), 2);
127 BOOST_REQUIRE(linkList[0].source == "/fake/path1");
128 BOOST_REQUIRE(linkList[0].target == "/fake/path2");
129 BOOST_REQUIRE(linkList[1].source == "/fake/path2");
130 BOOST_REQUIRE(linkList[1].target == "/fake/path2");
132 BOOST_REQUIRE_NO_THROW(container->removeLink(linkList[0]));
133 BOOST_REQUIRE_EQUAL(container->getLinks().size(), 1);
134 Link dummyLink({"a", "b"});
135 BOOST_REQUIRE_THROW(container->removeLink(dummyLink), ProvisionException);
136 BOOST_REQUIRE_NO_THROW(container->removeLink(linkList[1]));
137 BOOST_REQUIRE_EQUAL(container->getLinks().size(), 0);
140 BOOST_AUTO_TEST_CASE(ProvisioningConfigSerialization)
142 std::string tmpConfigFile = "/tmp/fileconfig.conf";
143 std::string tmpConfigMount = "/tmp/mountconfig.conf";
144 std::string tmpConfigLink = "/tmp/linkconfig.conf";
146 File saved_file ({File::Type::REGULAR, "path", 0747, 0777});
147 Mount saved_mount({"/fake/path1", "/fake/path2", "tmpfs", 077, "fake"});
148 Link saved_link ({"/fake/path1", "/fake/path2"});
149 config::saveToJsonFile(tmpConfigFile, saved_file);
150 config::saveToJsonFile(tmpConfigMount, saved_mount);
151 config::saveToJsonFile(tmpConfigLink, saved_link);
156 config::loadFromJsonFile(tmpConfigFile, loaded_file);
157 config::loadFromJsonFile(tmpConfigMount, loaded_mount);
158 config::loadFromJsonFile(tmpConfigLink, loaded_link);
159 BOOST_REQUIRE(saved_file == loaded_file);
160 BOOST_REQUIRE(saved_mount == loaded_mount);
161 BOOST_REQUIRE(saved_link == loaded_link);
164 BOOST_AUTO_TEST_SUITE_END()