Apache Mesos
utils.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __TESTS_UTILS_HPP__
18 #define __TESTS_UTILS_HPP__
19 
20 #include <stdint.h>
21 
22 #include <string>
23 
24 #include <stout/ip.hpp>
25 #include <stout/json.hpp>
26 #include <stout/try.hpp>
27 
28 namespace mesos {
29 namespace internal {
30 namespace tests {
31 
32 // Get the metrics snapshot.
33 // TODO(vinod): Move this into a libprocess utility header.
34 // TODO(bevers): Rename this to `getMetrics()`.
36 
37 template<typename T>
38 bool metricEquals(const std::string& metric, const T& value) {
40 
41  // NOTE: We can assume `JSON::Number` because all our
42  // metric classes (counters, gauges) do return numbers.
43  const Result<JSON::Number>& result = metrics.at<JSON::Number>(metric);
44  if (!result.isSome()) {
45  return false;
46  }
47 
48  return result->as<T>() == value;
49 }
50 
51 // Path finding utilities.
52 //
53 // Various tests need to access paths to load files or libraries. Normally,
54 // these paths are located within the build directory, often in the .libs
55 // directories generated by Automake. However, if the Mesos tests were built
56 // with --enable-tests-install, we may be running the tests from an installed
57 // version of the test suite. In this case, we should always find paths in the
58 // installation tree and never the build directory. If the build directory is
59 // still present, you can force these helpers to ignore it by passing the
60 // --build_dir=/none option to the test suite.
61 //
62 // The path helpers below all check for a build directory path, then fall back
63 // to an installation path only if --enable-tests-install was specified at
64 // configuration time.
65 
66 // Get the path to a named Mesos module.
67 std::string getModulePath(const std::string& name);
68 
69 // Get the path to libmesos-$(VERSION).so
70 std::string getLibMesosPath();
71 
72 // Get the launcher directory for mesos-executor, mesos-containerizer, etc.
73 std::string getLauncherDir();
74 
75 // Get the path to a named test helper.
76 std::string getTestHelperPath(const std::string& name);
77 
78 // Get the path to a named test script. In the Mesos source tree,
79 // these live in $MESOS_SOURCE_DIR/src/tests. In a tests install
80 // they live in $TESTLIBEXECDIR.
81 std::string getTestScriptPath(const std::string& name);
82 
83 // Get the path to the directory where test helpers are installed.
84 std::string getTestHelperDir();
85 
86 // Get the path to the directory where executables that require super-
87 // user privileges are installed.
88 std::string getSbinDir();
89 
90 // Get the path to the directory of the webui files/assets.
91 std::string getWebUIDir();
92 
93 // Get a random unused port.
94 //
95 // NOTE: While we make sure that the port returned by this function is
96 // unused when this function returns, it might become used before the
97 // caller can bind to it.
99 
100 // Get a non-loopback IP available on this host.
102 
103 } // namespace tests {
104 } // namespace internal {
105 } // namespace mesos {
106 
107 #endif // __TESTS_UTILS_HPP__
T as() const
Definition: json.hpp:120
Try< uint16_t > getFreePort()
Definition: check.hpp:33
std::string getSbinDir()
std::string getTestHelperDir()
Definition: check.hpp:30
Definition: json.hpp:158
Result< T > at(const std::string &key) const
Definition: json.hpp:478
Try< net::IP::Network > getNonLoopbackIP()
Definition: agent.hpp:25
std::string getLauncherDir()
std::string getLibMesosPath()
Definition: attributes.hpp:24
bool metricEquals(const std::string &metric, const T &value)
Definition: utils.hpp:38
std::string getTestScriptPath(const std::string &name)
Definition: json.hpp:93
std::string getWebUIDir()
std::string getModulePath(const std::string &name)
bool isSome() const
Definition: result.hpp:112
JSON::Object Metrics()
std::string getTestHelperPath(const std::string &name)
PID< MetricsProcess > metrics
constexpr const char * name
Definition: shell.hpp:41