hdfs_configuration_test.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "common/hdfs_configuration.h"
  19. #include "configuration_test.h"
  20. #include <gmock/gmock.h>
  21. #include <iostream>
  22. using ::testing::_;
  23. using namespace hdfs;
  24. namespace hdfs
  25. {
  26. TEST(HdfsConfigurationTest, TestDefaultOptions)
  27. {
  28. // Completely empty stream
  29. {
  30. ConfigurationLoader config_loader;
  31. config_loader.ClearSearchPath();
  32. HdfsConfiguration empty_config = config_loader.NewConfig<HdfsConfiguration>();
  33. Options options = empty_config.GetOptions();
  34. EXPECT_EQ(Options::kDefaultRpcTimeout, options.rpc_timeout);
  35. }
  36. }
  37. TEST(HdfsConfigurationTest, TestSetOptions)
  38. {
  39. // Completely empty stream
  40. {
  41. std::stringstream stream;
  42. simpleConfigStream(stream,
  43. HdfsConfiguration::kFsDefaultFsKey, "/FDFK",
  44. HdfsConfiguration::kDfsClientSocketTimeoutKey, 100,
  45. HdfsConfiguration::kIpcClientConnectMaxRetriesKey, 101,
  46. HdfsConfiguration::kIpcClientConnectRetryIntervalKey, 102,
  47. HdfsConfiguration::kIpcClientConnectTimeoutKey, 103,
  48. HdfsConfiguration::kHadoopSecurityAuthenticationKey, HdfsConfiguration::kHadoopSecurityAuthentication_kerberos
  49. );
  50. ConfigurationLoader config_loader;
  51. config_loader.ClearSearchPath();
  52. optional<HdfsConfiguration> config = config_loader.Load<HdfsConfiguration>(stream.str());
  53. EXPECT_TRUE(config && "Read stream");
  54. Options options = config->GetOptions();
  55. EXPECT_EQ("/FDFK", options.defaultFS.str());
  56. EXPECT_EQ(100, options.rpc_timeout);
  57. EXPECT_EQ(101, options.max_rpc_retries);
  58. EXPECT_EQ(102, options.rpc_retry_delay_ms);
  59. EXPECT_EQ(103, options.rpc_connect_timeout);
  60. EXPECT_EQ(Options::kKerberos, options.authentication);
  61. }
  62. }
  63. TEST(HdfsConfigurationTest, TestDefaultConfigs) {
  64. // Search path
  65. {
  66. TempDir tempDir;
  67. TempFile coreSite(tempDir.path + "/core-site.xml");
  68. writeSimpleConfig(coreSite.filename, "key1", "value1");
  69. TempFile hdfsSite(tempDir.path + "/hdfs-site.xml");
  70. writeSimpleConfig(hdfsSite.filename, "key2", "value2");
  71. ConfigurationLoader loader;
  72. loader.SetSearchPath(tempDir.path);
  73. optional<HdfsConfiguration> config = loader.LoadDefaultResources<HdfsConfiguration>();
  74. EXPECT_TRUE(config && "Parse streams");
  75. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  76. EXPECT_EQ("value2", config->GetWithDefault("key2", ""));
  77. }
  78. // Only core-site.xml available
  79. {
  80. TempDir tempDir;
  81. TempFile coreSite(tempDir.path + "/core-site.xml");
  82. writeSimpleConfig(coreSite.filename, "key1", "value1");
  83. ConfigurationLoader loader;
  84. loader.SetSearchPath(tempDir.path);
  85. optional<HdfsConfiguration> config = loader.LoadDefaultResources<HdfsConfiguration>();
  86. EXPECT_TRUE(config && "Parse streams");
  87. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  88. }
  89. // Only hdfs-site available
  90. {
  91. TempDir tempDir;
  92. TempFile hdfsSite(tempDir.path + "/hdfs-site.xml");
  93. writeSimpleConfig(hdfsSite.filename, "key2", "value2");
  94. ConfigurationLoader loader;
  95. loader.SetSearchPath(tempDir.path);
  96. optional<HdfsConfiguration> config = loader.LoadDefaultResources<HdfsConfiguration>();
  97. EXPECT_TRUE(config && "Parse streams");
  98. EXPECT_EQ("value2", config->GetWithDefault("key2", ""));
  99. }
  100. }
  101. TEST(HdfsConfigurationTest, TestConfigParserAPI) {
  102. // Config parser API
  103. {
  104. TempDir tempDir;
  105. TempFile coreSite(tempDir.path + "/core-site.xml");
  106. writeSimpleConfig(coreSite.filename, "key1", "value1");
  107. TempFile hdfsSite(tempDir.path + "/hdfs-site.xml");
  108. writeSimpleConfig(hdfsSite.filename, "key2", "value2");
  109. ConfigParser parser(tempDir.path);
  110. EXPECT_EQ("value1", parser.get_string_or("key1", ""));
  111. EXPECT_EQ("value2", parser.get_string_or("key2", ""));
  112. auto stats = parser.ValidateResources();
  113. EXPECT_EQ("core-site.xml", stats[0].first);
  114. EXPECT_EQ("OK", stats[0].second.ToString());
  115. EXPECT_EQ("hdfs-site.xml", stats[1].first);
  116. EXPECT_EQ("OK", stats[1].second.ToString());
  117. }
  118. {
  119. TempDir tempDir;
  120. TempFile coreSite(tempDir.path + "/core-site.xml");
  121. writeSimpleConfig(coreSite.filename, "key1", "value1");
  122. TempFile hdfsSite(tempDir.path + "/hdfs-site.xml");
  123. writeDamagedConfig(hdfsSite.filename, "key2", "value2");
  124. ConfigParser parser(tempDir.path);
  125. EXPECT_EQ("value1", parser.get_string_or("key1", ""));
  126. EXPECT_EQ("", parser.get_string_or("key2", ""));
  127. auto stats = parser.ValidateResources();
  128. EXPECT_EQ("core-site.xml", stats[0].first);
  129. EXPECT_EQ("OK", stats[0].second.ToString());
  130. EXPECT_EQ("hdfs-site.xml", stats[1].first);
  131. EXPECT_EQ("Exception:The configuration file has invalid xml around character 74", stats[1].second.ToString());
  132. }
  133. }
  134. int main(int argc, char *argv[])
  135. {
  136. // The following line must be executed to initialize Google Mock
  137. // (and Google Test) before running the tests.
  138. ::testing::InitGoogleMock(&argc, argv);
  139. return RUN_ALL_TESTS();
  140. }
  141. }