diff --git a/testenv/ChangeLog b/testenv/ChangeLog index 928d9310..c851c16a 100644 --- a/testenv/ChangeLog +++ b/testenv/ChangeLog @@ -1,3 +1,9 @@ +2014-03-13 Zihang Chen + + * test: (new package) package for test case classes + * WgetTest.py: Split into test/base_test.py and test/http_test.py. + * Test-*.py: Optimize the imports according to changes of WgetTest.py + 2014-03-13 Zihang Chen * server: (new package) package for the server classes diff --git a/testenv/README b/testenv/README index 7863c3d3..2d7f64ed 100644 --- a/testenv/README +++ b/testenv/README @@ -17,10 +17,9 @@ Structure: (both HTTP and FTP) for testing Wget. The HTTP server runs an instance of Python's http.server module. The FTP server is to be implemented. - * WgetTest.py: This file contains various functions and global variables for - each instance of the server that is initiated. It includes functions to - start and stop the server, to initialze the test environment and to cleanup - after a test. + * test: This package contains the test case classes for HTTP and FTP. The + test case classes includes methods for initializing and cleaning up of the + test environment. * Test-Proto.py: This is a prototype Test Case file. The file defines all the acceptable elements and their uses. Typically, one must copy this file diff --git a/testenv/Test--https.py b/testenv/Test--https.py index 066f2d91..1129416d 100755 --- a/testenv/Test--https.py +++ b/testenv/Test--https.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest, HTTPS, HTTP +from test.http_test import HTTPTest +from test.base_test import HTTP, HTTPS from misc.wget_file import WgetFile """ diff --git a/testenv/Test--spider-r.py b/testenv/Test--spider-r.py index c099f5a1..df023d3a 100755 --- a/testenv/Test--spider-r.py +++ b/testenv/Test--spider-r.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-Content-disposition-2.py b/testenv/Test-Content-disposition-2.py index 423ea270..3bf49405 100755 --- a/testenv/Test-Content-disposition-2.py +++ b/testenv/Test-Content-disposition-2.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-Content-disposition.py b/testenv/Test-Content-disposition.py index 45914b21..ce245994 100755 --- a/testenv/Test-Content-disposition.py +++ b/testenv/Test-Content-disposition.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-Head.py b/testenv/Test-Head.py index d0fad755..e3562529 100755 --- a/testenv/Test-Head.py +++ b/testenv/Test-Head.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-O.py b/testenv/Test-O.py index 4b3c26af..784a229d 100755 --- a/testenv/Test-O.py +++ b/testenv/Test-O.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-Parallel-Proto.py b/testenv/Test-Parallel-Proto.py index 6e09bfb5..97197122 100755 --- a/testenv/Test-Parallel-Proto.py +++ b/testenv/Test-Parallel-Proto.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest, HTTP, HTTPS +from test.http_test import HTTPTest +from misc.constants import HTTP, HTTPS from misc.wget_file import WgetFile """ diff --git a/testenv/Test-Post.py b/testenv/Test-Post.py index d7ad616e..8983454b 100755 --- a/testenv/Test-Post.py +++ b/testenv/Test-Post.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-Proto.py b/testenv/Test-Proto.py index 90fca8bb..650f43ca 100755 --- a/testenv/Test-Proto.py +++ b/testenv/Test-Proto.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest, HTTP, HTTPS +from test.http_test import HTTPTest +from misc.constants import HTTP, HTTPS from misc.wget_file import WgetFile """ diff --git a/testenv/Test-auth-basic-fail.py b/testenv/Test-auth-basic-fail.py index e2890d58..263f7566 100755 --- a/testenv/Test-auth-basic-fail.py +++ b/testenv/Test-auth-basic-fail.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-auth-basic.py b/testenv/Test-auth-basic.py index 2cafa40e..102bf8c6 100755 --- a/testenv/Test-auth-basic.py +++ b/testenv/Test-auth-basic.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-auth-both.py b/testenv/Test-auth-both.py index 214ef0eb..2da2840f 100755 --- a/testenv/Test-auth-both.py +++ b/testenv/Test-auth-both.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-auth-digest.py b/testenv/Test-auth-digest.py index 44e261f8..f6d28c7c 100755 --- a/testenv/Test-auth-digest.py +++ b/testenv/Test-auth-digest.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-auth-no-challenge-url.py b/testenv/Test-auth-no-challenge-url.py index e0f851f9..c39ebaa9 100755 --- a/testenv/Test-auth-no-challenge-url.py +++ b/testenv/Test-auth-no-challenge-url.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-auth-no-challenge.py b/testenv/Test-auth-no-challenge.py index 1d0e355e..f02c0307 100755 --- a/testenv/Test-auth-no-challenge.py +++ b/testenv/Test-auth-no-challenge.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-auth-retcode.py b/testenv/Test-auth-retcode.py index 5619f9f5..8719bd01 100755 --- a/testenv/Test-auth-retcode.py +++ b/testenv/Test-auth-retcode.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-auth-with-content-disposition.py b/testenv/Test-auth-with-content-disposition.py index a28b088f..f74a9592 100755 --- a/testenv/Test-auth-with-content-disposition.py +++ b/testenv/Test-auth-with-content-disposition.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-c-full.py b/testenv/Test-c-full.py index 5b035fdd..3cdcd768 100755 --- a/testenv/Test-c-full.py +++ b/testenv/Test-c-full.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-cookie-401.py b/testenv/Test-cookie-401.py index 3161d1d7..9488c34e 100755 --- a/testenv/Test-cookie-401.py +++ b/testenv/Test-cookie-401.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-cookie-domain-mismatch.py b/testenv/Test-cookie-domain-mismatch.py index e6bf7bb9..92487f43 100755 --- a/testenv/Test-cookie-domain-mismatch.py +++ b/testenv/Test-cookie-domain-mismatch.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-cookie-expires.py b/testenv/Test-cookie-expires.py index 78bc8b9b..48a93b99 100755 --- a/testenv/Test-cookie-expires.py +++ b/testenv/Test-cookie-expires.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/Test-cookie.py b/testenv/Test-cookie.py index df62d143..b70316d8 100755 --- a/testenv/Test-cookie.py +++ b/testenv/Test-cookie.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from sys import exit -from WgetTest import HTTPTest +from test.http_test import HTTPTest from misc.wget_file import WgetFile """ diff --git a/testenv/test/__init__.py b/testenv/test/__init__.py new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/testenv/test/__init__.py @@ -0,0 +1 @@ + diff --git a/testenv/WgetTest.py b/testenv/test/base_test.py similarity index 56% rename from testenv/WgetTest.py rename to testenv/test/base_test.py index 01ef4794..f63e6dfb 100644 --- a/testenv/WgetTest.py +++ b/testenv/test/base_test.py @@ -3,22 +3,25 @@ import shutil import shlex import sys import traceback +from server.http import http_server import re import time +import shlex +import shutil from subprocess import call from misc.colour_terminal import print_red, print_green, print_blue from difflib import unified_diff from exc.test_failed import TestFailed import conf -from server.http import http_server HTTP = "HTTP" HTTPS = "HTTPS" -""" Class that defines methods common to both HTTP and FTP Tests. """ - class CommonMethods: + + """ Class that defines methods common to both HTTP and FTP Tests. """ + TestFailed = TestFailed def init_test_env (self, name): @@ -131,107 +134,3 @@ class CommonMethods: r_obj = conf.find_conf(rule)(file_obj.rules[rule]) server_rules[rule] = r_obj return server_rules - - -""" Class for HTTP Tests. """ - -class HTTPTest (CommonMethods): - -# Temp Notes: It is expected that when pre-hook functions are executed, only an empty test-dir exists. -# pre-hook functions are executed just prior to the call to Wget is made. -# post-hook functions will be executed immediately after the call to Wget returns. - - def __init__ ( - self, - name="Unnamed Test", - pre_hook=dict(), - test_params=dict(), - post_hook=dict(), - servers=[HTTP] - ): - try: - self.Server_setup (name, pre_hook, test_params, post_hook, servers) - except TestFailed as tf: - print_red("Error: " + tf.error) - self.tests_passed = False - except Exception as ae: - print_red("Unhandled Exception Caught.") - print ( ae.__str__ ()) - traceback.print_exc () - self.tests_passed = False - else: - print_green("Test Passed") - finally: - self._exit_test () - - def Server_setup (self, name, pre_hook, test_params, post_hook, servers): - self.name = name - self.server_types = servers - self.servers = len (servers) - print_blue("Running Test " + self.name) - self.init_test_env (name) - self.server_list = list() - self.domain_list = list() - for server_type in servers: - server_inst = getattr (self, "init_" + server_type + "_Server") () - self.server_list.append (server_inst) - domain = self.get_domain_addr (server_inst.server_address) - self.domain_list.append (domain) - #self.server = self.init_HTTP_Server () - #self.domain = self.get_domain_addr (self.server.server_address) - - self.pre_hook_call (pre_hook) - self.call_test (test_params) - self.post_hook_call (post_hook) - - - def hook_call(self, configs, name): - for conf_name, conf_arg in configs.items(): - try: - # conf.find_conf(conf_name) returns the required conf class, - # then the class is instantiated with conf_arg, then the - # conf instance is called with this test instance itself to - # invoke the desired hook - conf.find_conf(conf_name)(conf_arg)(self) - except AttributeError as e: - print(e) - self.stop_HTTP_Server() - raise TestFailed("%s %s not defined." % - (name, conf_name)) - - - def pre_hook_call (self, pre_hook): - self.hook_call(pre_hook, 'Pre Test Function') - - def call_test (self, test_params): - self.hook_call(test_params, 'Test Option') - - try: - self.act_retcode = self.exec_wget (self.options, self.urls, self.domain_list) - except TestFailed as tf: - self.stop_HTTP_Server () - raise TestFailed (tf.__str__ ()) - self.stop_HTTP_Server () - - def post_hook_call (self, post_hook): - self.hook_call(post_hook, 'Post Test Function') - - def init_HTTP_Server (self): - server = http_server.HTTPd () - server.start () - return server - - def init_HTTPS_Server (self): - server = http_server.HTTPSd () - server.start () - return server - - def stop_HTTP_Server (self): - self.Request_remaining = list () - for server in self.server_list: - server_req = server.server_inst.get_req_headers () - self.Request_remaining.append (server_req) - server.server_inst.shutdown () - - -# vim: set ts=4 sts=4 sw=4 tw=80 et : diff --git a/testenv/test/http_test.py b/testenv/test/http_test.py new file mode 100644 index 00000000..6fa67a19 --- /dev/null +++ b/testenv/test/http_test.py @@ -0,0 +1,107 @@ +import traceback +import conf +from exc.test_failed import TestFailed +from misc.colour_terminal import print_red, print_green, print_blue +from server.http import http_server +from test.base_test import CommonMethods, HTTP + + +class HTTPTest (CommonMethods): + + """ Class for HTTP Tests. """ + +# Temp Notes: It is expected that when pre-hook functions are executed, only an empty test-dir exists. +# pre-hook functions are executed just prior to the call to Wget is made. +# post-hook functions will be executed immediately after the call to Wget returns. + + def __init__ ( + self, + name="Unnamed Test", + pre_hook=dict(), + test_params=dict(), + post_hook=dict(), + servers=[HTTP] + ): + try: + self.Server_setup (name, pre_hook, test_params, post_hook, servers) + except TestFailed as tf: + print_red("Error: " + tf.error) + self.tests_passed = False + except Exception as ae: + print_red("Unhandled Exception Caught.") + print ( ae.__str__ ()) + traceback.print_exc () + self.tests_passed = False + else: + print_green("Test Passed") + finally: + self._exit_test () + + def Server_setup (self, name, pre_hook, test_params, post_hook, servers): + self.name = name + self.server_types = servers + self.servers = len (servers) + print_blue("Running Test " + self.name) + self.init_test_env (name) + self.server_list = list() + self.domain_list = list() + for server_type in servers: + server_inst = getattr (self, "init_" + server_type + "_Server") () + self.server_list.append (server_inst) + domain = self.get_domain_addr (server_inst.server_address) + self.domain_list.append (domain) + #self.server = self.init_HTTP_Server () + #self.domain = self.get_domain_addr (self.server.server_address) + + self.pre_hook_call (pre_hook) + self.call_test (test_params) + self.post_hook_call (post_hook) + + + def hook_call(self, configs, name): + for conf_name, conf_arg in configs.items(): + try: + # conf.find_conf(conf_name) returns the required conf class, + # then the class is instantiated with conf_arg, then the + # conf instance is called with this test instance itself to + # invoke the desired hook + conf.find_conf(conf_name)(conf_arg)(self) + except AttributeError as e: + print(e) + self.stop_HTTP_Server() + raise TestFailed("%s %s not defined." % + (name, conf_name)) + + + def pre_hook_call (self, pre_hook): + self.hook_call(pre_hook, 'Pre Test Function') + + def call_test (self, test_params): + self.hook_call(test_params, 'Test Option') + + try: + self.act_retcode = self.exec_wget (self.options, self.urls, self.domain_list) + except TestFailed as tf: + self.stop_HTTP_Server () + raise TestFailed (tf.__str__ ()) + self.stop_HTTP_Server () + + def post_hook_call (self, post_hook): + self.hook_call(post_hook, 'Post Test Function') + + def init_HTTP_Server (self): + server = http_server.HTTPd () + server.start () + return server + + def init_HTTPS_Server (self): + server = http_server.HTTPSd () + server.start () + return server + + def stop_HTTP_Server (self): + self.Request_remaining = list () + for server in self.server_list: + server_req = server.server_inst.get_req_headers () + self.Request_remaining.append (server_req) + server.server_inst.shutdown ()