A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
TestBase.py
Go to the documentation of this file.
1
#! /usr/bin/env python3
2
#
3
# Copyright (c) 2014 Siddharth Santurkar
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License version 2 as
7
# published by the Free Software Foundation;
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
#
18
19
from
__future__
import
print_function
20
21
import
argparse
22
import
os
23
import
subprocess
24
import
sys
25
26
27
def
print_case_in_file
(case_string, out):
28
for
i
in
range(100):
29
print(
"-"
, end=
""
, file=out)
30
print(file=out)
31
print(
"running test case "
+ case_string, end=
"\n\n"
, file=out)
32
out.flush()
33
34
35
def
print_failed_cases
(failed_cases):
36
print(
"\nFailed Cases:"
)
37
for
case
in
failed_cases:
38
print(case)
39
40
41
def
print_cmds
(cmds):
42
print(
"Commands to be executed:"
)
43
for
cmd
in
cmds:
44
print(cmd.replace(sys.executable,
""
))
45
46
47
def
set_workdir
():
48
dir_files = [f
for
f
in
os.listdir(
"."
)
if
os.path.exists(f)]
49
if
not
"VERSION"
in
dir_files
and
not
"ns3"
in
dir_files:
50
if
(
51
os.path.split(os.path.abspath(
"."
))[1] ==
"tests"
52
and
os.path.split(os.path.abspath(os.pardir))[1] ==
"utils"
53
):
54
os.chdir(
"../../"
)
55
else
:
56
print(
"Error: Invalid working directory"
)
57
sys.exit(1)
58
59
60
61
class
TestBaseClass
:
62
"""
63
Generic class for testing tools based on provided commands and test cases.
64
"""
65
66
74
75
def
__init__
(self, argv, desc, mode):
76
"""!
77
Provide input argument list, description and mode of the suite being executed.
78
@param self this object
79
@param argv argument list
80
@param desc description
81
@param mode test mode
82
"""
83
self.
my_env
my_env = os.environ
84
set_workdir
()
85
self.
my_env
my_env[
"LD_LIBRARY_PATH"
] = os.getcwd() +
"/build"
86
self.
mode
mode = mode
87
self.
outfile
outfile =
"test-port-"
+ self.
mode
mode +
".out"
88
self.
options
options = self.
parseargs
parseargs(argv, desc)
89
90
def
parseargs
(self, argv, desc):
91
"""!
92
Parses the commandline arguments
93
@param self this object
94
@param argv argument list
95
@param desc description
96
@return command line arguments
97
"""
98
parser = argparse.ArgumentParser(description=desc)
99
parser.add_argument(
100
"-f"
,
101
"--file"
,
102
action=
"store"
,
103
dest=
"out_file"
,
104
default=self.
outfile
outfile,
105
metavar=
"FILE"
,
106
help=
"File to be used for storing the command specific output (Default: "
107
+ self.
outfile
outfile
108
+
")"
,
109
)
110
parser.add_argument(
111
"-c"
,
112
action=
"store_true"
,
113
dest=
"cmds"
,
114
default=
False
,
115
help=
"List out all the commands being tested"
,
116
)
117
parser.add_argument(
118
"-m"
,
119
action=
"store_true"
,
120
dest=
"mute"
,
121
default=
False
,
122
help=
"Sends only stderr output to FILE"
,
123
)
124
parser.add_argument(
125
"-x"
,
126
"--customcmd"
,
127
action=
"store"
,
128
dest=
"custcmd"
,
129
default=
None
,
130
help=
"Enter a comma-separated list of commands to override the existing ones. NOT APPLICABLE FOR TEST-PY SUITE."
,
131
)
132
return
parser.parse_args(argv)
133
134
def
override_cmds
(self):
135
"""!
136
Can be used by importing suite to handle custom commands
137
@param self this object
138
@return custom commands
139
"""
140
return
self.
options
options.custcmd
141
142
def
runtests
(self, cmds):
143
"""!
144
Execute the tests.
145
@param self this object
146
@param cmds test commands
147
@return error code
148
"""
149
if
self.
options
options.cmds:
150
print_cmds
(cmds)
151
return
152
base_dir = os.sep.join(
153
os.path.abspath(__file__).replace(os.path.pathsep,
"/"
).split(
"/"
)[:-3]
154
)
155
final_return = 0
156
total_tests = len(cmds)
157
passed = 0
158
progress = 0.0
159
failed_cases = []
160
with
open(self.
options
options.out_file,
"w"
, encoding=
"utf-8"
)
as
out:
161
outstream = out
162
with
open(os.devnull,
"w"
, encoding=
"utf-8"
)
as
sink:
163
if
self.
options
options.mute:
164
outstream = sink
165
for
cmd
in
cmds:
166
case_string = cmd.replace(sys.executable,
""
)
167
print(
"running test case: "
+ case_string)
168
print_case_in_file
(case_string, out)
169
progress += 1
170
ret = subprocess.call(
171
cmd, shell=
True
, env=self.
my_env
my_env, stdout=outstream, stderr=out, cwd=base_dir
172
)
173
if
not
ret:
174
passed += 1
175
else
:
176
final_return = 1
177
failed_cases.append(case_string)
178
print(
179
"[ %s out of %s ] test cases passed; Progress = %.2f%% \n"
180
% (passed, total_tests, progress * 100 / total_tests)
181
)
182
if
final_return != 0:
183
print_failed_cases
(failed_cases)
184
else
:
185
print(
"\nAll cases passed"
)
186
print(
"Detailed output available in "
+ self.
options
options.out_file, end=
"\n\n"
)
187
return
final_return
TestBase.TestBaseClass
TestBaseClass class.
Definition:
TestBase.py:61
TestBase.TestBaseClass.__init__
def __init__(self, argv, desc, mode)
Provide input argument list, description and mode of the suite being executed.
Definition:
TestBase.py:75
TestBase.TestBaseClass.parseargs
def parseargs(self, argv, desc)
Parses the commandline arguments.
Definition:
TestBase.py:90
TestBase.TestBaseClass.override_cmds
def override_cmds(self)
Can be used by importing suite to handle custom commands.
Definition:
TestBase.py:134
TestBase.TestBaseClass.mode
mode
mode
Definition:
TestBase.py:86
TestBase.TestBaseClass.options
options
options
Definition:
TestBase.py:88
TestBase.TestBaseClass.outfile
outfile
output file
Definition:
TestBase.py:87
TestBase.TestBaseClass.runtests
def runtests(self, cmds)
Execute the tests.
Definition:
TestBase.py:142
TestBase.TestBaseClass.my_env
my_env
os environment
Definition:
TestBase.py:83
TestBase.print_case_in_file
def print_case_in_file(case_string, out)
Definition:
TestBase.py:27
TestBase.print_cmds
def print_cmds(cmds)
Definition:
TestBase.py:41
TestBase.print_failed_cases
def print_failed_cases(failed_cases)
Definition:
TestBase.py:35
TestBase.set_workdir
def set_workdir()
Definition:
TestBase.py:47
utils
tests
TestBase.py
Generated on Sun Mar 3 2024 17:11:14 for ns-3 by
1.9.1