A Discrete-Event Network Simulator
API
TestBase.py
Go to the documentation of this file.
1 #! /usr/bin/env python3
2 
19 
20 from __future__ import print_function
21 import sys
22 import subprocess
23 import argparse
24 import os
25 
26 def print_case_in_file(case_string, out):
27  for i in range(100):
28  print("-", end = '', file = out)
29  print(file=out)
30  print("running test case " + case_string, end='\n\n', file = out)
31  out.flush()
32 
33 def print_failed_cases(failed_cases):
34  print("\nFailed Cases:")
35  for case in failed_cases:
36  print(case)
37 
38 def print_cmds(cmds):
39  print('Commands to be executed:')
40  for cmd in cmds:
41  print(cmd.replace(sys.executable, ''))
42 
44  dir_files = [ f for f in os.listdir('.') if os.path.exists(f) ]
45  if not 'VERSION' in dir_files and not 'ns3' in dir_files:
46  if os.path.split(os.path.abspath('.'))[1] == 'tests' and os.path.split(os.path.abspath(os.pardir))[1] == 'utils':
47  os.chdir('../../')
48  else:
49  print('Error: Invalid working directory')
50  sys.exit(1)
51 
52 
54  """
55  Generic class for testing tools based on provided commands and test cases.
56  """
57 
65 
66  def __init__(self, argv, desc, mode):
67  """!
68  Provide input argument list, description and mode of the suite being executed.
69  @param self this object
70  @param argv argument list
71  @param desc description
72  @param mode test mode
73  """
74  self.my_envmy_env = os.environ
75  set_workdir()
76  self.my_envmy_env['LD_LIBRARY_PATH'] = os.getcwd() + "/build"
77  self.modemode = mode
78  self.outfileoutfile = 'test-port-'+self.modemode+'.out'
79  self.optionsoptions = self.parseargsparseargs(argv , desc)
80 
81  def parseargs(self, argv, desc):
82  """!
83  Parses the commandline arguments
84  @param self this object
85  @param argv argument list
86  @param desc description
87  @return command line arguments
88  """
89  parser = argparse.ArgumentParser(description = desc)
90  parser.add_argument('-f', '--file', action='store', dest='out_file', default = self.outfileoutfile,
91  metavar="FILE",
92  help='File to be used for storing the command specific output (Default: '+self.outfileoutfile+')')
93  parser.add_argument('-c', action='store_true', dest='cmds', default=False,
94  help='List out all the commands being tested')
95  parser.add_argument('-m', action='store_true', dest='mute', default=False,
96  help='Sends only stderr output to FILE')
97  parser.add_argument('-x', '--customcmd', action='store', dest='custcmd', default = None,
98  help='Enter a comma-separated list of commands to override the existing ones. NOT APPLICABLE FOR TEST-PY SUITE.')
99  return parser.parse_args(argv)
100 
101  def override_cmds(self):
102  """!
103  Can be used by importing suite to handle custom commands
104  @param self this object
105  @return custom commands
106  """
107  return self.optionsoptions.custcmd
108 
109  def runtests(self, cmds):
110  """!
111  Execute the tests.
112  @param self this object
113  @param cmds test commands
114  @return error code
115  """
116  if self.optionsoptions.cmds:
117  print_cmds(cmds)
118  return
119 
120  final_return = 0
121  total_tests = len(cmds)
122  passed = 0
123  progress = 0.0
124  failed_cases = []
125  with open(self.optionsoptions.out_file, 'w') as out:
126  outstream = out
127  with open(os.devnull, 'w') as sink:
128  if self.optionsoptions.mute:
129  outstream = sink
130  for cmd in cmds:
131  case_string = cmd.replace(sys.executable, '')
132  print("running test case: " + case_string)
133  print_case_in_file(case_string, out)
134  progress += 1
135  ret = subprocess.call(cmd, shell=True, env=self.my_envmy_env, stdout=outstream, stderr=out)
136  if not ret:
137  passed += 1
138  else:
139  final_return = 1
140  failed_cases.append(case_string)
141  print("[ %s out of %s ] test cases passed; Progress = %.2f%% \n" % (passed, total_tests, progress*100/total_tests))
142  if final_return != 0:
143  print_failed_cases(failed_cases)
144  else:
145  print("\nAll cases passed")
146  print ("Detailed output available in " + self.optionsoptions.out_file, end='\n\n')
147  return final_return
TestBaseClass class.
Definition: TestBase.py:53
def __init__(self, argv, desc, mode)
Provide input argument list, description and mode of the suite being executed.
Definition: TestBase.py:66
def parseargs(self, argv, desc)
Parses the commandline arguments.
Definition: TestBase.py:81
def override_cmds(self)
Can be used by importing suite to handle custom commands.
Definition: TestBase.py:101
outfile
output file
Definition: TestBase.py:78
def runtests(self, cmds)
Execute the tests.
Definition: TestBase.py:109
my_env
os environment
Definition: TestBase.py:74
def print_case_in_file(case_string, out)
Definition: TestBase.py:26
def print_cmds(cmds)
Definition: TestBase.py:38
def print_failed_cases(failed_cases)
Definition: TestBase.py:33
def set_workdir()
Definition: TestBase.py:43