hdfs_test.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 "hdfs.h"
  19. void permission_disp(short permissions, char *rtr) {
  20. rtr[9] = '\0';
  21. int i;
  22. for(i=2;i>=0;i--)
  23. {
  24. short permissionsId = permissions >> (i * 3) & (short)7;
  25. char* perm;
  26. switch(permissionsId) {
  27. case 7:
  28. perm = "rwx"; break;
  29. case 6:
  30. perm = "rw-"; break;
  31. case 5:
  32. perm = "r-x"; break;
  33. case 4:
  34. perm = "r--"; break;
  35. case 3:
  36. perm = "-wx"; break;
  37. case 2:
  38. perm = "-w-"; break;
  39. case 1:
  40. perm = "--x"; break;
  41. case 0:
  42. perm = "---"; break;
  43. default:
  44. perm = "???";
  45. }
  46. strncpy(rtr, perm, 3);
  47. rtr+=3;
  48. }
  49. }
  50. int main(int argc, char **argv) {
  51. hdfsFS fs = hdfsConnectNewInstance("default", 0);
  52. if(!fs) {
  53. fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
  54. exit(-1);
  55. }
  56. hdfsFS lfs = hdfsConnectNewInstance(NULL, 0);
  57. if(!lfs) {
  58. fprintf(stderr, "Oops! Failed to connect to 'local' hdfs!\n");
  59. exit(-1);
  60. }
  61. const char* writePath = "/tmp/testfile.txt";
  62. {
  63. //Write tests
  64. hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY|O_CREAT, 0, 0, 0);
  65. if(!writeFile) {
  66. fprintf(stderr, "Failed to open %s for writing!\n", writePath);
  67. exit(-1);
  68. }
  69. fprintf(stderr, "Opened %s for writing successfully...\n", writePath);
  70. char* buffer = "Hello, World!";
  71. tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer)+1);
  72. fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
  73. tOffset currentPos = -1;
  74. if ((currentPos = hdfsTell(fs, writeFile)) == -1) {
  75. fprintf(stderr,
  76. "Failed to get current file position correctly! Got %ld!\n",
  77. currentPos);
  78. exit(-1);
  79. }
  80. fprintf(stderr, "Current position: %ld\n", currentPos);
  81. if (hdfsFlush(fs, writeFile)) {
  82. fprintf(stderr, "Failed to 'flush' %s\n", writePath);
  83. exit(-1);
  84. }
  85. fprintf(stderr, "Flushed %s successfully!\n", writePath);
  86. if (hdfsHFlush(fs, writeFile)) {
  87. fprintf(stderr, "Failed to 'hflush' %s\n", writePath);
  88. exit(-1);
  89. }
  90. fprintf(stderr, "HFlushed %s successfully!\n", writePath);
  91. hdfsCloseFile(fs, writeFile);
  92. }
  93. {
  94. //Read tests
  95. const char* readPath = "/tmp/testfile.txt";
  96. int exists = hdfsExists(fs, readPath);
  97. if (exists) {
  98. fprintf(stderr, "Failed to validate existence of %s\n", readPath);
  99. exit(-1);
  100. }
  101. hdfsFile readFile = hdfsOpenFile(fs, readPath, O_RDONLY, 0, 0, 0);
  102. if (!readFile) {
  103. fprintf(stderr, "Failed to open %s for reading!\n", readPath);
  104. exit(-1);
  105. }
  106. fprintf(stderr, "hdfsAvailable: %d\n", hdfsAvailable(fs, readFile));
  107. tOffset seekPos = 1;
  108. if(hdfsSeek(fs, readFile, seekPos)) {
  109. fprintf(stderr, "Failed to seek %s for reading!\n", readPath);
  110. exit(-1);
  111. }
  112. tOffset currentPos = -1;
  113. if((currentPos = hdfsTell(fs, readFile)) != seekPos) {
  114. fprintf(stderr,
  115. "Failed to get current file position correctly! Got %ld!\n",
  116. currentPos);
  117. exit(-1);
  118. }
  119. fprintf(stderr, "Current position: %ld\n", currentPos);
  120. static char buffer[32];
  121. tSize num_read_bytes = hdfsRead(fs, readFile, (void*)buffer,
  122. sizeof(buffer));
  123. fprintf(stderr, "Read following %d bytes:\n%s\n",
  124. num_read_bytes, buffer);
  125. num_read_bytes = hdfsPread(fs, readFile, 0, (void*)buffer,
  126. sizeof(buffer));
  127. fprintf(stderr, "Read following %d bytes:\n%s\n",
  128. num_read_bytes, buffer);
  129. hdfsCloseFile(fs, readFile);
  130. }
  131. int totalResult = 0;
  132. int result = 0;
  133. {
  134. //Generic file-system operations
  135. const char* srcPath = "/tmp/testfile.txt";
  136. const char* dstPath = "/tmp/testfile2.txt";
  137. fprintf(stderr, "hdfsCopy(remote-local): %s\n", ((result = hdfsCopy(fs, srcPath, lfs, srcPath)) ? "Failed!" : "Success!"));
  138. totalResult += result;
  139. fprintf(stderr, "hdfsCopy(remote-remote): %s\n", ((result = hdfsCopy(fs, srcPath, fs, dstPath)) ? "Failed!" : "Success!"));
  140. totalResult += result;
  141. fprintf(stderr, "hdfsMove(local-local): %s\n", ((result = hdfsMove(lfs, srcPath, lfs, dstPath)) ? "Failed!" : "Success!"));
  142. totalResult += result;
  143. fprintf(stderr, "hdfsMove(remote-local): %s\n", ((result = hdfsMove(fs, srcPath, lfs, srcPath)) ? "Failed!" : "Success!"));
  144. totalResult += result;
  145. fprintf(stderr, "hdfsRename: %s\n", ((result = hdfsRename(fs, dstPath, srcPath)) ? "Failed!" : "Success!"));
  146. totalResult += result;
  147. fprintf(stderr, "hdfsCopy(remote-remote): %s\n", ((result = hdfsCopy(fs, srcPath, fs, dstPath)) ? "Failed!" : "Success!"));
  148. totalResult += result;
  149. const char* slashTmp = "/tmp";
  150. const char* newDirectory = "/tmp/newdir";
  151. fprintf(stderr, "hdfsCreateDirectory: %s\n", ((result = hdfsCreateDirectory(fs, newDirectory)) ? "Failed!" : "Success!"));
  152. totalResult += result;
  153. fprintf(stderr, "hdfsSetReplication: %s\n", ((result = hdfsSetReplication(fs, srcPath, 2)) ? "Failed!" : "Success!"));
  154. totalResult += result;
  155. char buffer[256];
  156. const char *resp;
  157. fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", ((resp = hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer))) ? buffer : "Failed!"));
  158. totalResult += (resp ? 0 : 1);
  159. fprintf(stderr, "hdfsSetWorkingDirectory: %s\n", ((result = hdfsSetWorkingDirectory(fs, slashTmp)) ? "Failed!" : "Success!"));
  160. totalResult += result;
  161. fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", ((resp = hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer))) ? buffer : "Failed!"));
  162. totalResult += (resp ? 0 : 1);
  163. fprintf(stderr, "hdfsGetDefaultBlockSize: %ld\n", hdfsGetDefaultBlockSize(fs));
  164. fprintf(stderr, "hdfsGetCapacity: %ld\n", hdfsGetCapacity(fs));
  165. fprintf(stderr, "hdfsGetUsed: %ld\n", hdfsGetUsed(fs));
  166. hdfsFileInfo *fileInfo = NULL;
  167. if((fileInfo = hdfsGetPathInfo(fs, slashTmp)) != NULL) {
  168. fprintf(stderr, "hdfsGetPathInfo - SUCCESS!\n");
  169. fprintf(stderr, "Name: %s, ", fileInfo->mName);
  170. fprintf(stderr, "Type: %c, ", (char)(fileInfo->mKind));
  171. fprintf(stderr, "Replication: %d, ", fileInfo->mReplication);
  172. fprintf(stderr, "BlockSize: %ld, ", fileInfo->mBlockSize);
  173. fprintf(stderr, "Size: %ld, ", fileInfo->mSize);
  174. fprintf(stderr, "LastMod: %s", ctime(&fileInfo->mLastMod));
  175. fprintf(stderr, "Owner: %s, ", fileInfo->mOwner);
  176. fprintf(stderr, "Group: %s, ", fileInfo->mGroup);
  177. char permissions[10];
  178. permission_disp(fileInfo->mPermissions, permissions);
  179. fprintf(stderr, "Permissions: %d (%s)\n", fileInfo->mPermissions, permissions);
  180. hdfsFreeFileInfo(fileInfo, 1);
  181. } else {
  182. totalResult++;
  183. fprintf(stderr, "waah! hdfsGetPathInfo for %s - FAILED!\n", slashTmp);
  184. }
  185. hdfsFileInfo *fileList = 0;
  186. int numEntries = 0;
  187. if((fileList = hdfsListDirectory(fs, slashTmp, &numEntries)) != NULL) {
  188. int i = 0;
  189. for(i=0; i < numEntries; ++i) {
  190. fprintf(stderr, "Name: %s, ", fileList[i].mName);
  191. fprintf(stderr, "Type: %c, ", (char)fileList[i].mKind);
  192. fprintf(stderr, "Replication: %d, ", fileList[i].mReplication);
  193. fprintf(stderr, "BlockSize: %ld, ", fileList[i].mBlockSize);
  194. fprintf(stderr, "Size: %ld, ", fileList[i].mSize);
  195. fprintf(stderr, "LastMod: %s", ctime(&fileList[i].mLastMod));
  196. fprintf(stderr, "Owner: %s, ", fileList[i].mOwner);
  197. fprintf(stderr, "Group: %s, ", fileList[i].mGroup);
  198. char permissions[10];
  199. permission_disp(fileList[i].mPermissions, permissions);
  200. fprintf(stderr, "Permissions: %d (%s)\n", fileList[i].mPermissions, permissions);
  201. }
  202. hdfsFreeFileInfo(fileList, numEntries);
  203. } else {
  204. if (errno) {
  205. totalResult++;
  206. fprintf(stderr, "waah! hdfsListDirectory - FAILED!\n");
  207. } else {
  208. fprintf(stderr, "Empty directory!\n");
  209. }
  210. }
  211. char*** hosts = hdfsGetHosts(fs, srcPath, 0, 1);
  212. if(hosts) {
  213. fprintf(stderr, "hdfsGetHosts - SUCCESS! ... \n");
  214. int i=0;
  215. while(hosts[i]) {
  216. int j = 0;
  217. while(hosts[i][j]) {
  218. fprintf(stderr,
  219. "\thosts[%d][%d] - %s\n", i, j, hosts[i][j]);
  220. ++j;
  221. }
  222. ++i;
  223. }
  224. } else {
  225. totalResult++;
  226. fprintf(stderr, "waah! hdfsGetHosts - FAILED!\n");
  227. }
  228. char *newOwner = "root";
  229. // setting tmp dir to 777 so later when connectAsUser nobody, we can write to it
  230. short newPerm = 0666;
  231. // chown write
  232. fprintf(stderr, "hdfsChown: %s\n", ((result = hdfsChown(fs, writePath, NULL, "users")) ? "Failed!" : "Success!"));
  233. totalResult += result;
  234. fprintf(stderr, "hdfsChown: %s\n", ((result = hdfsChown(fs, writePath, newOwner, NULL)) ? "Failed!" : "Success!"));
  235. totalResult += result;
  236. // chmod write
  237. fprintf(stderr, "hdfsChmod: %s\n", ((result = hdfsChmod(fs, writePath, newPerm)) ? "Failed!" : "Success!"));
  238. totalResult += result;
  239. sleep(2);
  240. tTime newMtime = time(NULL);
  241. tTime newAtime = time(NULL);
  242. // utime write
  243. fprintf(stderr, "hdfsUtime: %s\n", ((result = hdfsUtime(fs, writePath, newMtime, newAtime)) ? "Failed!" : "Success!"));
  244. totalResult += result;
  245. // chown/chmod/utime read
  246. hdfsFileInfo *finfo = hdfsGetPathInfo(fs, writePath);
  247. fprintf(stderr, "hdfsChown read: %s\n", ((result = (strcmp(finfo->mOwner, newOwner) != 0)) ? "Failed!" : "Success!"));
  248. totalResult += result;
  249. fprintf(stderr, "hdfsChmod read: %s\n", ((result = (finfo->mPermissions != newPerm)) ? "Failed!" : "Success!"));
  250. totalResult += result;
  251. // will later use /tmp/ as a different user so enable it
  252. fprintf(stderr, "hdfsChmod: %s\n", ((result = hdfsChmod(fs, "/tmp/", 0777)) ? "Failed!" : "Success!"));
  253. totalResult += result;
  254. fprintf(stderr,"newMTime=%ld\n",newMtime);
  255. fprintf(stderr,"curMTime=%ld\n",finfo->mLastMod);
  256. fprintf(stderr, "hdfsUtime read (mtime): %s\n", ((result = (finfo->mLastMod != newMtime)) ? "Failed!" : "Success!"));
  257. totalResult += result;
  258. // No easy way to turn on access times from hdfs_test right now
  259. // fprintf(stderr, "hdfsUtime read (atime): %s\n", ((result = (finfo->mLastAccess != newAtime)) ? "Failed!" : "Success!"));
  260. // totalResult += result;
  261. hdfsFreeFileInfo(finfo, 1);
  262. // Clean up
  263. fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(fs, newDirectory, 1)) ? "Failed!" : "Success!"));
  264. totalResult += result;
  265. fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(fs, srcPath, 1)) ? "Failed!" : "Success!"));
  266. totalResult += result;
  267. fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(lfs, srcPath, 1)) ? "Failed!" : "Success!"));
  268. totalResult += result;
  269. fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(lfs, dstPath, 1)) ? "Failed!" : "Success!"));
  270. totalResult += result;
  271. fprintf(stderr, "hdfsExists: %s\n", ((result = hdfsExists(fs, newDirectory)) ? "Success!" : "Failed!"));
  272. totalResult += (result ? 0 : 1);
  273. }
  274. {
  275. // TEST APPENDS
  276. const char *writePath = "/tmp/appends";
  277. // CREATE
  278. hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY, 0, 0, 0);
  279. if(!writeFile) {
  280. fprintf(stderr, "Failed to open %s for writing!\n", writePath);
  281. exit(-1);
  282. }
  283. fprintf(stderr, "Opened %s for writing successfully...\n", writePath);
  284. char* buffer = "Hello,";
  285. tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer));
  286. fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
  287. if (hdfsFlush(fs, writeFile)) {
  288. fprintf(stderr, "Failed to 'flush' %s\n", writePath);
  289. exit(-1);
  290. }
  291. fprintf(stderr, "Flushed %s successfully!\n", writePath);
  292. hdfsCloseFile(fs, writeFile);
  293. // RE-OPEN
  294. writeFile = hdfsOpenFile(fs, writePath, O_WRONLY|O_APPEND, 0, 0, 0);
  295. if(!writeFile) {
  296. fprintf(stderr, "Failed to open %s for writing!\n", writePath);
  297. exit(-1);
  298. }
  299. fprintf(stderr, "Opened %s for writing successfully...\n", writePath);
  300. buffer = " World";
  301. num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer) + 1);
  302. fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
  303. if (hdfsFlush(fs, writeFile)) {
  304. fprintf(stderr, "Failed to 'flush' %s\n", writePath);
  305. exit(-1);
  306. }
  307. fprintf(stderr, "Flushed %s successfully!\n", writePath);
  308. hdfsCloseFile(fs, writeFile);
  309. // CHECK size
  310. hdfsFileInfo *finfo = hdfsGetPathInfo(fs, writePath);
  311. fprintf(stderr, "fileinfo->mSize: == total %s\n", ((result = (finfo->mSize == strlen("Hello, World") + 1)) ? "Success!" : "Failed!"));
  312. totalResult += (result ? 0 : 1);
  313. // READ and check data
  314. hdfsFile readFile = hdfsOpenFile(fs, writePath, O_RDONLY, 0, 0, 0);
  315. if (!readFile) {
  316. fprintf(stderr, "Failed to open %s for reading!\n", writePath);
  317. exit(-1);
  318. }
  319. char rdbuffer[32];
  320. tSize num_read_bytes = hdfsRead(fs, readFile, (void*)rdbuffer, sizeof(rdbuffer));
  321. fprintf(stderr, "Read following %d bytes:\n%s\n",
  322. num_read_bytes, rdbuffer);
  323. fprintf(stderr, "read == Hello, World %s\n", (result = (strcmp(rdbuffer, "Hello, World") == 0)) ? "Success!" : "Failed!");
  324. hdfsCloseFile(fs, readFile);
  325. // DONE test appends
  326. }
  327. totalResult += (hdfsDisconnect(fs) != 0);
  328. {
  329. //
  330. // Now test as connecting as a specific user
  331. // This is only meant to test that we connected as that user, not to test
  332. // the actual fs user capabilities. Thus just create a file and read
  333. // the owner is correct.
  334. const char *tuser = "nobody";
  335. const char* writePath = "/tmp/usertestfile.txt";
  336. fs = hdfsConnectAsUserNewInstance("default", 0, tuser);
  337. if(!fs) {
  338. fprintf(stderr, "Oops! Failed to connect to hdfs as user %s!\n",tuser);
  339. exit(-1);
  340. }
  341. hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY|O_CREAT, 0, 0, 0);
  342. if(!writeFile) {
  343. fprintf(stderr, "Failed to open %s for writing!\n", writePath);
  344. exit(-1);
  345. }
  346. fprintf(stderr, "Opened %s for writing successfully...\n", writePath);
  347. char* buffer = "Hello, World!";
  348. tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer)+1);
  349. fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
  350. if (hdfsFlush(fs, writeFile)) {
  351. fprintf(stderr, "Failed to 'flush' %s\n", writePath);
  352. exit(-1);
  353. }
  354. fprintf(stderr, "Flushed %s successfully!\n", writePath);
  355. hdfsCloseFile(fs, writeFile);
  356. hdfsFileInfo *finfo = hdfsGetPathInfo(fs, writePath);
  357. fprintf(stderr, "hdfs new file user is correct: %s\n", ((result = (strcmp(finfo->mOwner, tuser) != 0)) ? "Failed!" : "Success!"));
  358. totalResult += result;
  359. }
  360. totalResult += (hdfsDisconnect(fs) != 0);
  361. if (totalResult != 0) {
  362. return -1;
  363. } else {
  364. return 0;
  365. }
  366. }
  367. /**
  368. * vim: ts=4: sw=4: et:
  369. */