configuration_test.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "hdfspp/config_parser.h"
  21. #include "common/configuration.h"
  22. #include "common/configuration_loader.h"
  23. #include <cstdio>
  24. #include <fstream>
  25. #include <istream>
  26. #include <ftw.h>
  27. #include <gmock/gmock.h>
  28. namespace hdfs {
  29. template <typename T, typename U>
  30. void simpleConfigStreamProperty(std::stringstream& out, T key, U value) {
  31. out << "<property>"
  32. << "<name>" << key << "</name>"
  33. << "<value>" << value << "</value>"
  34. << "</property>";
  35. }
  36. template <typename T, typename U, typename... Args>
  37. void simpleConfigStreamProperty(std::stringstream& out, T key, U value,
  38. Args... args) {
  39. simpleConfigStreamProperty(out, key, value);
  40. simpleConfigStreamProperty(out, args...);
  41. }
  42. template <typename... Args>
  43. void simpleConfigStream(std::stringstream& out, Args... args) {
  44. out << "<configuration>";
  45. simpleConfigStreamProperty(out, args...);
  46. out << "</configuration>";
  47. }
  48. template <typename T, typename U>
  49. void damagedConfigStreamProperty(std::stringstream& out, T key, U value) {
  50. out << "<propertyy>"
  51. << "<name>" << key << "</name>"
  52. << "<value>" << value << "</value>"
  53. << "</property>";
  54. }
  55. template <typename T, typename U, typename... Args>
  56. void damagedConfigStreamProperty(std::stringstream& out, T key, U value,
  57. Args... args) {
  58. damagedConfigStreamProperty(out, key, value);
  59. damagedConfigStreamProperty(out, args...);
  60. }
  61. template <typename... Args>
  62. void damagedConfigStream(std::stringstream& out, Args... args) {
  63. out << "<configuration>";
  64. damagedConfigStreamProperty(out, args...);
  65. out << "</configuration>";
  66. }
  67. template <typename... Args>
  68. optional<Configuration> simpleConfig(Args... args) {
  69. std::stringstream stream;
  70. simpleConfigStream(stream, args...);
  71. ConfigurationLoader config_loader;
  72. config_loader.ClearSearchPath();
  73. optional<Configuration> parse = config_loader.Load<Configuration>(stream.str());
  74. EXPECT_TRUE((bool)parse);
  75. return parse;
  76. }
  77. template <typename... Args>
  78. void writeSimpleConfig(const std::string& filename, Args... args) {
  79. std::stringstream stream;
  80. simpleConfigStream(stream, args...);
  81. std::ofstream out;
  82. out.open(filename);
  83. out << stream.rdbuf();
  84. }
  85. template <typename... Args>
  86. void writeDamagedConfig(const std::string& filename, Args... args) {
  87. std::stringstream stream;
  88. damagedConfigStream(stream, args...);
  89. std::ofstream out;
  90. out.open(filename);
  91. out << stream.rdbuf();
  92. }
  93. // TempDir: is deleted on destruction
  94. class TempFile {
  95. public:
  96. std::string filename;
  97. char fn_buffer[128];
  98. int tempFileHandle;
  99. TempFile() : tempFileHandle(-1) {
  100. strncpy(fn_buffer, "/tmp/test_XXXXXXXXXX", sizeof(fn_buffer));
  101. tempFileHandle = mkstemp(fn_buffer);
  102. EXPECT_NE(-1, tempFileHandle);
  103. filename = fn_buffer;
  104. }
  105. TempFile(const std::string & fn) : filename(fn), tempFileHandle(-1) {
  106. strncpy(fn_buffer, fn.c_str(), sizeof(fn_buffer));
  107. fn_buffer[sizeof(fn_buffer)-1] = 0;
  108. }
  109. ~TempFile() { if(-1 != tempFileHandle) close(tempFileHandle); unlink(fn_buffer); }
  110. };
  111. // Callback to remove a directory in the nftw visitor
  112. int nftw_remove(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
  113. {
  114. (void)sb; (void)typeflag; (void)ftwbuf;
  115. int rv = remove(fpath);
  116. EXPECT_EQ(0, rv);
  117. return rv;
  118. }
  119. // TempDir: is created in ctor and recursively deletes in dtor
  120. class TempDir {
  121. public:
  122. std::string path;
  123. TempDir() {
  124. char fn_buffer[128];
  125. strncpy(fn_buffer, "/tmp/test_dir_XXXXXXXXXX", sizeof(fn_buffer));
  126. const char * returned_path = mkdtemp(fn_buffer);
  127. EXPECT_NE(nullptr, returned_path);
  128. path = returned_path;
  129. }
  130. ~TempDir() {
  131. if(!path.empty())
  132. nftw(path.c_str(), nftw_remove, 64, FTW_DEPTH | FTW_PHYS);
  133. }
  134. };
  135. }
  136. #endif