|
1 # *-* coding: utf-8 *-* |
|
2 # |
|
3 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 # All rights reserved. |
|
5 # This component and the accompanying materials are made available |
|
6 # under the terms of "Eclipse Public License v1.0" |
|
7 # which accompanies this distribution, and is available |
|
8 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
9 # |
|
10 # Initial Contributors: |
|
11 # Nokia Corporation - initial contribution. |
|
12 # |
|
13 # Contributors: |
|
14 # |
|
15 # Description: |
|
16 # |
|
17 ## |
|
18 # @author Teemu Rytkonen |
|
19 |
|
20 import sys, os, shutil, unittest |
|
21 import __init__ |
|
22 from testautomation.base_testcase import BaseTestCase |
|
23 |
|
24 ROOT_PATH = os.path.abspath(os.path.dirname(__file__)) |
|
25 TEMP_DIR = os.path.normpath(os.path.join(ROOT_PATH, 'temp/crml_dc')) |
|
26 |
|
27 if sys.platform == "win32": |
|
28 CONE_SCRIPT = "cone.cmd" |
|
29 else: |
|
30 CONE_SCRIPT = "cone.sh" |
|
31 |
|
32 def get_cmd(action='compare'): |
|
33 """Return the command used to run the ConE sub-action""" |
|
34 if 'CONE_PATH' in os.environ: |
|
35 CONE_CMD = os.path.join(os.environ['CONE_PATH'], CONE_SCRIPT) |
|
36 if not os.path.exists(CONE_CMD): |
|
37 raise RuntimeError("'%s' does not exist!" % CONE_CMD) |
|
38 return '"%s" %s' % (CONE_CMD, action) |
|
39 else: |
|
40 SOURCE_ROOT = os.path.normpath(os.path.join(ROOT_PATH, '../../..')) |
|
41 assert os.path.split(SOURCE_ROOT)[1] == 'source' |
|
42 cmd = 'python "%s" %s' % (os.path.normpath(os.path.join(SOURCE_ROOT, 'scripts/cone_tool.py')), action) |
|
43 return cmd |
|
44 |
|
45 def get_crml_dc_testdata_dir(): |
|
46 # If running from the working copy |
|
47 dir1 = os.path.normpath(os.path.join(ROOT_PATH, '../ConeCRMLPlugin/CRMLPlugin/tests')) |
|
48 if os.path.isdir(dir1): return dir1 |
|
49 |
|
50 # If running from standalone |
|
51 dir2 = os.path.normpath(os.path.join(ROOT_PATH, 'testdata/compare/crml_dc')) |
|
52 if os.path.isdir(dir2): return dir2 |
|
53 |
|
54 raise RuntimeError("CRML DC test data found neither in '%s' nor '%s'!" % (dir1, dir2)) |
|
55 |
|
56 |
|
57 class TestCompareAction(BaseTestCase): |
|
58 |
|
59 def setUp(self): |
|
60 if not os.path.exists(TEMP_DIR): |
|
61 os.makedirs(TEMP_DIR) |
|
62 |
|
63 def _run_crml_dc_test(self, crml_file_name_without_extension, do_filtering=True): |
|
64 testdata_dir = get_crml_dc_testdata_dir() |
|
65 report_file = 'crml_dc_%s.csv' % crml_file_name_without_extension |
|
66 if do_filtering: |
|
67 impl_filter = '%s' % crml_file_name_without_extension |
|
68 else: |
|
69 impl_filter = '.*' |
|
70 self._run_comparison_test( |
|
71 source_project = os.path.join(testdata_dir, 'comp_project_1'), |
|
72 source_conf = 'root.confml', |
|
73 target_project = os.path.join(testdata_dir, 'comp_project_2'), |
|
74 target_conf = 'root.confml', |
|
75 report_type = 'crml_dc_csv', |
|
76 report_file = report_file, |
|
77 impl_filter = impl_filter, |
|
78 check_against_expected_output = True) |
|
79 |
|
80 def test_crml_dc_00000001_simple_keys(self): self._run_crml_dc_test('00000001_simple_keys') |
|
81 def test_crml_dc_00000002_bitmask_keys(self): self._run_crml_dc_test('00000002_bitmask_keys') |
|
82 def test_crml_dc_00000003_key_ranges(self): self._run_crml_dc_test('00000003_key_ranges') |
|
83 def test_crml_dc_00000004_key_type_changed(self): self._run_crml_dc_test('00000004_key_type_changed') |
|
84 def test_crml_dc_00000005_repo_attrs_changed(self): self._run_crml_dc_test('00000005_repo_attrs_changed') |
|
85 def test_crml_dc_10000001_removed_repo(self): self._run_crml_dc_test('10000001_removed_repo') |
|
86 def test_crml_dc_20000001_added_repo(self): self._run_crml_dc_test('20000001_added_repo') |
|
87 def test_crml_dc_00000006_renamed_repo(self): self._run_crml_dc_test('00000006_renamed_repo') |
|
88 def test_crml_dc_30000000_duplicate_repo(self): self._run_crml_dc_test('30000000_duplicate_repo') |
|
89 def test_crml_dc_all(self): self._run_crml_dc_test('all', do_filtering=False) |
|
90 |
|
91 def test_crml_dc_html_report(self): |
|
92 testdata_dir = get_crml_dc_testdata_dir() |
|
93 |
|
94 # Ignore the portion of the data where the path to the target is shown |
|
95 ignore_patterns = [r'<tr>\s*<td>Target:</td>\s*<td>.*[\\,/]comp_project_2;root.confml</td>'] |
|
96 |
|
97 self._run_comparison_test( |
|
98 source_project = os.path.join(testdata_dir, 'comp_project_1'), |
|
99 source_conf = 'root.confml', |
|
100 target_project = os.path.join(testdata_dir, 'comp_project_2'), |
|
101 target_conf = 'root.confml', |
|
102 report_type = 'crml_dc', |
|
103 report_file = 'crml_dc.html', |
|
104 check_against_expected_output = True, |
|
105 data_ignore_patterns = ignore_patterns) |
|
106 |
|
107 def _run_comparison_test(self, **kwargs): |
|
108 """ |
|
109 Run comparison test. |
|
110 |
|
111 @param source_project: The source project, relative to the test data directory or an |
|
112 absolute path. |
|
113 @param source_conf: The source configuration. |
|
114 @param target_project: The target project, relative to the test data directory |
|
115 If not given or None, the source project will be used also for this. |
|
116 @param target_conf: The target configuration. |
|
117 @param template: The template file used for the report, relative to the test data directory. |
|
118 @param report_type: The report type. Should not be used with the 'template' parameter. |
|
119 @param report_file: The location where the report is written. This will also be used as |
|
120 the name of the expected report file against which the actual report is checked. |
|
121 @param impl_filter: Implementation filter to use. |
|
122 @param check_against_expected_output: If True, the actual report is checked against an |
|
123 expected file with the same name. Otherwise it is just checked that the output |
|
124 file has been created and it contains something. |
|
125 @param data_ignore_patterns: List of regular expression patterns for ignoring some portions |
|
126 of the data when checking against expected output. The patterns are used to remove |
|
127 data portions before doing the actual comparison. |
|
128 """ |
|
129 # Get parameters |
|
130 # --------------- |
|
131 def get_project_absdir(project_dir): |
|
132 if os.path.isabs(project_dir): |
|
133 return project_dir |
|
134 else: |
|
135 return os.path.normpath(os.path.join(TESTDATA_DIR, project_dir)) |
|
136 |
|
137 source_conf = kwargs['source_conf'] |
|
138 target_conf = kwargs['target_conf'] |
|
139 source_project = get_project_absdir(kwargs['source_project']) |
|
140 target_project = kwargs.get('target_project', None) |
|
141 |
|
142 if target_project != None: |
|
143 target_project = get_project_absdir(target_project) |
|
144 target_conf = target_project + ';' + target_conf |
|
145 |
|
146 template = kwargs.get('template', None) |
|
147 report_type = kwargs.get('report_type', None) |
|
148 if template and report_type: |
|
149 raise ValueError("Both 'template' and 'report_type' parameters given") |
|
150 elif not template and not report_type: |
|
151 raise ValueError("Neither 'template' not 'report_type' parameter given") |
|
152 elif template: |
|
153 template = os.path.normpath(os.path.join(TESTDATA_DIR, template)) |
|
154 |
|
155 report_file = kwargs['report_file'] |
|
156 check_against_expected_output = kwargs['check_against_expected_output'] |
|
157 actual_report = os.path.normpath(os.path.join(TEMP_DIR, report_file)) |
|
158 |
|
159 impl_filter = kwargs.get('impl_filter', None) |
|
160 |
|
161 |
|
162 # Generate output |
|
163 # ---------------- |
|
164 if report_type: |
|
165 command = '%s -p "%s" -s "%s" -t "%s" --report-type "%s" --report "%s"' \ |
|
166 % (get_cmd(), source_project, source_conf, target_conf, report_type, actual_report) |
|
167 else: |
|
168 command = '%s -p "%s" -s "%s" -t "%s" --template "%s" --report "%s"' \ |
|
169 % (get_cmd(), source_project, source_conf, target_conf, template, actual_report) |
|
170 |
|
171 if impl_filter: |
|
172 command += ' --impl-filter "%s"' % impl_filter |
|
173 |
|
174 self.remove_if_exists(actual_report) |
|
175 self.run_command(command) |
|
176 |
|
177 |
|
178 # Check output |
|
179 # ------------- |
|
180 if check_against_expected_output: |
|
181 expected_report = os.path.normpath(os.path.join(ROOT_PATH, 'testdata/crml_dc_expected', report_file)) |
|
182 ignore_patterns = kwargs.get('data_ignore_patterns', []) |
|
183 self.assert_file_contents_equal(expected_report, actual_report, ignore_patterns) |
|
184 else: |
|
185 self.assert_exists_and_contains_something(actual_report) |
|
186 |
|
187 |
|
188 if __name__ == '__main__': |
|
189 unittest.main() |