Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / JAVA / classes / testAFS.java
1 import java.io.File;
2 import java.util.GregorianCalendar;
3
4 import org.openafs.jafs.*;
5
6
7
8 /**
9 * @version 1.0
10 * @author
11 */
12 public class testAFS
13 {
14 public class TesterThread implements Runnable
15 {
16 private String user = null;
17 private String pass = null;
18 private String cell = null;
19 private boolean letItRun = true;
20
21 public TesterThread(String user, String pass, String cell)
22 {
23 this.user = user;
24 this.pass = pass;
25 this.cell = cell;
26 }
27 public void finish()
28 {
29 letItRun = false;
30 }
31 public void run()
32 {
33 while(letItRun)
34 {
35 try
36 {
37 Token t = new Token(user, pass, cell);
38 Cell c = new Cell(t, false);
39 c.getInfo();
40
41 for(int j=0; j<100; j++)
42 {
43 ACL acl = new ACL("/afs/." + cell, true);
44 }
45
46 c.close();
47 } catch(Exception e) {
48 e.printStackTrace();
49 }
50 Thread.yield();
51 }
52 }
53 }
54
55
56 static java.io.PrintStream out = System.out;
57 static String firstCellPathRW = null;
58
59 static boolean allowDump = true;
60
61 static int ident = 0;
62
63 static void DumpBegin()
64 {
65 if (allowDump)
66 {
67 for(int i=0; i<ident; i++)
68 out.print(" ");
69 }
70 }
71 static void Dump(String str)
72 {
73 if (allowDump)
74 out.print(str);
75 }
76 static void DumpEnd()
77 {
78 if (allowDump)
79 out.println();
80 }
81 static void DumpLn(String str)
82 {
83 DumpBegin();
84 Dump(str);
85 DumpEnd();
86 }
87
88 public static String getGregDateStr(GregorianCalendar gc)
89 {
90 if (gc==null)
91 return "null";
92 else
93 return gc.getTime().toString();
94 }
95
96 public static void DumpToken(Token t)
97 {
98 try
99 {
100 DumpLn("Token: user=" + t.getUsername() +
101 " cell=" + t.getCellName() + " expiration=" + t.getExpiration());
102 } catch(AFSException e) {
103 e.printStackTrace();
104 }
105 }
106
107 public static void DumpFile(org.openafs.jafs.File f)
108 {
109 try
110 {
111 DumpBegin();
112 f.refresh();
113 Dump("File name: " + f.getPath() + ": ");
114 if (f.isDirectory()) {
115 Dump("This is a directory.");
116 } else if (f.isLink()) {
117 Dump("This is a symbolic-link.");
118 Dump(" Its target is: " + f.getTarget());
119 f.validate();
120 if (f.isFile()) {
121 Dump(" This object is now a file!");
122 } else if (f.isDirectory()) {
123 Dump(" This object is now a directory!");
124 } else if (f.isMountPoint()) {
125 Dump(" This object is now a volume mount point!");
126 }
127 } else if (f.isMountPoint()) {
128 Dump(" This is a volume mount point.");
129 } else if (f.isFile()) {
130 Dump(" This is a file. Size: " + f.length());
131 }
132 DumpEnd();
133
134 ACL acl = new ACL(f.getPath());
135 ident++;
136 DumpLn(acl.toString());
137 ident--;
138 } catch(AFSException e) {
139 e.printStackTrace();
140 }
141 }
142
143 public static void DumpCell(Cell cell)
144 {
145 try
146 {
147 DumpLn("Cell: " + cell.getName());
148 ident++;
149 DumpLn("MaxGroupID: " + cell.getMaxGroupID());
150 DumpLn("MaxUserID: " + cell.getMaxUserID());
151 ident--;
152
153 //test some queries, don't write to output
154 cell.getInfo();
155 cell.getInfoGroups();
156 cell.getInfoServers();
157 cell.getInfoUsers();
158 } catch(AFSException e) {
159 e.printStackTrace();
160 }
161 }
162
163 public static void DumpServer(Server s)
164 {
165 DumpLn("Server: " + s.getName());
166 ident++;
167 try
168 {
169 try //pServer'SLES9 bug:
170 {
171 DumpLn("BinaryRestartTime: " + s.getBinaryRestartTime());
172 } catch(AFSException e) {
173 e.printStackTrace();
174 }
175 DumpLn("TotalFreeSpace:" + s.getTotalFreeSpace());
176 DumpLn("TotalSpace:" + s.getTotalSpace());
177 DumpLn("TotalUsedSpace:" + s.getTotalUsedSpace());
178 DumpLn("GeneralRestartTime:" + s.getGeneralRestartTime());
179 DumpBegin();
180 Dump("ip addresses: ");
181 String[] ipAddrs = s.getIPAddresses();
182 for(int i=0; i<ipAddrs.length; i++)
183 {
184 Dump(ipAddrs[i] + " ");
185 }
186 DumpEnd();
187 DumpBegin();
188 Dump("isFileServer: " + s.isFileServer());
189 Dump(" isBadFileserver: " + s.isBadFileServer());
190 Dump(" isDatabase: " + s.isDatabase());
191 Dump(" isBadDatabase: " + s.isBadDatabase());
192 } catch(AFSException e) {
193 e.printStackTrace();
194 }
195 ident--;
196
197 try
198 {
199 //test some queries, don't write to output
200 s.getInfo();
201 try
202 {
203 s.getInfoKeys();
204 } catch(AFSException e) {
205 e.printStackTrace();
206 }
207 try //is there any partitions? why parts can be null...
208 { //wrong programming concept: null instead of an empty array !!!
209 s.getInfoPartitions();
210 } catch(Exception e) {
211 e.printStackTrace();
212 }
213 s.getInfoProcesses();
214 } catch(AFSException e) {
215 e.printStackTrace();
216 }
217 }
218
219 public static void DumpVolume(Volume v)
220 {
221 try
222 {
223 DumpBegin();
224 Dump("Volume name: " + v.getName());
225 Dump(" ID: " + v.getID());
226 DumpEnd();
227 } catch(AFSException e) {
228 e.printStackTrace();
229 }
230
231 ident++;
232 try
233 {
234 DumpBegin();
235 Dump("BackupID: " + v.getBackupID());
236 Dump(" ReadOnlyID: " + v.getReadOnlyID());
237 Dump(" ReadWriteID: " + v.getReadWriteID());
238 DumpEnd();
239 DumpBegin();
240 Dump("LastUpdateDate: " + getGregDateStr(v.getLastUpdateDate()));
241 Dump(" CreationDate: " + getGregDateStr(v.getCreationDate()));
242 Dump(" AccessesSinceMidnight: " + v.getAccessesSinceMidnight());
243 DumpEnd();
244 DumpBegin();
245 Dump("FileCount: " + v.getFileCount());
246 Dump(" CurrentSize: " + v.getCurrentSize());
247 Dump(" TotalFreeSpace: " + v.getTotalFreeSpace());
248 DumpEnd();
249 DumpBegin();
250 Dump("Type: " + v.getType());
251 Dump(" Disposition: " + v.getDisposition());
252 DumpEnd();
253
254 //test some queries, don't write to output
255 v.getInfo();
256 } catch(AFSException e) {
257 e.printStackTrace();
258 }
259 ident--;
260 }
261
262 public static void DumpPartition(Partition p)
263 {
264 try
265 {
266 DumpBegin();
267 Dump("Partition name: " + p.getName());
268 Dump(" ID: " + p.getID());
269 Dump(" DeviceName: " + p.getDeviceName());
270 DumpEnd();
271 } catch(AFSException e) {
272 e.printStackTrace();
273 }
274 ident++;
275 try
276 {
277 DumpBegin();
278 Dump("TotalFreeSpace: " + p.getTotalFreeSpace());
279 Dump(" TotalQuota: " + p.getTotalQuota());
280 Dump(" TotalTotalSpace: " + p.getTotalSpace());
281 DumpEnd();
282
283 //test some queries, don't write to output
284 p.getInfo();
285 p.getInfoVolumes();
286 } catch(AFSException e) {
287 e.printStackTrace();
288 }
289 ident--;
290 }
291
292 public static void DumpGroup(Group g)
293 {
294 try
295 {
296 DumpBegin();
297 Dump("Group name: " + g.getName());
298 Dump(" Type: " + g.getType());
299 Dump(" UID: " + g.getUID());
300 DumpEnd();
301
302 //test some queries, don't write to output
303 g.getInfo();
304 } catch(AFSException e) {
305 e.printStackTrace();
306 }
307 }
308
309 public static void DumpUser(User u)
310 {
311 DumpLn("User name: " + u.getName());
312 ident++;
313 try
314 {
315
316 DumpLn("EncryptionKey" + u.getEncryptionKey());
317 DumpBegin();
318 Dump("DaysToPasswordExpire: " + u.getDaysToPasswordExpire());
319 Dump(" FailLoginCount: " + u.getFailLoginCount());
320 Dump(" KeyCheckSum: " + u.getKeyCheckSum());
321 DumpEnd();
322 DumpBegin();
323 Dump("UserExpirationDate: " + getGregDateStr(u.getUserExpirationDate()));
324 Dump(" MaxTicketLifetime: " + u.getMaxTicketLifetime());
325 Dump(" LockedUntilDate: " + getGregDateStr(u.getLockedUntilDate()));
326 DumpEnd();
327
328
329 //test some queries, don't write to output
330 u.getInfo();
331 u.getInfoGroups();
332 u.getInfoGroupsOwned();
333 } catch(AFSException e) {
334 e.printStackTrace();
335 }
336 ident--;
337 }
338
339 static void DumpProcess(org.openafs.jafs.Process p)
340 {
341 DumpLn("Process name: " + p.getName());
342 ident++;
343 try
344 {
345
346 DumpBegin();
347 Dump("StartTimeDate: " + getGregDateStr(p.getStartTimeDate()));
348 Dump(" ExitTimeDate: " + getGregDateStr(p.getExitTimeDate()));
349 DumpEnd();
350
351 //test some queries, don't write to output
352 p.getInfo();
353 } catch(AFSException e) {
354 e.printStackTrace();
355 }
356 ident--;
357 }
358
359 public static Token testToken(String user, String pass, String cell)
360 {
361 Token token = null;
362 try
363 {
364 token = new Token(user, pass, cell);
365 DumpToken(token);
366 testCell(token);
367 } catch(AFSException e) {
368 e.printStackTrace();
369 }
370 return token;
371 }
372
373 public static void testFilesRecursive(File dir)
374 {
375 try
376 {
377 java.io.File fj = new java.io.File(dir.getPath());
378 String[] strSubdirs = fj.list();
379 for(int i=0; i<strSubdirs.length; i++)
380 {
381 org.openafs.jafs.File f = new org.openafs.jafs.File(
382 dir.getPath() + "/" + strSubdirs[i]);
383 DumpFile(f);
384 f.validate();
385 if (f.isDirectory() || f.isMountPoint() || f.isLink())
386 {
387 testFilesRecursive(dir);
388 }
389 }
390 } catch(AFSFileException e) {
391 e.printStackTrace();
392 }
393 }
394
395 public static void testFiles()
396 throws AFSException, AFSFileException
397 {
398 org.openafs.jafs.File f = new org.openafs.jafs.File(firstCellPathRW);
399 DumpFile(f);
400 testFilesRecursive(f);
401 }
402
403 public static void testCell(Token token)
404 {
405 Cell cell = null;
406 try
407 {
408 cell = new Cell(token, false);
409
410 DumpCell(cell);
411 } catch(AFSException e) {
412 e.printStackTrace();
413 }
414 if (cell==null)
415 return;
416
417 ident++;
418 try
419 {
420 Group[] groups = cell.getGroups();
421 for(int i=0; i<groups.length; i++)
422 {
423 testGroup(groups[i]);
424 }
425
426 Server[] servers = cell.getServers();
427 for(int j=0; j<servers.length; j++)
428 {
429 testServer(servers[j]);
430 }
431 } catch(AFSException e) {
432 e.printStackTrace();
433 }
434 ident--;
435
436 try
437 {
438 if (cell!=null)
439 cell.close();
440 } catch(AFSException e) {
441 e.printStackTrace();
442 }
443 }
444
445 public static void testServer(Server server)
446 throws AFSException
447 {
448 DumpServer(server);
449 ident++;
450 try
451 {
452 Partition[] parts = server.getPartitions();
453 if (parts!=null)
454 {
455 for(int i=0; i<parts.length; i++)
456 {
457 testPartition(parts[i]);
458 }
459 }
460
461 org.openafs.jafs.Process[] procs = server.getProcesses();
462 if (procs!=null)
463 {
464 for(int i=0; i<procs.length; i++)
465 {
466 DumpProcess(procs[i]);
467 }
468 }
469
470 DumpLn("salvage...");
471 server.salvage();
472 DumpLn("getLog...");
473 try
474 {
475 server.getLog("/var/log/openafs/BosLog");
476 } catch(AFSException e) {
477 e.printStackTrace();
478 }
479 //DumpLn("stopAllProcesses...");
480 //server.stopAllProcesses();
481 //DumpLn("startAllProcesses...");
482 //server.startAllProcesses();
483 DumpLn("syncServer...");
484 server.syncServer();
485 DumpLn("syncVLDB...");
486 server.syncVLDB();
487 DumpLn("ok.");
488 } catch(AFSException e) {
489 e.printStackTrace();
490 }
491 ident--;
492 }
493
494 public static void testProcess(org.openafs.jafs.Process p)
495 throws AFSException
496 {
497 DumpProcess(p);
498 }
499
500 public static void testPartition(Partition part)
501 throws AFSException
502 {
503 DumpPartition(part);
504 ident++;
505
506 Volume[] vols = part.getVolumes();
507 for(int i=0; i<vols.length; i++)
508 {
509 testVolume(vols[i]);
510 }
511
512 ident--;
513 }
514
515 public static void testVolume(Volume vol)
516 throws AFSException
517 {
518 DumpVolume(vol);
519 }
520
521 public static void testGroup(Group group)
522 throws AFSException
523 {
524 DumpGroup(group);
525 ident++;
526
527 User[] users = group.getMembers();
528 for(int i=0; i<users.length; i++)
529 {
530 testUser(users[i]);
531 }
532
533 ident--;
534 }
535
536 public static void testUser(User user)
537 throws AFSException
538 {
539 DumpUser(user);
540 }
541
542 public static void testNewVolume(String cellName, String userName, String passString)
543 {
544 if (firstCellPathRW==null)
545 {
546 System.err.println("testNewVolume cannot be executed (null args).");
547 return;
548 }
549
550 String volName = "myTestJafsVolume92834";
551 String mpName = firstCellPathRW + "/" + volName;
552 try
553 {
554 Token token = new Token(cellName, userName, passString);
555 Cell cell = new Cell(token, false);
556 Server[] servers = cell.getServers();
557 if (servers.length==0)
558 {
559 System.err.println("Error: failed to run\"testNewVolume\"!");
560 return;
561 }
562 Partition firstPart = null;
563 int i;
564 for(i=0; i<servers.length; i++)
565 {
566 Partition[] parts = servers[i].getPartitions();
567 if (parts==null)
568 continue;
569
570 if (parts.length!=0)
571 {
572 firstPart = parts[0];
573 break;
574 }
575 }
576 if (firstPart==null)
577 {
578 System.err.println("Error: failed to find any partition on any server - \"testNewVolume\" couldn't be tested!");
579 return;
580 }
581
582 Volume v = new Volume(volName, firstPart);
583 DumpLn("Creating a new volume " + volName + " ...");
584 v.create(0);
585
586 v.lock();
587 v.unlock();
588
589 volName = "myTestJafsVolume2389";
590 DumpLn("Renaming volume to " + volName + " ...");
591 v.rename(volName);
592
593 DumpLn("Creating an rw mount point " + mpName + " ...");
594 v.createMountPoint(mpName, true);
595
596 DumpLn("Deleting mount point...");
597 java.lang.Runtime.getRuntime().exec("fs rmmount " + mpName);
598
599 DumpLn("Creating RO...");
600 Volume volRO = v.createReadOnly(firstPart);
601
602 DumpLn("Creating backup...");
603 v.createBackup();
604
605 DumpLn("Releaseing volume...");
606 v.release();
607
608 //v.moveTo(); //needs a more careful test env
609
610 DumpLn("Salvaging volume...");
611 v.salvage();
612
613 DumpLn("Deleting volume...");
614 v.delete();
615
616 DumpLn("Deleting RO site...");
617 volRO.delete();
618
619 DumpLn("OK.");
620 } catch(Exception e) {
621 e.printStackTrace();
622 }
623 }
624
625 public static void main(String[] args)
626 {
627
628 try
629 {
630 if (args.length<4)
631 {
632 System.err.println("testAFS <user> <pass> <cell_name> <# of cycles>");
633 return;
634 }
635
636 Class.forName("org.openafs.jafs.Token"); //initialization...
637 System.out.println("Java interface version: " + VersionInfo.getVersionOfJavaInterface());
638 System.out.println("Library version: " + VersionInfo.getVersionOfLibrary());
639 System.out.println("Build info: " + VersionInfo.getBuildInfo());
640
641 //first test whether token is valid
642 //and load libraries with it
643 Token t0 = new Token(args[0], args[1], args[2]);
644 t0.close();
645
646 System.out.print("Starting another tester thread...");
647 testAFS ta = new testAFS();
648 TesterThread tt = ta.new TesterThread(args[0], args[1], args[2]);
649 Thread tTest = new Thread(tt);
650 tTest.start();
651 System.out.println("started.");
652
653 firstCellPathRW = "/afs/." + args[2];
654 int numOfCycles = Integer.parseInt(args[3]);
655 for(int i=0; i<numOfCycles || numOfCycles==0; i++)
656 {
657 testToken(args[0], args[1], args[2]);
658
659 testFiles();
660
661 testNewVolume(args[0], args[1], args[2]);
662
663 System.out.print("ACL excercising...");
664 allowDump = false;
665 for(int j=0; j<100; j++)
666 {
667 testFiles();
668 System.out.print(".");
669 }
670 System.out.println();
671 allowDump = true;
672
673 AFSException afs = new AFSException(1);
674
675 System.out.println("cycle #" + (i+1) + "/" + numOfCycles + " done.");
676 }
677
678 tt.finish();
679 System.out.println("All done.");
680 } catch (Exception e) {
681 e.printStackTrace();
682 }
683 }
684 }