blob: 230273492a9a3b5be5d82a4b81434dabcd0847fb [file] [log] [blame]
Hao Zhou15d4d212023-07-11 20:18:04 +00001// Copyright 2023 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14#include "bm_config.h"
15
16#include "bmc_mode_enum.hpp"
17#include "errors.hpp"
18#include "file_system_mock.hpp"
19#include "handler.hpp"
20#include "handler_impl.hpp"
21
22#include <filesystem>
23#include <fstream>
24#include <functional>
25#include <memory>
26#include <string>
27
28#include <gtest/gtest.h>
29
30namespace google
31{
32namespace ipmi
33{
34using ::testing::_;
35using ::testing::Eq;
36using ::testing::IsNull;
37using ::testing::NotNull;
38using ::testing::Pointee;
39using ::testing::Return;
40using ::testing::StrEq;
41
42namespace fs = std::filesystem;
43
44class MockFsHandler : public Handler
45{
46 public:
47 MockFsHandler(std::unique_ptr<FileSystemMock> mock,
Patrick Williams8c0094e2024-08-16 15:22:37 -040048 const std::string& config = "") : Handler(config)
Hao Zhou15d4d212023-07-11 20:18:04 +000049 {
50 fsPtr_ = move(mock);
51 }
52
53 protected:
54 const std::unique_ptr<FileSystemInterface>& getFs() const override
55 {
56 return fsPtr_;
57 }
58
59 private:
60 std::unique_ptr<FileSystemInterface> fsPtr_;
61};
62
63TEST(BmcModeTransitionTest, DriveCleaningDoneAckFlag)
64{
65 auto fsMockPtr = std::make_unique<FileSystemMock>();
66 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
67 .WillOnce(Return(true));
68
69 MockFsHandler h(move(fsMockPtr));
70 EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::BM_MODE));
71}
72
73TEST(BmcModeTransitionTest, DriveCleaningDoneFlag)
74{
75 auto fsMockPtr = std::make_unique<FileSystemMock>();
76 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
77 .WillOnce(Return(false));
78 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneFlagPath), _))
79 .WillOnce(Return(true));
80 EXPECT_CALL(*fsMockPtr, rename(fs::path(bmDriveCleaningDoneFlagPath),
81 fs::path(bmDriveCleaningDoneAckFlagPath), _))
82 .Times(1);
83
84 MockFsHandler h(move(fsMockPtr));
85 EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::BM_MODE));
86}
87
88TEST(BmcModeTransitionTest, FirstCleaningFlag)
89{
90 auto fsMockPtr = std::make_unique<FileSystemMock>();
91 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
92 .WillOnce(Return(false));
93 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneFlagPath), _))
94 .WillOnce(Return(false));
95 EXPECT_CALL(*fsMockPtr, exists(fs::path(BM_SIGNAL_PATH), _))
96 .WillOnce(Return(true));
97 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningFlagPath), _))
98 .WillOnce(Return(false));
99 EXPECT_CALL(*fsMockPtr, create(StrEq(bmDriveCleaningFlagPath))).Times(1);
100
101 MockFsHandler h(move(fsMockPtr));
102 EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::BM_CLEANING_MODE));
103}
104
105TEST(BmcModeTransitionTest, NonBmMode)
106{
107 auto fsMockPtr = std::make_unique<FileSystemMock>();
108 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
109 .WillOnce(Return(false));
110 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneFlagPath), _))
111 .WillOnce(Return(false));
112 EXPECT_CALL(*fsMockPtr, exists(fs::path(BM_SIGNAL_PATH), _))
113 .WillOnce(Return(false));
114 MockFsHandler h(move(fsMockPtr));
115 EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::NON_BM_MODE));
116}
117
118} // namespace ipmi
119} // namespace google