|
| 1 | +#include "rebootbe.h" |
| 2 | + |
| 3 | +#include <gmock/gmock.h> |
| 4 | +#include <google/protobuf/util/json_util.h> |
| 5 | +#include <gtest/gtest.h> |
| 6 | +#include <unistd.h> |
| 7 | + |
| 8 | +#include <iostream> |
| 9 | +#include <string> |
| 10 | +#include <thread> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#include "absl/time/time.h" |
| 14 | +#include "mock_reboot_interfaces.h" |
| 15 | +#include "reboot_common.h" |
| 16 | +#include "select.h" |
| 17 | +#include "status_code_util.h" |
| 18 | +#include "system/system.pb.h" |
| 19 | +#include "timestamp.h" |
| 20 | + |
| 21 | +namespace rebootbackend { |
| 22 | + |
| 23 | +#define ONE_SECOND (1) |
| 24 | +#define TWO_SECONDS (2) |
| 25 | +#define TENTH_SECOND_MS (100) |
| 26 | +#define SELECT_TIMEOUT_250_MS (250) |
| 27 | + |
| 28 | +namespace gpu = ::google::protobuf::util; |
| 29 | +using namespace gnoi::system; |
| 30 | + |
| 31 | +using ::testing::_; |
| 32 | +using ::testing::AllOf; |
| 33 | +using ::testing::AtLeast; |
| 34 | +using ::testing::ExplainMatchResult; |
| 35 | +using ::testing::HasSubstr; |
| 36 | +using ::testing::InSequence; |
| 37 | +using ::testing::Invoke; |
| 38 | +using ::testing::NiceMock; |
| 39 | +using ::testing::Return; |
| 40 | +using ::testing::StrEq; |
| 41 | +using ::testing::StrictMock; |
| 42 | + |
| 43 | +MATCHER_P2(IsStatus, status, message, "") { |
| 44 | + return (arg.status().status() == status && |
| 45 | + ExplainMatchResult(message, arg.status().message(), result_listener)); |
| 46 | +} |
| 47 | + |
| 48 | +MATCHER_P3(ActiveCountMethod, active, count, method, "") { |
| 49 | + return (arg.active() == active && arg.count() == (uint32_t)count && |
| 50 | + arg.method() == method); |
| 51 | +} |
| 52 | + |
| 53 | +class RebootBETestWithoutStop : public ::testing::Test { |
| 54 | + protected: |
| 55 | + RebootBETestWithoutStop() |
| 56 | + : m_dbus_interface(), |
| 57 | + m_db("STATE_DB", 0), |
| 58 | + m_config_db("CONFIG_DB", 0), |
| 59 | + m_rebootbeRequestChannel(&m_db, REBOOT_REQUEST_NOTIFICATION_CHANNEL), |
| 60 | + m_rebootbeReponseChannel(&m_db, REBOOT_RESPONSE_NOTIFICATION_CHANNEL), |
| 61 | + m_rebootbe(m_dbus_interface) { |
| 62 | + sigterm_requested = false; |
| 63 | + |
| 64 | + m_s.addSelectable(&m_rebootbeReponseChannel); |
| 65 | + |
| 66 | + // Make the tests log to stdout, instead of syslog. |
| 67 | + swss::Table logging_table(&m_config_db, CFG_LOGGER_TABLE_NAME); |
| 68 | + logging_table.hset("rebootbackend", swss::DAEMON_LOGOUTPUT, "STDOUT"); |
| 69 | + swss::Logger::restartLogger(); |
| 70 | + } |
| 71 | + virtual ~RebootBETestWithoutStop() = default; |
| 72 | + |
| 73 | + void start_rebootbe() { |
| 74 | + m_rebootbe_thread = |
| 75 | + std::make_unique<std::thread>(&RebootBE::Start, &m_rebootbe); |
| 76 | + } |
| 77 | + |
| 78 | + void set_mock_defaults() { |
| 79 | + ON_CALL(m_dbus_interface, Reboot(_)) |
| 80 | + .WillByDefault(Return(DbusInterface::DbusResponse{ |
| 81 | + DbusInterface::DbusStatus::DBUS_SUCCESS, ""})); |
| 82 | + } |
| 83 | + |
| 84 | + void overwrite_reboot_timeout(uint32_t timeout_seconds) { |
| 85 | + m_rebootbe.m_RebootThread.m_reboot_timeout = timeout_seconds; |
| 86 | + } |
| 87 | + |
| 88 | + void send_stop_reboot_thread() { m_rebootbe.m_RebootThread.Stop(); } |
| 89 | + |
| 90 | + void SendRebootRequest(const std::string &op, const std::string &data, |
| 91 | + const std::string &field, const std::string &value) { |
| 92 | + std::vector<swss::FieldValueTuple> values; |
| 93 | + values.push_back(swss::FieldValueTuple{field, value}); |
| 94 | + |
| 95 | + m_rebootbeRequestChannel.send(op, data, values); |
| 96 | + } |
| 97 | + |
| 98 | + void SendRebootViaProto(RebootRequest &request) { |
| 99 | + std::string json_string; |
| 100 | + gpu::MessageToJsonString(request, &json_string); |
| 101 | + |
| 102 | + SendRebootRequest("Reboot", "StatusCode", DATA_TUPLE_KEY, json_string); |
| 103 | + } |
| 104 | + |
| 105 | + void SendRebootStatusRequest(void) { |
| 106 | + SendRebootRequest("RebootStatus", "StatusCode", DATA_TUPLE_KEY, |
| 107 | + "json status request"); |
| 108 | + } |
| 109 | + |
| 110 | + void start_reboot_via_rpc( |
| 111 | + RebootRequest &request, |
| 112 | + swss::StatusCode expected_result = swss::StatusCode::SWSS_RC_SUCCESS) { |
| 113 | + SendRebootViaProto(request); |
| 114 | + while (true) { |
| 115 | + int ret; |
| 116 | + swss::Selectable *sel; |
| 117 | + ret = m_s.select(&sel, SELECT_TIMEOUT_250_MS); |
| 118 | + if (ret != swss::Select::OBJECT) continue; |
| 119 | + if (sel != &m_rebootbeReponseChannel) continue; |
| 120 | + break; |
| 121 | + } |
| 122 | + std::string op, data; |
| 123 | + std::vector<swss::FieldValueTuple> ret_values; |
| 124 | + m_rebootbeReponseChannel.pop(op, data, ret_values); |
| 125 | + |
| 126 | + EXPECT_THAT(op, StrEq("Reboot")); |
| 127 | + EXPECT_THAT(data, StrEq(swss::statusCodeToStr(expected_result))); |
| 128 | + } |
| 129 | + |
| 130 | + gnoi::system::RebootStatusResponse do_reboot_status_rpc() { |
| 131 | + SendRebootStatusRequest(); |
| 132 | + while (true) { |
| 133 | + int ret; |
| 134 | + swss::Selectable *sel; |
| 135 | + ret = m_s.select(&sel, SELECT_TIMEOUT_250_MS); |
| 136 | + if (ret != swss::Select::OBJECT) continue; |
| 137 | + if (sel != &m_rebootbeReponseChannel) continue; |
| 138 | + break; |
| 139 | + } |
| 140 | + std::string op, data; |
| 141 | + std::vector<swss::FieldValueTuple> ret_values; |
| 142 | + m_rebootbeReponseChannel.pop(op, data, ret_values); |
| 143 | + |
| 144 | + EXPECT_THAT(op, StrEq("RebootStatus")); |
| 145 | + EXPECT_EQ(data, swss::statusCodeToStr(swss::StatusCode::SWSS_RC_SUCCESS)); |
| 146 | + |
| 147 | + std::string json_response; |
| 148 | + for (auto &fv : ret_values) { |
| 149 | + if (DATA_TUPLE_KEY == fvField(fv)) { |
| 150 | + json_response = fvValue(fv); |
| 151 | + } |
| 152 | + } |
| 153 | + gnoi::system::RebootStatusResponse response; |
| 154 | + gpu::JsonStringToMessage(json_response, &response); |
| 155 | + return response; |
| 156 | + } |
| 157 | + |
| 158 | + void GetNotificationResponse(swss::NotificationConsumer &consumer, |
| 159 | + std::string &op, std::string &data, |
| 160 | + std::vector<swss::FieldValueTuple> &values) { |
| 161 | + swss::Select s; |
| 162 | + s.addSelectable(&consumer); |
| 163 | + swss::Selectable *sel; |
| 164 | + s.select(&sel, SELECT_TIMEOUT_250_MS); |
| 165 | + |
| 166 | + consumer.pop(op, data, values); |
| 167 | + } |
| 168 | + |
| 169 | + NotificationResponse handle_reboot_request(std::string &json_request) { |
| 170 | + return m_rebootbe.HandleRebootRequest(json_request); |
| 171 | + } |
| 172 | + |
| 173 | + // Mock interfaces. |
| 174 | + NiceMock<MockDbusInterface> m_dbus_interface; |
| 175 | + |
| 176 | + // DB connectors |
| 177 | + swss::DBConnector m_db; |
| 178 | + swss::DBConnector m_config_db; |
| 179 | + |
| 180 | + // Reboot thread signaling. |
| 181 | + swss::NotificationProducer m_rebootbeRequestChannel; |
| 182 | + swss::Select m_s; |
| 183 | + swss::NotificationConsumer m_rebootbeReponseChannel; |
| 184 | + |
| 185 | + // Module under test. |
| 186 | + std::unique_ptr<std::thread> m_rebootbe_thread; |
| 187 | + RebootBE m_rebootbe; |
| 188 | +}; |
| 189 | + |
| 190 | +class RebootBETest : public RebootBETestWithoutStop { |
| 191 | + protected: |
| 192 | + ~RebootBETest() { |
| 193 | + m_rebootbe.Stop(); |
| 194 | + m_rebootbe_thread->join(); |
| 195 | + } |
| 196 | +}; |
| 197 | + |
| 198 | +// Test fixture to skip through the startup sequence into the main loop. |
| 199 | +// Param indicates if RebootBE should be initialized into a state where the |
| 200 | +// system came up in warmboot. |
| 201 | +class RebootBEAutoStartTest : public RebootBETest, |
| 202 | + public ::testing::WithParamInterface<bool> { |
| 203 | + protected: |
| 204 | + RebootBEAutoStartTest() { |
| 205 | + |
| 206 | + start_rebootbe(); |
| 207 | + |
| 208 | + std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
| 209 | + EXPECT_EQ(m_rebootbe.GetCurrentStatus(), RebootBE::RebManagerStatus::IDLE); |
| 210 | + } |
| 211 | +}; |
| 212 | + |
| 213 | +// Normal operation testing. |
| 214 | +TEST_P(RebootBEAutoStartTest, NonExistentMessage) { |
| 215 | + swss::NotificationConsumer consumer(&m_db, |
| 216 | + REBOOT_RESPONSE_NOTIFICATION_CHANNEL); |
| 217 | + |
| 218 | + // No "MESSAGE" in field/values |
| 219 | + SendRebootRequest("Reboot", "StatusCode", "field1", "field1_value"); |
| 220 | + EXPECT_EQ(m_rebootbe.GetCurrentStatus(), RebootBE::RebManagerStatus::IDLE); |
| 221 | + |
| 222 | + std::string op, data; |
| 223 | + std::vector<swss::FieldValueTuple> ret_values; |
| 224 | + GetNotificationResponse(consumer, op, data, ret_values); |
| 225 | + |
| 226 | + EXPECT_THAT(op, StrEq("Reboot")); |
| 227 | + EXPECT_THAT( |
| 228 | + data, |
| 229 | + StrEq(swss::statusCodeToStr(swss::StatusCode::SWSS_RC_INVALID_PARAM))); |
| 230 | +} |
| 231 | + |
| 232 | +TEST_P(RebootBEAutoStartTest, TestCancelReboot) { |
| 233 | + swss::NotificationConsumer consumer(&m_db, |
| 234 | + REBOOT_RESPONSE_NOTIFICATION_CHANNEL); |
| 235 | + |
| 236 | + SendRebootRequest("CancelReboot", "StatusCode", DATA_TUPLE_KEY, |
| 237 | + "json cancelreboot request"); |
| 238 | + EXPECT_EQ(m_rebootbe.GetCurrentStatus(), RebootBE::RebManagerStatus::IDLE); |
| 239 | + |
| 240 | + std::string op, data; |
| 241 | + std::vector<swss::FieldValueTuple> ret_values; |
| 242 | + GetNotificationResponse(consumer, op, data, ret_values); |
| 243 | + |
| 244 | + EXPECT_THAT(op, StrEq("CancelReboot")); |
| 245 | + EXPECT_THAT( |
| 246 | + data, |
| 247 | + StrEq(swss::statusCodeToStr(swss::StatusCode::SWSS_RC_UNIMPLEMENTED))); |
| 248 | +} |
| 249 | + |
| 250 | +TEST_P(RebootBEAutoStartTest, TestUnrecognizedOP) { |
| 251 | + swss::NotificationConsumer consumer(&m_db, |
| 252 | + REBOOT_RESPONSE_NOTIFICATION_CHANNEL); |
| 253 | + |
| 254 | + SendRebootRequest("NonOp", "StatusCode", DATA_TUPLE_KEY, "invalid op code"); |
| 255 | + EXPECT_EQ(m_rebootbe.GetCurrentStatus(), RebootBE::RebManagerStatus::IDLE); |
| 256 | + |
| 257 | + std::string op, data; |
| 258 | + std::vector<swss::FieldValueTuple> ret_values; |
| 259 | + GetNotificationResponse(consumer, op, data, ret_values); |
| 260 | + |
| 261 | + EXPECT_THAT(op, StrEq("NonOp")); |
| 262 | + EXPECT_THAT( |
| 263 | + data, |
| 264 | + StrEq(swss::statusCodeToStr(swss::StatusCode::SWSS_RC_INVALID_PARAM))); |
| 265 | +} |
| 266 | + |
| 267 | +TEST_P(RebootBEAutoStartTest, TestColdRebootDbusToCompletion) { |
| 268 | + DbusInterface::DbusResponse dbus_response{ |
| 269 | + DbusInterface::DbusStatus::DBUS_SUCCESS, ""}; |
| 270 | + EXPECT_CALL(m_dbus_interface, Reboot(_)) |
| 271 | + .Times(3) |
| 272 | + .WillRepeatedly(Return(dbus_response)); |
| 273 | + |
| 274 | + overwrite_reboot_timeout(1); |
| 275 | + RebootRequest request; |
| 276 | + request.set_method(RebootMethod::COLD); |
| 277 | + start_reboot_via_rpc(request); |
| 278 | + |
| 279 | + std::this_thread::sleep_for(std::chrono::milliseconds(TENTH_SECOND_MS)); |
| 280 | + EXPECT_EQ(m_rebootbe.GetCurrentStatus(), |
| 281 | + RebootBE::RebManagerStatus::COLD_REBOOT_IN_PROGRESS); |
| 282 | + sleep(TWO_SECONDS); |
| 283 | + |
| 284 | + EXPECT_EQ(m_rebootbe.GetCurrentStatus(), RebootBE::RebManagerStatus::IDLE); |
| 285 | + gnoi::system::RebootStatusResponse response = do_reboot_status_rpc(); |
| 286 | + EXPECT_THAT(response, ActiveCountMethod(false, 1, RebootMethod::COLD)); |
| 287 | + EXPECT_THAT(response, |
| 288 | + IsStatus(RebootStatus_Status::RebootStatus_Status_STATUS_FAILURE, |
| 289 | + "platform failed to reboot")); |
| 290 | + |
| 291 | + start_reboot_via_rpc(request); |
| 292 | + sleep(TWO_SECONDS); |
| 293 | + |
| 294 | + start_reboot_via_rpc(request); |
| 295 | + sleep(TWO_SECONDS); |
| 296 | + |
| 297 | + response = do_reboot_status_rpc(); |
| 298 | + // Verifiy count is 3 after three reboot attempts. |
| 299 | + EXPECT_THAT(response, ActiveCountMethod(false, 3, RebootMethod::COLD)); |
| 300 | + EXPECT_THAT(response, |
| 301 | + IsStatus(RebootStatus_Status::RebootStatus_Status_STATUS_FAILURE, |
| 302 | + "platform failed to reboot")); |
| 303 | +} |
| 304 | + |
| 305 | +TEST_P(RebootBEAutoStartTest, TestColdBootSigterm) { |
| 306 | + sigterm_requested = true; |
| 307 | + set_mock_defaults(); |
| 308 | + overwrite_reboot_timeout(1); |
| 309 | + |
| 310 | + RebootRequest request; |
| 311 | + request.set_method(RebootMethod::COLD); |
| 312 | + start_reboot_via_rpc(request); |
| 313 | + |
| 314 | + sleep(ONE_SECOND); |
| 315 | + |
| 316 | + EXPECT_EQ(m_rebootbe.GetCurrentStatus(), RebootBE::RebManagerStatus::IDLE); |
| 317 | + gnoi::system::RebootStatusResponse second_resp = do_reboot_status_rpc(); |
| 318 | + EXPECT_THAT(second_resp, ActiveCountMethod(false, 1, RebootMethod::COLD)); |
| 319 | + EXPECT_THAT( |
| 320 | + second_resp, |
| 321 | + IsStatus(RebootStatus_Status::RebootStatus_Status_STATUS_UNKNOWN, "")); |
| 322 | +} |
| 323 | + |
| 324 | +TEST_P(RebootBEAutoStartTest, TestColdBootDbusError) { |
| 325 | + // Return FAIL from dbus reboot call. |
| 326 | + DbusInterface::DbusResponse dbus_response{ |
| 327 | + DbusInterface::DbusStatus::DBUS_FAIL, "dbus reboot failed"}; |
| 328 | + EXPECT_CALL(m_dbus_interface, Reboot(_)) |
| 329 | + .Times(1) |
| 330 | + .WillOnce(Return(dbus_response)); |
| 331 | + |
| 332 | + RebootRequest request; |
| 333 | + request.set_method(RebootMethod::COLD); |
| 334 | + start_reboot_via_rpc(request); |
| 335 | + |
| 336 | + sleep(TWO_SECONDS); |
| 337 | + |
| 338 | + EXPECT_EQ(m_rebootbe.GetCurrentStatus(), RebootBE::RebManagerStatus::IDLE); |
| 339 | + gnoi::system::RebootStatusResponse second_resp = do_reboot_status_rpc(); |
| 340 | + EXPECT_THAT(second_resp, ActiveCountMethod(false, 1, RebootMethod::COLD)); |
| 341 | + EXPECT_THAT(second_resp, |
| 342 | + IsStatus(RebootStatus_Status::RebootStatus_Status_STATUS_FAILURE, |
| 343 | + "dbus reboot failed")); |
| 344 | +} |
| 345 | + |
| 346 | +TEST_P(RebootBEAutoStartTest, TestStopDuringColdBoot) { |
| 347 | + set_mock_defaults(); |
| 348 | + |
| 349 | + RebootRequest request; |
| 350 | + request.set_method(RebootMethod::COLD); |
| 351 | + start_reboot_via_rpc(request); |
| 352 | + std::this_thread::sleep_for(std::chrono::milliseconds(TENTH_SECOND_MS)); |
| 353 | + EXPECT_EQ(m_rebootbe.GetCurrentStatus(), |
| 354 | + RebootBE::RebManagerStatus::COLD_REBOOT_IN_PROGRESS); |
| 355 | + |
| 356 | + send_stop_reboot_thread(); |
| 357 | + std::this_thread::sleep_for(std::chrono::milliseconds(TENTH_SECOND_MS)); |
| 358 | + EXPECT_EQ(m_rebootbe.GetCurrentStatus(), RebootBE::RebManagerStatus::IDLE); |
| 359 | + |
| 360 | + gnoi::system::RebootStatusResponse response = do_reboot_status_rpc(); |
| 361 | + EXPECT_THAT(response, ActiveCountMethod(false, 1, RebootMethod::COLD)); |
| 362 | + EXPECT_THAT( |
| 363 | + response, |
| 364 | + IsStatus(RebootStatus_Status::RebootStatus_Status_STATUS_UNKNOWN, "")); |
| 365 | +} |
| 366 | + |
| 367 | +TEST_P(RebootBEAutoStartTest, TestInvalidJsonRebootRequest) { |
| 368 | + std::string json_request = "abcd"; |
| 369 | + NotificationResponse response = handle_reboot_request(json_request); |
| 370 | + EXPECT_EQ(swss::StatusCode::SWSS_RC_INTERNAL, response.status); |
| 371 | +} |
| 372 | + |
| 373 | +INSTANTIATE_TEST_SUITE_P(TestWithStartupWarmbootEnabledState, |
| 374 | + RebootBEAutoStartTest, testing::Values(true, false)); |
| 375 | + |
| 376 | +} // namespace rebootbackend |
0 commit comments