| #include "bmc/machine_configs/setup_configs.h" | |
| #include <cstdio> | |
| #include <fstream> | |
| #include <optional> | |
| #include <string> | |
| #include "one/offline_node_entities.pb.h" | |
| #include "bmc/daemon_context_bmc.h" | |
| #include "bmc/machine_configs/embedded_data.h" | |
| #include "parse_text_proto.h" | |
| #include "safepower_agent_config.pb.h" | |
| #include "gmock/gmock.h" | |
| #include "gtest/gtest.h" | |
| #include "absl/flags/declare.h" | |
| #include "absl/flags/flag.h" | |
| #include "absl/status/status.h" | |
| #include "absl/strings/string_view.h" | |
| ABSL_DECLARE_FLAG(std::string, config_file); | |
| ABSL_DECLARE_FLAG(std::string, legacy_config_file); | |
| namespace setup_configs { | |
| namespace { | |
| using safepower_agent::DaemonContextBMC; | |
| using safepower_agent::ParseTextProto; | |
| 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::optional<absl::string_view> | |
| offline_node_entities_path = std::nullopt) { | |
| std::fstream fout(std::string(file_path), | |
| std::fstream::out | std::fstream::trunc); | |
| fout << proto_str; | |
| if (offline_node_entities_path.has_value()) { | |
| fout << "offline_node_entities_path: \"" << *offline_node_entities_path | |
| << "\"\n"; | |
| } | |
| fout.close(); | |
| return absl::OkStatus(); | |
| } | |
| template <typename T> | |
| absl::Status WriteBinaryProto(absl::string_view file_path, const T& message) { | |
| std::fstream fout(std::string(file_path), std::fstream::out | | |
| std::fstream::trunc | | |
| std::fstream::binary); | |
| if (!message.SerializeToOstream(&fout)) { | |
| return absl::InternalError("Failed to write binary proto"); | |
| } | |
| fout.close(); | |
| return absl::OkStatus(); | |
| } | |
| TEST(setup_configs, testing_local_config) { | |
| DaemonContextBMC daemon_context; | |
| std::string file_name = std::tmpnam(nullptr); | |
| ASSERT_OK(WriteTextProto(file_name)); | |
| ASSERT_OK(LoadConfig(file_name, "non_existent_file")); | |
| EXPECT_EQ(DaemonContextBMC::Get().config().action_configs_size(), 1); | |
| } | |
| TEST(setup_configs, test_bakedin_config) { | |
| DaemonContextBMC daemon_context; | |
| ASSERT_OK(LoadConfig("non_existent_file", "non_existent_file_")); | |
| safepower_agent_config::SafePowerAgentConfig bakedin_config; | |
| ASSERT_TRUE( | |
| bakedin_config.ParseFromArray(bmc_machine_configs_binary_proto_bin, | |
| bmc_machine_configs_binary_proto_bin_len)); | |
| EXPECT_EQ(DaemonContextBMC::Get().config().action_configs_size(), | |
| bakedin_config.action_configs_size()); | |
| } | |
| // Runs LoadConfig with a non existent config file, | |
| // and an existing legacy_config_file | |
| TEST(setup_configs, test_legacy_config_fallback) { | |
| DaemonContextBMC daemon_context; | |
| std::string legacy_file_name = std::tmpnam(nullptr); | |
| ASSERT_OK(WriteTextProto(legacy_file_name)); | |
| absl::SetFlag(&FLAGS_config_file, "non_existent_file"); | |
| absl::SetFlag(&FLAGS_legacy_config_file, legacy_file_name); | |
| ASSERT_OK(LoadConfig()); | |
| EXPECT_EQ(DaemonContextBMC::Get().config().action_configs_size(), 1); | |
| } | |
| TEST(setup_configs, test_malformed_redfish_json_fallback) { | |
| DaemonContextBMC daemon_context; | |
| std::string bad_file_name = std::tmpnam(nullptr); | |
| std::fstream fout(bad_file_name, std::fstream::out | std::fstream::trunc); | |
| fout << R"pb( | |
| action_configs { | |
| action_name: "bad_json_action" | |
| redfish { | |
| uri: "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset" | |
| json_body: "{malformed_json" | |
| ip: "127.0.0.1" | |
| port: 80 | |
| } | |
| } | |
| )pb"; | |
| fout.close(); | |
| std::string legacy_file_name = std::tmpnam(nullptr); | |
| ASSERT_OK(WriteTextProto(legacy_file_name)); | |
| ASSERT_OK(LoadConfig(bad_file_name, legacy_file_name)); | |
| ASSERT_EQ(DaemonContextBMC::Get().config().action_configs_size(), 1); | |
| EXPECT_EQ(DaemonContextBMC::Get().config().action_configs(0).action_name(), | |
| "host_on_force"); | |
| } | |
| // Runs LoadConfig with an existent config file, | |
| // and non existent legacy_config_file | |
| TEST(setup_configs, test_config_file_only) { | |
| DaemonContextBMC daemon_context; | |
| std::string file_name = std::tmpnam(nullptr); | |
| ASSERT_OK(WriteTextProto(file_name)); | |
| absl::SetFlag(&FLAGS_config_file, file_name); | |
| absl::SetFlag(&FLAGS_legacy_config_file, "non_existent_file"); | |
| ASSERT_OK(LoadConfig()); | |
| EXPECT_EQ(DaemonContextBMC::Get().config().action_configs_size(), 1); | |
| } | |
| TEST(setup_configs, test_config_with_node_entities_path) { | |
| DaemonContextBMC daemon_context; | |
| // Clear any existing entities | |
| DaemonContextBMC::Get().set_offline_node_entities({}); | |
| // Create offline node entities file | |
| std::string entities_file_name = std::tmpnam(nullptr); | |
| auto entities = ParseTextProto< | |
| production_msv::node_entities_proto::OfflineNodeEntityInformation>( | |
| R"pb( | |
| entity_tag: "test-node-entity" | |
| resolved_config { machine_name: "test-machine" } | |
| )pb"); | |
| ASSERT_OK(entities); | |
| ASSERT_OK(WriteBinaryProto(entities_file_name, *entities)); | |
| // Create config file pointing to entities file | |
| std::string file_name = std::tmpnam(nullptr); | |
| ASSERT_OK(WriteTextProto(file_name, entities_file_name)); | |
| absl::SetFlag(&FLAGS_config_file, file_name); | |
| absl::SetFlag(&FLAGS_legacy_config_file, "non_existent_file"); | |
| ASSERT_OK(LoadConfig()); | |
| LoadOfflineNodeEntities(); | |
| EXPECT_EQ(DaemonContextBMC::Get().offline_node_entities().entity_tag(), | |
| "test-node-entity"); | |
| } | |
| TEST(setup_configs, test_grpc_config_port) { | |
| DaemonContextBMC daemon_context; | |
| std::string file_name = std::tmpnam(nullptr); | |
| std::fstream fout(file_name, std::fstream::out | std::fstream::trunc); | |
| fout << R"pb( | |
| grpc_config { port: 12345 } | |
| )pb"; | |
| fout.close(); | |
| ASSERT_OK(LoadConfig(file_name, "non_existent_file")); | |
| EXPECT_EQ(DaemonContextBMC::Get().config().grpc_config().port(), 12345); | |
| } | |
| TEST(setup_configs, test_grpc_config_default_port) { | |
| DaemonContextBMC daemon_context; | |
| std::string file_name = std::tmpnam(nullptr); | |
| std::fstream fout(file_name, std::fstream::out | std::fstream::trunc); | |
| // Empty config, should use defaults | |
| fout << R"pb( | |
| grpc_config {} | |
| )pb"; | |
| fout.close(); | |
| ASSERT_OK(LoadConfig(file_name, "non_existent_file")); | |
| EXPECT_EQ(DaemonContextBMC::Get().config().grpc_config().port(), 20120); | |
| } | |
| } // namespace | |
| } // namespace setup_configs |