| #include "internal_boot_counter.h" |
| |
| #include <optional> |
| #include <string> |
| |
| #include "file/base/helpers.h" |
| #include "file/base/options.h" |
| #include "file/base/path.h" |
| #include "state_persistence.pb.h" |
| #include "test_daemon_context.h" |
| #include "gmock/gmock.h" |
| #include "gtest/gtest.h" |
| #include "absl/status/status.h" |
| #include "absl/strings/string_view.h" |
| |
| namespace safepower_agent { |
| namespace { |
| |
| using ::testing::_; |
| using ::testing::Return; |
| |
| std::string GetBootIdPath() { |
| // Create a temporary file for boot_id |
| return file::JoinPath( |
| testing::TempDir(), |
| testing::UnitTest::GetInstance()->current_test_info()->name()); |
| } |
| |
| void WriteBootId(absl::string_view boot_id_path, absl::string_view boot_id) { |
| ASSERT_OK(file::SetContents(boot_id_path, boot_id, file::Defaults())); |
| } |
| |
| TEST(InternalBootCounterTest, IncrementBootCounterOnFirstRun) { |
| TestDaemonContext daemon_context; |
| std::string boot_id_path = GetBootIdPath(); |
| WriteBootId(boot_id_path, "boot-id-1"); |
| |
| EXPECT_CALL(daemon_context.mock_persistent_storage_manager(), |
| ReadSavedActions()) |
| .WillOnce(Return(safepower_agent_persistence_proto::SavedActions())); |
| |
| EXPECT_CALL( |
| daemon_context.mock_persistent_storage_manager(), |
| WriteSavedActionsChange(testing::Property( |
| &safepower_agent_persistence_proto::SavedActions::boot_counter, 1))) |
| .WillOnce(Return(absl::OkStatus())); |
| |
| InternalBootCounter boot_counter(boot_id_path); |
| EXPECT_EQ(boot_counter.CheckAndIncrement(), absl::OkStatus()); |
| EXPECT_EQ(boot_counter.value(), 1); |
| remove(boot_id_path.c_str()); |
| } |
| |
| TEST(InternalBootCounterTest, DoesNotIncrementIfBootIdIsSame) { |
| TestDaemonContext daemon_context; |
| std::string boot_id_path = GetBootIdPath(); |
| WriteBootId(boot_id_path, "boot-id-1"); |
| |
| safepower_agent_persistence_proto::SavedActions saved_actions; |
| saved_actions.set_boot_id("boot-id-1"); |
| saved_actions.set_boot_counter(5); |
| |
| EXPECT_CALL(daemon_context.mock_persistent_storage_manager(), |
| ReadSavedActions()) |
| .WillOnce(Return(saved_actions)); |
| |
| EXPECT_CALL(daemon_context.mock_persistent_storage_manager(), |
| WriteSavedActionsChange(_)) |
| .Times(0); |
| |
| InternalBootCounter boot_counter(boot_id_path); |
| EXPECT_EQ(boot_counter.CheckAndIncrement(), absl::OkStatus()); |
| EXPECT_EQ(boot_counter.value(), 5); |
| remove(boot_id_path.c_str()); |
| } |
| |
| TEST(InternalBootCounterTest, IncrementsIfBootIdChanges) { |
| TestDaemonContext daemon_context; |
| std::string boot_id_path = GetBootIdPath(); |
| WriteBootId(boot_id_path, "boot-id-2"); |
| |
| safepower_agent_persistence_proto::SavedActions saved_actions; |
| saved_actions.set_boot_id("boot-id-1"); |
| saved_actions.set_boot_counter(5); |
| |
| EXPECT_CALL(daemon_context.mock_persistent_storage_manager(), |
| ReadSavedActions()) |
| .WillOnce(Return(saved_actions)); |
| |
| EXPECT_CALL( |
| daemon_context.mock_persistent_storage_manager(), |
| WriteSavedActionsChange(testing::Property( |
| &safepower_agent_persistence_proto::SavedActions::boot_counter, 6))) |
| .WillOnce(Return(absl::OkStatus())); |
| |
| InternalBootCounter boot_counter(boot_id_path); |
| EXPECT_EQ(boot_counter.CheckAndIncrement(), absl::OkStatus()); |
| EXPECT_EQ(boot_counter.value(), 6); |
| remove(boot_id_path.c_str()); |
| } |
| |
| TEST(InternalBootCounterTest, ReturnsErrorIfReadFails) { |
| TestDaemonContext daemon_context; |
| std::string boot_id_path = GetBootIdPath(); |
| WriteBootId(boot_id_path, "boot-id-1"); |
| |
| EXPECT_CALL(daemon_context.mock_persistent_storage_manager(), |
| ReadSavedActions()) |
| .WillOnce(Return(absl::InternalError("Read failed"))); |
| |
| InternalBootCounter boot_counter(boot_id_path); |
| EXPECT_EQ(boot_counter.CheckAndIncrement(), |
| absl::InternalError("Read failed")); |
| EXPECT_EQ(boot_counter.value(), std::nullopt); |
| remove(boot_id_path.c_str()); |
| } |
| |
| TEST(InternalBootCounterTest, UpdatesCacheEvenIfWriteFails) { |
| TestDaemonContext daemon_context; |
| std::string boot_id_path = GetBootIdPath(); |
| WriteBootId(boot_id_path, "boot-id-2"); |
| |
| safepower_agent_persistence_proto::SavedActions saved_actions; |
| saved_actions.set_boot_id("boot-id-1"); |
| saved_actions.set_boot_counter(5); |
| |
| EXPECT_CALL(daemon_context.mock_persistent_storage_manager(), |
| ReadSavedActions()) |
| .WillOnce(Return(saved_actions)); |
| |
| EXPECT_CALL(daemon_context.mock_persistent_storage_manager(), |
| WriteSavedActionsChange(_)) |
| .WillOnce(Return(absl::InternalError("Write failed"))); |
| |
| InternalBootCounter boot_counter(boot_id_path); |
| EXPECT_EQ(boot_counter.CheckAndIncrement(), |
| absl::InternalError("Write failed")); |
| EXPECT_EQ(boot_counter.value(), 6); |
| remove(boot_id_path.c_str()); |
| } |
| |
| } // namespace |
| } // namespace safepower_agent |