test_libwebhdfs_ops.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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/hdfs.h"
  19. #include "libhdfs-tests/native_mini_dfs.h"
  20. #include <inttypes.h>
  21. #include <jni.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <unistd.h>
  27. static struct NativeMiniDfsCluster *cluster;
  28. void permission_disp(short permissions, char *rtr)
  29. {
  30. rtr[9] = '\0';
  31. int i;
  32. short perm;
  33. for(i = 2; i >= 0; i--)
  34. {
  35. perm = permissions >> (i * 3);
  36. rtr[0] = perm & 4 ? 'r' : '-';
  37. rtr[1] = perm & 2 ? 'w' : '-';
  38. rtr[2] = perm & 1 ? 'x' : '-';
  39. rtr += 3;
  40. }
  41. }
  42. int main(int argc, char **argv)
  43. {
  44. char buffer[32];
  45. tSize num_written_bytes;
  46. const char* slashTmp = "/tmp";
  47. int nnPort;
  48. char *rwTemplate, *rwTemplate2, *newDirTemplate,
  49. *appendTemplate, *userTemplate, *rwPath = NULL;
  50. const char* fileContents = "Hello, World!";
  51. const char* nnHost = NULL;
  52. if (argc != 2) {
  53. fprintf(stderr, "usage: test_libwebhdfs_ops <username>\n");
  54. exit(1);
  55. }
  56. struct NativeMiniDfsConf conf = {
  57. .doFormat = 1, .webhdfsEnabled = 1, .namenodeHttpPort = 50070,
  58. };
  59. cluster = nmdCreate(&conf);
  60. if (!cluster) {
  61. fprintf(stderr, "Failed to create the NativeMiniDfsCluster.\n");
  62. exit(1);
  63. }
  64. if (nmdWaitClusterUp(cluster)) {
  65. fprintf(stderr, "Error when waiting for cluster to be ready.\n");
  66. exit(1);
  67. }
  68. if (nmdGetNameNodeHttpAddress(cluster, &nnPort, &nnHost)) {
  69. fprintf(stderr, "Error when retrieving namenode host address.\n");
  70. exit(1);
  71. }
  72. hdfsFS fs = hdfsConnectAsUserNewInstance(nnHost, nnPort, argv[1]);
  73. if(!fs) {
  74. fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
  75. exit(-1);
  76. }
  77. {
  78. // Write tests
  79. rwTemplate = strdup("/tmp/helloWorldXXXXXX");
  80. if (!rwTemplate) {
  81. fprintf(stderr, "Failed to create rwTemplate!\n");
  82. exit(1);
  83. }
  84. rwPath = mktemp(rwTemplate);
  85. // hdfsOpenFile
  86. hdfsFile writeFile = hdfsOpenFile(fs, rwPath,
  87. O_WRONLY|O_CREAT, 0, 0, 0);
  88. if(!writeFile) {
  89. fprintf(stderr, "Failed to open %s for writing!\n", rwPath);
  90. exit(1);
  91. }
  92. fprintf(stderr, "Opened %s for writing successfully...\n", rwPath);
  93. // hdfsWrite
  94. num_written_bytes = hdfsWrite(fs, writeFile, (void*)fileContents,
  95. (int) strlen(fileContents) + 1);
  96. if (num_written_bytes != strlen(fileContents) + 1) {
  97. fprintf(stderr, "Failed to write correct number of bytes - "
  98. "expected %d, got %d\n",
  99. (int)(strlen(fileContents) + 1), (int) num_written_bytes);
  100. exit(1);
  101. }
  102. fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
  103. // hdfsTell
  104. tOffset currentPos = -1;
  105. if ((currentPos = hdfsTell(fs, writeFile)) == -1) {
  106. fprintf(stderr,
  107. "Failed to get current file position correctly. Got %"
  108. PRId64 "!\n", currentPos);
  109. exit(1);
  110. }
  111. fprintf(stderr, "Current position: %" PRId64 "\n", currentPos);
  112. hdfsCloseFile(fs, writeFile);
  113. // Done test write
  114. }
  115. sleep(1);
  116. {
  117. //Read tests
  118. int available = 0, exists = 0;
  119. // hdfsExists
  120. exists = hdfsExists(fs, rwPath);
  121. if (exists) {
  122. fprintf(stderr, "Failed to validate existence of %s\n", rwPath);
  123. exists = hdfsExists(fs, rwPath);
  124. if (exists) {
  125. fprintf(stderr,
  126. "Still failed to validate existence of %s\n", rwPath);
  127. exit(1);
  128. }
  129. }
  130. hdfsFile readFile = hdfsOpenFile(fs, rwPath, O_RDONLY, 0, 0, 0);
  131. if (!readFile) {
  132. fprintf(stderr, "Failed to open %s for reading!\n", rwPath);
  133. exit(1);
  134. }
  135. if (!hdfsFileIsOpenForRead(readFile)) {
  136. fprintf(stderr, "hdfsFileIsOpenForRead: we just opened a file "
  137. "with O_RDONLY, and it did not show up as 'open for "
  138. "read'\n");
  139. exit(1);
  140. }
  141. available = hdfsAvailable(fs, readFile);
  142. fprintf(stderr, "hdfsAvailable: %d\n", available);
  143. // hdfsSeek, hdfsTell
  144. tOffset seekPos = 1;
  145. if(hdfsSeek(fs, readFile, seekPos)) {
  146. fprintf(stderr, "Failed to seek %s for reading!\n", rwPath);
  147. exit(1);
  148. }
  149. tOffset currentPos = -1;
  150. if((currentPos = hdfsTell(fs, readFile)) != seekPos) {
  151. fprintf(stderr,
  152. "Failed to get current file position correctly! Got %"
  153. PRId64 "!\n", currentPos);
  154. exit(1);
  155. }
  156. fprintf(stderr, "Current position: %" PRId64 "\n", currentPos);
  157. if(hdfsSeek(fs, readFile, 0)) {
  158. fprintf(stderr, "Failed to seek %s for reading!\n", rwPath);
  159. exit(1);
  160. }
  161. // hdfsRead
  162. memset(buffer, 0, sizeof(buffer));
  163. tSize num_read_bytes = hdfsRead(fs, readFile, buffer, sizeof(buffer));
  164. if (strncmp(fileContents, buffer, strlen(fileContents)) != 0) {
  165. fprintf(stderr, "Failed to read (direct). "
  166. "Expected %s but got %s (%d bytes)\n",
  167. fileContents, buffer, num_read_bytes);
  168. exit(1);
  169. }
  170. fprintf(stderr, "Read following %d bytes:\n%s\n",
  171. num_read_bytes, buffer);
  172. if (hdfsSeek(fs, readFile, 0L)) {
  173. fprintf(stderr, "Failed to seek to file start!\n");
  174. exit(1);
  175. }
  176. // hdfsPread
  177. memset(buffer, 0, strlen(fileContents + 1));
  178. num_read_bytes = hdfsPread(fs, readFile, 0, buffer, sizeof(buffer));
  179. fprintf(stderr, "Read following %d bytes:\n%s\n",
  180. num_read_bytes, buffer);
  181. hdfsCloseFile(fs, readFile);
  182. // Done test read
  183. }
  184. int totalResult = 0;
  185. int result = 0;
  186. {
  187. //Generic file-system operations
  188. char *srcPath = rwPath;
  189. char buffer[256];
  190. const char *resp;
  191. rwTemplate2 = strdup("/tmp/helloWorld2XXXXXX");
  192. if (!rwTemplate2) {
  193. fprintf(stderr, "Failed to create rwTemplate2!\n");
  194. exit(1);
  195. }
  196. char *dstPath = mktemp(rwTemplate2);
  197. newDirTemplate = strdup("/tmp/newdirXXXXXX");
  198. if (!newDirTemplate) {
  199. fprintf(stderr, "Failed to create newDirTemplate!\n");
  200. exit(1);
  201. }
  202. char *newDirectory = mktemp(newDirTemplate);
  203. // hdfsRename
  204. fprintf(stderr, "hdfsRename: %s\n",
  205. ((result = hdfsRename(fs, rwPath, dstPath)) ?
  206. "Failed!" : "Success!"));
  207. totalResult += result;
  208. fprintf(stderr, "hdfsRename back: %s\n",
  209. ((result = hdfsRename(fs, dstPath, srcPath)) ?
  210. "Failed!" : "Success!"));
  211. totalResult += result;
  212. // hdfsCreateDirectory
  213. fprintf(stderr, "hdfsCreateDirectory: %s\n",
  214. ((result = hdfsCreateDirectory(fs, newDirectory)) ?
  215. "Failed!" : "Success!"));
  216. totalResult += result;
  217. // hdfsSetReplication
  218. fprintf(stderr, "hdfsSetReplication: %s\n",
  219. ((result = hdfsSetReplication(fs, srcPath, 1)) ?
  220. "Failed!" : "Success!"));
  221. totalResult += result;
  222. // hdfsGetWorkingDirectory, hdfsSetWorkingDirectory
  223. fprintf(stderr, "hdfsGetWorkingDirectory: %s\n",
  224. ((resp = hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer))) ?
  225. buffer : "Failed!"));
  226. totalResult += (resp ? 0 : 1);
  227. const char* path[] = {"/foo", "/foo/bar", "foobar", "//foo/bar//foobar",
  228. "foo//bar", "foo/bar///", "/", "////"};
  229. int i;
  230. for (i = 0; i < 8; i++) {
  231. fprintf(stderr, "hdfsSetWorkingDirectory: %s, %s\n",
  232. ((result = hdfsSetWorkingDirectory(fs, path[i])) ?
  233. "Failed!" : "Success!"),
  234. hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer)));
  235. totalResult += result;
  236. }
  237. fprintf(stderr, "hdfsSetWorkingDirectory: %s\n",
  238. ((result = hdfsSetWorkingDirectory(fs, slashTmp)) ?
  239. "Failed!" : "Success!"));
  240. totalResult += result;
  241. fprintf(stderr, "hdfsGetWorkingDirectory: %s\n",
  242. ((resp = hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer))) ?
  243. buffer : "Failed!"));
  244. totalResult += (resp ? 0 : 1);
  245. // hdfsGetPathInfo
  246. hdfsFileInfo *fileInfo = NULL;
  247. if((fileInfo = hdfsGetPathInfo(fs, slashTmp)) != NULL) {
  248. fprintf(stderr, "hdfsGetPathInfo - SUCCESS!\n");
  249. fprintf(stderr, "Name: %s, ", fileInfo->mName);
  250. fprintf(stderr, "Type: %c, ", (char)(fileInfo->mKind));
  251. fprintf(stderr, "Replication: %d, ", fileInfo->mReplication);
  252. fprintf(stderr, "BlockSize: %"PRId64", ", fileInfo->mBlockSize);
  253. fprintf(stderr, "Size: %"PRId64", ", fileInfo->mSize);
  254. fprintf(stderr, "LastMod: %s", ctime(&fileInfo->mLastMod));
  255. fprintf(stderr, "Owner: %s, ", fileInfo->mOwner);
  256. fprintf(stderr, "Group: %s, ", fileInfo->mGroup);
  257. char permissions[10];
  258. permission_disp(fileInfo->mPermissions, permissions);
  259. fprintf(stderr, "Permissions: %d (%s)\n",
  260. fileInfo->mPermissions, permissions);
  261. hdfsFreeFileInfo(fileInfo, 1);
  262. } else {
  263. totalResult++;
  264. fprintf(stderr, "hdfsGetPathInfo for %s - FAILED!\n", slashTmp);
  265. }
  266. // hdfsListDirectory
  267. hdfsFileInfo *fileList = 0;
  268. int numEntries = 0;
  269. if((fileList = hdfsListDirectory(fs, slashTmp, &numEntries)) != NULL) {
  270. int i = 0;
  271. for(i=0; i < numEntries; ++i) {
  272. fprintf(stderr, "Name: %s, ", fileList[i].mName);
  273. fprintf(stderr, "Type: %c, ", (char)fileList[i].mKind);
  274. fprintf(stderr, "Replication: %d, ", fileList[i].mReplication);
  275. fprintf(stderr, "BlockSize: %"PRId64", ", fileList[i].mBlockSize);
  276. fprintf(stderr, "Size: %"PRId64", ", fileList[i].mSize);
  277. fprintf(stderr, "LastMod: %s", ctime(&fileList[i].mLastMod));
  278. fprintf(stderr, "Owner: %s, ", fileList[i].mOwner);
  279. fprintf(stderr, "Group: %s, ", fileList[i].mGroup);
  280. char permissions[10];
  281. permission_disp(fileList[i].mPermissions, permissions);
  282. fprintf(stderr, "Permissions: %d (%s)\n",
  283. fileList[i].mPermissions, permissions);
  284. }
  285. hdfsFreeFileInfo(fileList, numEntries);
  286. } else {
  287. if (errno) {
  288. totalResult++;
  289. fprintf(stderr, "waah! hdfsListDirectory - FAILED!\n");
  290. } else {
  291. fprintf(stderr, "Empty directory!\n");
  292. }
  293. }
  294. char *newOwner = "root";
  295. // Setting tmp dir to 777 so later when connectAsUser nobody,
  296. // we can write to it
  297. short newPerm = 0666;
  298. // hdfsChown
  299. fprintf(stderr, "hdfsChown: %s\n",
  300. ((result = hdfsChown(fs, rwPath, NULL, "users")) ?
  301. "Failed!" : "Success!"));
  302. totalResult += result;
  303. fprintf(stderr, "hdfsChown: %s\n",
  304. ((result = hdfsChown(fs, rwPath, newOwner, NULL)) ?
  305. "Failed!" : "Success!"));
  306. totalResult += result;
  307. // hdfsChmod
  308. fprintf(stderr, "hdfsChmod: %s\n",
  309. ((result = hdfsChmod(fs, rwPath, newPerm)) ?
  310. "Failed!" : "Success!"));
  311. totalResult += result;
  312. sleep(2);
  313. tTime newMtime = time(NULL);
  314. tTime newAtime = time(NULL);
  315. // utime write
  316. fprintf(stderr, "hdfsUtime: %s\n",
  317. ((result = hdfsUtime(fs, rwPath, newMtime, newAtime)) ?
  318. "Failed!" : "Success!"));
  319. totalResult += result;
  320. // chown/chmod/utime read
  321. hdfsFileInfo *finfo = hdfsGetPathInfo(fs, rwPath);
  322. fprintf(stderr, "hdfsChown read: %s\n",
  323. ((result = (strcmp(finfo->mOwner, newOwner) != 0)) ?
  324. "Failed!" : "Success!"));
  325. totalResult += result;
  326. fprintf(stderr, "hdfsChmod read: %s\n",
  327. ((result = (finfo->mPermissions != newPerm)) ?
  328. "Failed!" : "Success!"));
  329. totalResult += result;
  330. // will later use /tmp/ as a different user so enable it
  331. fprintf(stderr, "hdfsChmod: %s\n",
  332. ((result = hdfsChmod(fs, slashTmp, 0777)) ?
  333. "Failed!" : "Success!"));
  334. totalResult += result;
  335. fprintf(stderr,"newMTime=%ld\n",newMtime);
  336. fprintf(stderr,"curMTime=%ld\n",finfo->mLastMod);
  337. fprintf(stderr, "hdfsUtime read (mtime): %s\n",
  338. ((result = (finfo->mLastMod != newMtime / 1000)) ?
  339. "Failed!" : "Success!"));
  340. totalResult += result;
  341. // Clean up
  342. hdfsFreeFileInfo(finfo, 1);
  343. fprintf(stderr, "hdfsDelete: %s\n",
  344. ((result = hdfsDelete(fs, newDirectory, 1)) ?
  345. "Failed!" : "Success!"));
  346. totalResult += result;
  347. fprintf(stderr, "hdfsDelete: %s\n",
  348. ((result = hdfsDelete(fs, srcPath, 1)) ?
  349. "Failed!" : "Success!"));
  350. totalResult += result;
  351. fprintf(stderr, "hdfsExists: %s\n",
  352. ((result = hdfsExists(fs, newDirectory)) ?
  353. "Success!" : "Failed!"));
  354. totalResult += (result ? 0 : 1);
  355. // Done test generic operations
  356. }
  357. {
  358. // Test Appends
  359. appendTemplate = strdup("/tmp/appendsXXXXXX");
  360. if (!appendTemplate) {
  361. fprintf(stderr, "Failed to create appendTemplate!\n");
  362. exit(1);
  363. }
  364. char *appendPath = mktemp(appendTemplate);
  365. const char* helloBuffer = "Hello,";
  366. hdfsFile writeFile = NULL;
  367. // Create
  368. writeFile = hdfsOpenFile(fs, appendPath, O_WRONLY, 0, 0, 0);
  369. if(!writeFile) {
  370. fprintf(stderr, "Failed to open %s for writing!\n", appendPath);
  371. exit(1);
  372. }
  373. fprintf(stderr, "Opened %s for writing successfully...\n", appendPath);
  374. num_written_bytes = hdfsWrite(fs, writeFile, helloBuffer,
  375. (int) strlen(helloBuffer));
  376. fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
  377. hdfsCloseFile(fs, writeFile);
  378. fprintf(stderr, "hdfsSetReplication: %s\n",
  379. ((result = hdfsSetReplication(fs, appendPath, 1)) ?
  380. "Failed!" : "Success!"));
  381. totalResult += result;
  382. // Re-Open for Append
  383. writeFile = hdfsOpenFile(fs, appendPath, O_WRONLY | O_APPEND, 0, 0, 0);
  384. if(!writeFile) {
  385. fprintf(stderr, "Failed to open %s for writing!\n", appendPath);
  386. exit(1);
  387. }
  388. fprintf(stderr, "Opened %s for appending successfully...\n",
  389. appendPath);
  390. helloBuffer = " World";
  391. num_written_bytes = hdfsWrite(fs, writeFile, helloBuffer,
  392. (int)strlen(helloBuffer) + 1);
  393. fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
  394. hdfsCloseFile(fs, writeFile);
  395. // Check size
  396. hdfsFileInfo *finfo = hdfsGetPathInfo(fs, appendPath);
  397. fprintf(stderr, "fileinfo->mSize: == total %s\n",
  398. ((result = (finfo->mSize == strlen("Hello, World") + 1)) ?
  399. "Success!" : "Failed!"));
  400. totalResult += (result ? 0 : 1);
  401. // Read and check data
  402. hdfsFile readFile = hdfsOpenFile(fs, appendPath, O_RDONLY, 0, 0, 0);
  403. if (!readFile) {
  404. fprintf(stderr, "Failed to open %s for reading!\n", appendPath);
  405. exit(1);
  406. }
  407. tSize num_read_bytes = hdfsRead(fs, readFile, buffer, sizeof(buffer));
  408. fprintf(stderr, "Read following %d bytes:\n%s\n",
  409. num_read_bytes, buffer);
  410. fprintf(stderr, "read == Hello, World %s\n",
  411. (result = (strcmp(buffer, "Hello, World") == 0)) ?
  412. "Success!" : "Failed!");
  413. hdfsCloseFile(fs, readFile);
  414. // Cleanup
  415. fprintf(stderr, "hdfsDelete: %s\n",
  416. ((result = hdfsDelete(fs, appendPath, 1)) ?
  417. "Failed!" : "Success!"));
  418. totalResult += result;
  419. // Done test appends
  420. }
  421. totalResult += (hdfsDisconnect(fs) != 0);
  422. {
  423. //
  424. // Now test as connecting as a specific user
  425. // This only meant to test that we connected as that user, not to test
  426. // the actual fs user capabilities. Thus just create a file and read
  427. // the owner is correct.
  428. const char *tuser = "nobody";
  429. userTemplate = strdup("/tmp/usertestXXXXXX");
  430. if (!userTemplate) {
  431. fprintf(stderr, "Failed to create userTemplate!\n");
  432. exit(1);
  433. }
  434. char* userWritePath = mktemp(userTemplate);
  435. hdfsFile writeFile = NULL;
  436. fs = hdfsConnectAsUserNewInstance("default", 50070, tuser);
  437. if(!fs) {
  438. fprintf(stderr,
  439. "Oops! Failed to connect to hdfs as user %s!\n",tuser);
  440. exit(1);
  441. }
  442. writeFile = hdfsOpenFile(fs, userWritePath, O_WRONLY|O_CREAT, 0, 0, 0);
  443. if(!writeFile) {
  444. fprintf(stderr, "Failed to open %s for writing!\n", userWritePath);
  445. exit(1);
  446. }
  447. fprintf(stderr, "Opened %s for writing successfully...\n",
  448. userWritePath);
  449. num_written_bytes = hdfsWrite(fs, writeFile, fileContents,
  450. (int)strlen(fileContents) + 1);
  451. fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
  452. hdfsCloseFile(fs, writeFile);
  453. hdfsFileInfo *finfo = hdfsGetPathInfo(fs, userWritePath);
  454. if (finfo) {
  455. fprintf(stderr, "hdfs new file user is correct: %s\n",
  456. ((result = (strcmp(finfo->mOwner, tuser) != 0)) ?
  457. "Failed!" : "Success!"));
  458. } else {
  459. fprintf(stderr,
  460. "hdfsFileInfo returned by hdfsGetPathInfo is NULL\n");
  461. result = -1;
  462. }
  463. totalResult += result;
  464. // Cleanup
  465. fprintf(stderr, "hdfsDelete: %s\n",
  466. ((result = hdfsDelete(fs, userWritePath, 1)) ?
  467. "Failed!" : "Success!"));
  468. totalResult += result;
  469. // Done test specific user
  470. }
  471. totalResult += (hdfsDisconnect(fs) != 0);
  472. // Shutdown the native minidfscluster
  473. nmdShutdown(cluster);
  474. nmdFree(cluster);
  475. fprintf(stderr, "totalResult == %d\n", totalResult);
  476. if (totalResult != 0) {
  477. return -1;
  478. } else {
  479. return 0;
  480. }
  481. }
  482. /**
  483. * vim: ts=4: sw=4: et:
  484. */