configurationengine/source/scripts/tests/unittest_compare.py
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     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 """
       
    18 Test the configuration
       
    19 """
       
    20 import unittest
       
    21 import string
       
    22 import sys
       
    23 import os
       
    24 import shutil
       
    25 import subprocess
       
    26 import difflib
       
    27 import __init__
       
    28 from testautomation.base_testcase import BaseTestCase
       
    29 from scripttest_common import get_cmd
       
    30 
       
    31 
       
    32 ROOT_PATH       = os.path.dirname(os.path.abspath(__file__))
       
    33 TESTDATA_DIR    = os.path.normpath(os.path.join(ROOT_PATH, 'testdata/compare'))
       
    34 TEMP_DIR        = os.path.normpath(os.path.join(ROOT_PATH, 'temp/compare'))
       
    35 
       
    36 from cone.public import api
       
    37 
       
    38 #import conesub_compare
       
    39 class TestCompareAction(BaseTestCase):
       
    40     
       
    41     def setUp(self):
       
    42         if not os.path.exists(TEMP_DIR):
       
    43             os.makedirs(TEMP_DIR)
       
    44     
       
    45     #def test_parse_target_configuration(self):
       
    46     #    act = conesub_compare.CompareAction('','','')
       
    47     #    self.assertEquals(act.parse_target_configuration("root.confml"), ('','root.confml'))
       
    48     #    self.assertEquals(act.parse_target_configuration("x:\foo.cpf;root.confml"), ('x:\foo.cpf','root.confml'))
       
    49     #    self.assertEquals(act.parse_target_configuration("root\project;root.confml"), ('root\project','root.confml'))
       
    50     #    self.assertEquals(act.parse_target_configuration("http://testserver:8000/external;foo/bar/root.confml"), ('http://testserver:8000/external','foo/bar/root.confml'))
       
    51 
       
    52     def test_default_compare(self):
       
    53         # Test comparison using default parameters
       
    54         orig_workdir = os.getcwd()
       
    55         os.chdir(os.path.join(TEMP_DIR))
       
    56         try:
       
    57             project = os.path.join(TESTDATA_DIR, "project1.zip")
       
    58             source_conf = "root1.confml"
       
    59             target_conf = "root2.confml"
       
    60             
       
    61             report_file = os.path.join(TEMP_DIR, "data_comparison.html")
       
    62             self.remove_if_exists(report_file)
       
    63             
       
    64             command = '%s -p "%s" -s "%s" -t "%s"' \
       
    65                 % (get_cmd('compare'), project, source_conf, target_conf)
       
    66             self.run_command(command)
       
    67             self.assert_exists_and_contains_something(report_file)
       
    68         finally:
       
    69             os.chdir(orig_workdir)
       
    70 
       
    71     def test_compare_api_same_root(self):
       
    72         self._run_comparison_test(
       
    73             source_project  = 'project1.zip',
       
    74             source_conf     = 'root1.confml',
       
    75             target_project  = None,
       
    76             target_conf     = 'root1.confml',
       
    77             template        = 'api_template.txt',
       
    78             report_file     = 'api_p1r1_vs_p1r1.txt',
       
    79             check_against_expected_output = True)
       
    80     
       
    81     def test_compare_data_same_root(self):
       
    82         self._run_comparison_test(
       
    83             source_project  = 'project1.zip',
       
    84             source_conf     = 'root1.confml',
       
    85             target_project  = None,
       
    86             target_conf     = 'root1.confml',
       
    87             template        = 'data_template.txt',
       
    88             report_file     = 'data_p1r1_vs_p1r1.txt',
       
    89             check_against_expected_output = True)
       
    90     
       
    91     def test_compare_api_same_project_different_root(self):
       
    92         self._run_comparison_test(
       
    93             source_project  = 'project1.zip',
       
    94             source_conf     = 'root1.confml',
       
    95             target_project  = None,
       
    96             target_conf     = 'root4.confml',
       
    97             template        = 'api_template.txt',
       
    98             report_file     = 'api_p1r1_vs_p1r4.txt',
       
    99             check_against_expected_output = True)
       
   100     
       
   101     def test_compare_data_same_project_different_root(self):
       
   102         self._run_comparison_test(
       
   103             source_project  = 'project1.zip',
       
   104             source_conf     = 'root1.confml',
       
   105             target_project  = None,
       
   106             target_conf     = 'root4.confml',
       
   107             template        = 'data_template.txt',
       
   108             report_file     = 'data_p1r1_vs_p1r4.txt',
       
   109             check_against_expected_output = True)
       
   110     
       
   111     def test_compare_api_proj1_vs_proj2(self):
       
   112         self._run_comparison_test(
       
   113             source_project  = 'project1.zip',
       
   114             source_conf     = 'root1.confml',
       
   115             target_project  = 'project2.zip',
       
   116             target_conf     = 'root1.confml',
       
   117             template        = 'api_template.txt',
       
   118             report_file     = 'api_p1r1_vs_p2r1.txt',
       
   119             check_against_expected_output = True)
       
   120     
       
   121     def test_compare_data_proj1_vs_proj2(self):
       
   122         self._run_comparison_test(
       
   123             source_project  = 'project1.zip',
       
   124             source_conf     = 'root1.confml',
       
   125             target_project  = 'project2.zip',
       
   126             target_conf     = 'root1.confml',
       
   127             template        = 'data_template.txt',
       
   128             report_file     = 'data_p1r1_vs_p2r1.txt',
       
   129             check_against_expected_output = True)
       
   130     
       
   131     def test_comparison_using_type_api(self):
       
   132         self._run_comparison_test(
       
   133             source_project  = 'project1.zip',
       
   134             source_conf     = 'root1.confml',
       
   135             target_project  = None,
       
   136             target_conf     = 'root4.confml',
       
   137             report_type     = 'api',
       
   138             report_file     = 'api_p1r1_vs_p1r4.html',
       
   139             check_against_expected_output = False)
       
   140     
       
   141     def test_comparison_using_type_data(self):
       
   142         self._run_comparison_test(
       
   143             source_project  = 'project1.zip',
       
   144             source_conf     = 'root1.confml',
       
   145             target_project  = None,
       
   146             target_conf     = 'root4.confml',
       
   147             report_type     = 'data',
       
   148             report_file     = 'data_p1r1_vs_p1r4.html',
       
   149             check_against_expected_output = False)
       
   150 
       
   151     def _run_comparison_test(self, **kwargs):
       
   152         """
       
   153         Run comparison test.
       
   154         
       
   155         @param source_project: The source project, relative to the test data directory or an
       
   156             absolute path.
       
   157         @param source_conf: The source configuration.
       
   158         @param target_project: The target project, relative to the test data directory
       
   159             If not given or None, the source project will be used also for this.
       
   160         @param target_conf: The target configuration.
       
   161         @param template: The template file used for the report, relative to the test data directory.
       
   162         @param report_type: The report type. Should not be used with the 'template' parameter.
       
   163         @param report_file: The location where the report is written. This will also be used as
       
   164             the name of the expected report file against which the actual report is checked.
       
   165         @param impl_filter: Implementation filter to use.
       
   166         @param check_against_expected_output: If True, the actual report is checked against an
       
   167             expected file with the same name. Otherwise it is just checked that the output
       
   168             file has been created and it contains something.
       
   169         """
       
   170         # Get parameters
       
   171         # ---------------
       
   172         def get_project_absdir(project_dir):
       
   173             if os.path.isabs(project_dir):
       
   174                 return project_dir
       
   175             else:
       
   176                 return os.path.normpath(os.path.join(TESTDATA_DIR, project_dir))
       
   177         
       
   178         source_conf = kwargs['source_conf']
       
   179         target_conf = kwargs['target_conf']
       
   180         source_project = get_project_absdir(kwargs['source_project'])
       
   181         target_project = kwargs.get('target_project', None)
       
   182         
       
   183         if target_project != None:
       
   184             target_project = get_project_absdir(target_project)
       
   185             target_conf = target_project + ';' + target_conf
       
   186         
       
   187         template = kwargs.get('template', None)
       
   188         report_type = kwargs.get('report_type', None)
       
   189         if template and report_type:
       
   190             raise ValueError("Both 'template' and 'report_type' parameters given")
       
   191         elif not template and not report_type:
       
   192             raise ValueError("Neither 'template' not 'report_type' parameter given")
       
   193         elif template:
       
   194             template = os.path.normpath(os.path.join(TESTDATA_DIR, template))
       
   195         
       
   196         report_file = kwargs['report_file']
       
   197         check_against_expected_output = kwargs['check_against_expected_output']
       
   198         actual_report = os.path.normpath(os.path.join(TEMP_DIR, report_file))
       
   199         
       
   200         impl_filter = kwargs.get('impl_filter', None)
       
   201         
       
   202         
       
   203         # Generate output
       
   204         # ----------------
       
   205         if report_type:
       
   206             command = '%s -p "%s" -s "%s" -t "%s" --report-type "%s" --report "%s"' \
       
   207                 % (get_cmd('compare'), source_project, source_conf, target_conf, report_type, actual_report)
       
   208         else:
       
   209             command = '%s -p "%s" -s "%s" -t "%s" --template "%s" --report "%s"' \
       
   210                 % (get_cmd('compare'), source_project, source_conf, target_conf, template, actual_report)
       
   211         
       
   212         if impl_filter:
       
   213             command += ' --impl-filter "%s"' % impl_filter
       
   214         
       
   215         self.remove_if_exists(actual_report)
       
   216         self.run_command(command)
       
   217         
       
   218         
       
   219         # Check output
       
   220         # -------------
       
   221         if check_against_expected_output:
       
   222             expected_report = os.path.normpath(os.path.join(TESTDATA_DIR, 'expected', report_file))
       
   223             self.assert_file_contents_equal(expected_report, actual_report)
       
   224         else:
       
   225             self.assert_exists_and_contains_something(actual_report)
       
   226         
       
   227         
       
   228 if __name__ == '__main__':
       
   229       unittest.main()