hdfs_configuration_test.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. using ::testing::_;
  22. using namespace hdfs;
  23. namespace hdfs
  24. {
  25. TEST(HdfsConfigurationTest, TestDefaultOptions)
  26. {
  27. // Completely empty stream
  28. {
  29. HdfsConfiguration empty_config = ConfigurationLoader().New<HdfsConfiguration>();
  30. Options options = empty_config.GetOptions();
  31. EXPECT_EQ(Options::kDefaultRpcTimeout, options.rpc_timeout);
  32. }
  33. }
  34. TEST(HdfsConfigurationTest, TestSetOptions)
  35. {
  36. // Completely empty stream
  37. {
  38. std::stringstream stream;
  39. simpleConfigStream(stream,
  40. HdfsConfiguration::kFsDefaultFsKey, "/FDFK",
  41. HdfsConfiguration::kDfsClientSocketTimeoutKey, 100,
  42. HdfsConfiguration::kIpcClientConnectMaxRetriesKey, 101,
  43. HdfsConfiguration::kIpcClientConnectRetryIntervalKey, 102,
  44. HdfsConfiguration::kIpcClientConnectTimeoutKey, 103,
  45. HdfsConfiguration::kHadoopSecurityAuthenticationKey, HdfsConfiguration::kHadoopSecurityAuthentication_kerberos
  46. );
  47. optional<HdfsConfiguration> config = ConfigurationLoader().Load<HdfsConfiguration>(stream.str());
  48. EXPECT_TRUE(config && "Read stream");
  49. Options options = config->GetOptions();
  50. EXPECT_EQ("/FDFK", options.defaultFS.str());
  51. EXPECT_EQ(100, options.rpc_timeout);
  52. EXPECT_EQ(101, options.max_rpc_retries);
  53. EXPECT_EQ(102, options.rpc_retry_delay_ms);
  54. EXPECT_EQ(103, options.rpc_connect_timeout);
  55. EXPECT_EQ(Options::kKerberos, options.authentication);
  56. }
  57. }
  58. TEST(HdfsConfigurationTest, TestDefaultConfigs) {
  59. // Search path
  60. {
  61. TempDir tempDir;
  62. TempFile coreSite(tempDir.path + "/core-site.xml");
  63. writeSimpleConfig(coreSite.filename, "key1", "value1");
  64. TempFile hdfsSite(tempDir.path + "/hdfs-site.xml");
  65. writeSimpleConfig(hdfsSite.filename, "key2", "value2");
  66. ConfigurationLoader loader;
  67. loader.SetSearchPath(tempDir.path);
  68. optional<HdfsConfiguration> config = loader.LoadDefaultResources<HdfsConfiguration>();
  69. EXPECT_TRUE(config && "Parse streams");
  70. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  71. EXPECT_EQ("value2", config->GetWithDefault("key2", ""));
  72. }
  73. // Only core-site.xml available
  74. {
  75. TempDir tempDir;
  76. TempFile coreSite(tempDir.path + "/core-site.xml");
  77. writeSimpleConfig(coreSite.filename, "key1", "value1");
  78. ConfigurationLoader loader;
  79. loader.SetSearchPath(tempDir.path);
  80. optional<HdfsConfiguration> config = loader.LoadDefaultResources<HdfsConfiguration>();
  81. EXPECT_TRUE(config && "Parse streams");
  82. EXPECT_EQ("value1", config->GetWithDefault("key1", ""));
  83. }
  84. // Only hdfs-site available
  85. {
  86. TempDir tempDir;
  87. TempFile hdfsSite(tempDir.path + "/hdfs-site.xml");
  88. writeSimpleConfig(hdfsSite.filename, "key2", "value2");
  89. ConfigurationLoader loader;
  90. loader.SetSearchPath(tempDir.path);
  91. optional<HdfsConfiguration> config = loader.LoadDefaultResources<HdfsConfiguration>();
  92. EXPECT_TRUE(config && "Parse streams");
  93. EXPECT_EQ("value2", config->GetWithDefault("key2", ""));
  94. }
  95. }
  96. int main(int argc, char *argv[])
  97. {
  98. // The following line must be executed to initialize Google Mock
  99. // (and Google Test) before running the tests.
  100. ::testing::InitGoogleMock(&argc, argv);
  101. return RUN_ALL_TESTS();
  102. }
  103. }