Apache Mesos
utils.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_TESTS_UTILS_HPP__
14 #define __STOUT_TESTS_UTILS_HPP__
15 
16 #include <string>
17 
18 #include <gtest/gtest.h>
19 
20 #include <stout/gtest.hpp>
21 #include <stout/try.hpp>
22 
23 #include <stout/os/chdir.hpp>
24 #include <stout/os/getcwd.hpp>
25 #include <stout/os/mkdtemp.hpp>
26 #include <stout/os/realpath.hpp>
27 #include <stout/os/rmdir.hpp>
28 
29 #if __FreeBSD__
30 #include <stout/os/sysctl.hpp>
31 #endif
32 
33 template <typename T>
35 {
36 protected:
37  void SetUp() override
38  {
39  T::SetUp();
40 
42  }
43 
44  void TearDown() override
45  {
47 
48  T::TearDown();
49  }
50 
52  {
53  // Save the current working directory.
54  cwd = os::getcwd();
55 
56  // Create a temporary directory for the test.
57  Try<std::string> directory = os::mkdtemp();
58 
59  if (directory.isError()) {
60  return Error("Failed to mkdtemp: " + directory.error());
61  }
62 
63  // We get the `realpath` of the temporary directory because some
64  // platforms, like macOS, symlink `/tmp` to `/private/var`, but
65  // return the symlink name when creating temporary directories.
66  // This is problematic because a lot of tests compare the
67  // `realpath` of a temporary file.
69 
70  if (realpath.isError()) {
71  return Error("Failed to get realpath of '" + directory.get() + "'"
72  ": " + realpath.error());
73  } else if (realpath.isNone()) {
74  return Error("Failed to get realpath of '" + directory.get() + "'"
75  ": No such directory");
76  }
77 
78  sandbox = realpath.get();
79 
80  // Run the test out of the temporary directory we created.
81  Try<Nothing> chdir = os::chdir(sandbox.get());
82 
83  if (chdir.isError()) {
84  return Error("Failed to chdir into '" + sandbox.get() + "'"
85  ": " + chdir.error());
86  }
87 
88  return Nothing();
89  }
90 
92  {
93  // Return to previous working directory and cleanup the sandbox.
95 
96  if (chdir.isError()) {
97  return Error("Failed to chdir into '" + cwd + "': " + chdir.error());
98  }
99 
100  if (sandbox.isSome()) {
102  if (rmdir.isError()) {
103  return Error("Failed to rmdir '" + sandbox.get() + "'"
104  ": " + rmdir.error());
105  }
106  }
107 
108  return Nothing();
109  }
110 
111  // A temporary directory for test purposes.
112  // Not to be confused with the "sandbox" that tasks are run in.
114 
115 private:
116  std::string cwd;
117 };
118 
119 
121  : public MixinTemporaryDirectoryTest<::testing::Test> {};
122 
123 
124 #ifdef __FreeBSD__
125 inline bool isJailed() {
126  int mib[4];
127  size_t len = 4;
128  ::sysctlnametomib("security.jail.jailed", mib, &len);
129  Try<int> jailed = os::sysctl(mib[0], mib[1], mib[2]).integer();
130  if (jailed.isSome()) {
131  return jailed.get() == 1;
132  }
133 
134  return false;
135 }
136 #endif
137 
138 #endif // __STOUT_TESTS_UTILS_HPP__
Try< Nothing > SetUpMixin()
Definition: utils.hpp:51
Try< Nothing > rmdir(const std::string &directory, bool recursive=true, bool removeRoot=true, bool continueOnError=false)
Definition: rmdir.hpp:42
Definition: nothing.hpp:16
Definition: errorbase.hpp:36
T & get()&
Definition: try.hpp:80
Definition: check.hpp:33
Result< std::string > realpath(const std::string &path)
Definition: realpath.hpp:24
std::string getcwd()
Definition: getcwd.hpp:23
Definition: check.hpp:30
bool isSome() const
Definition: option.hpp:116
Definition: utils.hpp:120
Definition: utils.hpp:34
Option< std::string > sandbox
Definition: utils.hpp:113
void SetUp() override
Definition: utils.hpp:37
bool isSome() const
Definition: try.hpp:77
const T & get() const &
Definition: option.hpp:119
Integer integer() const
Definition: sysctl.hpp:182
static Try error(const E &e)
Definition: try.hpp:43
Try< Nothing > TearDownMixin()
Definition: utils.hpp:91
bool isError() const
Definition: try.hpp:78
Definition: sysctl.hpp:59
Try< Nothing > chdir(const std::string &directory)
Definition: chdir.hpp:25
#define ASSERT_SOME(actual)
Definition: gtest.hpp:128
Try< std::string > mkdtemp(const std::string &path=path::join(os::temp(),"XXXXXX"))
Definition: mkdtemp.hpp:38
void TearDown() override
Definition: utils.hpp:44