Updated timestamps
[deliverable/titan.core.git] / mctr2 / mctr / ttcn3_start
1 #!/bin/sh
2
3 ###############################################################################
4 # Copyright (c) 2000-2015 Ericsson Telecom AB
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Eclipse Public License v1.0
7 # which accompanies this distribution, and is available at
8 # http://www.eclipse.org/legal/epl-v10.html
9 ###############################################################################
10
11
12 # hide next line from expect \
13 if [ "xy" != "x`expect -c 'puts "y"'`" ]; then
14 # hide next line from expect \
15 echo "$0 requires 'expect'. Please install 'expect' and make sure it is in the PATH.";
16 # hide next line from expect \
17 exit 1
18 # hide next line from expect \
19 fi
20
21
22
23
24 # the next line restarts using expect \
25 exec expect "$0" "$@"
26
27 #####################################################
28 ## ##
29 ## EXPECT script to automate TTCN-3 testing ##
30 ## ##
31 ## ETH/RUS Tibor Csöndes 2003.05.22. v1.1 ##
32 ## ETH/RZX Janos Zoltan Szabo 2005.01.01. v1.2 ##
33 ## ETH/RZX Tibor Csöndes 2005.03.23. v1.3 ##
34 ## ETH/RZR Csaba Ráduly 2010.04.06. v1.4 ##
35 ## ETH/XZD Jeno Balasko 2010.10.14 v1.5 ##
36 ## ETH/XZD Jeno Balasko 2011.11.23 v1.6 ##
37 ## ETH/XZR Adam Delic 2012.02.22 v1.7 ##
38 #####################################################
39
40 puts "ttcn3_start: Starting the test suite"
41
42 # Procedure for waiting the command prompt of MC
43
44 proc wait_mc_prompt {} {
45 global mctr_id
46 set prompt "MC2> "
47 expect {
48 -i $mctr_id -exact "$prompt" {
49 } -i $mctr_id -re "^$prompt.*\r\n" {
50 send -i $mctr_id "\r"
51 exp_continue
52 } -i $mctr_id -re "^.*\r\n" {
53 exp_continue
54 } -i $mctr_id eof {
55 wait -i $mctr_id
56 puts "ttcn3_start: error: MC has terminated unexpectedly"
57 exit 5
58 }
59 }
60 }
61
62 # Procedure for cleaning up after a fatal error
63
64 proc error_cleanup {error_msg error_retcode} {
65 global mctr_id hc_id
66 puts "ttcn3_start: error: $error_msg"
67 send -i $mctr_id "exit\r"
68 expect -i $mctr_id eof
69 expect -i $hc_id eof
70 wait -i $hc_id
71 wait -i $mctr_id
72 exit $error_retcode
73 }
74
75 # procedure for parsing and extracting options from argv
76 # http://wiki.tcl.tk/17342
77 proc getopt {_argv name {_var ""} {default ""}} {
78 upvar 1 $_argv argv $_var var
79 set pos [lsearch -regexp $argv ^$name]
80 if {$pos>=0} {
81 set to $pos
82 # It would be better to use operator "ne" instead of "!=", but
83 # rhea has only Tcl 8.3.1 (vintage 2001) which doesn't understand it
84 if {$_var != ""} {
85 set var [lindex $argv [incr to]]
86 }
87 set argv [lreplace $argv $pos $to]
88 return 1
89 } else {
90 if {[llength [info level 0]] == 5} {set var $default}
91 return 0
92 }
93 }
94
95 set ip ""
96 getopt argv -ip ip
97
98 # Checking the number of arguments
99
100 if {[llength $argv] < 1} {
101 puts "usage: ttcn3_start \[-ip host_ip_address\] executable \[file.cfg\] {module_name\[.testcase_name\]}"
102 exit 2
103 }
104
105
106 # Setting the executable name variable ETS from argument
107
108 set first_arg [lindex $argv 0]
109
110 if {[file exists $first_arg]} {
111 set ETS $first_arg
112 } elseif {[file exists $first_arg.exe]} {
113 set ETS $first_arg.exe
114 } else {
115 puts "ttcn3_start: cannot find executable $first_arg"
116 exit 3
117 }
118
119
120 # Setting ETS_basename: executable name without extension
121
122 if {[string tolower [string range $ETS [expr [string length $ETS] - 4] end]] == ".exe"} {
123 set ETS_basename [string range $ETS 0 [expr [string length $ETS] - 5]]
124 } else {
125 set ETS_basename $ETS
126 }
127
128 if {[string index $ETS 0] != "/"} {
129 # Add a ./ prefix if ETS is a relative pathname
130 set ETS ./$ETS
131 }
132
133 # Setting the configuration file variable config_file
134
135 if {[llength $argv] > 1 && [file exists [lindex $argv 1]]} {
136 set config_file [lindex $argv 1]
137 set start_index 2
138 } else {
139 puts "ttcn3_start: warning: no configuration file was specified or the file name was misspelled"
140 set start_index 1
141 if {[file exists $ETS_basename.cfg]} {
142 set config_file $ETS_basename.cfg
143 puts "ttcn3_start: note: using default configuration file $ETS_basename.cfg"
144 }
145 # if 2nd parameter exists then it must be an existing test case
146 if {[llength $argv] > 1} {
147 # run $ETS -l to list test cases and check the 2nd parameter against that list
148 # if its not in the list stop with an error
149 # (the control part is also in the list but it works as if it was a testcase)
150 set second_argument [lindex $argv 1]
151 spawn $ETS -l
152 expect eof
153 set tc_name_found 0
154 # split into testcases on newlines
155 set testcase_names [split $expect_out(buffer)]
156 foreach testcase_name $testcase_names {
157 if {$second_argument==$testcase_name} {
158 set tc_name_found 1
159 break
160 }
161 }
162 if {$tc_name_found==0} {
163 puts "ttcn3_start: error: the second parameter is neither a configuration file nor an existing test case"
164 exit 11
165 }
166 }
167 }
168
169
170 # Checking TTCN3_DIR environment variable and setting mctr_cli
171
172 if {[info exists env(TTCN3_DIR)]} {
173 set mctr $env(TTCN3_DIR)/bin/mctr_cli
174 } else {
175 puts "ttcn3_start: warning: TTCN3_DIR environment variable is not set"
176 set mctr mctr_cli
177 }
178
179 set timeout -1
180
181 # Setting the hostname
182
183 set hostname [info hostname]
184 if { $ip != "" } {
185 set hostname $ip
186 }
187
188 # Start Main Controller
189
190 if {[info exists config_file]} {
191 spawn $mctr $config_file
192 } else {
193 spawn $mctr
194 }
195
196 set mctr_id $spawn_id
197 expect {
198 -re "Listening on( IP address )?(\[a-zA-Z0-9\.:%\]*)( and)? TCP port (\[0-9\]+)\..*\r\n" {
199 if { $ip != "" } {
200 set hostname $ip
201 puts ">>> Branch 1"
202 } elseif { $expect_out(2,string) != "" } {
203 set hostname $expect_out(2,string)
204 puts ">>>Branch 2"
205 } else {
206 puts "$hostname is the default"
207 }
208 set port $expect_out(4,string)
209 wait_mc_prompt
210 } -re "Entering batch mode\..*\r" {
211 puts "ttcn3_start: error: this script cannot be used when MC is run in batch mode"
212 puts " hint: Remove option NumHCs from section \[MAIN_CONTROLLER\] of the"
213 puts " configuration file."
214 exec kill -KILL [exp_pid]
215 wait -i $mctr_id
216 exit 4
217 }
218 -re "Error was found.*\r" {
219 puts "Please check the error message above"
220 exit 4
221 }
222 eof {
223 spawn $mctr -v
224 expect eof
225 puts "ttcn3_start: The Main controller exited unexpectedly. In case of license problem you can order license at ttcn.ericsson.se"
226 exit 4
227 }
228 }
229
230 # Start Host controller
231
232 spawn $ETS $hostname $port
233 set hc_id $spawn_id
234 expect {
235 -i $hc_id -exact "TTCN-3 Host Controller" {
236 } -i $hc_id -re ".*\r" {
237 exp_continue
238 } -i $hc_id eof {
239 error_cleanup "program $ETS is not a TTCN-3 executable in parallel mode" 6
240 }
241 }
242
243 expect {
244 -i $hc_id -re "Dynamic test case error" {
245 error_cleanup "program $ETS could not connect to the MC" 7
246 } -i $hc_id -re ".*\r" {
247 exp_continue
248 } -i $mctr_id "New HC connected from " {
249 } -i $hc_id eof {
250 error_cleanup "Host Controller with id $hc_id stopped unexpectedly" 10
251 }
252 }
253
254
255 # Create Main Test Component
256
257 send -i $mctr_id "cmtc\r"
258 expect {
259 -i $mctr_id -re "MTC is created\..*\r\n" {
260 wait_mc_prompt
261 } -i $mctr_id -re "Cannot create MTC" {
262 error_cleanup "the MTC cannot be created. " 8
263 } -i $mctr_id -re "No such host:\..*\r\n" {
264 error_cleanup "the MTC cannot be created on an unknown host. " 9
265 } -i $hc_id eof {
266 error_cleanup "Something went wrong... " 10
267 }
268 }
269
270 # Start Main Test Component
271
272 if {$start_index < [llength $argv]} {
273 # Use the list of test cases given in the command line.
274 for {set i $start_index} {$i < [llength $argv]} {incr i} {
275 send -i $mctr_id "smtc [lindex $argv $i]\r"
276 expect {
277 -i $hc_id -re ".*\r" {
278 exp_continue
279 } -i $mctr_id -exact "Test execution finished." {
280 } -i $mctr_id -exact "MTC terminated." {
281 error_cleanup "the MTC terminated unexpectedly" 10
282 }
283 }
284 }
285 } elseif {[info exists config_file]} {
286 # The configuration file is present. Use its [EXECUTE] section.
287 send -i $mctr_id "smtc\r"
288 expect {
289 -i $hc_id -re ".*\r" {
290 exp_continue
291 } -i $mctr_id -exact "Execution of \[EXECUTE\] section finished." {
292 } -i $mctr_id -exact "No \[EXECUTE\] section was given in the configuration file." {
293 } -i $mctr_id -exact "MTC terminated." {
294 error_cleanup "the MTC terminated unexpectedly" 10
295 }
296 }
297 } else {
298 # There is neither testcase name nor configuration file.
299 # Use the name of the executable as smtc argument.
300 set last_slash [string last "/" $ETS_basename]
301 if {$last_slash == -1} {
302 set module_name $ETS_basename
303 } else {
304 set module_name [string range $ETS_basename [expr $last_slash + 1] end]
305 }
306 send -i $mctr_id "smtc $module_name\r"
307 expect {
308 -i $hc_id -re ".*\r" {
309 exp_continue
310 } -i $mctr_id -exact "Test execution finished." {
311 } -i $mctr_id -exact "MTC terminated." {
312 error_cleanup "the MTC terminated unexpectedly" 10
313 }
314 }
315 }
316
317
318 # Exit Main Test Component
319
320 send -i $mctr_id "emtc\r"
321 expect -i $mctr_id -re "MTC terminated\..*\r\n"
322 wait_mc_prompt
323
324 # Quit from Main Controller
325
326 send -i $mctr_id "exit\r"
327
328 # Catch the last lines of the output
329
330 expect {
331 -i $hc_id eof {
332 expect -i $mctr_id eof
333 } -i $mctr_id eof {
334 expect -i $hc_id eof
335 }
336 }
337
338 # Wait until both MC and HC terminate
339
340 wait -i $hc_id
341 wait -i $mctr_id
This page took 0.038105 seconds and 5 git commands to generate.