configuration_test.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #ifndef TESTS_CONFIGURATION_H_
  19. #define TESTS_CONFIGURATION_H_
  20. #include <algorithm>
  21. #include "hdfspp/config_parser.h"
  22. #include "common/configuration.h"
  23. #include "common/configuration_loader.h"
  24. #include "x-platform/syscall.h"
  25. #include <cstdio>
  26. #include <fstream>
  27. #include <istream>
  28. #include <string>
  29. #include <utility>
  30. #include <vector>
  31. #include <ftw.h>
  32. #include <unistd.h>
  33. #include <gmock/gmock.h>
  34. #include <gtest/gtest.h>
  35. namespace hdfs {
  36. template <typename T, typename U>
  37. void simpleConfigStreamProperty(std::stringstream& out, T key, U value) {
  38. out << "<property>"
  39. << "<name>" << key << "</name>"
  40. << "<value>" << value << "</value>"
  41. << "</property>";
  42. }
  43. template <typename T, typename U, typename... Args>
  44. void simpleConfigStreamProperty(std::stringstream& out, T key, U value,
  45. Args... args) {
  46. simpleConfigStreamProperty(out, key, value);
  47. simpleConfigStreamProperty(out, args...);
  48. }
  49. template <typename... Args>
  50. void simpleConfigStream(std::stringstream& out, Args... args) {
  51. out << "<configuration>";
  52. simpleConfigStreamProperty(out, args...);
  53. out << "</configuration>";
  54. }
  55. template <typename T, typename U>
  56. void damagedConfigStreamProperty(std::stringstream& out, T key, U value) {
  57. out << "<propertyy>"
  58. << "<name>" << key << "</name>"
  59. << "<value>" << value << "</value>"
  60. << "</property>";
  61. }
  62. template <typename T, typename U, typename... Args>
  63. void damagedConfigStreamProperty(std::stringstream& out, T key, U value,
  64. Args... args) {
  65. damagedConfigStreamProperty(out, key, value);
  66. damagedConfigStreamProperty(out, args...);
  67. }
  68. template <typename... Args>
  69. void damagedConfigStream(std::stringstream& out, Args... args) {
  70. out << "<configuration>";
  71. damagedConfigStreamProperty(out, args...);
  72. out << "</configuration>";
  73. }
  74. template <typename... Args>
  75. optional<Configuration> simpleConfig(Args... args) {
  76. std::stringstream stream;
  77. simpleConfigStream(stream, args...);
  78. ConfigurationLoader config_loader;
  79. config_loader.ClearSearchPath();
  80. optional<Configuration> parse = config_loader.Load<Configuration>(stream.str());
  81. EXPECT_TRUE((bool)parse);
  82. return parse;
  83. }
  84. template <typename... Args>
  85. void writeSimpleConfig(const std::string& filename, Args... args) {
  86. std::stringstream stream;
  87. simpleConfigStream(stream, args...);
  88. std::ofstream out;
  89. out.open(filename);
  90. out << stream.rdbuf();
  91. }
  92. template <typename... Args>
  93. void writeDamagedConfig(const std::string& filename, Args... args) {
  94. std::stringstream stream;
  95. damagedConfigStream(stream, args...);
  96. std::ofstream out;
  97. out.open(filename);
  98. out << stream.rdbuf();
  99. }
  100. // TempDir: is deleted on destruction
  101. class TempFile {
  102. public:
  103. TempFile() {
  104. std::vector<char> tmp_buf(filename_.begin(), filename_.end());
  105. fd_ = XPlatform::Syscall::CreateAndOpenTempFile(tmp_buf);
  106. EXPECT_NE(fd_, -1);
  107. filename_.assign(tmp_buf.data());
  108. }
  109. TempFile(std::string fn) : filename_(std::move(fn)) {}
  110. TempFile(const TempFile& other) = default;
  111. TempFile(TempFile&& other) noexcept
  112. : filename_{std::move(other.filename_)}, fd_{other.fd_} {}
  113. TempFile& operator=(const TempFile& other) {
  114. if (&other != this) {
  115. filename_ = other.filename_;
  116. fd_ = other.fd_;
  117. }
  118. return *this;
  119. }
  120. TempFile& operator=(TempFile&& other) noexcept {
  121. if (&other != this) {
  122. filename_ = std::move(other.filename_);
  123. fd_ = other.fd_;
  124. }
  125. return *this;
  126. }
  127. [[nodiscard]] const std::string& GetFileName() const { return filename_; }
  128. ~TempFile() {
  129. if (-1 != fd_) {
  130. EXPECT_NE(XPlatform::Syscall::CloseFile(fd_), -1);
  131. }
  132. unlink(filename_.c_str());
  133. }
  134. private:
  135. std::string filename_{"/tmp/test_XXXXXXXXXX"};
  136. int fd_{-1};
  137. };
  138. // Callback to remove a directory in the nftw visitor
  139. int nftw_remove(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
  140. {
  141. (void)sb; (void)typeflag; (void)ftwbuf;
  142. int rv = remove(fpath);
  143. EXPECT_EQ(0, rv);
  144. return rv;
  145. }
  146. // TempDir: is created in ctor and recursively deletes in dtor
  147. class TempDir {
  148. public:
  149. TempDir() {
  150. std::vector<char> path_pattern(path_.begin(), path_.end());
  151. is_path_init_ = XPlatform::Syscall::CreateTempDir(path_pattern);
  152. EXPECT_TRUE(is_path_init_);
  153. path_.assign(path_pattern.data());
  154. }
  155. TempDir(const TempDir& other) = default;
  156. TempDir(TempDir&& other) noexcept : path_{std::move(other.path_)} {}
  157. TempDir& operator=(const TempDir& other) {
  158. if (&other != this) {
  159. path_ = other.path_;
  160. }
  161. return *this;
  162. }
  163. TempDir& operator=(TempDir&& other) noexcept {
  164. if (&other != this) {
  165. path_ = std::move(other.path_);
  166. }
  167. return *this;
  168. }
  169. [[nodiscard]] const std::string& GetPath() const { return path_; }
  170. ~TempDir() {
  171. if (is_path_init_) {
  172. nftw(path_.c_str(), nftw_remove, 64, FTW_DEPTH | FTW_PHYS);
  173. }
  174. }
  175. private:
  176. std::string path_{"/tmp/test_dir_XXXXXXXXXX"};
  177. bool is_path_init_{false};
  178. };
  179. }
  180. #endif