lxcpp: cgroups API implementation (part 1)
[platform/core/security/vasum.git] / tests / unit_tests / lxcpp / ut-cgroups.cpp
1 /*
2  *  Copyright (C) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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.
7  *
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.
12  *
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
16  */
17
18 /**
19  * @file
20  * @author  Krzysztof Dynowski (k.dynowski@samsung.com)
21  * @brief   Unit tests of lxcpp cgroups managment
22  */
23
24 #include "config.hpp"
25 #include "ut.hpp"
26 #include "logger/logger.hpp"
27
28 #include "lxcpp/cgroups/subsystem.hpp"
29
30 namespace {
31
32 struct Fixture {
33     Fixture() {}
34     ~Fixture() {}
35 };
36
37 } // namespace
38
39 BOOST_FIXTURE_TEST_SUITE(LxcppCGroupsSuite, Fixture)
40
41 using namespace lxcpp;
42
43 // assume cgroups are supprted by the system
44 BOOST_AUTO_TEST_CASE(GetAvailable)
45 {
46     std::vector<std::string> subs;
47     BOOST_CHECK_NO_THROW(subs = Subsystem::availableSubsystems());
48     BOOST_CHECK(subs.size() > 0);
49     for (auto n : subs){
50         Subsystem s(n);
51         LOGD(s.getName() << ": " << (s.isAttached()?s.getMountPoint():"[not attached]"));
52     }
53 }
54
55 BOOST_AUTO_TEST_CASE(GetCGroupsByPid)
56 {
57     std::vector<std::string> cg;
58     BOOST_CHECK_NO_THROW(cg=Subsystem::getCGroups(::getpid()));
59     BOOST_CHECK(cg.size() > 0);
60 }
61
62 BOOST_AUTO_TEST_CASE(SubsysAttach)
63 {
64     Subsystem sub("freezer");
65     BOOST_CHECK_MESSAGE(sub.getName() == "freezer", "freezer not equal");
66     BOOST_CHECK_MESSAGE(sub.isAvailable(), "freezer not found");
67
68     if (!sub.isAvailable()) return ;
69
70
71     if (sub.isAttached()) {
72         std::string mp = sub.getMountPoint();
73         BOOST_CHECK_NO_THROW(Subsystem::detach(mp));
74         BOOST_CHECK_NO_THROW(Subsystem::attach(mp, {sub.getName()}));
75     }
76     else {
77         std::string mp = "/sys/fs/cgroup/" + sub.getName();
78         BOOST_CHECK_NO_THROW(Subsystem::attach(mp, {sub.getName()}));
79         BOOST_CHECK_NO_THROW(Subsystem::detach(mp));
80     }
81 }
82
83 BOOST_AUTO_TEST_SUITE_END()