xref: /openbmc/google-ipmi-sys/test/bm_mode_transition_unittest.cpp (revision 8c0094e4eb82029af1d111d2736111cbdda59b63)
115d4d21cSHao Zhou // Copyright 2023 Google LLC
215d4d21cSHao Zhou //
315d4d21cSHao Zhou // Licensed under the Apache License, Version 2.0 (the "License");
415d4d21cSHao Zhou // you may not use this file except in compliance with the License.
515d4d21cSHao Zhou // You may obtain a copy of the License at
615d4d21cSHao Zhou //
715d4d21cSHao Zhou //      http://www.apache.org/licenses/LICENSE-2.0
815d4d21cSHao Zhou //
915d4d21cSHao Zhou // Unless required by applicable law or agreed to in writing, software
1015d4d21cSHao Zhou // distributed under the License is distributed on an "AS IS" BASIS,
1115d4d21cSHao Zhou // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1215d4d21cSHao Zhou // See the License for the specific language governing permissions and
1315d4d21cSHao Zhou // limitations under the License.
1415d4d21cSHao Zhou #include "bm_config.h"
1515d4d21cSHao Zhou 
1615d4d21cSHao Zhou #include "bmc_mode_enum.hpp"
1715d4d21cSHao Zhou #include "errors.hpp"
1815d4d21cSHao Zhou #include "file_system_mock.hpp"
1915d4d21cSHao Zhou #include "handler.hpp"
2015d4d21cSHao Zhou #include "handler_impl.hpp"
2115d4d21cSHao Zhou 
2215d4d21cSHao Zhou #include <filesystem>
2315d4d21cSHao Zhou #include <fstream>
2415d4d21cSHao Zhou #include <functional>
2515d4d21cSHao Zhou #include <memory>
2615d4d21cSHao Zhou #include <string>
2715d4d21cSHao Zhou 
2815d4d21cSHao Zhou #include <gtest/gtest.h>
2915d4d21cSHao Zhou 
3015d4d21cSHao Zhou namespace google
3115d4d21cSHao Zhou {
3215d4d21cSHao Zhou namespace ipmi
3315d4d21cSHao Zhou {
3415d4d21cSHao Zhou using ::testing::_;
3515d4d21cSHao Zhou using ::testing::Eq;
3615d4d21cSHao Zhou using ::testing::IsNull;
3715d4d21cSHao Zhou using ::testing::NotNull;
3815d4d21cSHao Zhou using ::testing::Pointee;
3915d4d21cSHao Zhou using ::testing::Return;
4015d4d21cSHao Zhou using ::testing::StrEq;
4115d4d21cSHao Zhou 
4215d4d21cSHao Zhou namespace fs = std::filesystem;
4315d4d21cSHao Zhou 
4415d4d21cSHao Zhou class MockFsHandler : public Handler
4515d4d21cSHao Zhou {
4615d4d21cSHao Zhou   public:
MockFsHandler(std::unique_ptr<FileSystemMock> mock,const std::string & config="")4715d4d21cSHao Zhou     MockFsHandler(std::unique_ptr<FileSystemMock> mock,
48*8c0094e4SPatrick Williams                   const std::string& config = "") : Handler(config)
4915d4d21cSHao Zhou     {
5015d4d21cSHao Zhou         fsPtr_ = move(mock);
5115d4d21cSHao Zhou     }
5215d4d21cSHao Zhou 
5315d4d21cSHao Zhou   protected:
getFs() const5415d4d21cSHao Zhou     const std::unique_ptr<FileSystemInterface>& getFs() const override
5515d4d21cSHao Zhou     {
5615d4d21cSHao Zhou         return fsPtr_;
5715d4d21cSHao Zhou     }
5815d4d21cSHao Zhou 
5915d4d21cSHao Zhou   private:
6015d4d21cSHao Zhou     std::unique_ptr<FileSystemInterface> fsPtr_;
6115d4d21cSHao Zhou };
6215d4d21cSHao Zhou 
TEST(BmcModeTransitionTest,DriveCleaningDoneAckFlag)6315d4d21cSHao Zhou TEST(BmcModeTransitionTest, DriveCleaningDoneAckFlag)
6415d4d21cSHao Zhou {
6515d4d21cSHao Zhou     auto fsMockPtr = std::make_unique<FileSystemMock>();
6615d4d21cSHao Zhou     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
6715d4d21cSHao Zhou         .WillOnce(Return(true));
6815d4d21cSHao Zhou 
6915d4d21cSHao Zhou     MockFsHandler h(move(fsMockPtr));
7015d4d21cSHao Zhou     EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::BM_MODE));
7115d4d21cSHao Zhou }
7215d4d21cSHao Zhou 
TEST(BmcModeTransitionTest,DriveCleaningDoneFlag)7315d4d21cSHao Zhou TEST(BmcModeTransitionTest, DriveCleaningDoneFlag)
7415d4d21cSHao Zhou {
7515d4d21cSHao Zhou     auto fsMockPtr = std::make_unique<FileSystemMock>();
7615d4d21cSHao Zhou     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
7715d4d21cSHao Zhou         .WillOnce(Return(false));
7815d4d21cSHao Zhou     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneFlagPath), _))
7915d4d21cSHao Zhou         .WillOnce(Return(true));
8015d4d21cSHao Zhou     EXPECT_CALL(*fsMockPtr, rename(fs::path(bmDriveCleaningDoneFlagPath),
8115d4d21cSHao Zhou                                    fs::path(bmDriveCleaningDoneAckFlagPath), _))
8215d4d21cSHao Zhou         .Times(1);
8315d4d21cSHao Zhou 
8415d4d21cSHao Zhou     MockFsHandler h(move(fsMockPtr));
8515d4d21cSHao Zhou     EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::BM_MODE));
8615d4d21cSHao Zhou }
8715d4d21cSHao Zhou 
TEST(BmcModeTransitionTest,FirstCleaningFlag)8815d4d21cSHao Zhou TEST(BmcModeTransitionTest, FirstCleaningFlag)
8915d4d21cSHao Zhou {
9015d4d21cSHao Zhou     auto fsMockPtr = std::make_unique<FileSystemMock>();
9115d4d21cSHao Zhou     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
9215d4d21cSHao Zhou         .WillOnce(Return(false));
9315d4d21cSHao Zhou     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneFlagPath), _))
9415d4d21cSHao Zhou         .WillOnce(Return(false));
9515d4d21cSHao Zhou     EXPECT_CALL(*fsMockPtr, exists(fs::path(BM_SIGNAL_PATH), _))
9615d4d21cSHao Zhou         .WillOnce(Return(true));
9715d4d21cSHao Zhou     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningFlagPath), _))
9815d4d21cSHao Zhou         .WillOnce(Return(false));
9915d4d21cSHao Zhou     EXPECT_CALL(*fsMockPtr, create(StrEq(bmDriveCleaningFlagPath))).Times(1);
10015d4d21cSHao Zhou 
10115d4d21cSHao Zhou     MockFsHandler h(move(fsMockPtr));
10215d4d21cSHao Zhou     EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::BM_CLEANING_MODE));
10315d4d21cSHao Zhou }
10415d4d21cSHao Zhou 
TEST(BmcModeTransitionTest,NonBmMode)10515d4d21cSHao Zhou TEST(BmcModeTransitionTest, NonBmMode)
10615d4d21cSHao Zhou {
10715d4d21cSHao Zhou     auto fsMockPtr = std::make_unique<FileSystemMock>();
10815d4d21cSHao Zhou     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
10915d4d21cSHao Zhou         .WillOnce(Return(false));
11015d4d21cSHao Zhou     EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneFlagPath), _))
11115d4d21cSHao Zhou         .WillOnce(Return(false));
11215d4d21cSHao Zhou     EXPECT_CALL(*fsMockPtr, exists(fs::path(BM_SIGNAL_PATH), _))
11315d4d21cSHao Zhou         .WillOnce(Return(false));
11415d4d21cSHao Zhou     MockFsHandler h(move(fsMockPtr));
11515d4d21cSHao Zhou     EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::NON_BM_MODE));
11615d4d21cSHao Zhou }
11715d4d21cSHao Zhou 
11815d4d21cSHao Zhou } // namespace ipmi
11915d4d21cSHao Zhou } // namespace google
120