Windowsでの動作用にparisを修正
@@ -0,0 +1,355 @@ | ||
1 | +<?php | |
2 | +include_once('autotest.conf.php'); | |
3 | + | |
4 | +define("DROP_DATABASE", "d"); // Drop database | |
5 | +define("UPDATE_TESTLINK", "u"); // Update testlink | |
6 | +define("COPY_TESTLINK", "c"); // Copy testlink | |
7 | +define("UPDATE_TESTCASE", "g"); // Get testcase | |
8 | +define("COPY_TESTCASE", "r"); // Replica testcase | |
9 | +define("SHOW_HELP", "h"); // HELP | |
10 | + | |
11 | + | |
12 | +/* for Windows, PHP < 5.3.0 */ | |
13 | +if(!function_exists('getopt')) { | |
14 | + /** | |
15 | + * lazy getopt() implementation. | |
16 | + */ | |
17 | + function getopt($option_candidate_str) | |
18 | + { | |
19 | + global $argc, $argv; | |
20 | + | |
21 | + $options = array(); | |
22 | + $option_candidate_array = explode(":", $option_candidate_str); | |
23 | + | |
24 | + for($index = 1; $index < $argc; $index++) { | |
25 | + $argment_str = $argv[$index]; | |
26 | + | |
27 | + if($argment_str[0] === "-") { | |
28 | + $option_str = substr($argment_str, 1); | |
29 | + if (in_array($option_str, $option_candidate_array)) { | |
30 | + $options[$option_str] = True; | |
31 | + } | |
32 | + } | |
33 | + } | |
34 | + | |
35 | + return $options; | |
36 | + } | |
37 | +} | |
38 | + | |
39 | +/** | |
40 | + * get command line arguments. | |
41 | + * @return arguments array | |
42 | + */ | |
43 | +function get_argument() | |
44 | +{ | |
45 | + $opts = DROP_DATABASE . ":"; | |
46 | + $opts .= UPDATE_TESTLINK . ":"; | |
47 | + $opts .= COPY_TESTLINK . ":"; | |
48 | + $opts .= UPDATE_TESTCASE . ":"; | |
49 | + $opts .= COPY_TESTCASE . ":"; | |
50 | + $opts .= SHOW_HELP . ":"; | |
51 | + | |
52 | + return getopt($opts); | |
53 | +} | |
54 | + | |
55 | +/** | |
56 | + * Automation testing do clean install so drop old testlink database. | |
57 | + */ | |
58 | +function drop_database() | |
59 | +{ | |
60 | + printf("host is %s\nuser is %s\n", TA_DATABASE_HOST, TA_DATABASE_USER); | |
61 | + | |
62 | + $link = mysql_connect(TA_DATABASE_HOST, TA_DATABASE_USER, TA_DATABASE_PASSWD) | |
63 | + or die("Cannot connect to the ".TA_DATABASE_HOST); | |
64 | + | |
65 | + printf("Login success! then, drop database %s\n", TA_DATABASE_NAME); | |
66 | + | |
67 | + $query = sprintf("drop database %s", TA_DATABASE_NAME); | |
68 | + | |
69 | + if (!mysql_query($query)) | |
70 | + printf("Cannot drop database %s\n", TA_DATABASE_NAME); | |
71 | + else | |
72 | + printf("drop database %s was success\n", TA_DATABASE_NAME); | |
73 | + | |
74 | + mysql_close($link) or die ("Cannot close to ".TA_DATABASE_HOST); | |
75 | +} | |
76 | + | |
77 | +/** | |
78 | + * copy files from $copy_from dir to $copy to dir. | |
79 | + * this function use shell command instead of copy() or whatever. | |
80 | + * | |
81 | + * @param $copy_from | |
82 | + * @param $copy_to | |
83 | + * @return command line string | |
84 | + */ | |
85 | +function create_copy_command_string($copy_from, $copy_to) | |
86 | +{ | |
87 | + $ret = ""; | |
88 | + | |
89 | + if (!strcmp(TA_OS, "linux")) | |
90 | + $ret = "cp -af ".$copy_from." ".$copy_to; | |
91 | + else if (!strcmp(TA_OS, "windows")) | |
92 | + $ret = "xcopy ".$copy_from." ".$copy_to." /e /y /i"; | |
93 | + else | |
94 | + die("Unknow os %s\n".TA_OS); | |
95 | + | |
96 | + return $ret; | |
97 | +} | |
98 | + | |
99 | +/** | |
100 | + * remove old files from $copy_to dir. | |
101 | + * this function use shell command instead of unlink() or whatever. | |
102 | + * | |
103 | + * @param $copy_to | |
104 | + * @return command line string | |
105 | + */ | |
106 | +function create_rm_command_string($copy_to) | |
107 | +{ | |
108 | + $ret = ""; | |
109 | + | |
110 | + if (!strcmp(TA_OS, "linux")) | |
111 | + $ret = "rm -rf ".$copy_to; | |
112 | + else if (!strcmp(TA_OS, "windows")) | |
113 | + $ret = "rd /s /q ".$copy_to; | |
114 | + else | |
115 | + die("Unknow os %s\n".TA_OS); | |
116 | + | |
117 | + return $ret; | |
118 | +} | |
119 | + | |
120 | +/** | |
121 | + * create cvs update command line. | |
122 | + * @return command line string | |
123 | + */ | |
124 | +function create_cvs_command_string() | |
125 | +{ | |
126 | + $ret = ""; | |
127 | + | |
128 | + if (!strcmp(TA_OS, "linux")) | |
129 | + $ret = "cvs up"; | |
130 | + else if (!strcmp(TA_OS, "windows")) | |
131 | + $ret = "cvs.exe up"; | |
132 | + else | |
133 | + die("Unknow os %s\n".TA_OS); | |
134 | + | |
135 | + return $ret; | |
136 | +} | |
137 | + | |
138 | +/** | |
139 | + * do shell command. | |
140 | + * @paarm $cmd command line string which you want to do. | |
141 | + */ | |
142 | +function do_command($cmd) | |
143 | +{ | |
144 | + $ret = TRUE; | |
145 | + | |
146 | + printf("doing [%s]\n", $cmd); | |
147 | + | |
148 | + if (strcmp($cmd, "")) | |
149 | + system($cmd); | |
150 | +} | |
151 | + | |
152 | +/** | |
153 | + * get delimiter. | |
154 | + * @return "/" or "\\" | |
155 | + */ | |
156 | +function get_delimiter() | |
157 | +{ | |
158 | + $ret = ""; | |
159 | + | |
160 | + if (!strcmp(TA_OS, "linux")) | |
161 | + $ret = "/"; | |
162 | + else if (!strcmp(TA_OS, "windows")) | |
163 | + $ret = "\\"; | |
164 | + else | |
165 | + die("Unknow os %s\n".TA_OS); | |
166 | + | |
167 | + return $ret; | |
168 | +} | |
169 | + | |
170 | +/** | |
171 | + * do cvs update. | |
172 | + * @param $dname directory name which you want to update | |
173 | + */ | |
174 | +function do_cvs_update($dname) | |
175 | +{ | |
176 | + $olddir = getcwd(); | |
177 | + | |
178 | + chdir($dname); | |
179 | + | |
180 | + system(create_cvs_command_string()); | |
181 | + | |
182 | + chdir($olddir); | |
183 | +} | |
184 | + | |
185 | +/** | |
186 | + * update testlink directory via cvs command. | |
187 | + */ | |
188 | +function update_testlink() | |
189 | +{ | |
190 | + do_cvs_update(TA_TESTLINK_COPY_FROM); | |
191 | +} | |
192 | + | |
193 | +/** | |
194 | + * update testcase directory via cvs command. | |
195 | + */ | |
196 | +function update_testcase() | |
197 | +{ | |
198 | + do_cvs_update(TA_TESTCASE_COPY_FROM); | |
199 | +} | |
200 | + | |
201 | +/** | |
202 | + * change directory permission because some directories have to be writable. | |
203 | + * @param $name directory name | |
204 | + */ | |
205 | +function do_chmod($name) | |
206 | +{ | |
207 | + if (strcmp($name, "")) | |
208 | + chmod(TA_TESTLINK_COPY_TO.get_delimiter().$name, 0777); | |
209 | + else | |
210 | + chmod(TA_TESTLINK_COPY_TO, 0777); | |
211 | +} | |
212 | + | |
213 | +/** | |
214 | + * copy testlink directory to your web server directory | |
215 | + */ | |
216 | +function copy_testlink() | |
217 | +{ | |
218 | + $cmd = create_rm_command_string(TA_TESTLINK_COPY_TO); | |
219 | + do_command($cmd); | |
220 | + | |
221 | + $cmd = create_copy_command_string(TA_TESTLINK_COPY_FROM, TA_TESTLINK_COPY_TO); | |
222 | + do_command($cmd); | |
223 | + | |
224 | + // some directories should be writable. | |
225 | + do_chmod("/gui/templates_c/"); | |
226 | + do_chmod("/logs"); | |
227 | + do_chmod("/upload_area/"); | |
228 | + do_chmod(""); | |
229 | + | |
230 | +} | |
231 | + | |
232 | +/** | |
233 | + * copy testcases to your working directory. | |
234 | + */ | |
235 | +function copy_testcase() | |
236 | +{ | |
237 | + $cmd = create_rm_command_string(TA_TESTCASE_COPY_TO); | |
238 | + do_command($cmd); | |
239 | + | |
240 | + $cmd = create_copy_command_string(TA_TESTCASE_COPY_FROM, TA_TESTCASE_COPY_TO); | |
241 | + do_command($cmd); | |
242 | +} | |
243 | + | |
244 | +/** | |
245 | + * replace url which in test cases. | |
246 | + * if your Testlink url is not like this "http://localhost/testlink", you need to call this function. | |
247 | + */ | |
248 | +function replace_url() | |
249 | +{ | |
250 | + $d = dir(TA_TESTCASE_COPY_TO) or die("cannot open ".TA_TESTCASE_COPY_TO); | |
251 | + while (($ent = $d->read()) != false) { | |
252 | + if (strstr($ent, ".html")) { | |
253 | + $f = TA_TESTCASE_COPY_TO.get_delimiter().$ent; | |
254 | + $str = file_get_contents($f); | |
255 | + | |
256 | + $ret = ereg_replace(TA_URL_FROM, TA_URL_TO, $str); | |
257 | + | |
258 | + file_put_contents($f, $ret) or die("cannot write data"); | |
259 | + } | |
260 | + } | |
261 | +} | |
262 | + | |
263 | +/** | |
264 | + * replace database loing account and password which are in install-mysql.html. | |
265 | + */ | |
266 | +function relpace_db_admin_passwd() | |
267 | +{ | |
268 | + $f = TA_TESTCASE_COPY_TO.get_delimiter().TA_INSTALL_TEST_CASE; | |
269 | + | |
270 | + $str = file_get_contents($f); | |
271 | + $str = ereg_replace(TA_ADMIN_NAME_FROM, TA_ADMIN_NAME_TO, $str); | |
272 | + $str = ereg_replace(TA_ADMIN_PASS_FROM, TA_ADMIN_PASS_TO, $str); | |
273 | + | |
274 | + file_put_contents($f, $str) or die("cannot write data"); | |
275 | +} | |
276 | + | |
277 | +/** | |
278 | + * delete your database login password from test result file. | |
279 | + */ | |
280 | +function delete_password_from_result_html() | |
281 | +{ | |
282 | + $f = TA_TEST_RESULT; | |
283 | + $str = file_get_contents($f); | |
284 | + | |
285 | + $ret = ereg_replace(TA_DATABASE_PASSWD, "", $str); | |
286 | + | |
287 | + file_put_contents($f, $ret) or die("cannot write data"); | |
288 | +} | |
289 | + | |
290 | +/** | |
291 | + * create selenium rc command line and do it. | |
292 | + */ | |
293 | +function run_selenium() | |
294 | +{ | |
295 | + $cmd = sprintf("java -jar %s -htmlSuite %s \"%s\" %s %s -timeout %s", | |
296 | + TA_SELENIUM_RC, TA_BROWSER, TA_TESTLINK_SERVER, | |
297 | + TA_TEST_RUNNER, TA_TEST_RESULT, TA_TIMEOUT); | |
298 | + printf("\n%s\n", $cmd); | |
299 | + system($cmd); | |
300 | + | |
301 | + delete_password_from_result_html(); | |
302 | +} | |
303 | + | |
304 | +/** | |
305 | + * show how to use this php script. | |
306 | + */ | |
307 | +function usage() | |
308 | +{ | |
309 | + printf("usage autotest.php [ducgrh]\n"); | |
310 | + printf("\t -d: drop database\n"); | |
311 | + printf("\t -u: update testlink using cvs command\n"); | |
312 | + printf("\t -c: copy testlink to your web server directory\n"); | |
313 | + printf("\t -g: update testcase using cvs command\n"); | |
314 | + printf("\t -r: copy testcase to your working directory\n"); | |
315 | + printf("\t -h: print this message\n"); | |
316 | + exit(0); | |
317 | +} | |
318 | + | |
319 | +/** | |
320 | + * start automation test. | |
321 | + */ | |
322 | +function start() | |
323 | +{ | |
324 | + $opt = get_argument(); | |
325 | + | |
326 | + // check command line arguments | |
327 | + | |
328 | + if (array_key_exists(SHOW_HELP, $opt)) | |
329 | + usage(); | |
330 | + | |
331 | + if (array_key_exists(DROP_DATABASE, $opt)) | |
332 | + drop_database(); | |
333 | + | |
334 | + if (array_key_exists(UPDATE_TESTLINK, $opt)) | |
335 | + update_testlink(); | |
336 | + | |
337 | + if (array_key_exists(COPY_TESTLINK, $opt)) | |
338 | + copy_testlink(); | |
339 | + | |
340 | + if (array_key_exists(UPDATE_TESTCASE, $opt)) | |
341 | + update_testcase(); | |
342 | + | |
343 | + if (array_key_exists(COPY_TESTCASE, $opt)) { | |
344 | + copy_testcase(); | |
345 | + replace_url(); | |
346 | + relpace_db_admin_passwd(); | |
347 | + } | |
348 | + | |
349 | + run_selenium(); | |
350 | +} | |
351 | + | |
352 | +// this script start from here. | |
353 | +start(); | |
354 | +?> | |
355 | + |