test_gnunet_statistics.py.in 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!@PYTHON@
  2. from __future__ import print_function
  3. import os
  4. import sys
  5. import shutil
  6. import re
  7. import subprocess
  8. import time
  9. if os.name == "nt":
  10. tmp = os.getenv ("TEMP")
  11. else:
  12. tmp = "/tmp"
  13. if os.name == 'nt':
  14. st = './gnunet-statistics.exe'
  15. arm = 'gnunet-arm.exe'
  16. else:
  17. st = './gnunet-statistics'
  18. arm = 'gnunet-arm'
  19. run_st = [st, '-c', 'test_statistics_api_data.conf']
  20. run_arm = [arm, '-c', 'test_statistics_api_data.conf']
  21. debug = os.getenv ('DEBUG')
  22. if debug:
  23. run_arm += [debug.split (' ')]
  24. def cleanup ():
  25. shutil.rmtree (os.path.join (tmp, "test-gnunet-statistics"), True)
  26. def sub_run (args, want_stdo = True, want_stde = False, nofail = False):
  27. if want_stdo:
  28. stdo = subprocess.PIPE
  29. else:
  30. stdo = None
  31. if want_stde:
  32. stde = subprocess.PIPE
  33. else:
  34. stde = None
  35. p = subprocess.Popen (args, stdout = stdo, stderr = stde)
  36. stdo, stde = p.communicate ()
  37. if not nofail:
  38. if p.returncode != 0:
  39. sys.exit (p.returncode)
  40. return (p.returncode, stdo, stde)
  41. def fail (result):
  42. print (result)
  43. r_arm (['-e'], want_stdo = False)
  44. sys.exit (1)
  45. def r_arm (extra_args, **kw):
  46. rc, stdo, stde = sub_run (run_arm + extra_args, **kw)
  47. if rc != 0:
  48. fail ("FAIL: error running {}".format (run_arm))
  49. return (rc, stdo, stde)
  50. def r_st (extra_args, normal = True, **kw):
  51. rc, stdo, stde = sub_run (run_st + extra_args, **kw)
  52. if normal:
  53. if rc != 0:
  54. fail ("FAIL: error running {}".format (run_st))
  55. else:
  56. if rc == 0:
  57. fail ("FAIL: expected error while running {}".format (run_st))
  58. return (rc, stdo, stde)
  59. def restart ():
  60. print ("Restarting service...")
  61. t = r_arm (['-k', 'statistics'])
  62. time.sleep (1)
  63. t = r_arm (['-i', 'statistics'])
  64. time.sleep (1)
  65. cleanup ()
  66. print ("Preparing: Starting service...")
  67. t = r_arm (['-s'], want_stdo = False)
  68. time.sleep (1)
  69. t = r_arm (['-i', 'statistics'], want_stdo = False)
  70. time.sleep (1)
  71. print ("TEST: Bad argument checking...", end='')
  72. r_st (['-x'], normal = False, nofail = True, want_stdo = False, want_stde = True)
  73. print ("PASS")
  74. print ("TEST: Set value...", end='')
  75. r_st (['-n', 'test', '-s', 'subsystem', b'42'], nofail = True, want_stdo = False)
  76. print ("PASS")
  77. print ("TEST: Set another value...", end='')
  78. r_st (['-n', 'other', '-s', 'osystem', b'43'], nofail = True, want_stdo = False)
  79. print ("PASS")
  80. print ("TEST: Viewing all stats...", end='')
  81. rc, stdo, stde = r_st ([], nofail = True, want_stdo = True)
  82. if len (stdo.splitlines ()) != 2:
  83. fail ("FAIL: unexpected output:\n{}".format (stdo))
  84. print ("PASS")
  85. print ("TEST: Viewing stats by name...", end='')
  86. rc, stdo, stde = r_st (['-n', 'other'], nofail = True, want_stdo = True)
  87. if len ([x for x in stdo.splitlines () if re.search (b'43', x)]) != 1:
  88. fail ("FAIL: unexpected output:\n{}".format (stdo))
  89. print ("PASS")
  90. print ("TEST: Viewing stats by subsystem...", end='')
  91. rc, stdo, stde = r_st (['-s', 'subsystem'], nofail = True, want_stdo = True)
  92. if len ([x for x in stdo.splitlines () if re.search (b'42', x)]) != 1:
  93. fail ("FAIL: unexpected output:\n{}".format (stdo))
  94. print ("PASS")
  95. print ("TEST: Set persistent value...", end='')
  96. rc, stdo, stde = r_st (['-n', 'lasting', '-s', 'subsystem', '40', '-p'], nofail = True, want_stdo = False)
  97. rc, stdo, stde = r_st ([], nofail = True, want_stdo = True)
  98. if len ([x for x in stdo.splitlines () if re.search (b'40', x)]) != 1:
  99. fail ("FAIL: unexpected output:\n{}".format (stdo))
  100. print ("PASS")
  101. restart ()
  102. print ("TEST: Checking persistence...", end='')
  103. rc, stdo, stde = r_st ([], nofail = True, want_stdo = True)
  104. if len ([x for x in stdo.splitlines () if re.search (b'40', x)]) != 1:
  105. fail ("FAIL: unexpected output:\n{}".format (stdo))
  106. print ("PASS")
  107. print ("TEST: Removing persistence...", end='')
  108. rc, stdo, stde = r_st (['-n', 'lasting', '-s', 'subsystem', '40'], nofail = True, want_stdo = False)
  109. rc, stdo, stde = r_st ([], nofail = True, want_stdo = True)
  110. if len ([x for x in stdo.splitlines () if re.search (b'!', x)]) != 0:
  111. fail ("FAIL: unexpected output:\n{}".format (stdo))
  112. print ("PASS")
  113. restart ()
  114. print ("TEST: Checking removed persistence...", end='')
  115. rc, stdo, stde = r_st ([], nofail = True, want_stdo = True)
  116. if len ([x for x in stdo.splitlines () if re.search (b'40', x)]) != 0:
  117. fail ("FAIL: unexpected output:\n{}".format (stdo))
  118. print ("PASS")
  119. print ("Stopping service...")
  120. t = r_arm (['-e'], want_stdo = False)
  121. time.sleep (1)
  122. cleanup ()