Clean up mount tests.

This change makes it simpler to write mount tests that examine the optional
field of mountinfo. It also cleans up the mounts in existing tests so they
don't pollute mountinfo after exit.

PiperOrigin-RevId: 561439839
This commit is contained in:
Lucas Manning
2023-08-30 13:56:03 -07:00
committed by gVisor bot
parent 18961d06f1
commit c2a7efe6a2
4 changed files with 209 additions and 261 deletions
File diff suppressed because it is too large Load Diff
+2
View File
@@ -167,8 +167,10 @@ cc_library(
deps = [
":cleanup",
":posix_error",
":temp_path",
":test_util",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
gtest,
"@com_google_absl//absl/types:span",
+51
View File
@@ -17,10 +17,17 @@
#include <sys/syscall.h>
#include <unistd.h>
#include <cerrno>
#include <string>
#include <string_view>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
#include "absl/types/span.h"
#include "test/util/posix_error.h"
namespace gvisor {
namespace testing {
@@ -176,5 +183,49 @@ absl::flat_hash_map<std::string, std::string> ParseMountOptions(
return entries;
}
PosixErrorOr<absl::flat_hash_map<std::string, std::vector<MountOptional>>>
MountOptionals() {
absl::flat_hash_map<std::string, std::vector<MountOptional>> optionals;
ASSIGN_OR_RETURN_ERRNO(std::vector<ProcMountInfoEntry> mounts,
ProcSelfMountInfoEntries());
for (const auto& e : mounts) {
MountOptional opt;
opt.shared = 0;
opt.master = 0;
opt.propagate_from = 0;
std::vector<std::string_view> tags = absl::StrSplit(e.optional, ' ');
for (std::string_view tag : tags) {
PosixError err = ParseOptionalTag(tag, &opt);
if (!err.ok()) return err;
}
if (optionals.contains(e.mount_point)) {
optionals[e.mount_point].push_back(opt);
} else {
optionals[e.mount_point] = {opt};
}
}
return optionals;
}
PosixError ParseOptionalTag(std::string_view tag, MountOptional* opt) {
std::vector<std::string_view> key_value = absl::StrSplit(tag, ':');
if (key_value.size() != 2) return PosixError(0);
if (key_value[0] == "shared") {
if (!absl::SimpleAtoi(key_value[1], &opt->shared))
return PosixError(EINVAL,
"could not parse shared value in optional string");
} else if (key_value[0] == "master") {
if (!absl::SimpleAtoi(key_value[1], &opt->master))
return PosixError(EINVAL,
"could not parse master value in optional string");
} else if (key_value[0] == "propagate_from") {
if (!absl::SimpleAtoi(key_value[1], &opt->propagate_from))
return PosixError(EINVAL,
"could not parse propagate_from in optional string");
}
return PosixError(0);
}
} // namespace testing
} // namespace gvisor
+19
View File
@@ -20,6 +20,8 @@
#include <functional>
#include <string>
#include <string_view>
#include <vector>
#include "gmock/gmock.h"
#include "absl/container/flat_hash_map.h"
@@ -93,6 +95,23 @@ PosixErrorOr<std::vector<ProcMountInfoEntry>> ProcSelfMountInfoEntriesFrom(
absl::flat_hash_map<std::string, std::string> ParseMountOptions(
std::string mopts);
struct MountOptional {
int shared;
int master;
int propagate_from;
};
// MountOptionals returns a map of mount points to their optional fields as
// found in mountinfo. Duplicate mount points with different mount points will
// map to a vector of optionals. The order of the optionals is determined by
// their order in mountinfo (the order they were mounted in).
PosixErrorOr<absl::flat_hash_map<std::string, std::vector<MountOptional>>>
MountOptionals();
// ParseOptionalTag is a helper that parses a single entry in a mount's
// set of optional tags.
PosixError ParseOptionalTag(std::string_view tag, MountOptional* opt);
} // namespace testing
} // namespace gvisor