Apache Mesos
environment.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 __STOUT_TESTS_ENVIRONMENT_HPP__
18 #define __STOUT_TESTS_ENVIRONMENT_HPP__
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include <gtest/gtest.h>
25 
26 #include <stout/strings.hpp>
27 
28 namespace stout {
29 namespace internal {
30 namespace tests {
31 
33 {
34 public:
35  TestFilter() = default;
36  virtual ~TestFilter() = default;
37 
38  // Returns true iff the test should be disabled.
39  virtual bool disable(const ::testing::TestInfo* test) const = 0;
40 
41  // Returns whether the test name or parameterization matches the pattern.
42  static bool matches(
43  const ::testing::TestInfo* test,
44  const std::string& pattern)
45  {
46  if (strings::contains(test->test_case_name(), pattern) ||
47  strings::contains(test->name(), pattern)) {
48  return true;
49  }
50 
51  if (test->type_param() != nullptr &&
52  strings::contains(test->type_param(), pattern)) {
53  return true;
54  }
55 
56  if (test->value_param() != nullptr &&
57  strings::contains(test->value_param(), pattern)) {
58  return true;
59  }
60 
61  return false;
62  }
63 };
64 
65 
66 // Return list of disabled tests based on test name based filters.
67 static std::vector<std::string> disabled(
68  const ::testing::UnitTest* unitTest,
69  const std::vector<std::shared_ptr<TestFilter>>& filters)
70 {
71  std::vector<std::string> disabled;
72 
73  for (int i = 0; i < unitTest->total_test_case_count(); i++) {
74  const ::testing::TestCase* testCase = unitTest->GetTestCase(i);
75 
76  for (int j = 0; j < testCase->total_test_count(); j++) {
77  const ::testing::TestInfo* test = testCase->GetTestInfo(j);
78 
79  foreach (const std::shared_ptr<TestFilter>& filter, filters) {
80  if (filter->disable(test)) {
81  disabled.push_back(
82  test->test_case_name() + std::string(".") + test->name());
83  break;
84  }
85  }
86  }
87  }
88 
89  return disabled;
90 }
91 
92 
93 // Used to set up and manage the test environment.
94 class Environment : public ::testing::Environment
95 {
96 public:
97  // We use the constructor to setup specific tests by updating the
98  // gtest filter. We do this so that we can selectively run tests that
99  // require root or specific OS support (e.g., cgroups). Note that this
100  // should not effect any other filters that have been put in place
101  // either on the command line or via an environment variable.
102  // NOTE: This should be done before invoking `RUN_ALL_TESTS`.
103  Environment(const std::vector<std::shared_ptr<TestFilter>>& filters)
104  {
105  // First we split the current filter into enabled and disabled tests
106  // (which are separated by a '-').
107  const std::string& filtered_tests = ::testing::GTEST_FLAG(filter);
108 
109  // An empty filter indicates no tests should be run.
110  if (filtered_tests.empty()) {
111  return;
112  }
113 
114  std::string enabled_tests;
115  std::string disabled_tests;
116 
117  size_t dash = filtered_tests.find('-');
118  if (dash != std::string::npos) {
119  enabled_tests = filtered_tests.substr(0, dash);
120  disabled_tests = filtered_tests.substr(dash + 1);
121  } else {
122  enabled_tests = filtered_tests;
123  }
124 
125  // Use universal filter if not specified.
126  if (enabled_tests.empty()) {
127  enabled_tests = "*";
128  }
129 
130  // Ensure disabled tests end with ":" separator before we add more.
131  if (!disabled_tests.empty() && !strings::endsWith(disabled_tests, ":")) {
132  disabled_tests += ":";
133  }
134 
135  // Construct the filter string to handle system or platform specific tests.
136  ::testing::UnitTest* unitTest = ::testing::UnitTest::GetInstance();
137 
138  disabled_tests += strings::join(":", disabled(unitTest, filters));
139 
140  // Now update the gtest flag.
141  ::testing::GTEST_FLAG(filter) = enabled_tests + "-" + disabled_tests;
142  }
143 };
144 
145 } // namespace tests {
146 } // namespace internal {
147 } // namespace stout {
148 
149 #endif // __STOUT_TESTS_ENVIRONMENT_HPP__
bool endsWith(const std::string &s, const std::string &suffix)
Definition: strings.hpp:393
std::stringstream & join(std::stringstream &stream, const std::string &separator, T &&...args)
Definition: strings.hpp:306
bool contains(const std::string &s, const std::string &substr)
Definition: strings.hpp:406
static bool matches(const ::testing::TestInfo *test, const std::string &pattern)
Definition: environment.hpp:42
Result< std::vector< Filter< Classifier > > > filters(const std::string &_link, const Handle &parent)
Definition: internal.hpp:776
Definition: environment.hpp:94
Definition: attributes.hpp:24
Definition: environment.hpp:28
Environment(const std::vector< std::shared_ptr< TestFilter >> &filters)
Definition: environment.hpp:103
Definition: environment.hpp:32
virtual bool disable(const ::testing::TestInfo *test) const =0
void filter(Filter *filter)