logparser.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 import os
3 import sys
4 import subprocess
5 import time
6 
7 script_path = os.path.dirname(os.path.realpath(sys.argv[0]))
8 
9 try:
10  import junitparser
11 except ImportError:
12  import subprocess
13  print("junitparser module was not found, installing...")
14  subprocess.call([os.path.join('.', script_path, 'test_installer.sh')])
15 
16 from junitparser import TestCase, TestSuite, JUnitXml, Skipped, Element
17 
18 class Result(Skipped):
19  _tag = 'result'
20  _inlinemsg = None
21  _message = None
22  _type = None
23 
24  def __init__(self, inlinemsg=None, message=None, type_=None):
25  if inlinemsg is None:
26  inlinemsg = self._inlinemsg
27  if inlinemsg is not None:
28  self._opentag = '<' + self._tag + '>'
29  self._closetag = '</' + self._tag + '>'
30  self._elem = Element.fromstring(self._opentag + inlinemsg + self._closetag)._elem
31  else:
32  if message is None:
33  message = self._message
34  if type_ is None:
35  type_ = self._type
36  super().__init__(message=message, type_=type_)
37  self.message = message
38  self.type = type_
39 
40 class Failure(Result):
41  _tag = 'failure'
42  _message = 'error'
43  _type = 'failed'
44 
45 class Success(Result):
46  _message = 'success'
47  _type = 'ok'
48 
49 BIN_NAME = os.path.join('.', script_path, 'swarmio-simulator')
50 PROCESS_START_DELAY_S = 1
51 PROCESSES_RUN_S = 1
52 PROCESS_TIMEOUT_S = 2
53 
54 files = os.listdir('.')
55 proc1 = subprocess.Popen(BIN_NAME, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
56 time.sleep(PROCESS_START_DELAY_S)
57 
58 new_files = os.listdir('.')
59 proc1_log = [f for f in new_files if f not in files][0]
60 
61 files = os.listdir('.')
62 proc2 = subprocess.Popen(BIN_NAME, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
63 
64 time.sleep(PROCESSES_RUN_S)
65 
66 # Create cases
67 program_starting_case = TestCase('program_starting_case')
68 discovery_case = TestCase('discovery_case')
69 
70 try:
71  out, err = proc1.communicate(input=b'\n', timeout=PROCESS_TIMEOUT_S)
72  proc1_out = out.decode('unicode_escape').split('\n')
73  out, err = proc2.communicate(input=b'\n', timeout=PROCESS_TIMEOUT_S)
74  proc2_out = out.decode('unicode_escape').split('\n')
75 
76  new_files = os.listdir('.')
77  proc2_log = [f for f in new_files if f not in files][0]
78 
79  def parse_log(log_fname, other_node):
80  found_other_node = False
81  with open(log_fname) as fp:
82  for i, line in enumerate(fp):
83  if i > 2:
84  if '\t' in line:
85  spl = line.split('\t')
86  ltime = spl[0]
87  lmsg = spl[1]
88  if other_node in line:
89  found_other_node = True
90  else:
91  ltime = ''
92  lmsg = line
93  elif 'log format' in line.lower():
94  s = line.find('[') + 1
95  e = line.find(']')
96  log_frmt = line[s:e]
97  return found_other_node
98 
99  uuidpos = proc2_out[0].find('UUID: ') + len('UUID: ')
100  proc1_found_proc2 = parse_log(proc1_log, proc2_out[0][uuidpos:])
101  proc2_found_proc1 = parse_log(proc2_log, proc1_out[0][uuidpos:])
102 
103  # Set case results
104  program_starting_case.result = Success()
105  if proc1_found_proc2 and proc2_found_proc1:
106  discovery_case.result = Success()
107  else:
108  discovery_case.result = Failure(message='discovery error')
109 except Exception:
110  if proc1.poll() == None:
111  proc1.kill()
112  proc1.communicate()
113  if proc2.poll() == None:
114  proc2.kill()
115  proc2.communicate()
116  # Set case results
117  program_starting_case.result = Failure(message='program crash error')
118  discovery_case.result = Failure(message='discovery error')
119 
120 # Create suite and add cases
121 suite = TestSuite('testsuite')
122 #suite.add_property('build', '55')
123 suite.add_testcase(program_starting_case)
124 suite.add_testcase(discovery_case)
125 
126 # Add suite to JunitXml
127 xml = JUnitXml()
128 xml.add_testsuite(suite)
129 
130 test_reporst_dir = os.path.join('tests', 'test-reports')
131 if not os.path.exists(test_reporst_dir):
132  os.makedirs(test_reporst_dir)
133 
134 xml.write(os.path.join(test_reporst_dir, 'result.xml'), pretty=True)
def __init__(self, inlinemsg=None, message=None, type_=None)
Definition: logparser.py:24
def parse_log(log_fname, other_node)
Definition: logparser.py:79


swarmros
Author(s):
autogenerated on Fri Apr 3 2020 03:42:48