Create package test for test case classes

delete mode 100644 testenv/WgetTest.py
 create mode 100644 testenv/test/__init__.py
 create mode 100644 testenv/test/base_test.py
 create mode 100644 testenv/test/http_test.py
This commit is contained in:
Zihang Chen 2014-03-13 19:06:55 +08:00 committed by Giuseppe Scrivano
parent 507383df07
commit d26c8ebb16
27 changed files with 148 additions and 133 deletions

View File

@ -1,3 +1,9 @@
2014-03-13 Zihang Chen <chsc4698@gmail.com>
* 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 <chsc4698@gmail.com>
* server: (new package) package for the server classes

View File

@ -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

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

View File

@ -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
"""

1
testenv/test/__init__.py Normal file
View File

@ -0,0 +1 @@

View File

@ -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 :

107
testenv/test/http_test.py Normal file
View File

@ -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 ()