test.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #! /usr/bin/python
  2. import sys
  3. import os
  4. def usage():
  5. print "Usage: %s <limit> /path/to/exec [args...]" % sys.argv[0]
  6. sys.exit(1)
  7. if len(sys.argv) <= 3:
  8. usage()
  9. faildir = os.path.dirname(sys.argv[0])
  10. executable = faildir + "/fail.sh"
  11. llim = int(sys.argv[1])
  12. ulim = int(sys.argv[2])
  13. print "Injecting failures for allocations %d-%d" % (llim, ulim)
  14. print "Failure injector is at %s" % executable
  15. fd = os.dup(2)
  16. logfd = os.open("log.%d" % os.getpid(), os.O_APPEND | os.O_WRONLY | os.O_CREAT, 0644)
  17. os.dup2(logfd, 1)
  18. os.dup2(logfd, 2)
  19. args = [executable, 1]
  20. args.extend(sys.argv[3:])
  21. counter = 0
  22. for i in range(llim, ulim):
  23. args[1] = str(i)
  24. rc = os.spawnv(os.P_WAIT, executable, args)
  25. if rc == 0:
  26. os.write(fd, "No failure for iteration %d\n" % i)
  27. elif rc < 0:
  28. os.write(fd, "Signal %d on iteration %d\n" % (-rc, i))
  29. elif rc != 1:
  30. os.write(fd, "Exit code %d on iteration %d\n" % (rc, i))
  31. elif counter == 100:
  32. os.write(fd, "Iteration %d\n" % i)
  33. counter = 0
  34. counter = counter + 1
  35. os.close(logfd)