@@ -25,23 +25,36 @@ using namespace velox;
2525
2626class ConfigTest : public testing ::Test {
2727 protected:
28- void setUpConfigFilePath () {
28+ void SetUp () override {
2929 velox::filesystems::registerLocalFileSystem ();
30+ setUpConfigFilePath ();
31+ }
32+
33+ void TearDown () override {
34+ cleanupConfigFilePath ();
35+ }
3036
37+ void setUpConfigFilePath () {
3138 char path[] = " /tmp/velox_system_config_test_XXXXXX" ;
3239 const char * tempDirectoryPath = mkdtemp (path);
3340 if (tempDirectoryPath == nullptr ) {
3441 throw std::logic_error (" Cannot open temp directory" );
3542 }
36- configFilePath = tempDirectoryPath;
37- configFilePath += " /config.properties" ;
43+ configFilePath_ = tempDirectoryPath;
44+ configFilePath_ += " /config.properties" ;
45+ }
46+
47+ void cleanupConfigFilePath () {
48+ auto fileSystem = filesystems::getFileSystem (configFilePath_, nullptr );
49+ auto dirPath = std::filesystem::path (configFilePath_).parent_path ();
50+ fileSystem->rmdir (dirPath.string ());
3851 }
3952
4053 void writeDefaultConfigFile (bool isMutable) {
41- auto fileSystem = filesystems::getFileSystem (configFilePath , nullptr );
42- auto sysConfigFile = fileSystem->openFileForWrite (configFilePath );
54+ auto fileSystem = filesystems::getFileSystem (configFilePath_ , nullptr );
55+ auto sysConfigFile = fileSystem->openFileForWrite (configFilePath_ );
4356 sysConfigFile->append (
44- fmt::format (" {}={}\n " , SystemConfig::kPrestoVersion , prestoVersion ));
57+ fmt::format (" {}={}\n " , SystemConfig::kPrestoVersion , kPrestoVersion_ ));
4558 sysConfigFile->append (
4659 fmt::format (" {}=11kB\n " , SystemConfig::kQueryMaxMemoryPerNode ));
4760 if (isMutable) {
@@ -52,8 +65,8 @@ class ConfigTest : public testing::Test {
5265 }
5366
5467 void writeConfigFile (const std::string& config) {
55- auto fileSystem = filesystems::getFileSystem (configFilePath , nullptr );
56- auto sysConfigFile = fileSystem->openFileForWrite (configFilePath );
68+ auto fileSystem = filesystems::getFileSystem (configFilePath_ , nullptr );
69+ auto sysConfigFile = fileSystem->openFileForWrite (configFilePath_ );
5770 sysConfigFile->append (config);
5871 sysConfigFile->close ();
5972 }
@@ -65,40 +78,41 @@ class ConfigTest : public testing::Test {
6578 std::make_unique<config::ConfigBase>(std::move (properties)));
6679 }
6780
68- std::string configFilePath ;
69- const std::string prestoVersion {" SystemConfigTest1" };
70- const std::string prestoVersion2 {" SystemConfigTest2" };
81+ std::string configFilePath_ ;
82+ const std::string_view kPrestoVersion_ {" SystemConfigTest1" };
83+ const std::string_view kPrestoVersion2_ {" SystemConfigTest2" };
7184};
7285
7386TEST_F (ConfigTest, defaultSystemConfig) {
74- setUpConfigFilePath ();
7587 writeDefaultConfigFile (false );
7688 auto systemConfig = SystemConfig::instance ();
77- systemConfig->initialize (configFilePath );
89+ systemConfig->initialize (configFilePath_ );
7890
7991 ASSERT_FALSE (systemConfig->mutableConfig ());
80- ASSERT_EQ (prestoVersion , systemConfig->prestoVersion ());
92+ ASSERT_EQ (kPrestoVersion_ , systemConfig->prestoVersion ());
8193 ASSERT_EQ (11 << 10 , systemConfig->queryMaxMemoryPerNode ());
8294 ASSERT_THROW (
8395 systemConfig->setValue (
84- std::string (SystemConfig::kPrestoVersion ), prestoVersion2),
96+ std::string (SystemConfig::kPrestoVersion ),
97+ std::string (kPrestoVersion2_ )),
8598 VeloxException);
8699}
87100
88101TEST_F (ConfigTest, mutableSystemConfig) {
89- setUpConfigFilePath ();
90102 writeDefaultConfigFile (true );
91103 auto systemConfig = SystemConfig::instance ();
92- systemConfig->initialize (configFilePath );
104+ systemConfig->initialize (configFilePath_ );
93105
94106 ASSERT_TRUE (systemConfig->mutableConfig ());
95- ASSERT_EQ (prestoVersion , systemConfig->prestoVersion ());
107+ ASSERT_EQ (kPrestoVersion_ , systemConfig->prestoVersion ());
96108 ASSERT_EQ (
97- prestoVersion ,
109+ kPrestoVersion_ ,
98110 systemConfig
99- ->setValue (std::string (SystemConfig::kPrestoVersion ), prestoVersion2)
111+ ->setValue (
112+ std::string (SystemConfig::kPrestoVersion ),
113+ std::string (kPrestoVersion2_ ))
100114 .value ());
101- ASSERT_EQ (prestoVersion2 , systemConfig->prestoVersion ());
115+ ASSERT_EQ (kPrestoVersion2_ , systemConfig->prestoVersion ());
102116 ASSERT_EQ (
103117 " 11kB" ,
104118 systemConfig
@@ -206,7 +220,6 @@ TEST_F(ConfigTest, remoteFunctionServer) {
206220}
207221
208222TEST_F (ConfigTest, parseValid) {
209- setUpConfigFilePath ();
210223 writeConfigFile (
211224 " #comment\n "
212225 " #a comment with space\n "
@@ -220,7 +233,7 @@ TEST_F(ConfigTest, parseValid) {
220233 " key1= value with space\n "
221234 " key2=value=with=key=word\n "
222235 " emptyvaluekey=" );
223- auto configMap = presto::util::readConfig (configFilePath );
236+ auto configMap = presto::util::readConfig (configFilePath_ );
224237 ASSERT_EQ (configMap.size (), 6 );
225238
226239 std::unordered_map<std::string, std::string> expected{
@@ -237,10 +250,11 @@ TEST_F(ConfigTest, parseInvalid) {
237250 auto testInvalid = [this ](
238251 const std::string& fileContent,
239252 const std::string& expectedErrorMsg) {
253+ cleanupConfigFilePath ();
240254 setUpConfigFilePath ();
241255 writeConfigFile (fileContent);
242256 VELOX_ASSERT_THROW (
243- presto::util::readConfig (configFilePath ), expectedErrorMsg);
257+ presto::util::readConfig (configFilePath_ ), expectedErrorMsg);
244258 };
245259 testInvalid (
246260 " noequalsign\n " , " No '=' sign found for property pair 'noequalsign'" );
@@ -255,4 +269,45 @@ TEST_F(ConfigTest, optionalNodeId) {
255269 EXPECT_EQ (nodeId, config.nodeId ());
256270}
257271
272+ TEST_F (ConfigTest, readConfigEnvVarTest) {
273+ const std::string kEnvVarName = " PRESTO_READ_CONFIG_TEST_VAR" ;
274+ const std::string kEmptyEnvVarName = " PRESTO_READ_CONFIG_TEST_EMPTY_VAR" ;
275+
276+ const std::string kPlainTextKey = " plain-text" ;
277+ const std::string kPlainTextValue = " plain-text-value" ;
278+
279+ const std::string kEnvVarKey = " env-var" ;
280+ const std::string kEnvVarValue = " env-var-value" ;
281+
282+ // Keys to test invalid environment variable values.
283+ const std::string kEnvVarKey2 = " env-var2" ;
284+ const std::string kEnvVarKey3 = " env-var3" ;
285+ const std::string kNoEnvVarKey = " no-env-var" ;
286+ const std::string kEmptyEnvVarKey = " empty-env-var" ;
287+
288+ writeConfigFile (
289+ fmt::format (" {}={}\n " , kPlainTextKey , kPlainTextValue ) +
290+ fmt::format (" {}=${{{}}}\n " , kEnvVarKey , kEnvVarName ) +
291+ fmt::format (" {}=${{{}\n " , kEnvVarKey2 , kEnvVarName ) +
292+ fmt::format (" {}={}}}\n " , kEnvVarKey3 , kEnvVarName ) +
293+ fmt::format (" {}=${{}}\n " , kNoEnvVarKey ) +
294+ fmt::format (" {}=${{{}}}\n " , kEmptyEnvVarKey , kEmptyEnvVarName ));
295+
296+ setenv (kEnvVarName .c_str (), kEnvVarValue .c_str (), 1 );
297+ setenv (kEmptyEnvVarName .c_str (), " " , 1 );
298+
299+ auto properties = presto::util::readConfig (configFilePath_);
300+ std::unordered_map<std::string, std::string> expected{
301+ {kPlainTextKey , kPlainTextValue },
302+ {kEnvVarKey , kEnvVarValue },
303+ {kEnvVarKey2 , " ${PRESTO_READ_CONFIG_TEST_VAR" },
304+ {kEnvVarKey3 , " PRESTO_READ_CONFIG_TEST_VAR}" },
305+ {kNoEnvVarKey , " ${}" },
306+ {kEmptyEnvVarKey , " " }};
307+ ASSERT_EQ (properties, expected);
308+
309+ unsetenv (kEnvVarName .c_str ());
310+ unsetenv (kEmptyEnvVarName .c_str ());
311+ }
312+
258313} // namespace facebook::presto::test
0 commit comments