| #include "bmc/machine_configs/setup_configs.h" |
| |
| #include <cstdio> |
| #include <fstream> |
| #include <memory> |
| #include <string> |
| #include <utility> |
| |
| #include "action_context.h" |
| #include "bmc/daemon_context_bmc.h" |
| #include "bmc/machine_configs/embedded_data.h" |
| #include "bmc/state_monitor_bmc.h" |
| #include "safepower_agent.pb.h" |
| #include "safepower_agent_config.pb.h" |
| #include "state_updater.h" |
| #include "gtest/gtest.h" |
| #include "absl/status/status.h" |
| #include "absl/strings/string_view.h" |
| |
| namespace setup_configs { |
| |
| namespace { |
| using safepower_agent::DaemonContextBMC; |
| |
| constexpr absl::string_view proto_str = |
| R"pb(gpowerd_config { |
| state_monitor_config |
| [ { |
| state_name: "PowerState" |
| state_gathering_info { |
| redfish { |
| uri: "/redfish/v1/Systems/system" |
| json_key: "PowerState" |
| } |
| collection_interval_ms: 1000 |
| prototype { power_state { state: POWER_STATE_POWERING_ON } } |
| } |
| system_component { node_entity_tag: "host-compute-node" } |
| }] |
| } |
| |
| action_configs { |
| action_name: "host_on_force" |
| action { |
| action_type: ACTION_TYPE_ON |
| action_severity: ACTION_SEVERITY_FORCE |
| target_component { node_entity_tag: "host-compute-node" } |
| } |
| redfish { |
| uri: "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset" |
| json_body: "{\"ResetType\" : \"ForceOn\"}" |
| ip: "127.0.0.1" |
| port: 80 |
| } |
| } |
| )pb"; |
| |
| absl::Status WriteTextProto(absl::string_view file_path) { |
| std::fstream fout(std::string(file_path), |
| std::fstream::out | std::fstream::trunc); |
| fout << proto_str; |
| fout.close(); |
| return absl::OkStatus(); |
| } |
| |
| TEST(setup_configs, testing_local_config) { |
| DaemonContextBMC daemon_context; |
| safepower_agent_proto::SystemState initialState; |
| auto reactor = std::make_shared< |
| safepower_agent::StateUpdater<safepower_agent_proto::SystemState>>( |
| std::move(initialState), true); |
| safepower_agent::StateMonitorBMC state(reactor); |
| safepower_agent::ActionContextManager ctx_manager(reactor); |
| |
| std::string file_name = std::tmpnam(nullptr); |
| ASSERT_TRUE(WriteTextProto(file_name).ok()); |
| ASSERT_TRUE(SetupConfigs(state, ctx_manager, file_name).ok()); |
| |
| safepower_agent_proto::GetSupportedActionsResponse resp; |
| ctx_manager.GetSupportedActions(resp); |
| EXPECT_EQ(resp.actions_size(), 1); |
| } |
| |
| TEST(setup_configs, test_bakedin_config) { |
| DaemonContextBMC daemon_context; |
| safepower_agent_proto::SystemState initialState; |
| auto reactor = std::make_shared< |
| safepower_agent::StateUpdater<safepower_agent_proto::SystemState>>( |
| std::move(initialState), true); |
| safepower_agent::StateMonitorBMC state(reactor); |
| safepower_agent::ActionContextManager ctx_manager(reactor); |
| |
| absl::Status status = SetupConfigs(state, ctx_manager, "non_existent_file"); |
| ASSERT_TRUE(status.ok()) << status; |
| safepower_agent_config::SafePowerAgentConfig bakedin_config; |
| ASSERT_TRUE( |
| bakedin_config.ParseFromArray(bmc_machine_configs_binary_proto_bin, |
| bmc_machine_configs_binary_proto_bin_len)); |
| safepower_agent_proto::GetSupportedActionsResponse resp; |
| ctx_manager.GetSupportedActions(resp); |
| EXPECT_EQ(resp.actions_size(), bakedin_config.action_configs_size()); |
| } |
| |
| } // namespace |
| |
| } // namespace setup_configs |