|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 |
|
17 import unittest |
|
18 import os, shutil |
|
19 import sys |
|
20 import logging |
|
21 import __init__ |
|
22 |
|
23 from cone.public import exceptions,plugin,api,container |
|
24 from cone.storage import filestorage |
|
25 from ruleplugin import ruleml |
|
26 from testautomation.base_testcase import BaseTestCase |
|
27 |
|
28 # Hardcoded value of testdata folder that must be under the current working dir |
|
29 ROOT_PATH = os.path.dirname(os.path.abspath(__file__)) |
|
30 testdata = os.path.join(ROOT_PATH,'errorruleproject') |
|
31 |
|
32 class TestErrorReporting(BaseTestCase): |
|
33 def setUp(self): |
|
34 pass |
|
35 |
|
36 def tearDown(self): |
|
37 pass |
|
38 |
|
39 def _prepare_workdir(self, workdir): |
|
40 workdir = os.path.join(ROOT_PATH, workdir) |
|
41 self.recreate_dir(workdir) |
|
42 return workdir |
|
43 |
|
44 def _prepare_log(self, log_file, level=logging.DEBUG, formatter="%(levelname)s - %(name)s - %(message)s", logger='cone'): |
|
45 FULL_PATH = os.path.join(ROOT_PATH, "temp", log_file) |
|
46 self.remove_if_exists(FULL_PATH) |
|
47 self.create_dir_for_file_path(FULL_PATH) |
|
48 |
|
49 handler = logging.FileHandler(FULL_PATH) |
|
50 handler.setLevel(level) |
|
51 frm = logging.Formatter(formatter) |
|
52 handler.setFormatter(frm) |
|
53 logger = logging.getLogger(logger) |
|
54 logger.addHandler(handler) |
|
55 |
|
56 return [FULL_PATH, handler, logger] |
|
57 |
|
58 def _execute_rules(self, project_location): |
|
59 project = api.Project(api.Storage.open(os.path.join(ROOT_PATH, project_location))) |
|
60 config = project.get_configuration('root.confml') |
|
61 |
|
62 implcontainer = plugin.get_impl_set(config, r'\.ruleml$') |
|
63 implcontainer.get_relation_container().execute() |
|
64 lastconfig = config.get_last_configuration() |
|
65 project.close() |
|
66 |
|
67 def test_terminal_expression_repr(self): |
|
68 log_file, handler, logger = self._prepare_log('test1.log') |
|
69 self._execute_rules('errorruleproject/test1') |
|
70 logger.removeHandler(handler) |
|
71 |
|
72 self.assert_file_does_not_contain(log_file, "<cone.public.rules.TerminalExpression object at") |
|
73 |
|
74 def test_invalid_python_code_eval(self): |
|
75 log_file, handler, logger = self._prepare_log('test2.log') |
|
76 self._execute_rules('errorruleproject/test2') |
|
77 logger.removeHandler(handler) |
|
78 self.assert_file_contains(log_file, "INFO - cone.ruleml - Set u'EvalTest2.StringResult' = None from '-> this is invalid python code'") |
|
79 self.assert_file_contains(log_file, "WARNING - cone.ruleml - Invalid syntax in eval: -> this is invalid python code") |
|
80 self.assert_file_contains(log_file, "ERROR - cone.ruleml_relation_container(implml/invalid_python_eval.ruleml) - '-> this is invalid python code'") |
|
81 |
|
82 def test_invalid_python_code_eval_globals(self): |
|
83 log_file, handler, logger = self._prepare_log('test3.log') |
|
84 self._execute_rules('errorruleproject/test3') |
|
85 logger.removeHandler(handler) |
|
86 self.assert_file_contains(log_file, "WARNING - cone.ruleml(implml/invalid_python_eval.ruleml) - Cannot import eval file: implml/scripts/test_eval.py. Exception: invalid syntax (<string>, line 17)") |
|
87 |
|
88 def test_invalid_file_reference_in_eval_globals_file_attribute(self): |
|
89 log_file, handler, logger = self._prepare_log('test4.log') |
|
90 self._execute_rules('errorruleproject/test4') |
|
91 logger.removeHandler(handler) |
|
92 self.assert_file_contains(log_file, "WARNING - cone.ruleml(implml/invalid_python_eval.ruleml) - Cannot import eval file: implml/scripts/not_valid_filename.py. Exception: implml/scripts/not_valid_filename.py, [Errno 2] No such file or directory:") |
|
93 self.assert_file_contains(log_file, '/implml/scripts/not_valid_filename.py') |
|
94 |
|
95 def test_runtime_error_when_running_an_eval_block_inside_rule(self): |
|
96 log_file, handler, logger = self._prepare_log('test5.log') |
|
97 self._execute_rules('errorruleproject/test5') |
|
98 logger.removeHandler(handler) |
|
99 |
|
100 self.assert_file_contains(log_file, "Execution failed for eval: 7/0 <type 'exceptions.ZeroDivisionError'>: integer division or modulo by zero") |
|
101 |
|
102 def test_references_non_existent_settings(self): |
|
103 log_file, handler, logger = self._prepare_log('test6.log') |
|
104 self._execute_rules('errorruleproject/test6') |
|
105 logger.removeHandler(handler) |
|
106 #self.assert_file_contains(log_file, "Execution failed for eval: 7/0 <type 'exceptions.ZeroDivisionError'>: integer division or modulo by zero") |
|
107 |
|
108 |
|
109 if __name__ == '__main__': |
|
110 unittest.main() |