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/rmdir.hpp>
27 
28 #if __FreeBSD__
29 #include <stout/os/sysctl.hpp>
30 #endif
31 
32 class TemporaryDirectoryTest : public ::testing::Test
33 {
34 protected:
35  virtual void SetUp()
36  {
37  // Save the current working directory.
38  cwd = os::getcwd();
39 
40  // Create a temporary directory for the test.
41  Try<std::string> directory = os::mkdtemp();
42 
43  ASSERT_SOME(directory) << "Failed to mkdtemp";
44 
45  sandbox = directory.get();
46 
47  // Run the test out of the temporary directory we created.
49  << "Failed to chdir into '" << sandbox.get() << "'";
50  }
51 
52  virtual void TearDown()
53  {
54  // Return to previous working directory and cleanup the sandbox.
55  ASSERT_SOME(os::chdir(cwd));
56 
57  if (sandbox.isSome()) {
59  }
60  }
61 
62  // A temporary directory for test purposes.
63  // Not to be confused with the "sandbox" that tasks are run in.
65 
66 private:
67  std::string cwd;
68 };
69 
70 
71 #ifdef __FreeBSD__
72 inline bool isJailed() {
73  int mib[4];
74  size_t len = 4;
75  ::sysctlnametomib("security.jail.jailed", mib, &len);
76  Try<int> jailed = os::sysctl(mib[0], mib[1], mib[2]).integer();
77  if (jailed.isSome()) {
78  return jailed.get() == 1;
79  }
80 
81  return false;
82 }
83 #endif
84 
85 #endif // __STOUT_TESTS_UTILS_HPP__
Try< Nothing > rmdir(const std::string &directory, bool recursive=true, bool removeRoot=true, bool continueOnError=false)
Definition: rmdir.hpp:43
T & get()&
Definition: try.hpp:73
Definition: check.hpp:33
std::string getcwd()
Definition: getcwd.hpp:23
virtual void TearDown()
Definition: utils.hpp:52
bool isSome() const
Definition: option.hpp:115
Definition: utils.hpp:32
bool isSome() const
Definition: try.hpp:70
const T & get() const &
Definition: option.hpp:118
Integer integer() const
Definition: sysctl.hpp:182
Definition: sysctl.hpp:59
virtual void SetUp()
Definition: utils.hpp:35
Option< std::string > sandbox
Definition: utils.hpp:64
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