Commit c39caeaa authored by Julien Muchembled's avatar Julien Muchembled

testnode: more code clean up

parent 9669729e
import unittest import unittest
from unittest import TestCase from unittest import TestCase
from erp5.util.testnode.testnode import TestNode from erp5.util.testnode.testnode import TestNode, test_type_registry
from erp5.util.testnode.NodeTestSuite import SlapOSInstance, NodeTestSuite from erp5.util.testnode.NodeTestSuite import SlapOSInstance, NodeTestSuite
from erp5.util.testnode.ProcessManager import ProcessManager, SubprocessError from erp5.util.testnode.ProcessManager import ProcessManager, SubprocessError
from erp5.util.testnode.Updater import Updater from erp5.util.testnode.Updater import Updater
from erp5.util.testnode.SlapOSMasterCommunicator import SlapOSMasterCommunicator from erp5.util.testnode.SlapOSMasterCommunicator import SlapOSMasterCommunicator
from erp5.util.testnode.SlapOSControler import SlapOSControler from erp5.util.testnode.SlapOSControler import SlapOSControler
from erp5.util.testnode.UnitTestRunner import UnitTestRunner
from erp5.util.testnode.ScalabilityTestRunner import ScalabilityTestRunner
from erp5.util.testnode.SlapOSControler import createFolder from erp5.util.testnode.SlapOSControler import createFolder
from erp5.util.taskdistribution import TaskDistributor from erp5.util.taskdistribution import TaskDistributor
...@@ -56,14 +54,6 @@ class ERP5TestNode(TestCase): ...@@ -56,14 +54,6 @@ class ERP5TestNode(TestCase):
print "TESTNODE LOG : %r, %r" % (arg, kw) print "TESTNODE LOG : %r, %r" % (arg, kw)
self.log = log self.log = log
def returnGoodClassRunner(self, test_type):
if test_type == 'UnitTest':
return UnitTestRunner
elif test_type == 'ScalabilityTest':
return ScalabilityTestRunner
else:
raise NotImplementedError
def tearDown(self): def tearDown(self):
shutil.rmtree(self._temp_dir, True) shutil.rmtree(self._temp_dir, True)
...@@ -127,7 +117,7 @@ class ERP5TestNode(TestCase): ...@@ -127,7 +117,7 @@ class ERP5TestNode(TestCase):
""" """
Update from zero/Regenerate the testsuite Update from zero/Regenerate the testsuite
""" """
node_test_suite.edit(working_directory=self.working_directory, node_test_suite.edit(
**self.getTestSuiteData(add_third_repository=add_third_repository, **self.getTestSuiteData(add_third_repository=add_third_repository,
add_broken_repository=add_broken_repository)[0]) add_broken_repository=add_broken_repository)[0])
...@@ -188,8 +178,9 @@ class ERP5TestNode(TestCase): ...@@ -188,8 +178,9 @@ class ERP5TestNode(TestCase):
node_test_suite = test_node.getNodeTestSuite('foo') node_test_suite = test_node.getNodeTestSuite('foo')
self.assertEquals(0, node_test_suite.retry_software_count) self.assertEquals(0, node_test_suite.retry_software_count)
node_test_suite.retry_software_count = 2 node_test_suite.retry_software_count = 2
self.assertIs(node_test_suite, test_node.getNodeTestSuite('foo'))
self.assertEquals(2, node_test_suite.retry_software_count) self.assertEquals(2, node_test_suite.retry_software_count)
node_test_suite = test_node.delNodeTestSuite('foo') del test_node.node_test_suite_dict['foo']
node_test_suite = test_node.getNodeTestSuite('foo') node_test_suite = test_node.getNodeTestSuite('foo')
self.assertEquals(0, node_test_suite.retry_software_count) self.assertEquals(0, node_test_suite.retry_software_count)
...@@ -199,7 +190,6 @@ class ERP5TestNode(TestCase): ...@@ -199,7 +190,6 @@ class ERP5TestNode(TestCase):
""" """
test_node = self.getTestNode() test_node = self.getTestNode()
node_test_suite = test_node.getNodeTestSuite('foo') node_test_suite = test_node.getNodeTestSuite('foo')
node_test_suite.edit(working_directory=self.working_directory)
self.assertEquals("%s/foo" % self.working_directory, self.assertEquals("%s/foo" % self.working_directory,
node_test_suite.working_directory) node_test_suite.working_directory)
self.assertEquals("%s/foo/test_suite" % self.working_directory, self.assertEquals("%s/foo/test_suite" % self.working_directory,
...@@ -471,16 +461,16 @@ shared = true ...@@ -471,16 +461,16 @@ shared = true
test_node = self.getTestNode() test_node = self.getTestNode()
test_suite_data = self.getTestSuiteData(add_third_repository=True) test_suite_data = self.getTestSuiteData(add_third_repository=True)
self.assertEquals([], os.listdir(self.working_directory)) self.assertEquals([], os.listdir(self.working_directory))
test_node.checkOldTestSuite(test_suite_data) test_node.purgeOldTestSuite(test_suite_data)
self.assertEquals([], os.listdir(self.working_directory)) self.assertEquals([], os.listdir(self.working_directory))
os.mkdir(os.path.join(self.working_directory, 'foo')) os.mkdir(os.path.join(self.working_directory, 'foo'))
self.assertEquals(['foo'], os.listdir(self.working_directory)) self.assertEquals(['foo'], os.listdir(self.working_directory))
test_node.checkOldTestSuite(test_suite_data) test_node.purgeOldTestSuite(test_suite_data)
self.assertEquals(['foo'], os.listdir(self.working_directory)) self.assertEquals(['foo'], os.listdir(self.working_directory))
os.mkdir(os.path.join(self.working_directory, 'bar')) os.mkdir(os.path.join(self.working_directory, 'bar'))
self.assertEquals(set(['bar','foo']), self.assertEquals(set(['bar','foo']),
set(os.listdir(self.working_directory))) set(os.listdir(self.working_directory)))
test_node.checkOldTestSuite(test_suite_data) test_node.purgeOldTestSuite(test_suite_data)
self.assertEquals(['foo'], os.listdir(self.working_directory)) self.assertEquals(['foo'], os.listdir(self.working_directory))
def test_09_runTestSuite(self, my_test_type='UnitTest'): def test_09_runTestSuite(self, my_test_type='UnitTest'):
...@@ -499,15 +489,13 @@ shared = true ...@@ -499,15 +489,13 @@ shared = true
test_node = self.getTestNode() test_node = self.getTestNode()
test_node.process_manager.spawn = spawn test_node.process_manager.spawn = spawn
RunnerClass = self.returnGoodClassRunner(my_test_type) runner = test_type_registry[my_test_type](test_node)
runner = self.returnGoodClassRunner(my_test_type)(test_node)
slapos_controler = runner._getSlapOSControler(self.working_directory)
# Create and initialise/regenerate a nodetestsuite # Create and initialise/regenerate a nodetestsuite
node_test_suite = test_node.getNodeTestSuite('foo') node_test_suite = test_node.getNodeTestSuite('foo')
self.updateNodeTestSuiteData(node_test_suite) self.updateNodeTestSuiteData(node_test_suite)
node_test_suite.revision_list = ('dummy', (0, '')), node_test_suite.revision_list = ('dummy', (0, '')),
path = slapos_controler.instance_root + '/a/bin/runTestSuite' path = runner.getInstanceRoot(node_test_suite) + '/a/bin/runTestSuite'
os.makedirs(os.path.dirname(path)) os.makedirs(os.path.dirname(path))
os.close(os.open(path, os.O_CREAT)) os.close(os.open(path, os.O_CREAT))
...@@ -542,11 +530,9 @@ shared = true ...@@ -542,11 +530,9 @@ shared = true
def test_10_prepareSlapOS(self, my_test_type='UnitTest'): def test_10_prepareSlapOS(self, my_test_type='UnitTest'):
test_node = self.getTestNode() test_node = self.getTestNode()
test_node_slapos = SlapOSInstance() test_node_slapos = SlapOSInstance(self.slapos_directory)
RunnerClass = self.returnGoodClassRunner(my_test_type) runner = test_type_registry[my_test_type](test_node)
runner = RunnerClass(test_node)
node_test_suite = test_node.getNodeTestSuite('foo') node_test_suite = test_node.getNodeTestSuite('foo')
node_test_suite.edit(working_directory=self.working_directory)
status_dict = {"status_code" : 0} status_dict = {"status_code" : 0}
global call_list global call_list
call_list = [] call_list = []
...@@ -660,7 +646,7 @@ shared = true ...@@ -660,7 +646,7 @@ shared = true
original_sleep = time.sleep original_sleep = time.sleep
time.sleep = doNothing time.sleep = doNothing
self.generateTestRepositoryList() self.generateTestRepositoryList()
RunnerClass = self.returnGoodClassRunner(my_test_type) RunnerClass = test_type_registry[my_test_type]
# Patch # Patch
if my_test_type == "ScalabilityTest": if my_test_type == "ScalabilityTest":
original_getSlaposAccountKey = TaskDistributor.getSlaposAccountKey original_getSlaposAccountKey = TaskDistributor.getSlaposAccountKey
...@@ -742,7 +728,6 @@ shared = true ...@@ -742,7 +728,6 @@ shared = true
def test_14_createFolder(self): def test_14_createFolder(self):
test_node = self.getTestNode() test_node = self.getTestNode()
node_test_suite = test_node.getNodeTestSuite('foo') node_test_suite = test_node.getNodeTestSuite('foo')
node_test_suite.edit(working_directory=self.working_directory)
folder = node_test_suite.test_suite_directory folder = node_test_suite.test_suite_directory
self.assertEquals(False, os.path.exists(folder)) self.assertEquals(False, os.path.exists(folder))
createFolder(folder) createFolder(folder)
...@@ -818,7 +803,7 @@ shared = true ...@@ -818,7 +803,7 @@ shared = true
self.assertEquals(1, len([x for x in suite_log.readlines() \ self.assertEquals(1, len([x for x in suite_log.readlines() \
if x.find("Activated logfile")>=0])) if x.find("Activated logfile")>=0]))
RunnerClass = self.returnGoodClassRunner(my_test_type) RunnerClass = test_type_registry[my_test_type]
original_sleep = time.sleep original_sleep = time.sleep
time.sleep = doNothing time.sleep = doNothing
self.generateTestRepositoryList() self.generateTestRepositoryList()
...@@ -938,8 +923,7 @@ shared = true ...@@ -938,8 +923,7 @@ shared = true
SlapOSControler.initializeSlapOSControler SlapOSControler.initializeSlapOSControler
initial_runSoftwareRelease = SlapOSControler.runSoftwareRelease initial_runSoftwareRelease = SlapOSControler.runSoftwareRelease
test_node = self.getTestNode() test_node = self.getTestNode()
RunnerClass = self.returnGoodClassRunner(my_test_type) runner = test_type_registry[my_test_type](test_node)
runner = RunnerClass(test_node)
node_test_suite = test_node.getNodeTestSuite('foo') node_test_suite = test_node.getNodeTestSuite('foo')
init_call_kw_list = [] init_call_kw_list = []
def initializeSlapOSControler(self, **kw): def initializeSlapOSControler(self, **kw):
...@@ -1045,7 +1029,7 @@ shared = true ...@@ -1045,7 +1029,7 @@ shared = true
os.makedirs(test_result_path_root) os.makedirs(test_result_path_root)
self.generateTestRepositoryList() self.generateTestRepositoryList()
# Select the good runner to modify # Select the good runner to modify
RunnerClass = self.returnGoodClassRunner('ScalabilityTest') RunnerClass = test_type_registry['ScalabilityTest']
# Patch methods # Patch methods
original_sleep = time.sleep original_sleep = time.sleep
original_getSlaposAccountKey = TaskDistributor.getSlaposAccountKey original_getSlaposAccountKey = TaskDistributor.getSlaposAccountKey
......
...@@ -37,38 +37,28 @@ class SlapOSInstance(object): ...@@ -37,38 +37,28 @@ class SlapOSInstance(object):
Base of an software instance, Base of an software instance,
store variables used during software installation store variables used during software installation
""" """
def __init__(self): def __init__(self, working_directory):
self.retry_software_count = 0 self.retry_software_count = 0
self.retry = False self.retry = False
self.working_directory = working_directory
def edit(self, **kw):
self.__dict__.update(**kw)
self._checkData()
def _checkData(self):
pass
class NodeTestSuite(SlapOSInstance): class NodeTestSuite(SlapOSInstance):
""" """
""" """
def __init__(self, reference): def __init__(self, reference, working_directory):
super(NodeTestSuite, self).__init__() d = os.path.join(working_directory, reference)
super(NodeTestSuite, self).__init__(d)
self.reference = reference self.reference = reference
self.cluster_configuration = {} self.cluster_configuration = {}
self.test_suite_directory = os.path.join(d, 'test_suite')
self.custom_profile_path = os.path.join(d, 'software.cfg')
createFolder(d)
def _checkData(self): def edit(self, **kw):
if getattr(self, "working_directory", None) is not None: self.__dict__.update(**kw)
if not(self.working_directory.endswith(os.path.sep + self.reference)): for vcs_repository in kw.get('vcs_repository_list', ()):
self.working_directory = os.path.join(self.working_directory,
self.reference)
createFolder(self.working_directory)
self.test_suite_directory = os.path.join(
self.working_directory, "test_suite")
self.custom_profile_path = os.path.join(self.working_directory,
'software.cfg')
if getattr(self, "vcs_repository_list", None) is not None:
for vcs_repository in self.vcs_repository_list:
buildout_section_id = vcs_repository.get('buildout_section_id') buildout_section_id = vcs_repository.get('buildout_section_id')
repository_id = buildout_section_id or \ repository_id = buildout_section_id or \
vcs_repository.get('url').split('/')[-1].split('.')[0] vcs_repository.get('url').split('/')[-1].split('.')[0]
......
...@@ -99,12 +99,6 @@ class SlapOSControler(object): ...@@ -99,12 +99,6 @@ class SlapOSControler(object):
else: else:
raise ValueError("Configuration file not found.") raise ValueError("Configuration file not found.")
def getInstanceRequestedState(self, reference):
try:
return self.instance_config[reference]['requested_state']
except Exception:
raise ValueError("Instance '%s' not exist" %self.instance_config[reference])
def request(self, reference, software_url, software_type=None, def request(self, reference, software_url, software_type=None,
software_configuration=None, computer_guid=None, state='started'): software_configuration=None, computer_guid=None, state='started'):
""" """
...@@ -219,7 +213,7 @@ class SlapOSControler(object): ...@@ -219,7 +213,7 @@ class SlapOSControler(object):
self.software_path_list = software_path_list self.software_path_list = software_path_list
self.log('SlapOSControler, initialize, reset_software: %r', reset_software) self.log('SlapOSControler, initialize, reset_software: %r', reset_software)
config = self.config config = self.config
slapos_config_dict = self.config.copy() slapos_config_dict = config.copy()
slapos_config_dict.update(software_root=self.software_root, slapos_config_dict.update(software_root=self.software_root,
instance_root=self.instance_root, instance_root=self.instance_root,
proxy_database=self.proxy_database) proxy_database=self.proxy_database)
......
...@@ -102,7 +102,7 @@ class UnitTestRunner(object): ...@@ -102,7 +102,7 @@ class UnitTestRunner(object):
# report-url, report-project and suite-url are required to seleniumrunner # report-url, report-project and suite-url are required to seleniumrunner
# instance. This is a hack which must be removed. # instance. This is a hack which must be removed.
config = self.testnode.config config = self.testnode.config
return self._prepareSlapOS(config['slapos_directory'], return self._prepareSlapOS(test_node_slapos.working_directory,
test_node_slapos, self.testnode.log, create_partition=0, test_node_slapos, self.testnode.log, create_partition=0,
software_path_list=config.get("software_list"), software_path_list=config.get("software_list"),
cluster_configuration={ cluster_configuration={
...@@ -120,14 +120,18 @@ class UnitTestRunner(object): ...@@ -120,14 +120,18 @@ class UnitTestRunner(object):
software_path_list=[node_test_suite.custom_profile_path], software_path_list=[node_test_suite.custom_profile_path],
cluster_configuration={'_': json.dumps(node_test_suite.cluster_configuration)}) cluster_configuration={'_': json.dumps(node_test_suite.cluster_configuration)})
def getInstanceRoot(self, node_test_suite):
return self._getSlapOSControler(
node_test_suite.working_directory).instance_root
def runTestSuite(self, node_test_suite, portal_url, log=None): def runTestSuite(self, node_test_suite, portal_url, log=None):
config = self.testnode.config config = self.testnode.config
slapos_controler = self._getSlapOSControler(self.testnode.working_directory) run_test_suite_path_list = glob.glob(
run_test_suite_path_list = sorted(glob.glob("%s/*/bin/runTestSuite" % \ self.getInstanceRoot(node_test_suite) + "/*/bin/runTestSuite")
slapos_controler.instance_root)) try:
if not len(run_test_suite_path_list): run_test_suite_path = min(run_test_suite_path_list)
except ValueError:
raise ValueError('No runTestSuite provided in installed partitions.') raise ValueError('No runTestSuite provided in installed partitions.')
run_test_suite_path = run_test_suite_path_list[0]
# Deal with Shebang size limitation # Deal with Shebang size limitation
invocation_list = dealShebang(run_test_suite_path) invocation_list = dealShebang(run_test_suite_path)
invocation_list += (run_test_suite_path, invocation_list += (run_test_suite_path,
......
...@@ -29,14 +29,8 @@ import argparse ...@@ -29,14 +29,8 @@ import argparse
import logging import logging
import logging.handlers import logging.handlers
import os import os
import pkg_resources
from testnode import TestNode from .testnode import TestNode
CONFIG = {
'computer_id': 'COMPUTER',
'partition_reference': 'test0',
}
def main(*args): def main(*args):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
...@@ -54,6 +48,10 @@ def main(*args): ...@@ -54,6 +48,10 @@ def main(*args):
logging.basicConfig(level=logging.INFO, logging.basicConfig(level=logging.INFO,
format=logger_format) format=logger_format)
logger = logging.getLogger('erp5testnode') logger = logging.getLogger('erp5testnode')
CONFIG = {
'logger': logger.info,
'partition_reference': 'test0',
}
if parsed_argument.console or parsed_argument.logfile: if parsed_argument.console or parsed_argument.logfile:
if parsed_argument.console: if parsed_argument.console:
logger.addHandler(logging.StreamHandler()) logger.addHandler(logging.StreamHandler())
...@@ -64,11 +62,10 @@ def main(*args): ...@@ -64,11 +62,10 @@ def main(*args):
maxBytes=20000000, backupCount=4) maxBytes=20000000, backupCount=4)
file_handler.setFormatter(formatter) file_handler.setFormatter(formatter)
logger.addHandler(file_handler) logger.addHandler(file_handler)
logger.info('Activated logfile %r output' % parsed_argument.logfile) logger.info('Activated logfile %r output', parsed_argument.logfile)
CONFIG['log_file'] = parsed_argument.logfile CONFIG['log_file'] = parsed_argument.logfile
else: else:
logger.addHandler(logging.NullHandler()) logger.addHandler(logging.NullHandler())
CONFIG['logger'] = logger.info
config = ConfigParser.SafeConfigParser() config = ConfigParser.SafeConfigParser()
# do not change case of option keys # do not change case of option keys
config.optionxform = str config.optionxform = str
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment