Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions core/src/components/minimal_server_component_list_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <userver/components/minimal_server_component_list.hpp>

#include <cstdlib>

#include <fmt/format.h>
#include <gmock/gmock.h>

Expand All @@ -11,11 +13,14 @@
#include <userver/engine/run_standalone.hpp>
#include <userver/engine/sleep.hpp>
#include <userver/formats/json/exception.hpp>
#include <userver/formats/yaml/serialize.hpp>
#include <userver/formats/yaml/value_builder.hpp>
#include <userver/fs/blocking/read.hpp>
#include <userver/fs/blocking/temp_directory.hpp> // for fs::blocking::TempDirectory
#include <userver/fs/blocking/write.hpp> // for fs::blocking::RewriteFileContents
#include <userver/logging/component.hpp>
#include <userver/utils/async.hpp>
#include <userver/utils/scope_guard.hpp>

#include <components/component_list_test.hpp>
#include <engine/io/tests/net_listener.hpp>
Expand Down Expand Up @@ -86,6 +91,16 @@ class ServerMinimalComponentList : public ComponentList {

std::uint16_t GetServerPort() const { return server_port_; }

std::string GetStaticConfigWithPortEnv(std::uint16_t fallback_port) const {
formats::yaml::ValueBuilder config{formats::yaml::FromString(GetStaticConfig())};
config.Remove("config_vars");
auto listener = config["components_manager"]["components"]["server"]["listener"];
listener.Remove("port");
listener["port#env"] = "USERVER_TEST_LISTENER_PORT";
listener["port#fallback"] = fallback_port;
return formats::yaml::ToString(config.ExtractValue());
}

private:
fs::blocking::TempDirectory temp_root_ = fs::blocking::TempDirectory::Create();
std::uint16_t server_port_ = FindFreePort();
Expand Down Expand Up @@ -124,6 +139,30 @@ TEST_F(ServerMinimalComponentList, Basic) {
components::RunOnce(components::InMemoryConfig{GetStaticConfig()}, components::MinimalServerComponentList());
}

TEST_F(ServerMinimalComponentList, ListenerPortFromEnv) {
// NOLINTNEXTLINE(concurrency-mt-unsafe)
ASSERT_EQ(::setenv("USERVER_TEST_LISTENER_PORT", fmt::format("{}", GetServerPort()).c_str(), 1), 0);
const utils::ScopeGuard env_guard{[] {
// NOLINTNEXTLINE(concurrency-mt-unsafe)
::unsetenv("USERVER_TEST_LISTENER_PORT");
}};

components::RunOnce(
components::InMemoryConfig{GetStaticConfigWithPortEnv(0)},
components::MinimalServerComponentList()
);
}

TEST_F(ServerMinimalComponentList, ListenerPortFromEnvFallback) {
// NOLINTNEXTLINE(concurrency-mt-unsafe)
ASSERT_EQ(::unsetenv("USERVER_TEST_LISTENER_PORT"), 0);

components::RunOnce(
components::InMemoryConfig{GetStaticConfigWithPortEnv(GetServerPort())},
components::MinimalServerComponentList()
);
}

TEST_F(ServerMinimalComponentList, TraceSwitching) {
constexpr std::string_view kConfigVarsTemplate = R"(
tracer_log_path: {0}
Expand Down
2 changes: 1 addition & 1 deletion core/src/server/net/listener_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ ListenerConfig Parse(const yaml_config::YamlConfig& value, formats::parse::To<Li
config.backlog = value["backlog"].As<int>(config.backlog);

config.ports = value["ports"].As<std::vector<PortConfig>>({});
if (value.HasMember("port") || value.HasMember("unix-socket") || value.HasMember("listen-socket-fd")) {
if (!value["port"].IsMissing() || !value["unix-socket"].IsMissing() || !value["listen-socket-fd"].IsMissing()) {
config.ports.emplace_back(value.As<PortConfig>());
}
if (config.ports.empty()) {
Expand Down
Loading