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 Krzysztof Dynowski (k.dynowski@samsung.com)
21 * @brief Unit tests of lxcpp cgroups managment
26 #include "logger/logger.hpp"
28 #include "lxcpp/cgroups/devices.hpp"
29 #include "lxcpp/exception.hpp"
30 #include "utils/text.hpp"
41 BOOST_FIXTURE_TEST_SUITE(LxcppCGroupsSuite, Fixture)
43 using namespace lxcpp;
45 // assume cgroups are supprted by the system
46 BOOST_AUTO_TEST_CASE(GetAvailable)
48 std::vector<std::string> subs;
49 BOOST_CHECK_NO_THROW(subs = Subsystem::availableSubsystems());
50 BOOST_CHECK_MESSAGE(subs.size() > 0, "Control groups not supported");
53 LOGD(s.getName() << ": " << (s.isAttached() ? s.getMountPoint() : "[not attached]"));
57 BOOST_AUTO_TEST_CASE(GetCGroupsByPid)
59 std::vector<std::string> cg;
60 BOOST_CHECK_NO_THROW(cg=Subsystem::getCGroups(::getpid()));
61 BOOST_CHECK(cg.size() > 0);
64 BOOST_AUTO_TEST_CASE(GetPidsByCGroup)
66 CGroup cg = CGroup::getCGroup("memory", ::getpid());
67 std::vector<pid_t> pids;
68 BOOST_CHECK_NO_THROW(pids = cg.getPids());
69 BOOST_CHECK(pids.size() > 0);
72 BOOST_AUTO_TEST_CASE(SubsysAttach)
74 Subsystem sub("freezer");
75 BOOST_CHECK_MESSAGE(sub.isAvailable(), "freezer not found");
77 if (!sub.isAvailable()) return ;
79 if (sub.isAttached()) {
80 std::string mp = sub.getMountPoint();
81 BOOST_CHECK_NO_THROW(Subsystem::detach(mp));
82 BOOST_CHECK(Subsystem(sub.getName()).isAttached()==false);
83 BOOST_CHECK_NO_THROW(Subsystem::attach(mp, {sub.getName()}));
84 BOOST_CHECK(Subsystem(sub.getName()).isAttached()==true);
87 std::string mp = "/sys/fs/cgroup/" + sub.getName();
88 BOOST_CHECK_NO_THROW(Subsystem::attach(mp, {sub.getName()}));
89 BOOST_CHECK(Subsystem(sub.getName()).isAttached()==true);
90 BOOST_CHECK_NO_THROW(Subsystem::detach(mp));
91 BOOST_CHECK(Subsystem(sub.getName()).isAttached()==false);
95 BOOST_AUTO_TEST_CASE(ControlGroupParams)
97 CGroup memg("memory:/ut-params");
98 BOOST_CHECK(memg.exists() == false);
99 BOOST_CHECK_NO_THROW(memg.create());
100 BOOST_CHECK(memg.exists() == true);
102 if (!memg.exists()) return ;
104 memg.assignPid(::getpid());
105 memg.setValue("limit_in_bytes", "10k");
106 memg.setValue("soft_limit_in_bytes", "10k");
107 BOOST_CHECK_THROW(memg.setValue("non-existing-name", "xxx"), CGroupException);
109 LOGD("limit_in_bytes: " << memg.getValue("limit_in_bytes"));
110 LOGD("soft_limit_in_bytes: " << memg.getValue("soft_limit_in_bytes"));
111 LOGD("max_usage_in_bytes: " << memg.getValue("max_usage_in_bytes"));
113 CGroup("memory:/").assignPid(::getpid());
114 BOOST_CHECK_NO_THROW(memg.destroy());
117 BOOST_AUTO_TEST_CASE(DevicesParams)
119 DevicesCGroup devcg("/");
120 std::vector<DevicePermission> list = devcg.list();
121 for (__attribute__((unused)) const auto& i : list) {
122 LOGD(std::string("perm = ") + i.type + " " +
123 std::to_string(i.major) + ":" + std::to_string(i.minor) + " " + i.permission);
127 BOOST_AUTO_TEST_SUITE_END()