--- /dev/null
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import unittest
+import os
+import sys
+import time
+
+# Test of SalomeLauncher.
+# This test should be run in the salome environment, using "salome shell"
+# If YACS_ROOT_DIR is not set, the test of submitting a YACS schema will be
+# skiped.
+class TestCompo(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ # Prepare the test directory
+ import shutil
+ cls.test_dir = os.path.join(os.getcwd(), "test_dir")
+ cls.suffix = time.strftime("-%Y-%m-%d-%H-%M-%S")
+ shutil.rmtree(cls.test_dir, ignore_errors=True)
+ os.mkdir(cls.test_dir)
+
+ ##############################
+ # test of python_salome job
+ ##############################
+ def test_salome_py_job(self):
+ case_test_dir = os.path.join(TestCompo.test_dir, "salome_py")
+ os.mkdir(case_test_dir)
+
+ old_dir = os.getcwd()
+ os.chdir(case_test_dir)
+
+ # job script
+ script_file = "myScript.py"
+ job_script_file = os.path.join(case_test_dir, script_file)
+ script_text = """#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# verify import salome
+import salome
+salome.salome_init()
+
+f = open('result.txt', 'w')
+f.write("Salut!")
+f.close()
+
+import os
+os.mkdir("subdir")
+f = open(os.path.join("subdir",'autre.txt'), 'w')
+f.write("Hello!")
+f.close()
+"""
+ f = open(job_script_file, "w")
+ f.write(script_text)
+ f.close()
+
+ local_result_dir = os.path.join(case_test_dir, "result_simple_py_job")
+ job_params = salome.JobParameters()
+ job_params.job_name = "MyJob"
+ job_params.job_type = "python_salome"
+ job_params.job_file = job_script_file
+ job_params.in_files = []
+ job_params.out_files = ["result.txt", "subdir"]
+ job_params.result_directory = local_result_dir
+ job_params.resource_required = salome.ResourceParameters()
+ job_params.resource_required.nb_proc = 1
+
+ job_params.work_directory = "/tmp/job_salome_py" + self.suffix
+
+ launcher = salome.naming_service.Resolve('/SalomeLauncher')
+ job_id = launcher.createJob(job_params)
+
+ launcher.launchJob(job_id)
+
+ import time
+ jobState = launcher.getJobState(job_id)
+ print "Job %d state: %s" % (job_id,jobState)
+ while jobState != "FINISHED" and jobState != "FAILED" :
+ time.sleep(5)
+ jobState = launcher.getJobState(job_id)
+ print "Job %d state: %s" % (job_id,jobState)
+ pass
+
+ self.assertEqual(jobState, "FINISHED")
+
+ # getJobResults to default directory (result_directory)
+ launcher.getJobResults(job_id, "")
+ try:
+ f = open(os.path.join(local_result_dir, "result.txt"), 'r')
+ text = f.read()
+ f.close()
+ self.assertEqual(text, "Salut!")
+ except IOError,ex:
+ self.fail("IO exception:" + str(ex));
+
+ try:
+ f = open(os.path.join(local_result_dir, "subdir", "autre.txt"), 'r')
+ text = f.read()
+ f.close()
+ self.assertEqual(text, "Hello!")
+ except IOError,ex:
+ self.fail("IO exception:" + str(ex));
+
+ # getJobResults to a specific directory
+ mydir = os.path.join(case_test_dir, "custom_result_dir")
+ launcher.getJobResults(job_id, mydir)
+ try:
+ f = open(os.path.join(mydir, "result.txt"), 'r')
+ text = f.read()
+ f.close()
+ self.assertEqual(text, "Salut!")
+ except IOError,ex:
+ self.fail("IO exception:" + str(ex));
+
+ try:
+ f = open(os.path.join(mydir, "subdir", "autre.txt"), 'r')
+ text = f.read()
+ f.close()
+ self.assertEqual(text, "Hello!")
+ except IOError,ex:
+ self.fail("IO exception:" + str(ex));
+ pass
+
+ os.chdir(old_dir)
+
+ ##############################
+ # test of command job type
+ ##############################
+ def test_command(self):
+ case_test_dir = os.path.join(TestCompo.test_dir, "command")
+ os.mkdir(case_test_dir)
+
+ # job script
+ data_file = "in.txt"
+ script_file = "myEnvScript.py"
+ script_text = """#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import os,sys
+
+text_result = os.getenv("ENV_TEST_VAR","")
+
+f = open('result.txt', 'w')
+f.write(text_result)
+f.close()
+
+in_f = open("in.txt", "r")
+in_text = in_f.read()
+in_f.close()
+
+os.mkdir("subdir")
+f = open(os.path.join("subdir",'altul.txt'), 'w')
+f.write(in_text)
+f.close()
+"""
+ abs_script_file = os.path.join(case_test_dir, script_file)
+ f = open(abs_script_file, "w")
+ f.write(script_text)
+ f.close()
+ os.chmod(abs_script_file, 0o755)
+
+ #environement script
+ env_file = "myEnv.sh"
+ env_text = """export ENV_TEST_VAR="expected"
+"""
+ f = open(os.path.join(case_test_dir, env_file), "w")
+ f.write(env_text)
+ f.close()
+
+ # write data file
+ f = open(os.path.join(case_test_dir, data_file), "w")
+ f.write("expected data")
+ f.close()
+
+ # job params
+ local_result_dir = os.path.join(case_test_dir, "resultats_env")
+ job_params = salome.JobParameters()
+ job_params.job_name = "CommandJob"
+ job_params.job_type = "command"
+ job_params.job_file = script_file
+ job_params.env_file = env_file
+ job_params.in_files = [data_file]
+ job_params.out_files = ["result.txt", "subdir"]
+ job_params.local_directory = case_test_dir
+ job_params.result_directory = local_result_dir
+ job_params.resource_required = salome.ResourceParameters()
+ job_params.resource_required.nb_proc = 1
+ job_params.work_directory = "/tmp/command_job" + self.suffix
+
+ # create and launch the job
+ launcher = salome.naming_service.Resolve('/SalomeLauncher')
+ job_id = launcher.createJob(job_params)
+ launcher.launchJob(job_id)
+
+ # wait for the end of the job
+ import time
+ jobState = launcher.getJobState(job_id)
+ print "Job %d state: %s" % (job_id,jobState)
+ while jobState != "FINISHED" and jobState != "FAILED" :
+ time.sleep(3)
+ jobState = launcher.getJobState(job_id)
+ print "Job %d state: %s" % (job_id,jobState)
+ pass
+
+ # verify the results
+ self.assertEqual(jobState, "FINISHED")
+ launcher.getJobResults(job_id, "")
+ try:
+ f = open(os.path.join(local_result_dir, "result.txt"), 'r')
+ text = f.read()
+ f.close()
+ self.assertEqual(text, "expected")
+ except IOError,ex:
+ self.fail("IO exception:" + str(ex));
+
+ ##############################
+ # test of yacs job type
+ ##############################
+ def test_yacs(self):
+ yacs_path = os.getenv("YACS_ROOT_DIR", "")
+ if not os.path.isdir(yacs_path):
+ self.skipTest("Needs YACS module to run. Please define YACS_ROOT_DIR.")
+
+ case_test_dir = os.path.join(TestCompo.test_dir, "yacs")
+ os.mkdir(case_test_dir)
+
+ #environement script
+ env_file = "myEnv.sh"
+ env_text = """export ENV_TEST_VAR="expected"
+"""
+ f = open(os.path.join(case_test_dir, env_file), "w")
+ f.write(env_text)
+ f.close()
+
+ # job script
+ script_text = """<?xml version='1.0' encoding='iso-8859-1' ?>
+<proc name="newSchema_1">
+ <property name="DefaultStudyID" value="1"/>
+ <container name="DefaultContainer">
+ <property name="container_kind" value="Salome"/>
+ <property name="attached_on_cloning" value="0"/>
+ <property name="container_name" value="FactoryServer"/>
+ <property name="name" value="localhost"/>
+ </container>
+ <inline name="PyScript0">
+ <script><code><![CDATA[import os
+text_result = os.getenv("ENV_TEST_VAR","")
+f = open('result.txt', 'w')
+f.write(text_result)
+f.close()
+]]></code></script>
+ <load container="DefaultContainer"/>
+ </inline>
+</proc>
+"""
+ yacs_file = "mySchema.xml"
+ job_script_file = os.path.join(case_test_dir, yacs_file)
+ f = open(job_script_file, "w")
+ f.write(script_text)
+ f.close()
+
+ local_result_dir = os.path.join(case_test_dir, "result_yacs_job")
+ job_params = salome.JobParameters()
+ job_params.job_name = "MyYacsJob"
+ job_params.job_type = "yacs_file"
+ job_params.job_file = job_script_file
+ job_params.env_file = os.path.join(case_test_dir,env_file)
+ #job_params.in_files = [case_test_dir]
+ job_params.out_files = ["result.txt"]
+ job_params.result_directory = local_result_dir
+ job_params.resource_required = salome.ResourceParameters()
+ job_params.resource_required.nb_proc = 1
+
+ job_params.work_directory = "/tmp/job_yacs" + self.suffix
+
+ launcher = salome.naming_service.Resolve('/SalomeLauncher')
+ job_id = launcher.createJob(job_params)
+
+ launcher.launchJob(job_id)
+
+ import time
+ jobState = launcher.getJobState(job_id)
+ print "Job %d state: %s" % (job_id,jobState)
+ while jobState != "FINISHED" and jobState != "FAILED" :
+ time.sleep(5)
+ jobState = launcher.getJobState(job_id)
+ print "Job %d state: %s" % (job_id,jobState)
+ pass
+
+ self.assertEqual(jobState, "FINISHED")
+
+ # getJobResults to default directory (result_directory)
+ launcher.getJobResults(job_id, "")
+ try:
+ f = open(os.path.join(local_result_dir, "result.txt"), 'r')
+ text = f.read()
+ f.close()
+ self.assertEqual(text, "expected")
+ except IOError,ex:
+ self.fail("IO exception:" + str(ex));
+
+if __name__ == '__main__':
+ # creat study
+ import salome
+ salome.salome_init()
+ unittest.main()
+
+ # load catalogs
+# mc = salome.naming_service.Resolve('/Kernel/ModulCatalog')
+# ior = salome.orb.object_to_string(mc)
+# import SALOMERuntime
+# SALOMERuntime.RuntimeSALOME_setRuntime()
+# salome_runtime = SALOMERuntime.getSALOMERuntime()
+# session_catalog = salome_runtime.loadCatalog("session", ior)
+# salome_runtime.addCatalog(session_catalog)
+
--- /dev/null
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+
+usage="""
+This script prepares the test environment and runs a test script:
+ - clean and create test directory
+ - create a SALOME application
+ - launch salome
+ - launch the test script within SALOME environment
+ - kill salome
+
+ This script uses the following environment variables:
+ - ROOT_SALOME : directory which contains salome_context.cfg.
+ This variable is usually defined in salome_prerequisites.sh
+ - KERNEL_ROOT_DIR and YACS_ROOT_DIR : directories of modules installation
+ Those variables are usually defined in salome_modules.sh
+ Environment variables can be passed to the script using the -d option.
+"""
+
+import os
+import sys
+
+class TestEnvironment:
+ def setUp(self):
+ import shutil
+ shutil.rmtree("appli", ignore_errors=True)
+
+ # create config_appli.xml in current directory
+ salome_path = os.getenv("ROOT_SALOME", "")
+ salome_context_file = os.path.join(salome_path, "salome_context.cfg")
+ if not os.path.isfile(salome_context_file):
+ print "File salome_context.cfg not found."
+ print "Search path:" + salome_path
+ print "This test needs ROOT_SALOME environment variable in order to run"
+ exit(1)
+
+ config_appli_text = '''<application>
+<context path="''' + salome_context_file + '''"/>
+<modules>
+ <module name="KERNEL" path="'''
+ kernel_path = os.getenv("KERNEL_ROOT_DIR", "")
+ if not os.path.isdir(kernel_path) :
+ print "KERNEL_ROOT_DIR not defined"
+ exit(1)
+ pass
+
+ config_appli_text += kernel_path + '"/>'
+
+ # some tests need YACS module.
+ yacs_path = os.getenv("YACS_ROOT_DIR", "")
+ if os.path.isdir(yacs_path):
+ cls.hasYacs = True
+ config_appli_text += '''
+ <module name="YACS" path="'''
+ config_appli_text += yacs_path + '"/>'
+ pass
+ config_appli_text += '''
+</modules>
+</application>'''
+
+ f = open("config_appli.xml", 'w')
+ f.write(config_appli_text)
+ f.close()
+
+ # create a SALOME application
+ appli_gen_file = os.path.join(kernel_path,
+ "bin","salome","appli_gen.py")
+ appli_dir = "appli"
+ os.system(appli_gen_file + " --prefix="+appli_dir+
+ " --config=config_appli.xml")
+
+ # start salome
+ import imp
+ sys.path[:0] = [os.path.join(appli_dir, "bin", "salome", "appliskel")]
+ self.salome_module = imp.load_source("SALOME", os.path.join(appli_dir, "salome"))
+ try:
+ self.salome_module.main(["start", "-t"])
+ except SystemExit, e:
+ # There is an exit() call in salome.main. Just ignore it.
+ pass
+
+ def run(self, script):
+ ret = 0
+ try:
+ ret = self.salome_module.main(["shell", script])
+ except SystemExit, e:
+ # return exit value
+ ret = e.code
+ return ret
+
+ def tearDown(self):
+ try:
+ self.salome_module.main(["killall"])
+ except SystemExit, e:
+ pass
+ pass
+ pass #class TestEnvironment
+
+if __name__ == '__main__':
+ import argparse
+ parser = argparse.ArgumentParser(description=usage,
+ formatter_class=argparse.RawDescriptionHelpFormatter)
+ parser.add_argument('command', help="Test command to be run.")
+ parser.add_argument('-d', '--define', action='append', help="VARIABLE=VALUE")
+
+ args = parser.parse_args()
+ for opt in args.define:
+ opts = opt.split('=', 1)
+ os.environ[opts[0]] = opts[1]
+
+ envTest = TestEnvironment()
+ envTest.setUp()
+ ret = envTest.run(args.command)
+ envTest.tearDown()
+ exit(ret)
+ pass
+