lxcpp: cgroups configration (part 3)
[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 "config/manager.hpp"
26 #include "ut.hpp"
27 #include "logger/logger.hpp"
28
29 #include "lxcpp/cgroups/devices.hpp"
30 #include "lxcpp/cgroups/cgroup-config.hpp"
31
32 #include "lxcpp/commands/cgroups.hpp"
33
34 #include "lxcpp/exception.hpp"
35 #include "utils/text.hpp"
36
37 namespace {
38
39 struct Fixture {
40     Fixture() {}
41     ~Fixture() {}
42 };
43
44 } // namespace
45
46 BOOST_FIXTURE_TEST_SUITE(LxcppCGroupsSuite, Fixture)
47
48 using namespace lxcpp;
49
50 // assume cgroups are supprted by the system
51 BOOST_AUTO_TEST_CASE(GetAvailable)
52 {
53     std::vector<std::string> subs;
54
55     BOOST_CHECK_NO_THROW(subs = Subsystem::availableSubsystems());
56     BOOST_CHECK_MESSAGE(subs.size() > 0, "Control groups not supported");
57     for (auto n : subs){
58         Subsystem s(n);
59         LOGD(s.getName() << ": " << (s.isAttached() ? s.getMountPoint() : "[not attached]"));
60     }
61 }
62
63 BOOST_AUTO_TEST_CASE(GetCGroupsByPid)
64 {
65     std::vector<std::string> cg;
66
67     BOOST_CHECK_NO_THROW(cg=Subsystem::getCGroups(::getpid()));
68     BOOST_CHECK(cg.size() > 0);
69 }
70
71 BOOST_AUTO_TEST_CASE(GetPidsByCGroup)
72 {
73     CGroup cg = CGroup::getCGroup("memory", ::getpid());
74     std::vector<pid_t> pids;
75
76     BOOST_CHECK_NO_THROW(pids = cg.getPids());
77     BOOST_CHECK(pids.size() > 0);
78 }
79
80 BOOST_AUTO_TEST_CASE(SubsysAttach)
81 {
82     Subsystem sub("freezer");
83
84     BOOST_CHECK_MESSAGE(sub.isAvailable(), "freezer not found");
85
86     if (!sub.isAvailable()) return ;
87
88     if (sub.isAttached()) {
89         std::string mp = sub.getMountPoint();
90         BOOST_CHECK_NO_THROW(Subsystem::detach(mp));
91         BOOST_CHECK(Subsystem(sub.getName()).isAttached()==false);
92         BOOST_CHECK_NO_THROW(Subsystem::attach(mp, {sub.getName()}));
93         BOOST_CHECK(Subsystem(sub.getName()).isAttached()==true);
94     }
95     else {
96         std::string mp = "/sys/fs/cgroup/" + sub.getName();
97         BOOST_CHECK_NO_THROW(Subsystem::attach(mp, {sub.getName()}));
98         BOOST_CHECK(Subsystem(sub.getName()).isAttached()==true);
99         BOOST_CHECK_NO_THROW(Subsystem::detach(mp));
100         BOOST_CHECK(Subsystem(sub.getName()).isAttached()==false);
101     }
102 }
103
104 BOOST_AUTO_TEST_CASE(ControlGroupParams)
105 {
106     CGroup memg("memory:/ut-params");
107
108     // this test can fail if prev. test round left unexpected
109     BOOST_CHECK_MESSAGE(memg.exists() == false, "Cgroup alredy exists");
110     BOOST_CHECK_NO_THROW(memg.create());
111     BOOST_CHECK(memg.exists() == true);
112
113     if (!memg.exists()) {
114         return ;
115     }
116
117     BOOST_CHECK_NO_THROW(memg.assignPid(::getpid()));
118     BOOST_CHECK_NO_THROW(memg.assignGroup(::getpid()));
119
120     BOOST_CHECK_NO_THROW(memg.setValue("limit_in_bytes", "10k"));
121     BOOST_CHECK_NO_THROW(memg.setValue("soft_limit_in_bytes", "10k"));
122     BOOST_CHECK_THROW(memg.getValue("non-existing-name"), CGroupException);
123     BOOST_CHECK_THROW(memg.setValue("non-existing-name", "xxx"), CGroupException);
124
125     LOGD("limit_in_bytes: " << memg.getValue("limit_in_bytes"));
126     LOGD("soft_limit_in_bytes: " << memg.getValue("soft_limit_in_bytes"));
127     LOGD("max_usage_in_bytes: " << memg.getValue("max_usage_in_bytes"));
128
129     CGroup memtop("memory:/");
130     memtop.assignPid(::getpid());
131     memtop.setCommonValue("procs", std::to_string(::getpid()));
132
133     BOOST_CHECK_NO_THROW(memg.destroy());
134 }
135
136 BOOST_AUTO_TEST_CASE(DevicesParams)
137 {
138     DevicesCGroup devgrp("/tmp");
139
140     BOOST_CHECK_NO_THROW(devgrp.create());
141
142     std::vector<DevicePermission> list;
143     BOOST_CHECK_NO_THROW(list = devgrp.list());
144     for (__attribute__((unused)) const auto& i : list) {
145         LOGD(std::string("perm = ") + i.type + " " +
146              std::to_string(i.major) + ":" + std::to_string(i.minor) + " " + i.permission);
147     }
148
149     BOOST_CHECK_NO_THROW(devgrp.destroy());
150 }
151
152 BOOST_AUTO_TEST_CASE(CGroupConfigSerialization)
153 {
154     CGroupsConfig cfg;
155     std::string tmpConfigFile = "/tmp/cgconfig.conf";
156
157     BOOST_CHECK_NO_THROW(config::saveToJsonString(cfg));
158
159     cfg.subsystems.push_back(SubsystemConfig{"cpu", "/tmp/cgroup/cpu"});
160
161     CGroupConfig cpucfg = {"cpu", "/testcpu", {}, {}};
162     cfg.cgroups.push_back(cpucfg);
163     config::saveToJsonFile(tmpConfigFile, cfg);
164
165     CGroupsConfig cfg2;
166     BOOST_CHECK_NO_THROW(config::loadFromJsonFile(tmpConfigFile, cfg2));
167     BOOST_CHECK(cfg2.subsystems.size()==cfg.subsystems.size());
168 }
169
170 BOOST_AUTO_TEST_CASE(CGroupCommands)
171 {
172     CGroupsConfig cfg;
173     Subsystem sub("cpu");
174     std::string mp = sub.isAttached() ? sub.getMountPoint() : "/tmp/cgroup/cpu";
175
176     cfg.subsystems.push_back(SubsystemConfig{"cpu", mp});
177     CGroupConfig cpucfg = {"cpu", "/testcpu", {}, {}};
178     cfg.cgroups.push_back(cpucfg);
179
180     CGroupMakeAll cmd(cfg);
181     BOOST_CHECK_NO_THROW(cmd.execute());
182
183     CGroup cpugrp("cpu", "/testcpu");
184     BOOST_CHECK_NO_THROW(cpugrp.destroy());
185 }
186
187 BOOST_AUTO_TEST_SUITE_END()