libsnapshot: Switch to IPropertyFetcher.

vts_libsnapshot_test uses IPropertyFetcher, as does liblp, but
libsnapshot doesn't. Fix this by switching relevant cases in
utility.cpp.

This also changes SnapshotTestPropertyFetcher from a mocked class to a
pseudo-implementation. This is useful because sometimes we want the
actual device's properties and sometimes we don't, and this setup is
more flexible.

Bug: 208944665
Test: vts_libsnapshot_test
Change-Id: I58621281715b3efb045b6be6f788934fddaa1a0c
This commit is contained in:
David Anderson 2022-07-28 11:27:07 -07:00
parent d3ecda57c4
commit 76c6c5bc2f
4 changed files with 62 additions and 19 deletions

View file

@ -17,6 +17,7 @@
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <android-base/file.h>
@ -165,27 +166,25 @@ class DeviceMapperWrapper : public android::dm::IDeviceMapper {
android::dm::IDeviceMapper& impl_;
};
class SnapshotTestPropertyFetcher : public android::fs_mgr::testing::MockPropertyFetcher {
class SnapshotTestPropertyFetcher : public android::fs_mgr::IPropertyFetcher {
public:
SnapshotTestPropertyFetcher(const std::string& slot_suffix) {
using testing::Return;
ON_CALL(*this, GetProperty("ro.boot.slot_suffix", _)).WillByDefault(Return(slot_suffix));
ON_CALL(*this, GetBoolProperty("ro.boot.dynamic_partitions", _))
.WillByDefault(Return(true));
ON_CALL(*this, GetBoolProperty("ro.boot.dynamic_partitions_retrofit", _))
.WillByDefault(Return(false));
ON_CALL(*this, GetBoolProperty("ro.virtual_ab.enabled", _)).WillByDefault(Return(true));
}
explicit SnapshotTestPropertyFetcher(const std::string& slot_suffix,
std::unordered_map<std::string, std::string>&& props = {});
std::string GetProperty(const std::string& key, const std::string& defaultValue) override;
bool GetBoolProperty(const std::string& key, bool defaultValue) override;
static void SetUp(const std::string& slot_suffix = "_a") { Reset(slot_suffix); }
static void TearDown() { Reset("_a"); }
private:
static void Reset(const std::string& slot_suffix) {
IPropertyFetcher::OverrideForTesting(
std::make_unique<NiceMock<SnapshotTestPropertyFetcher>>(slot_suffix));
std::make_unique<SnapshotTestPropertyFetcher>(slot_suffix));
}
private:
std::unordered_map<std::string, std::string> properties_;
};
// Helper for error-spam-free cleanup.

View file

@ -627,7 +627,7 @@ TEST_F(SnapshotTest, FirstStageMountAndMerge) {
if (gIsSnapuserdRequired) {
ASSERT_EQ(status.compression_algorithm(), "gz");
} else {
ASSERT_EQ(status.compression_algorithm(), "none");
ASSERT_EQ(status.compression_algorithm(), "");
}
DeviceMapper::TargetInfo target;

View file

@ -18,10 +18,12 @@
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/parsebool.h>
#include <android-base/properties.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <gtest/gtest.h>
#include <liblp/property_fetcher.h>
#include <openssl/sha.h>
#include <payload_consumer/file_descriptor.h>
@ -278,5 +280,38 @@ bool IsVirtualAbEnabled() {
return android::base::GetBoolProperty("ro.virtual_ab.enabled", false);
}
SnapshotTestPropertyFetcher::SnapshotTestPropertyFetcher(
const std::string& slot_suffix, std::unordered_map<std::string, std::string>&& props)
: properties_(std::move(props)) {
properties_["ro.boot.slot_suffix"] = slot_suffix;
properties_["ro.boot.dynamic_partitions"] = "true";
properties_["ro.boot.dynamic_partitions_retrofit"] = "false";
properties_["ro.virtual_ab.enabled"] = "true";
}
std::string SnapshotTestPropertyFetcher::GetProperty(const std::string& key,
const std::string& defaultValue) {
auto iter = properties_.find(key);
if (iter == properties_.end()) {
return android::base::GetProperty(key, defaultValue);
}
return iter->second;
}
bool SnapshotTestPropertyFetcher::GetBoolProperty(const std::string& key, bool defaultValue) {
auto iter = properties_.find(key);
if (iter == properties_.end()) {
return android::base::GetBoolProperty(key, defaultValue);
}
switch (android::base::ParseBool(iter->second)) {
case android::base::ParseBoolResult::kTrue:
return true;
case android::base::ParseBoolResult::kFalse:
return false;
default:
return defaultValue;
}
}
} // namespace snapshot
} // namespace android

View file

@ -26,6 +26,7 @@
#include <android-base/properties.h>
#include <android-base/strings.h>
#include <fs_mgr/roots.h>
#include <liblp/property_fetcher.h>
using android::dm::kSectorSize;
using android::fiemap::FiemapStatus;
@ -33,6 +34,7 @@ using android::fs_mgr::EnsurePathMounted;
using android::fs_mgr::EnsurePathUnmounted;
using android::fs_mgr::Fstab;
using android::fs_mgr::GetEntryForPath;
using android::fs_mgr::IPropertyFetcher;
using android::fs_mgr::MetadataBuilder;
using android::fs_mgr::Partition;
using android::fs_mgr::ReadDefaultFstab;
@ -185,11 +187,13 @@ void AppendExtent(RepeatedPtrField<chromeos_update_engine::Extent>* extents, uin
}
bool GetLegacyCompressionEnabledProperty() {
return android::base::GetBoolProperty("ro.virtual_ab.compression.enabled", false);
auto fetcher = IPropertyFetcher::GetInstance();
return fetcher->GetBoolProperty("ro.virtual_ab.compression.enabled", false);
}
bool GetUserspaceSnapshotsEnabledProperty() {
return android::base::GetBoolProperty("ro.virtual_ab.userspace.snapshots.enabled", false);
auto fetcher = IPropertyFetcher::GetInstance();
return fetcher->GetBoolProperty("ro.virtual_ab.userspace.snapshots.enabled", false);
}
bool CanUseUserspaceSnapshots() {
@ -197,9 +201,11 @@ bool CanUseUserspaceSnapshots() {
return false;
}
auto fetcher = IPropertyFetcher::GetInstance();
const std::string UNKNOWN = "unknown";
const std::string vendor_release =
android::base::GetProperty("ro.vendor.build.version.release_or_codename", UNKNOWN);
fetcher->GetProperty("ro.vendor.build.version.release_or_codename", UNKNOWN);
// No user-space snapshots if vendor partition is on Android 12
if (vendor_release.find("12") != std::string::npos) {
@ -217,11 +223,13 @@ bool CanUseUserspaceSnapshots() {
}
bool GetIouringEnabledProperty() {
return android::base::GetBoolProperty("ro.virtual_ab.io_uring.enabled", false);
auto fetcher = IPropertyFetcher::GetInstance();
return fetcher->GetBoolProperty("ro.virtual_ab.io_uring.enabled", false);
}
bool GetXorCompressionEnabledProperty() {
return android::base::GetBoolProperty("ro.virtual_ab.compression.xor.enabled", false);
auto fetcher = IPropertyFetcher::GetInstance();
return fetcher->GetBoolProperty("ro.virtual_ab.compression.xor.enabled", false);
}
std::string GetOtherPartitionName(const std::string& name) {
@ -233,7 +241,8 @@ std::string GetOtherPartitionName(const std::string& name) {
}
bool IsDmSnapshotTestingEnabled() {
return android::base::GetBoolProperty("snapuserd.test.dm.snapshots", false);
auto fetcher = IPropertyFetcher::GetInstance();
return fetcher->GetBoolProperty("snapuserd.test.dm.snapshots", false);
}
} // namespace snapshot