hdfs_builder_test.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "hdfspp/hdfs_ext.h"
  19. #include "configuration_test.h"
  20. #include "utils/temp-file.h"
  21. #include "utils/temp-dir.h"
  22. #include <gmock/gmock.h>
  23. #include <google/protobuf/stubs/common.h>
  24. using ::testing::_;
  25. using namespace hdfs;
  26. TEST(HdfsBuilderTest, TestStubBuilder) {
  27. {
  28. TestUtils::TempDir tempDir1;
  29. hdfsBuilder *builder =
  30. hdfsNewBuilderFromDirectory(tempDir1.GetPath().c_str());
  31. hdfsFreeBuilder(builder);
  32. }
  33. {
  34. hdfsBuilder * builder = hdfsNewBuilderFromDirectory("/this/path/does/not/exist");
  35. hdfsFreeBuilder(builder);
  36. }
  37. }
  38. TEST(HdfsBuilderTest, TestRead)
  39. {
  40. // Reading string values
  41. {
  42. TestUtils::TempDir tempDir1;
  43. TestUtils::TempFile tempFile1(tempDir1.GetPath() + "/core-site.xml");
  44. writeSimpleConfig(tempFile1.GetFileName(), "key1", "value1");
  45. hdfsBuilder *builder =
  46. hdfsNewBuilderFromDirectory(tempDir1.GetPath().c_str());
  47. char * readVal = nullptr;
  48. int result = hdfsBuilderConfGetStr(builder, "key1", &readVal);
  49. ASSERT_EQ(0, result);
  50. ASSERT_NE(nullptr, readVal);
  51. EXPECT_EQ("value1", std::string(readVal));
  52. hdfsConfStrFree(readVal);
  53. readVal = nullptr;
  54. result = hdfsBuilderConfGetStr(builder, "key2", &readVal);
  55. ASSERT_EQ(0, result);
  56. EXPECT_EQ(nullptr, readVal);
  57. hdfsFreeBuilder(builder);
  58. }
  59. // Reading int values
  60. {
  61. TestUtils::TempDir tempDir1;
  62. TestUtils::TempFile tempFile1(tempDir1.GetPath() + "/core-site.xml");
  63. writeSimpleConfig(tempFile1.GetFileName(), "key1", "100");
  64. hdfsBuilder *builder =
  65. hdfsNewBuilderFromDirectory(tempDir1.GetPath().c_str());
  66. int readVal = -1;
  67. int result = hdfsBuilderConfGetInt(builder, "key1", &readVal);
  68. EXPECT_EQ(0, result);
  69. EXPECT_EQ(100, readVal);
  70. readVal = -1;
  71. result = hdfsBuilderConfGetInt(builder, "key2", &readVal);
  72. EXPECT_EQ(0, result);
  73. EXPECT_EQ(-1, readVal);
  74. hdfsFreeBuilder(builder);
  75. }
  76. }
  77. TEST(HdfsBuilderTest, TestSet)
  78. {
  79. {
  80. // Setting values in an empty builder
  81. // Don't use default, or it will load any data in /etc/hadoop
  82. hdfsBuilder * builder = hdfsNewBuilderFromDirectory("/this/path/does/not/exist");
  83. int result = hdfsBuilderConfSetStr(builder, "key1", "100");
  84. EXPECT_EQ(0, result);
  85. int readVal = -1;
  86. result = hdfsBuilderConfGetInt(builder, "key1", &readVal);
  87. EXPECT_EQ(0, result);
  88. EXPECT_EQ(100, readVal);
  89. // Set value in non-empty builder
  90. result = hdfsBuilderConfSetStr(builder, "key2", "200");
  91. EXPECT_EQ(0, result);
  92. readVal = -1;
  93. result = hdfsBuilderConfGetInt(builder, "key2", &readVal);
  94. EXPECT_EQ(0, result);
  95. EXPECT_EQ(200, readVal);
  96. // Overwrite value
  97. result = hdfsBuilderConfSetStr(builder, "key2", "300");
  98. EXPECT_EQ(0, result);
  99. readVal = -1;
  100. result = hdfsBuilderConfGetInt(builder, "key2", &readVal);
  101. EXPECT_EQ(0, result);
  102. EXPECT_EQ(300, readVal);
  103. hdfsFreeBuilder(builder);
  104. }
  105. }
  106. int main(int argc, char *argv[]) {
  107. /*
  108. * The following line must be executed to initialize Google Mock
  109. * (and Google Test) before running the tests.
  110. */
  111. ::testing::InitGoogleMock(&argc, argv);
  112. int exit_code = RUN_ALL_TESTS();
  113. // Clean up static data and prevent valgrind memory leaks
  114. google::protobuf::ShutdownProtobufLibrary();
  115. return exit_code;
  116. }