Skip to content

Commit 6abec27

Browse files
ypomortsevCommit Bot
authored andcommitted
[modular] Support reading config from /pkg/data/startup.config
basemgr will read config from /pkg/data/startup.config if it exists and fall back to the previously default /config/data/startup.config. Overridden/persistent config takes precedence as before. Fixed: 91317 Change-Id: I3888cfdfaff5884c0b2542371b66feb553b09ce8 Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/635901 Reviewed-by: Yaar Schnitman <[email protected]> Reviewed-by: Gabe Schine <[email protected]> Commit-Queue: Yegor Pomortsev <[email protected]>
1 parent 1a26107 commit 6abec27

File tree

4 files changed

+64
-19
lines changed

4 files changed

+64
-19
lines changed

src/modular/lib/modular_config/modular_config.cc

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,17 @@ ModularConfigReader::ModularConfigReader(fbl::unique_fd root_dir) : root_dir_(st
9494
FX_CHECK(root_dir_.is_valid());
9595

9696
// 1. Figure out where the config file is.
97-
std::string config_path = GetDefaultConfigPath();
97+
std::string config_path = GetConfigDataConfigPath();
9898
if (OverriddenConfigExists()) {
9999
config_path = GetOverriddenConfigPath();
100100
} else if (PersistentConfigOverrideAllowed() && PersistentConfigExists()) {
101101
config_path = GetPersistentConfigPath();
102-
FX_LOGS(INFO) << "Reading persistent configuration from /" << config_path;
102+
} else if (PackagedConfigExists()) {
103+
config_path = GetPackagedConfigPath();
103104
}
104105

106+
FX_LOGS(INFO) << "Reading configuration from /" << config_path;
107+
105108
// 2. Read the file
106109
std::string config;
107110
if (!files::ReadFileToStringAt(root_dir_.get(), config_path, &config)) {
@@ -120,8 +123,8 @@ ModularConfigReader ModularConfigReader::CreateFromNamespace() {
120123
}
121124

122125
// static
123-
std::string ModularConfigReader::GetDefaultConfigPath() {
124-
return files::JoinPath(StripLeadingSlash(modular_config::kDefaultConfigDir),
126+
std::string ModularConfigReader::GetConfigDataConfigPath() {
127+
return files::JoinPath(StripLeadingSlash(modular_config::kConfigDataDir),
125128
modular_config::kStartupConfigFilePath);
126129
}
127130

@@ -137,9 +140,15 @@ std::string ModularConfigReader::GetPersistentConfigPath() {
137140
modular_config::kStartupConfigFilePath);
138141
}
139142

143+
// static
144+
std::string ModularConfigReader::GetPackagedConfigPath() {
145+
return files::JoinPath(StripLeadingSlash(modular_config::kPackageDataDir),
146+
modular_config::kStartupConfigFilePath);
147+
}
148+
140149
// static
141150
std::string ModularConfigReader::GetAllowPersistentConfigOverridePath() {
142-
return files::JoinPath(StripLeadingSlash(modular_config::kDefaultConfigDir),
151+
return files::JoinPath(StripLeadingSlash(modular_config::kConfigDataDir),
143152
modular_config::kAllowPersistentConfigOverrideFilePath);
144153
}
145154

@@ -151,6 +160,10 @@ bool ModularConfigReader::PersistentConfigExists() {
151160
return files::IsFileAt(root_dir_.get(), GetPersistentConfigPath());
152161
}
153162

163+
bool ModularConfigReader::PackagedConfigExists() {
164+
return files::IsFileAt(root_dir_.get(), GetPackagedConfigPath());
165+
}
166+
154167
bool ModularConfigReader::PersistentConfigOverrideAllowed() {
155168
return files::IsFileAt(root_dir_.get(), GetAllowPersistentConfigOverridePath());
156169
}

src/modular/lib/modular_config/modular_config.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,30 @@ class ModularConfigReader {
6969
// incoming namespace.
7070
static ModularConfigReader CreateFromNamespace();
7171

72-
// Returns the path to the default config file.
73-
static std::string GetDefaultConfigPath();
72+
// Returns the path to the config file in the /config/data directory.
73+
static std::string GetConfigDataConfigPath();
7474

7575
// Returns the path to the overridden config file.
7676
static std::string GetOverriddenConfigPath();
7777

7878
// Returns the path to the persistent config file.
7979
static std::string GetPersistentConfigPath();
8080

81+
// Returns the path to the config file in the current package.
82+
static std::string GetPackagedConfigPath();
83+
8184
// Returns the path to the allow_persistent_config_override marker file.
8285
static std::string GetAllowPersistentConfigOverridePath();
8386

84-
// Returns true if configurations exist at the overridden config path.
87+
// Returns true if a configuration file exists at the overridden config path.
8588
bool OverriddenConfigExists();
8689

87-
// Returns true if configuration exists at the persistent config path.
90+
// Returns true if a configuration file exists at the persistent config path.
8891
bool PersistentConfigExists();
8992

93+
// Returns true if a configuration file exists in the current package.
94+
bool PackagedConfigExists();
95+
9096
// Returns true if the allow_persistent_config_override marker file exists.
9197
bool PersistentConfigOverrideAllowed();
9298

src/modular/lib/modular_config/modular_config_constants.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ constexpr char kBasemgrConfigName[] = "basemgr";
1111
constexpr char kSessionmgrConfigName[] = "sessionmgr";
1212
constexpr char kSessionmgrUrl[] = "fuchsia-pkg://fuchsia.com/sessionmgr#meta/sessionmgr.cmx";
1313

14-
constexpr char kDefaultConfigDir[] = "/config/data";
14+
constexpr char kConfigDataDir[] = "/config/data";
15+
constexpr char kPackageDataDir[] = "/pkg/data";
1516
constexpr char kOverriddenConfigDir[] = "/config_override/data";
1617
constexpr char kPersistentConfigDir[] = "/cache";
1718

src/modular/lib/modular_config/modular_config_unittest.cc

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ bool CreateFileAt(int root_fd, const std::string& path, std::string_view data) {
4242
class ModularConfigReaderTest : public gtest::RealLoopFixture {};
4343
class ModularConfigWriterTest : public gtest::RealLoopFixture {};
4444

45-
// Test that ModularConfigReader finds and reads the startup.config file given a
46-
// root directory that contains config data.
45+
// Test that ModularConfigReader finds and reads the startup.config file from the overridden
46+
// config directory.
4747
TEST_F(ModularConfigReaderTest, OverrideConfigDir) {
4848
constexpr char kSessionShellForTest[] =
4949
"fuchsia-pkg://example.com/ModularConfigReaderTest#meta/"
@@ -62,8 +62,35 @@ TEST_F(ModularConfigReaderTest, OverrideConfigDir) {
6262
kSessionShellForTest);
6363

6464
modular::PseudoDirServer server(modular::MakeFilePathWithContents(
65-
files::JoinPath(modular_config::kOverriddenConfigDir, modular_config::kStartupConfigFilePath),
66-
config_contents));
65+
modular::ModularConfigReader::GetOverriddenConfigPath(), config_contents));
66+
67+
modular::ModularConfigReader reader(server.OpenAt("."));
68+
auto config = reader.GetBasemgrConfig();
69+
70+
// Verify that ModularConfigReader parsed the config value we gave it.
71+
EXPECT_EQ(kSessionShellForTest, config.session_shell_map().at(0).config().app_config().url());
72+
}
73+
74+
// Test that ModularConfigReader finds and reads the startup.config file in the package directory.
75+
TEST_F(ModularConfigReaderTest, PackagedConfigDir) {
76+
constexpr char kSessionShellForTest[] =
77+
"fuchsia-pkg://example.com/ModularConfigReaderTest#meta/"
78+
"ModularConfigReaderTest.cmx";
79+
80+
std::string config_contents = fxl::Substitute(
81+
R"({
82+
"basemgr": {
83+
"session_shells": [
84+
{
85+
"url": "$0"
86+
}
87+
]
88+
}
89+
})",
90+
kSessionShellForTest);
91+
92+
modular::PseudoDirServer server(modular::MakeFilePathWithContents(
93+
modular::ModularConfigReader::GetPackagedConfigPath(), config_contents));
6794

6895
modular::ModularConfigReader reader(server.OpenAt("."));
6996
auto config = reader.GetBasemgrConfig();
@@ -160,8 +187,7 @@ TEST_F(ModularConfigReaderTest, GetConfigAsString) {
160187

161188
// Host |config_contents|, parse it into |first_reader|, and write configs into |read_config_str|
162189
modular::PseudoDirServer server(modular::MakeFilePathWithContents(
163-
files::JoinPath(modular_config::kDefaultConfigDir, modular_config::kStartupConfigFilePath),
164-
config_contents));
190+
modular::ModularConfigReader::GetConfigDataConfigPath(), config_contents));
165191

166192
modular::ModularConfigReader first_reader(server.OpenAt("."));
167193
auto basemgr_config = first_reader.GetBasemgrConfig();
@@ -171,8 +197,7 @@ TEST_F(ModularConfigReaderTest, GetConfigAsString) {
171197

172198
// Host the new config string and parse it into |second_reader|
173199
modular::PseudoDirServer server_after_read(modular::MakeFilePathWithContents(
174-
files::JoinPath(modular_config::kDefaultConfigDir, modular_config::kStartupConfigFilePath),
175-
read_config_str));
200+
modular::ModularConfigReader::GetConfigDataConfigPath(), read_config_str));
176201

177202
modular::ModularConfigReader second_reader(server_after_read.OpenAt("."));
178203

@@ -436,7 +461,7 @@ TEST_F(ModularConfigReaderTest, ReadPersistentConfig) {
436461

437462
// The /config/data/startup.config file contains an empty config.
438463
ASSERT_TRUE(
439-
CreateFileAt(root_fd.get(), modular::ModularConfigReader::GetDefaultConfigPath(), "{}"));
464+
CreateFileAt(root_fd.get(), modular::ModularConfigReader::GetConfigDataConfigPath(), "{}"));
440465

441466
// Allow persistent config_override.
442467
ASSERT_TRUE(CreateFileAt(root_fd.get(),

0 commit comments

Comments
 (0)