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 class TemporaryDirectoryTest : public ::testing::Test
34 {
35 protected:
36  void SetUp() override
37  {
38  // Save the current working directory.
39  cwd = os::getcwd();
40 
41  // Create a temporary directory for the test.
42  Try<std::string> directory = os::mkdtemp();
43  ASSERT_SOME(directory) << "Failed to mkdtemp";
44 
45  // We get the `realpath` of the temporary directory because some
46  // platforms, like macOS, symlink `/tmp` to `/private/var`, but
47  // return the symlink name when creating temporary directories.
48  // This is problematic because a lot of tests compare the
49  // `realpath` of a temporary file.
51  ASSERT_SOME(realpath) << "Failed to get realpath of '" << directory.get()
52  << "': "
53  << (realpath.isError() ? realpath.error()
54  : "No such directory");
55  sandbox = realpath.get();
56 
57  // Run the test out of the temporary directory we created.
59  << "Failed to chdir into '" << sandbox.get() << "'";
60  }
61 
62  void TearDown() override
63  {
64  // Return to previous working directory and cleanup the sandbox.
65  ASSERT_SOME(os::chdir(cwd));
66 
67  if (sandbox.isSome()) {
69  }
70  }
71 
72  // A temporary directory for test purposes.
73  // Not to be confused with the "sandbox" that tasks are run in.
75 
76 private:
77  std::string cwd;
78 };
79 
80 
81 #ifdef __FreeBSD__
82 inline bool isJailed() {
83  int mib[4];
84  size_t len = 4;
85  ::sysctlnametomib("security.jail.jailed", mib, &len);
86  Try<int> jailed = os::sysctl(mib[0], mib[1], mib[2]).integer();
87  if (jailed.isSome()) {
88  return jailed.get() == 1;
89  }
90 
91  return false;
92 }
93 #endif
94 
95 #endif // __STOUT_TESTS_UTILS_HPP__
Try< Nothing > rmdir(const std::string &directory, bool recursive=true, bool removeRoot=true, bool continueOnError=false)
Definition: rmdir.hpp:42
T & get()&
Definition: try.hpp:73
Definition: check.hpp:33
Result< std::string > realpath(const std::string &path)
Definition: realpath.hpp:24
static Result< T > error(const std::string &message)
Definition: result.hpp:53
std::string getcwd()
Definition: getcwd.hpp:23
Definition: check.hpp:30
bool isSome() const
Definition: option.hpp:115
Definition: utils.hpp:33
bool isSome() const
Definition: try.hpp:70
const T & get() const &
Definition: option.hpp:118
const T & get() const
Definition: result.hpp:115
Integer integer() const
Definition: sysctl.hpp:182
void SetUp() override
Definition: utils.hpp:36
Definition: sysctl.hpp:59
bool isError() const
Definition: result.hpp:113
Option< std::string > sandbox
Definition: utils.hpp:74
Try< Nothing > chdir(const std::string &directory)
Definition: chdir.hpp:25
#define ASSERT_SOME(actual)
Definition: gtest.hpp:128
void TearDown() override
Definition: utils.hpp:62
Try< std::string > mkdtemp(const std::string &path=path::join(os::temp(),"XXXXXX"))
Definition: mkdtemp.hpp:38