hdfs-tool-tests.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. #ifndef LIBHDFSPP_TOOLS_HDFS_TOOL_TESTS
  18. #define LIBHDFSPP_TOOLS_HDFS_TOOL_TESTS
  19. #include <memory>
  20. #include <string>
  21. /**
  22. * This file contains the generalized test cases to run against the derivatives
  23. * of {@link hdfs::tools::HdfsTool}.
  24. *
  25. * Each test case passes the arguments to the {@link hdfs::tools::HdfsTool} and
  26. * calls the method to set the expectation on the instance of {@link
  27. * hdfs::tools::HdfsTool} as defined in its corresponding mock implementation.
  28. */
  29. template <class T> std::unique_ptr<T> PassAPath() {
  30. constexpr auto argc = 2;
  31. static std::string exe("hdfs_tool_name");
  32. static std::string arg1("a/b/c");
  33. static char *argv[] = {exe.data(), arg1.data()};
  34. auto hdfs_tool = std::make_unique<T>(argc, argv);
  35. hdfs_tool->SetExpectations(PassAPath<T>, {arg1});
  36. return hdfs_tool;
  37. }
  38. template <class T> std::unique_ptr<T> PassRecursive() {
  39. constexpr auto argc = 2;
  40. static std::string exe("hdfs_tool_name");
  41. static std::string arg1("-R");
  42. static char *argv[] = {exe.data(), arg1.data()};
  43. auto hdfs_tool = std::make_unique<T>(argc, argv);
  44. hdfs_tool->SetExpectations(PassRecursive<T>, {arg1});
  45. return hdfs_tool;
  46. }
  47. template <class T> std::unique_ptr<T> PassRecursivePath() {
  48. constexpr auto argc = 3;
  49. static std::string exe("hdfs_tool_name");
  50. static std::string arg1("-R");
  51. static std::string arg2("a/b/c");
  52. static char *argv[] = {exe.data(), arg1.data(), arg2.data()};
  53. auto hdfs_tool = std::make_unique<T>(argc, argv);
  54. hdfs_tool->SetExpectations(PassRecursivePath<T>, {arg1, arg2});
  55. return hdfs_tool;
  56. }
  57. template <class T> std::unique_ptr<T> PassFOptAndAPath() {
  58. constexpr auto argc = 3;
  59. static std::string exe("hdfs_tool_name");
  60. static std::string arg1("-f");
  61. static std::string arg2("a/b/c");
  62. static char *argv[] = {exe.data(), arg1.data(), arg2.data()};
  63. auto hdfs_tool = std::make_unique<T>(argc, argv);
  64. hdfs_tool->SetExpectations(PassFOptAndAPath<T>, {arg1, arg2});
  65. return hdfs_tool;
  66. }
  67. template <class T> std::unique_ptr<T> CallHelp() {
  68. constexpr auto argc = 2;
  69. static std::string exe("hdfs_tool_name");
  70. static std::string arg1("-h");
  71. static char *argv[] = {exe.data(), arg1.data()};
  72. auto hdfs_tool = std::make_unique<T>(argc, argv);
  73. hdfs_tool->SetExpectations(CallHelp<T>);
  74. return hdfs_tool;
  75. }
  76. template <class T> std::unique_ptr<T> Pass2Paths() {
  77. constexpr auto argc = 3;
  78. static std::string exe("hdfs_tool_name");
  79. static std::string arg1("a/b/c");
  80. static std::string arg2("d/e/f");
  81. static char *argv[] = {exe.data(), arg1.data(), arg2.data()};
  82. auto hdfs_tool = std::make_unique<T>(argc, argv);
  83. hdfs_tool->SetExpectations(Pass2Paths<T>, {arg1, arg2});
  84. return hdfs_tool;
  85. }
  86. template <class T> std::unique_ptr<T> Pass3Paths() {
  87. constexpr auto argc = 4;
  88. static std::string exe("hdfs_tool_name");
  89. static std::string arg1("a/b/c");
  90. static std::string arg2("d/e/f");
  91. static std::string arg3("g/h/i");
  92. static char *argv[] = {exe.data(), arg1.data(), arg2.data(), arg3.data()};
  93. auto hdfs_tool = std::make_unique<T>(argc, argv);
  94. hdfs_tool->SetExpectations(Pass3Paths<T>, {arg1, arg2, arg3});
  95. return hdfs_tool;
  96. }
  97. template <class T> std::unique_ptr<T> PassNOptAndAPath() {
  98. constexpr auto argc = 4;
  99. static std::string exe("hdfs_tool_name");
  100. static std::string arg1("-n");
  101. static std::string arg2("some_name");
  102. static std::string arg3("g/h/i");
  103. static char *argv[] = {exe.data(), arg1.data(), arg2.data(), arg3.data()};
  104. auto hdfs_tool = std::make_unique<T>(argc, argv);
  105. hdfs_tool->SetExpectations(PassNOptAndAPath<T>, {arg1, arg2, arg3});
  106. return hdfs_tool;
  107. }
  108. template <class T> std::unique_ptr<T> PassOwnerAndAPath() {
  109. constexpr auto argc = 3;
  110. static std::string exe("hdfs_tool_name");
  111. static std::string arg1("new_owner:new_group");
  112. static std::string arg2("g/h/i");
  113. static char *argv[] = {exe.data(), arg1.data(), arg2.data()};
  114. auto hdfs_tool = std::make_unique<T>(argc, argv);
  115. hdfs_tool->SetExpectations(PassOwnerAndAPath<T>, {arg1, arg2});
  116. return hdfs_tool;
  117. }
  118. template <class T> std::unique_ptr<T> PassRecursiveOwnerAndAPath() {
  119. constexpr auto argc = 4;
  120. static std::string exe("hdfs_tool_name");
  121. static std::string arg1("-R");
  122. static std::string arg2("new_owner:new_group");
  123. static std::string arg3("g/h/i");
  124. static char *argv[] = {exe.data(), arg1.data(), arg2.data(), arg3.data()};
  125. auto hdfs_tool = std::make_unique<T>(argc, argv);
  126. hdfs_tool->SetExpectations(PassRecursiveOwnerAndAPath<T>, {arg1, arg2, arg3});
  127. return hdfs_tool;
  128. }
  129. template <class T> std::unique_ptr<T> PassPermissionsAndAPath() {
  130. constexpr auto argc = 3;
  131. static std::string exe("hdfs_tool_name");
  132. static std::string arg1("757");
  133. static std::string arg2("g/h/i");
  134. static char *argv[] = {exe.data(), arg1.data(), arg2.data()};
  135. auto hdfs_tool = std::make_unique<T>(argc, argv);
  136. hdfs_tool->SetExpectations(PassPermissionsAndAPath<T>, {arg1, arg2});
  137. return hdfs_tool;
  138. }
  139. template <class T> std::unique_ptr<T> PassRecursivePermissionsAndAPath() {
  140. constexpr auto argc = 4;
  141. static std::string exe("hdfs_tool_name");
  142. static std::string arg1("-R");
  143. static std::string arg2("757");
  144. static std::string arg3("g/h/i");
  145. static char *argv[] = {exe.data(), arg1.data(), arg2.data(), arg3.data()};
  146. auto hdfs_tool = std::make_unique<T>(argc, argv);
  147. hdfs_tool->SetExpectations(PassRecursivePermissionsAndAPath<T>,
  148. {arg1, arg2, arg3});
  149. return hdfs_tool;
  150. }
  151. template <class T> std::unique_ptr<T> PassQOpt() {
  152. constexpr auto argc = 2;
  153. static std::string exe("hdfs_tool_name");
  154. static std::string arg1("-q");
  155. static char *argv[] = {exe.data(), arg1.data()};
  156. auto hdfs_tool = std::make_unique<T>(argc, argv);
  157. hdfs_tool->SetExpectations(PassQOpt<T>, {arg1});
  158. return hdfs_tool;
  159. }
  160. template <class T> std::unique_ptr<T> PassQOptAndPath() {
  161. constexpr auto argc = 3;
  162. static std::string exe("hdfs_tool_name");
  163. static std::string arg1("-q");
  164. static std::string arg2("a/b/c");
  165. static char *argv[] = {exe.data(), arg1.data(), arg2.data()};
  166. auto hdfs_tool = std::make_unique<T>(argc, argv);
  167. hdfs_tool->SetExpectations(PassQOptAndPath<T>, {arg1, arg2});
  168. return hdfs_tool;
  169. }
  170. template <class T> std::unique_ptr<T> PassPOpt() {
  171. constexpr auto argc = 2;
  172. static std::string exe("hdfs_tool_name");
  173. static std::string arg1("-p");
  174. static char *argv[] = {exe.data(), arg1.data()};
  175. auto hdfs_tool = std::make_unique<T>(argc, argv);
  176. hdfs_tool->SetExpectations(PassPOpt<T>, {arg1});
  177. return hdfs_tool;
  178. }
  179. template <class T> std::unique_ptr<T> PassMOpt() {
  180. constexpr auto argc = 2;
  181. static std::string exe("hdfs_tool_name");
  182. static std::string arg1("-m");
  183. static char *argv[] = {exe.data(), arg1.data()};
  184. auto hdfs_tool = std::make_unique<T>(argc, argv);
  185. hdfs_tool->SetExpectations(PassMOpt<T>, {arg1});
  186. return hdfs_tool;
  187. }
  188. template <class T> std::unique_ptr<T> PassFOpt() {
  189. constexpr auto argc = 2;
  190. static std::string exe("hdfs_tool_name");
  191. static std::string arg1("-f");
  192. static char *argv[] = {exe.data(), arg1.data()};
  193. auto hdfs_tool = std::make_unique<T>(argc, argv);
  194. hdfs_tool->SetExpectations(PassFOpt<T>, {arg1});
  195. return hdfs_tool;
  196. }
  197. template <class T> std::unique_ptr<T> PassPOptAndPath() {
  198. constexpr auto argc = 3;
  199. static std::string exe("hdfs_tool_name");
  200. static std::string arg1("-p");
  201. static std::string arg2("a/b/c");
  202. static char *argv[] = {exe.data(), arg1.data(), arg2.data()};
  203. auto hdfs_tool = std::make_unique<T>(argc, argv);
  204. hdfs_tool->SetExpectations(PassPOptAndPath<T>, {arg1, arg2});
  205. return hdfs_tool;
  206. }
  207. template <class T> std::unique_ptr<T> PassMOptPermissionsAndAPath() {
  208. constexpr auto argc = 4;
  209. static std::string exe("hdfs_tool_name");
  210. static std::string arg1("-m");
  211. static std::string arg2("757");
  212. static std::string arg3("g/h/i");
  213. static char *argv[] = {exe.data(), arg1.data(), arg2.data(), arg3.data()};
  214. auto hdfs_tool = std::make_unique<T>(argc, argv);
  215. hdfs_tool->SetExpectations(PassMOptPermissionsAndAPath<T>,
  216. {arg1, arg2, arg3});
  217. return hdfs_tool;
  218. }
  219. template <class T> std::unique_ptr<T> PassMPOptsPermissionsAndAPath() {
  220. constexpr auto argc = 5;
  221. static std::string exe("hdfs_tool_name");
  222. static std::string arg1("-m");
  223. static std::string arg2("757");
  224. static std::string arg3("-p");
  225. static std::string arg4("g/h/i");
  226. static char *argv[] = {exe.data(), arg1.data(), arg2.data(), arg3.data(),
  227. arg4.data()};
  228. auto hdfs_tool = std::make_unique<T>(argc, argv);
  229. hdfs_tool->SetExpectations(PassMPOptsPermissionsAndAPath<T>,
  230. {arg1, arg2, arg3, arg4});
  231. return hdfs_tool;
  232. }
  233. template <class T> std::unique_ptr<T> PassNStrMNumAndAPath() {
  234. constexpr auto argc = 6;
  235. static std::string exe("hdfs_tool_name");
  236. static std::string arg1("-n");
  237. static std::string arg2("some_str");
  238. static std::string arg3("-m");
  239. static std::string arg4("757");
  240. static std::string arg5("some/path");
  241. static char *argv[] = {exe.data(), arg1.data(), arg2.data(),
  242. arg3.data(), arg4.data(), arg5.data()};
  243. auto hdfs_tool = std::make_unique<T>(argc, argv);
  244. hdfs_tool->SetExpectations(PassNStrMNumAndAPath<T>,
  245. {arg1, arg2, arg3, arg4, arg5});
  246. return hdfs_tool;
  247. }
  248. template <class T> std::unique_ptr<T> PassNOpt() {
  249. constexpr auto argc = 2;
  250. static std::string exe("hdfs_tool_name");
  251. static std::string arg1("-n");
  252. static char *argv[] = {exe.data(), arg1.data()};
  253. auto hdfs_tool = std::make_unique<T>(argc, argv);
  254. hdfs_tool->SetExpectations(PassNOpt<T>, {arg1});
  255. return hdfs_tool;
  256. }
  257. #endif