process-cpp 3.0.0
A simple convenience library for handling processes in C++11.
linux_process_test.cpp
Go to the documentation of this file.
1/*
2 * Copyright © 2013 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This program 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
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored by: Thomas Voß <thomas.voss@canonical.com>
17 */
18
19#include <core/posix/fork.h>
21
26
27#include <gtest/gtest.h>
28
29#include <map>
30
31TEST(LinuxProcess, accessing_proc_stats_works)
32{
33 auto child = core::posix::fork(
34 [](){ while(true); return core::posix::exit::Status::success;},
36
38 EXPECT_NO_THROW(child >> stat);
40}
41
42TEST(LinuxProcess, accessing_proc_oom_score_works)
43{
45 EXPECT_NO_THROW(core::posix::this_process::instance() >> oom_score);
46}
47
48TEST(LinuxProcess, accessing_proc_oom_score_adj_works)
49{
51 EXPECT_NO_THROW(core::posix::this_process::instance() >> oom_score_adj);
52}
53
54TEST(LinuxProcess, adjusting_proc_oom_score_adj_works)
55{
57 {
59 };
60 EXPECT_NO_THROW(core::posix::this_process::instance() << oom_score_adj);
61 EXPECT_NO_THROW(core::posix::this_process::instance() >> oom_score_adj);
63 oom_score_adj.value);
64}
65
66// For this test we assume that we are not privileged and that the test binary
67// does not have CAP_SYS_RESOURCE capabilities.
68TEST(LinuxProcess, adjusting_proc_oom_score_adj_to_privileged_values_only_works_if_root)
69{
71 {
73 };
74 EXPECT_NO_THROW(core::posix::this_process::instance() << oom_score_adj);
75 EXPECT_NO_THROW(core::posix::this_process::instance() >> oom_score_adj);
76
77 // If we are running on virtualized builders or buildds we are running under a fakeroot environment.
78 // However, that environment does not give us the required privileges and capabilities to adjust OOM values
79 // as we like. At any rate, this check seems to be flaky and we just comment it out.
80 // EXPECT_NE(core::posix::linux::proc::process::OomScoreAdj::min_value(),
81 // oom_score_adj.value);
82}
83
84TEST(LinuxProcess, trying_to_write_an_invalid_oom_score_adj_throws)
85{
87 {
89 };
90
92}
93
94TEST(LinuxProcess, adjusting_proc_oom_adj_works)
95{
97 {
99 };
100 EXPECT_NO_THROW(core::posix::this_process::instance() << oom_adj);
101 EXPECT_NO_THROW(core::posix::this_process::instance() >> oom_adj);
103 oom_adj.value);
104}
105
106// For this test we assume that we are not privileged and that the test binary
107// does not have CAP_SYS_RESOURCE capabilities.
108TEST(LinuxProcess, adjusting_proc_oom_adj_to_privileged_values_does_not_work)
109{
111 {
113 };
114 EXPECT_NO_THROW(core::posix::this_process::instance() << oom_adj);
115 EXPECT_NO_THROW(core::posix::this_process::instance() >> oom_adj);
116
117 // If we are running on virtualized builders or buildds we are running under a fakeroot environment.
118 // However, that environment does not give us the required privileges and capabilities to adjust OOM values
119 // as we like. At any rate, this check seems to be flaky and we just comment it out.
120 // EXPECT_NE(core::posix::linux::proc::process::OomAdj::min_value(),
121 // oom_adj.value);
122}
123
124TEST(LinuxProcess, trying_to_write_an_invalid_oom_adj_throws)
125{
127 {
129 };
130
132}
EXPECT_ANY_THROW(auto death_observer=core::posix::ChildProcess::DeathObserver::create_once_with_signal_trap(trap))
TEST(LinuxProcess, accessing_proc_stats_works)
CORE_POSIX_DLL_PUBLIC Process instance() noexcept(true)
Returns a Process instance corresponding to this process.
CORE_POSIX_DLL_PUBLIC ChildProcess fork(const std::function< posix::exit::Status()> &main, const StandardStream &flags)
fork forks a new process and executes the provided main function in the newly forked process.
Definition fork.cpp:57
static int max_value()
Returns the maximum valid value.
Definition oom_adj.cpp:50
static int min_value()
Returns the minimum valid value.
Definition oom_adj.cpp:45
static int min_value()
Returns the minimum valid value.
static int max_value()
Returns the maximum valid value.
The Stat struct encapsulates status information about a process.
Definition stat.h:42