02_File.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/python
  2. import os, sys, mmap, random, string
  3. from regression import Regression
  4. loader = os.environ['PAL_LOADER']
  5. def prepare_files(args):
  6. global file_exist
  7. file_exist = ''.join([random.choice(string.ascii_letters) for i in range(mmap.PAGESIZE)])
  8. with open("file_exist.tmp", "w") as f:
  9. f.write(file_exist)
  10. if os.path.exists("file_nonexist.tmp"):
  11. os.remove("file_nonexist.tmp")
  12. with open("file_delete.tmp", "w") as f:
  13. f.write(file_exist)
  14. # Running File
  15. regression = Regression(loader, "File", prepare_files)
  16. regression.add_check(name="Basic File Opening",
  17. check=lambda res: "File Open Test 1 OK" in res[0].log and
  18. "File Open Test 2 OK" in res[0].log and
  19. "File Open Test 3 OK" in res[0].log)
  20. regression.add_check(name="Basic File Creation",
  21. check=lambda res: "File Creation Test 1 OK" in res[0].log and
  22. "File Creation Test 2 OK" in res[0].log and
  23. "File Creation Test 3 OK" in res[0].log)
  24. regression.add_check(name="File Reading",
  25. check=lambda res: ("Read Test 1 (0th - 40th): " + file_exist[0:40]) in res[0].log and
  26. ("Read Test 2 (0th - 40th): " + file_exist[0:40]) in res[0].log and
  27. ("Read Test 3 (200th - 240th): " + file_exist[200:240]) in res[0].log)
  28. def check_write(res):
  29. global file_exist
  30. with open("file_nonexist.tmp", "r") as f:
  31. file_nonexist = f.read()
  32. return file_exist[0:40] == file_nonexist[200:240] and \
  33. file_exist[200:240] == file_nonexist[0:40]
  34. regression.add_check(name="File Writing", check=check_write)
  35. regression.add_check(name="File Attribute Query",
  36. check=lambda res: ("Query: type = 1, size = %d" % (mmap.PAGESIZE)) in res[0].log)
  37. regression.add_check(name="File Attribute Query by Handle",
  38. check=lambda res: ("Query by Handle: type = 1, size = %d" % (mmap.PAGESIZE)) in res[0].log)
  39. regression.add_check(name="File Mapping",
  40. check=lambda res: ("Map Test 1 (0th - 40th): " + file_exist[0:40]) in res[0].log and
  41. ("Map Test 2 (200th - 240th): " + file_exist[200:240]) in res[0].log)
  42. regression.add_check(name="Set File Length",
  43. check=lambda res: os.stat("file_nonexist.tmp").st_size == mmap.ALLOCATIONGRANULARITY)
  44. regression.add_check(name="File Deletion",
  45. check=lambda res: not os.path.exists("file_delete.tmp"))
  46. rv = regression.run_checks()
  47. if rv: sys.exit(rv)