source: proiecte/HadoopJUnit/hadoop-0.20.1/src/c++/libhdfs/hdfs_test.c @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

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