blob: b39d56ca38788f70f11b53e77cb9714fa8f8cb89 [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,
48 const std::string& config = "") :
49 Handler(config)
50 {
51 fsPtr_ = move(mock);
52 }
53
54 protected:
55 const std::unique_ptr<FileSystemInterface>& getFs() const override
56 {
57 return fsPtr_;
58 }
59
60 private:
61 std::unique_ptr<FileSystemInterface> fsPtr_;
62};
63
64TEST(BmcModeTransitionTest, DriveCleaningDoneAckFlag)
65{
66 auto fsMockPtr = std::make_unique<FileSystemMock>();
67 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
68 .WillOnce(Return(true));
69
70 MockFsHandler h(move(fsMockPtr));
71 EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::BM_MODE));
72}
73
74TEST(BmcModeTransitionTest, DriveCleaningDoneFlag)
75{
76 auto fsMockPtr = std::make_unique<FileSystemMock>();
77 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
78 .WillOnce(Return(false));
79 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneFlagPath), _))
80 .WillOnce(Return(true));
81 EXPECT_CALL(*fsMockPtr, rename(fs::path(bmDriveCleaningDoneFlagPath),
82 fs::path(bmDriveCleaningDoneAckFlagPath), _))
83 .Times(1);
84
85 MockFsHandler h(move(fsMockPtr));
86 EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::BM_MODE));
87}
88
89TEST(BmcModeTransitionTest, FirstCleaningFlag)
90{
91 auto fsMockPtr = std::make_unique<FileSystemMock>();
92 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
93 .WillOnce(Return(false));
94 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneFlagPath), _))
95 .WillOnce(Return(false));
96 EXPECT_CALL(*fsMockPtr, exists(fs::path(BM_SIGNAL_PATH), _))
97 .WillOnce(Return(true));
98 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningFlagPath), _))
99 .WillOnce(Return(false));
100 EXPECT_CALL(*fsMockPtr, create(StrEq(bmDriveCleaningFlagPath))).Times(1);
101
102 MockFsHandler h(move(fsMockPtr));
103 EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::BM_CLEANING_MODE));
104}
105
106TEST(BmcModeTransitionTest, NonBmMode)
107{
108 auto fsMockPtr = std::make_unique<FileSystemMock>();
109 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneAckFlagPath), _))
110 .WillOnce(Return(false));
111 EXPECT_CALL(*fsMockPtr, exists(fs::path(bmDriveCleaningDoneFlagPath), _))
112 .WillOnce(Return(false));
113 EXPECT_CALL(*fsMockPtr, exists(fs::path(BM_SIGNAL_PATH), _))
114 .WillOnce(Return(false));
115 MockFsHandler h(move(fsMockPtr));
116 EXPECT_EQ(h.getBmcMode(), static_cast<uint8_t>(BmcMode::NON_BM_MODE));
117}
118
119} // namespace ipmi
120} // namespace google