update_engine: Make SystemState accessible from everywhere
SystemState is supposed to be a global context and is used lamost
everywhere. So instead of passing it to functions and keeping multiple
pointers to it, its better to do what we did in dlcservice and make it a
singleton class with a getter that can be get from everywhere.
BUG=b:171829801
TEST=unittests
Change-Id: I3b2de9394b7769b3911195ca52d61dbe49afd4dd
Reviewed-on: https://chromium-review.googlesource.com/c/aosp/platform/system/update_engine/+/2521792
Commit-Queue: Amin Hassani <ahassani@chromium.org>
Tested-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Jae Hoon Kim <kimjae@chromium.org>
diff --git a/download_action_unittest.cc b/download_action_unittest.cc
index 5264b0f..565c678 100644
--- a/download_action_unittest.cc
+++ b/download_action_unittest.cc
@@ -57,7 +57,9 @@
using testing::Return;
using testing::SetArgPointee;
-class DownloadActionTest : public ::testing::Test {};
+class DownloadActionTest : public ::testing::Test {
+ void SetUp() { FakeSystemState::CreateInstance(); }
+};
namespace {
@@ -128,9 +130,9 @@
void TestWithData(const brillo::Blob& data,
int fail_write,
bool use_download_delegate) {
+ FakeSystemState::CreateInstance();
brillo::FakeMessageLoop loop(nullptr);
loop.SetAsCurrent();
- FakeSystemState fake_system_state;
ScopedTempFile output_temp_file;
TestDirectFileWriter writer;
@@ -149,9 +151,9 @@
install_plan.target_slot = 1;
// We mark both slots as bootable. Only the target slot should be unbootable
// after the download starts.
- fake_system_state.fake_boot_control()->SetSlotBootable(
+ FakeSystemState::Get()->fake_boot_control()->SetSlotBootable(
install_plan.source_slot, true);
- fake_system_state.fake_boot_control()->SetSlotBootable(
+ FakeSystemState::Get()->fake_boot_control()->SetSlotBootable(
install_plan.target_slot, true);
auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
feeder_action->set_obj(install_plan);
@@ -161,9 +163,8 @@
// takes ownership of passed in HttpFetcher
auto download_action =
std::make_unique<DownloadAction>(&prefs,
- fake_system_state.boot_control(),
- fake_system_state.hardware(),
- &fake_system_state,
+ FakeSystemState::Get()->boot_control(),
+ FakeSystemState::Get()->hardware(),
http_fetcher,
false /* interactive */);
download_action->SetTestFileWriter(&writer);
@@ -194,9 +195,9 @@
loop.Run();
EXPECT_FALSE(loop.PendingTasks());
- EXPECT_TRUE(fake_system_state.fake_boot_control()->IsSlotBootable(
+ EXPECT_TRUE(FakeSystemState::Get()->fake_boot_control()->IsSlotBootable(
install_plan.source_slot));
- EXPECT_FALSE(fake_system_state.fake_boot_control()->IsSlotBootable(
+ EXPECT_FALSE(FakeSystemState::Get()->fake_boot_control()->IsSlotBootable(
install_plan.target_slot));
}
} // namespace
@@ -251,8 +252,7 @@
payload_datas.emplace_back(2 * kMockHttpFetcherChunkSize);
brillo::FakeMessageLoop loop(nullptr);
loop.SetAsCurrent();
- FakeSystemState fake_system_state;
- EXPECT_CALL(*fake_system_state.mock_payload_state(), NextPayload())
+ EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(), NextPayload())
.WillOnce(Return(true));
MockFileWriter mock_file_writer;
@@ -277,9 +277,8 @@
// takes ownership of passed in HttpFetcher
auto download_action =
std::make_unique<DownloadAction>(&prefs,
- fake_system_state.boot_control(),
- fake_system_state.hardware(),
- &fake_system_state,
+ FakeSystemState::Get()->boot_control(),
+ FakeSystemState::Get()->hardware(),
http_fetcher,
false /* interactive */);
download_action->SetTestFileWriter(&mock_file_writer);
@@ -346,6 +345,7 @@
}
void TestTerminateEarly(bool use_download_delegate) {
+ FakeSystemState::CreateInstance();
brillo::FakeMessageLoop loop(nullptr);
loop.SetAsCurrent();
@@ -362,13 +362,12 @@
InstallPlan install_plan;
install_plan.payloads.resize(1);
feeder_action->set_obj(install_plan);
- FakeSystemState fake_system_state_;
+
MockPrefs prefs;
auto download_action = std::make_unique<DownloadAction>(
&prefs,
- fake_system_state_.boot_control(),
- fake_system_state_.hardware(),
- &fake_system_state_,
+ FakeSystemState::Get()->boot_control(),
+ FakeSystemState::Get()->hardware(),
new MockHttpFetcher(data.data(), data.size(), nullptr),
false /* interactive */);
download_action->SetTestFileWriter(&writer);
@@ -461,6 +460,7 @@
} // namespace
TEST(DownloadActionTest, PassObjectOutTest) {
+ FakeSystemState::CreateInstance();
brillo::FakeMessageLoop loop(nullptr);
loop.SetAsCurrent();
@@ -475,12 +475,10 @@
auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
feeder_action->set_obj(install_plan);
MockPrefs prefs;
- FakeSystemState fake_system_state_;
auto download_action =
std::make_unique<DownloadAction>(&prefs,
- fake_system_state_.boot_control(),
- fake_system_state_.hardware(),
- &fake_system_state_,
+ FakeSystemState::Get()->boot_control(),
+ FakeSystemState::Get()->hardware(),
new MockHttpFetcher("x", 1, nullptr),
false /* interactive */);
download_action->SetTestFileWriter(&writer);
@@ -512,12 +510,15 @@
class P2PDownloadActionTest : public testing::Test {
protected:
P2PDownloadActionTest()
- : start_at_offset_(0), fake_um_(fake_system_state_.fake_clock()) {}
+ : start_at_offset_(0), fake_um_(FakeSystemState::Get()->fake_clock()) {}
~P2PDownloadActionTest() override {}
// Derived from testing::Test.
- void SetUp() override { loop_.SetAsCurrent(); }
+ void SetUp() override {
+ loop_.SetAsCurrent();
+ FakeSystemState::CreateInstance();
+ }
// Derived from testing::Test.
void TearDown() override { EXPECT_FALSE(loop_.PendingTasks()); }
@@ -539,14 +540,14 @@
"cros_au",
3,
base::TimeDelta::FromDays(5)));
- fake_system_state_.set_p2p_manager(p2p_manager_.get());
+ FakeSystemState::Get()->set_p2p_manager(p2p_manager_.get());
}
// To be called by tests to perform the download. The
// |use_p2p_to_share| parameter is used to indicate whether the
// payload should be shared via p2p.
void StartDownload(bool use_p2p_to_share) {
- EXPECT_CALL(*fake_system_state_.mock_payload_state(),
+ EXPECT_CALL(*FakeSystemState::Get()->mock_payload_state(),
GetUsingP2PForSharing())
.WillRepeatedly(Return(use_p2p_to_share));
@@ -564,9 +565,8 @@
// Note that DownloadAction takes ownership of the passed in HttpFetcher.
auto download_action = std::make_unique<DownloadAction>(
&prefs,
- fake_system_state_.boot_control(),
- fake_system_state_.hardware(),
- &fake_system_state_,
+ FakeSystemState::Get()->boot_control(),
+ FakeSystemState::Get()->hardware(),
new MockHttpFetcher(data_.c_str(), data_.length(), nullptr),
false /* interactive */);
auto http_fetcher = download_action->http_fetcher();
@@ -603,9 +603,6 @@
// The ActionProcessor used for running the actions.
ActionProcessor processor_;
- // A fake system state.
- FakeSystemState fake_system_state_;
-
// The data being downloaded.
string data_;