summaryrefslogtreecommitdiffhomepage
path: root/test/util
diff options
context:
space:
mode:
authorFabricio Voznika <fvoznika@google.com>2021-04-22 20:04:17 -0700
committergVisor bot <gvisor-bot@google.com>2021-04-22 20:06:37 -0700
commit9e4aa04ad1f6e63f4aa99f516b19e4ff2592918b (patch)
treeee636bcd7428f8f4e20d84c08c8487d806b69ce3 /test/util
parentd1859fe179bae1614ba91a45497ad63400210863 (diff)
Remove side effect from mount tests
Dropping CAP_SYS_ADMIN and not restoring it causes other tests to be skipped. PiperOrigin-RevId: 370002644
Diffstat (limited to 'test/util')
-rw-r--r--test/util/capability_util.h13
-rw-r--r--test/util/posix_error.h7
2 files changed, 18 insertions, 2 deletions
diff --git a/test/util/capability_util.h b/test/util/capability_util.h
index a03bc7e05..f2c370125 100644
--- a/test/util/capability_util.h
+++ b/test/util/capability_util.h
@@ -99,14 +99,23 @@ PosixErrorOr<bool> CanCreateUserNamespace();
class AutoCapability {
public:
AutoCapability(int cap, bool set) : cap_(cap), set_(set) {
- EXPECT_NO_ERRNO(SetCapability(cap_, set_));
+ const bool has = EXPECT_NO_ERRNO_AND_VALUE(HaveCapability(cap));
+ if (set != has) {
+ EXPECT_NO_ERRNO(SetCapability(cap_, set_));
+ applied_ = true;
+ }
}
- ~AutoCapability() { EXPECT_NO_ERRNO(SetCapability(cap_, !set_)); }
+ ~AutoCapability() {
+ if (applied_) {
+ EXPECT_NO_ERRNO(SetCapability(cap_, !set_));
+ }
+ }
private:
int cap_;
bool set_;
+ bool applied_ = false;
};
} // namespace testing
diff --git a/test/util/posix_error.h b/test/util/posix_error.h
index 27557ad44..9ca09b77c 100644
--- a/test/util/posix_error.h
+++ b/test/util/posix_error.h
@@ -438,6 +438,13 @@ IsPosixErrorOkAndHolds(InnerMatcher&& inner_matcher) {
std::move(_expr_result).ValueOrDie(); \
})
+#define EXPECT_NO_ERRNO_AND_VALUE(expr) \
+ ({ \
+ auto _expr_result = (expr); \
+ EXPECT_NO_ERRNO(_expr_result); \
+ std::move(_expr_result).ValueOrDie(); \
+ })
+
} // namespace testing
} // namespace gvisor