コード例 #1
0
    def testShutdownTerminateStopAll(self):
        """Tests that --stop-all kills all servers on the host."""

        # NOTE: Yet again keep the lexicographical flow, no renames!

        test_cfg = env.import_test_cfg(self._test_workspace)
        codechecker_1 = test_cfg['codechecker_1']
        codechecker_2 = test_cfg['codechecker_2']
        EVENT_1.clear()
        EVENT_2.clear()
        start_server(codechecker_1, EVENT_1, ['--skip-db-cleanup'])
        start_server(codechecker_2, EVENT_2, ['--skip-db-cleanup'])

        self.assertEqual(len(instance_manager.get_instances(self.home)), 2,
                         "Two servers were started but they don't appear "
                         "in the instance list.")

        # Kill the servers via cmdline.
        self.assertEqual(0, self.run_cmd([env.codechecker_cmd(),
                                          'server', '--stop-all']),
                         "The stop-all command didn't return exit code 0.")

        self.assertEqual(len(instance_manager.get_instances(self.home)), 0,
                         "Both servers were allegedly stopped but they "
                         "did not disappear.")
コード例 #2
0
ファイル: template_test.py プロジェクト: Ericsson/codechecker
    def setUp(self):
        """
        WARNING!!!
        This is an example how to get the configurations needed by the tests.
        WARNING!!!
        """

        # TEST_WORKSPACE is automatically set by test package __init__.py .
        test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + test_workspace)

        # Get the test configuration from the prepared int the test workspace.
        test_cfg = env.import_test_cfg(test_workspace)

        # Get the test project configuration from the prepared test workspace.
        self._testproject_data = env.setup_test_proj_cfg(test_workspace)
        self.assertIsNotNone(self._testproject_data)

        # Setup a viewer client to test viewer API calls.
        self._cc_client = env.setup_viewer_client(test_workspace)
        self.assertIsNotNone(self._cc_client)

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()

        # Get the run names which belong to this test.
        run_names = env.get_run_names(test_workspace)

        runs = self._cc_client.getRunData(None)
        test_runs = [run for run in runs if run.name in run_names]
コード例 #3
0
ファイル: test_ctu.py プロジェクト: Ericsson/codechecker
    def setUp(self):
        """ Set up workspace."""

        # TEST_WORKSPACE is automatically set by test package __init__.py .
        self.test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + self.test_workspace)

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()
        self.env = env.codechecker_env()
        self.report_dir = os.path.join(self.test_workspace, 'reports')
        os.makedirs(self.report_dir)
        self.test_dir = os.path.join(os.path.dirname(__file__), 'test_files')

        # Get if clang is CTU-capable or not.
        cmd = [self._codechecker_cmd, 'analyze', '-h']
        output, _ = call_command(cmd, cwd=self.test_dir, env=self.env)
        self.ctu_capable = '--ctu-' in output
        print("'analyze' reported CTU-compatibility? " + str(self.ctu_capable))

        # Fix the "template" build JSONs to contain a proper directory
        # so the tests work.
        raw_buildlog = os.path.join(self.test_dir, 'buildlog.json')
        with open(raw_buildlog) as log_file:
            build_json = json.load(log_file)
            for command in build_json:
                command['directory'] = self.test_dir

        self.__old_pwd = os.getcwd()
        os.chdir(self.test_workspace)
        self.buildlog = os.path.join(self.test_workspace, 'buildlog.json')
        with open(self.buildlog, 'w') as log_file:
            json.dump(build_json, log_file)
コード例 #4
0
    def setUp(self):

        # Get the test workspace.
        self.test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + self.test_workspace)

        self._codechecker_cfg = env.import_codechecker_cfg(self.test_workspace)
        self._reports_dir = self._codechecker_cfg['reportdir']

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()
        self._test_dir = os.path.join(self.test_workspace, 'test_files')
        self._product_name = self._codechecker_cfg['viewer_product']
        self._analyzer_stats_dir = os.path.join(self.test_workspace,
                                                'analysis_statistics')
        try:
            os.makedirs(self._test_dir)
        except os.error:
            # Directory already exists.
            pass

        # Setup a viewer client to test viewer API calls.
        self._cc_client = env.setup_viewer_client(self.test_workspace)
        self.assertIsNotNone(self._cc_client)

        # Change working dir to testfile dir so CodeChecker can be run easily.
        self.__old_pwd = os.getcwd()
        os.chdir(self._test_dir)

        self._source_file = "main.cpp"

        # Init project dir.
        makefile_content = "all:\n\t$(CXX) -c main.cpp -o /dev/null\n"
        project_info_content = {
            "name": "hello",
            "clean_cmd": "",
            "build_cmd": "make"
        }

        makefile = os.path.join(self._test_dir, 'Makefile')
        with open(makefile, 'w') as make_f:
            make_f.write(makefile_content)

        project_info = os.path.join(self._test_dir, 'project_info.json')
        with open(project_info, 'w') as info_f:
            json.dump(project_info_content, info_f)

        self.sources = ["""
int main()
{
  return 1 / 0; // Division by zero
}""", """
int main()
{
  return 0;
  xxx // Will cause a compilation error
}"""]
コード例 #5
0
    def setUp(self):
        """ Set up workspace."""

        # TEST_WORKSPACE is automatically set by test package __init__.py .
        self.test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + self.test_workspace)

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()
        self.env = env.codechecker_env()
        self.report_dir = os.path.join(self.test_workspace, 'reports')
        os.makedirs(self.report_dir)
コード例 #6
0
ファイル: test_analyze.py プロジェクト: Ericsson/codechecker
    def setUp(self):

        # TEST_WORKSPACE is automatically set by test package __init__.py .
        self.test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + self.test_workspace)

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()
        self.report_dir = os.path.join(self.test_workspace, "reports")
        self.test_dir = os.path.join(os.path.dirname(__file__), 'test_files')
        # Change working dir to testfile dir so CodeChecker can be run easily.
        self.__old_pwd = os.getcwd()
        os.chdir(self.test_dir)
コード例 #7
0
ファイル: test_ssl.py プロジェクト: Ericsson/codechecker
    def test_nonauth_storage(self):
        """
        Storing the result should fail.
        Authentication is required by the server but before the
        store command there was no login so storing the report should fail.
        """

        test_dir = os.path.dirname(os.path.realpath(__file__))
        report_file = os.path.join(test_dir, 'clang-5.0-trunk.plist')

        codechecker_cfg = self._test_cfg['codechecker_cfg']

        store_cmd = [env.codechecker_cmd(), 'store', '--name', 'auth',
                     # Use the 'Default' product.
                     '--url', env.parts_to_url(codechecker_cfg),
                     report_file]

        with self.assertRaises(subprocess.CalledProcessError):
            subprocess.check_output(store_cmd)
コード例 #8
0
ファイル: test_diff.py プロジェクト: Ericsson/codechecker
    def setUp(self):
        test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + test_workspace)

        # Get the clang version which is tested.
        self._clang_to_test = env.clang_to_test()

        self._testproject_data = env.setup_test_proj_cfg(test_workspace)
        self.assertIsNotNone(self._testproject_data)

        self._cc_client = env.setup_viewer_client(test_workspace)
        self.assertIsNotNone(self._cc_client)

        # Get the run names which belong to this test.
        run_names = env.get_run_names(test_workspace)

        runs = self._cc_client.getRunData(None)

        test_runs = [run for run in runs if run.name in run_names]
        for r in test_runs:
            print(r)

        # There should be at least two runs for this test.
        self.assertIsNotNone(runs)
        self.assertNotEqual(len(runs), 0)
        self.assertGreaterEqual(len(runs), 2)

        # Name order matters from __init__ !
        self._base_runid = test_runs[0].runId  # base
        self._new_runid = test_runs[1].runId  # new
        self._update_runid = test_runs[2].runId  # updated

        self._codechecker_cmd = env.codechecker_cmd()
        self._report_dir = os.path.join(test_workspace, "reports")
        self._report_dir_baseline = os.path.join(test_workspace,
                                                 "reports_baseline")
        self._test_config = env.import_test_cfg(test_workspace)
        self._run_names = env.get_run_names(test_workspace)
        self._html_reports = os.path.join(test_workspace, "html_reports")

        self._url = env.parts_to_url(self._test_config['codechecker_cfg'])
コード例 #9
0
    def setUp(self):

        # TEST_WORKSPACE is automatically set by test package __init__.py .
        self.test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + self.test_workspace)

        # Get the test project configuration from the prepared test workspace.
        self._testproject_data = env.setup_test_proj_cfg(self.test_workspace)
        self.assertIsNotNone(self._testproject_data)

        # Setup a viewer client to test viewer API calls.
        self._cc_client = env.setup_viewer_client(self.test_workspace)
        self.assertIsNotNone(self._cc_client)

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()
        self._codechecker_cfg = env.import_codechecker_cfg(self.test_workspace)

        self.env = env.codechecker_env()
コード例 #10
0
    def setUp(self):

        test_workspace = os.environ.get('TEST_WORKSPACE')

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + test_workspace)

        codechecker_cfg = env.import_test_cfg(
            test_workspace)['codechecker_cfg']
        self.server_url = env.parts_to_url(codechecker_cfg)

        # Get the test project configuration from the prepared test workspace.
        self._testproject_data = env.setup_test_proj_cfg(test_workspace)
        self.assertIsNotNone(self._testproject_data)

        # Setup a viewer client to test viewer API calls.
        self._cc_client = env.setup_viewer_client(test_workspace)
        self.assertIsNotNone(self._cc_client)

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()

        self._test_config = env.import_test_cfg(test_workspace)
コード例 #11
0
    def setUp(self):
        """ Set up workspace."""

        # TEST_WORKSPACE is automatically set by test package __init__.py .
        self.test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + self.test_workspace)

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()
        self.env = env.codechecker_env()
        self.report_dir = os.path.join(self.test_workspace, 'reports')
        os.makedirs(self.report_dir)

        # Get if clang is CTU-capable or not.
        cmd = [self._codechecker_cmd, 'analyze', '-h']
        output, _, result = call_command(cmd,
                                         cwd=self.test_workspace,
                                         env=self.env)
        self.assertEqual(result, 0, "Analyzing failed.")
        setattr(self, CTU_ATTR, is_ctu_capable(output))
        print("'analyze' reported CTU compatibility? " +
              str(getattr(self, CTU_ATTR)))

        setattr(self, ON_DEMAND_ATTR, is_ctu_on_demand_capable(output))
        print("'analyze' reported CTU-on-demand-compatibility? " +
              str(getattr(self, ON_DEMAND_ATTR)))

        setattr(
            self, DISPLAY_PROGRESS_ATTR,
            is_ctu_display_progress_capable(self.__getClangSaPath(), self.env))

        print("Has display-ctu-progress=true? " +
              str(getattr(self, DISPLAY_PROGRESS_ATTR)))

        self.__old_pwd = os.getcwd()
コード例 #12
0
    def setUp(self):

        # TEST_WORKSPACE is automatically set by test package __init__.py .
        test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + test_workspace)

        # Get the test project configuration from the prepared test workspace.
        self._testproject_data = env.setup_test_proj_cfg(test_workspace)
        self.assertIsNotNone(self._testproject_data)

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()

        self.env = env.codechecker_env()

        # Get if the package is able to collect statistics or not.
        cmd = [self._codechecker_cmd, 'analyze', '-h']
        output, _ = call_command(cmd, cwd=test_workspace, env=self.env)
        self.stats_capable = '--stats' in output
        print("'analyze' reported statistics collector-compatibility? " +
              str(self.stats_capable))

        test_project_path = self._testproject_data['project_path']
        test_project_build = shlex.split(self._testproject_data['build_cmd'])

        # Create compilation log used in the tests.
        log_cmd = [
            self._codechecker_cmd, 'log', '-o', 'compile_command.json', '-b'
        ]
        log_cmd.extend(test_project_build)
        output, err = call_command(log_cmd,
                                   cwd=test_project_path,
                                   env=self.env)
        print(output)
        print(err)
コード例 #13
0
    def setUp(self):
        """ Set up workspace."""

        # TEST_WORKSPACE is automatically set by test package __init__.py .
        self.test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + self.test_workspace)

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()
        self.env = env.codechecker_env()
        self.report_dir = os.path.join(self.test_workspace, 'reports')
        os.makedirs(self.report_dir)
        self.test_dir = os.path.join(os.path.dirname(__file__), 'test_files')

        # Get if clang is CTU-capable or not.
        cmd = [self._codechecker_cmd, 'analyze', '-h']
        output = subprocess.check_output(cmd,
                                         stderr=subprocess.STDOUT,
                                         cwd=self.test_dir,
                                         env=self.env)
        self.ctu_capable = '--ctu-' in output
        print("'analyze' reported CTU-compatibility? " + str(self.ctu_capable))

        # Fix the "template" build JSONs to contain a proper directory
        # so the tests work.
        raw_buildlog = os.path.join(self.test_dir, 'buildlog.json')
        with open(raw_buildlog) as log_file:
            build_json = json.load(log_file)
            for command in build_json:
                command['directory'] = self.test_dir

        os.chdir(self.test_workspace)
        self.buildlog = os.path.join(self.test_workspace, 'buildlog.json')
        with open(self.buildlog, 'w') as log_file:
            json.dump(build_json, log_file)
コード例 #14
0
    def setUp(self):
        # TEST_WORKSPACE is automatically set by test package __init__.py .
        test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + test_workspace)

        # Get the test configuration from the prepared int the test workspace.
        self._test_cfg = env.import_test_cfg(test_workspace)

        # Get the test project configuration from the prepared test workspace.
        self._testproject_data = env.setup_test_proj_cfg(test_workspace)
        self.assertIsNotNone(self._testproject_data)

        # Setup a viewer client to test viewer API calls.
        self._cc_client = env.setup_viewer_client(test_workspace)
        self.assertIsNotNone(self._cc_client)

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()

        # Get the run names which belong to this test.
        self._run_names = env.get_run_names(test_workspace)

        self._local_test_project = \
            self._test_cfg['test_project']['project_path_local']
        self._remote_test_project = \
            self._test_cfg['test_project']['project_path_remote']

        self._local_reports = os.path.join(self._local_test_project,
                                           'reports')
        self._remote_reports = os.path.join(self._remote_test_project,
                                            'reports')

        self._url = env.parts_to_url(self._test_cfg['codechecker_cfg'])

        self._env = self._test_cfg['codechecker_cfg']['check_env']
コード例 #15
0
    def setUp(self):
        test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + test_workspace)

        # Get the clang version which is tested.
        self._clang_to_test = env.clang_to_test()

        self._testproject_data = env.setup_test_proj_cfg(test_workspace)
        self.assertIsNotNone(self._testproject_data)

        self._cc_client = env.setup_viewer_client(test_workspace)
        self.assertIsNotNone(self._cc_client)

        # Get the run names which belong to this test.
        run_names = env.get_run_names(test_workspace)

        runs = self._cc_client.getRunData()

        test_runs = [run for run in runs if run.name in run_names]
        for r in test_runs:
            print(r)

        # There should be at least two runs for this test.
        self.assertIsNotNone(runs)
        self.assertNotEqual(len(runs), 0)
        self.assertGreaterEqual(len(runs), 2)

        # Name order matters from __init__ !
        self._base_runid = test_runs[0].runId  # base
        self._new_runid = test_runs[1].runId  # new

        self._codechecker_cmd = env.codechecker_cmd()
        self._report_dir = os.path.join(test_workspace, "reports")
        self._test_config = env.import_test_cfg(test_workspace)
        self._run_names = env.get_run_names(test_workspace)
コード例 #16
0
    def setUp(self):

        # TEST_WORKSPACE is automatically set by test package __init__.py .
        test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + test_workspace)

        # Get the test project configuration from the prepared test workspace.
        self._testproject_data = env.setup_test_proj_cfg(test_workspace)
        self.assertIsNotNone(self._testproject_data)

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()

        self.env = env.codechecker_env()

        # Get if the package is able to collect statistics or not.
        cmd = [self._codechecker_cmd, 'analyze', '-h']
        output, _ = call_command(cmd, cwd=test_workspace, env=self.env)
        self.stats_capable = '--stats' in output
        print("'analyze' reported statistics collector-compatibility? " +
              str(self.stats_capable))

        test_project_path = self._testproject_data['project_path']
        test_project_build = shlex.split(self._testproject_data['build_cmd'])

        # Create compilation log used in the tests.
        log_cmd = [self._codechecker_cmd, 'log', '-o', 'compile_command.json',
                   '-b']
        log_cmd.extend(test_project_build)
        output, err = call_command(log_cmd,
                                   cwd=test_project_path,
                                   env=self.env)
        print(output)
        print(err)
コード例 #17
0
ファイル: test_config.py プロジェクト: engr-basit/codechecker
    def setUp(self):

        # TEST_WORKSPACE is automatically set by test package __init__.py .
        self.test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + self.test_workspace)

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()

        self.reports_dir = os.path.join(self.test_workspace, "reports")
        self.config_file = os.path.join(self.test_workspace,
                                        "codechecker.json")
        self.build_json = os.path.join(self.test_workspace,
                                       "build_simple.json")
        self.source_file = os.path.join(self.test_workspace, "simple.cpp")

        # Create a compilation database.
        build_log = [{
            "directory": self.test_workspace,
            "command": "g++ -c " + self.source_file,
            "file": self.source_file
        }]

        with open(self.build_json, 'w', encoding="utf-8",
                  errors="ignore") as outfile:
            json.dump(build_log, outfile)

        # Test file contents
        simple_file_content = "int main() { return 1/0; }"

        # Write content to the test file
        with open(self.source_file, 'w', encoding="utf-8",
                  errors="ignore") as source:
            source.write(simple_file_content)
コード例 #18
0
    def test_valid_config(self):
        """ Store with a valid configuration file. """
        cc_env = env.codechecker_env()
        cc_env["CC_REPORT_DIR"] = self.codechecker_cfg['reportdir']

        with open(self.config_file, 'w+', encoding="utf-8",
                  errors="ignore") as config_f:
            json.dump(
                {
                    'store': [
                        '--name=' + 'store_config', '--trim-path-prefix=$HOME',
                        '--url=' + env.parts_to_url(self.codechecker_cfg),
                        '$CC_REPORT_DIR'
                    ]
                }, config_f)

        store_cmd = [
            env.codechecker_cmd(), 'store', '--config', self.config_file
        ]

        subprocess.check_output(store_cmd,
                                env=cc_env,
                                encoding="utf-8",
                                errors="ignore")
コード例 #19
0
    def test_invalid_subcommand(self):
        """ Call CodeChecker with and invalid subcommand. """

        dummy_cmd = [env.codechecker_cmd(), "dummy"]
        self.assertEqual(1, run_cmd(dummy_cmd)[0])
コード例 #20
0
    def setUp(self):

        # Get the test workspace.
        self.test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + self.test_workspace)

        self._codechecker_cfg = env.import_codechecker_cfg(self.test_workspace)
        self._reports_dir = self._codechecker_cfg['reportdir']

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()
        self._test_dir = os.path.join(self.test_workspace, 'test_files')
        self._product_name = self._codechecker_cfg['viewer_product']
        self._analyzer_stats_dir = os.path.join(self.test_workspace,
                                                'analysis_statistics')
        try:
            os.makedirs(self._test_dir)
        except os.error:
            # Directory already exists.
            pass

        # Remove analyzer statistics directory if it exists before store.
        if os.path.exists(self._analyzer_stats_dir):
            shutil.rmtree(self._analyzer_stats_dir, ignore_errors=True)

        # Remove reports directory if it exists and create an empty one.
        if os.path.exists(self._reports_dir):
            shutil.rmtree(self._reports_dir, ignore_errors=True)

        # Setup a viewer client to test viewer API calls.
        self._cc_client = env.setup_viewer_client(self.test_workspace)
        self.assertIsNotNone(self._cc_client)

        # Change working dir to testfile dir so CodeChecker can be run easily.
        self.__old_pwd = os.getcwd()
        os.chdir(self._test_dir)

        self._source_file = os.path.join(self._test_dir, "main.cpp")

        # Init project dir.
        makefile_content = "all:\n\t$(CXX) -c main.cpp -o /dev/null\n"
        project_info_content = {
            "name": "hello",
            "clean_cmd": "",
            "build_cmd": "make"
        }

        makefile = os.path.join(self._test_dir, 'Makefile')
        with open(makefile, 'w', encoding="utf-8", errors="ignore") as make_f:
            make_f.write(makefile_content)

        project_info = os.path.join(self._test_dir, 'project_info.json')
        with open(project_info, 'w', encoding="utf-8",
                  errors="ignore") as info_f:
            json.dump(project_info_content, info_f)

        self.sources = [
            """
int main()
{
  return 1 / 0; // Division by zero
}""", """
int main()
{
  return 0;
  xxx // Will cause a compilation error
}"""
        ]
コード例 #21
0
    def test_analyzers(self):
        """ Listing available analyzers. """

        analyzers_cmd = [env.codechecker_cmd(), 'analyzers']
        self.assertEqual(0, run_cmd(analyzers_cmd)[0])
コード例 #22
0
    def test_tim_path_prefix_store(self):
        """Trim the path prefix from the sored reports.

        The source file paths are converted to absolute with the
        temporary test directory, the test trims that temporary
        test directory from the source file path during the storage.
        """

        test_dir = os.path.dirname(os.path.realpath(__file__))

        report_dir = os.path.join(test_dir, "test_proj")

        codechecker_cfg = self._test_cfg["codechecker_cfg"]

        # Copy report files to a temporary directory not to modify the
        # files in the repository.
        # Report files will be overwritten during the tests.
        temp_workspace = os.path.join(codechecker_cfg["workspace"],
                                      "test_proj")
        shutil.copytree(report_dir, temp_workspace)

        report_file = os.path.join(temp_workspace, "divide_zero.plist")

        # Convert file paths to absolute in the report.
        plist_test.prefix_file_path(report_file, temp_workspace)

        report_content = {}
        with open(report_file, mode="rb") as rf:
            report_content = plistlib.load(rf)

        trimmed_paths = [
            util.trim_path_prefixes(path, [temp_workspace])
            for path in report_content["files"]
        ]

        run_name = "store_test"
        store_cmd = [
            env.codechecker_cmd(),
            "store",
            temp_workspace,
            "--name",
            run_name,
            "--url",
            env.parts_to_url(codechecker_cfg),
            "--trim-path-prefix",
            temp_workspace,
            "--verbose",
            "debug",
        ]

        try:
            out = subprocess.check_output(store_cmd,
                                          encoding="utf-8",
                                          errors="ignore")
            print(out)
        except subprocess.CalledProcessError as cerr:
            print(cerr.stdout)
            print(cerr.stderr)
            raise

        query_cmd = [
            env.codechecker_cmd(),
            "cmd",
            "results",
            run_name,
            # Use the 'Default' product.
            "--url",
            env.parts_to_url(codechecker_cfg),
            "-o",
            "json",
        ]

        out = subprocess.check_output(query_cmd,
                                      encoding="utf-8",
                                      errors="ignore")
        reports = json.loads(out)

        print(json.dumps(reports, indent=2))

        self.assertEqual(len(reports), 4)
        for report in reports:
            self.assertIn(report["checkedFile"], trimmed_paths)
コード例 #23
0
    def test_personal_access_tokens(self):
        """ Test personal access token commands. """
        codechecker_cfg = self._test_cfg['codechecker_cfg']
        host = codechecker_cfg['viewer_host']
        port = codechecker_cfg['viewer_port']

        new_token_cmd = [
            env.codechecker_cmd(), 'cmd', 'token', 'new', '--url',
            env.parts_to_url(codechecker_cfg)
        ]

        with self.assertRaises(subprocess.CalledProcessError):
            subprocess.check_output(new_token_cmd,
                                    encoding="utf-8",
                                    errors="ignore")

        # Login to the server.
        auth_client = env.setup_auth_client(self._test_workspace,
                                            session_token='_PROHIBIT')

        # A non-authenticated session should return an empty user.
        user = auth_client.getLoggedInUser()
        self.assertEqual(user, "")

        # Create a SUPERUSER login.
        session_token = auth_client.performLogin("Username:Password",
                                                 "cc:test")

        self.assertIsNotNone(session_token,
                             "Valid credentials didn't give us a token!")

        cred_manager = UserCredentials()
        cred_manager.save_token(host, port, session_token)

        # Run the new token command after login.
        subprocess.check_output(new_token_cmd,
                                encoding="utf-8",
                                errors="ignore")

        # List personal access tokens.
        list_token_cmd = [
            env.codechecker_cmd(), 'cmd', 'token', 'list', '--url',
            env.parts_to_url(codechecker_cfg), '-o', 'json'
        ]

        out_json = subprocess.check_output(list_token_cmd,
                                           encoding="utf-8",
                                           errors="ignore")
        tokens = json.loads(out_json)
        self.assertEqual(len(tokens), 1)

        # Remove personal access token.
        del_token_cmd = [
            env.codechecker_cmd(), 'cmd', 'token', 'del', '--url',
            env.parts_to_url(codechecker_cfg), tokens[0]['token']
        ]

        subprocess.check_output(del_token_cmd,
                                encoding="utf-8",
                                errors="ignore")

        cred_manager.save_token(host, port, session_token, True)
コード例 #24
0
    def testShutdownTerminateByCmdline(self):
        """Tests that the command-line command actually kills the server,
        and that it does not kill anything else."""

        # NOTE: Yet again keep the lexicographical flow, no renames!

        test_cfg = env.import_test_cfg(self._test_workspace)
        codechecker_1 = test_cfg['codechecker_1']
        codechecker_2 = test_cfg['codechecker_2']
        EVENT_2.clear()
        start_server(codechecker_2, EVENT_2, ['--skip-db-cleanup'])

        # Kill the server, but yet again give a grace period.
        self.assertEqual(0, self.run_cmd([env.codechecker_cmd(),
                                          'server', '--stop',
                                          '--view-port',
                                          str(codechecker_2['viewer_port']),
                                          '--workspace',
                                          self._test_workspace]),
                         "The stop command didn't return exit code 0.")

        # Check if the remaining server is still there,
        # we need to make sure that --stop only kills the specified server!
        instance_1 = [i for i in instance_manager.get_instances(self.home)
                      if i['port'] == codechecker_1['viewer_port'] and
                      i['workspace'] == self._test_workspace]
        instance_2 = [i for i in instance_manager.get_instances(self.home)
                      if i['port'] == codechecker_2['viewer_port'] and
                      i['workspace'] == self._test_workspace]

        self.assertNotEqual(instance_1, [],
                            "The stopped server deleted another server's "
                            "record from the instance list!")

        self.assertEqual(instance_2, [],
                         "The stopped server did not disappear from the"
                         " instance list.")

        # Kill the first server via cmdline too.
        self.assertEqual(0, self.run_cmd([env.codechecker_cmd(),
                                          'server', '--stop',
                                          '--view-port',
                                          str(codechecker_1['viewer_port']),
                                          '--workspace',
                                          self._test_workspace]),
                         "The stop command didn't return exit code 0.")

        instance_1 = [i for i in instance_manager.get_instances(self.home)
                      if i['port'] == codechecker_1['viewer_port'] and
                      i['workspace'] == self._test_workspace]
        instance_2 = [i for i in instance_manager.get_instances(self.home)
                      if i['port'] == codechecker_2['viewer_port'] and
                      i['workspace'] == self._test_workspace]

        self.assertEqual(instance_1, [],
                         "The stopped server did not disappear from the"
                         " instance list.")

        self.assertEqual(instance_2, [],
                         "The stopped server made another server's record "
                         "appear in the instance list.")
コード例 #25
0
    def test_server_help(self):
        """ Get help for server subcmd. """

        srv_help = [env.codechecker_cmd(), 'server', '--help']
        self.assertEqual(0, run_cmd(srv_help))
コード例 #26
0
    def test_version_help(self):
        """ Test the 'version' subcommand. """

        version_help = [env.codechecker_cmd(), 'version', '--help']
        self.assertEqual(0, run_cmd(version_help))
コード例 #27
0
ファイル: test_store.py プロジェクト: uhziel/codechecker
        def store_multiple_report_dirs(report_dirs):
            """ """
            run_name = "multiple_report_dirs"
            store_cmd = [
                env.codechecker_cmd(), "store", *report_dirs, "--name",
                run_name, "--url",
                env.parts_to_url(self._codechecker_cfg)
            ]

            proc = subprocess.Popen(store_cmd,
                                    encoding="utf-8",
                                    errors="ignore",
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
            _, err = proc.communicate()

            self.assertNotIn("UserWarning: Duplicate name", err)

            # Check the reports.
            query_cmd = [
                env.codechecker_cmd(), "cmd", "results", run_name, "--url",
                env.parts_to_url(self._codechecker_cfg), "-o", "json"
            ]

            out = subprocess.check_output(query_cmd,
                                          encoding="utf-8",
                                          errors="ignore")
            reports = json.loads(out)

            self.assertTrue(
                any(r['checkerId'] == 'core.DivideZero' for r in reports))

            self.assertTrue(
                any(r['checkerId'] == 'deadcode.DeadStores' for r in reports))

            # Get analysis info.
            limit = None
            offset = 0
            report = [
                r for r in reports if r['checkerId'] == 'core.DivideZero'
            ][0]

            # Get analysis info for a run.
            analysis_info_filter = AnalysisInfoFilter(runId=report['runId'])
            analysis_info = self._cc_client.getAnalysisInfo(
                analysis_info_filter, limit, offset)
            self.assertEqual(len(analysis_info), 2)
            self.assertTrue(
                any(report_dir1 in i.analyzerCommand for i in analysis_info))
            self.assertTrue(
                any(report_dir2 in i.analyzerCommand for i in analysis_info))

            # Get analysis info for a report.
            analysis_info_filter = AnalysisInfoFilter(
                reportId=report['reportId'])

            analysis_info = self._cc_client.getAnalysisInfo(
                analysis_info_filter, limit, offset)
            self.assertEqual(len(analysis_info), 1)
            self.assertTrue(
                any(report_dir2 in i.analyzerCommand for i in analysis_info))

            # Get analysis infor for run history.
            query_cmd = [
                env.codechecker_cmd(), "cmd", "history", "-n", run_name,
                "--url",
                env.parts_to_url(self._codechecker_cfg), "-o", "json"
            ]

            out = subprocess.check_output(query_cmd,
                                          encoding="utf-8",
                                          errors="ignore")
            history = json.loads(out)
            self.assertTrue(history)

            h = max(history, key=lambda h: h["id"])

            analysis_info_filter = AnalysisInfoFilter(runHistoryId=h['id'])
            analysis_info = self._cc_client.getAnalysisInfo(
                analysis_info_filter, limit, offset)
            self.assertEqual(len(analysis_info), 2)
            self.assertTrue(
                any(report_dir1 in i.analyzerCommand for i in analysis_info))
            self.assertTrue(
                any(report_dir2 in i.analyzerCommand for i in analysis_info))

            # Check the reports.
            rm_cmd = [
                env.codechecker_cmd(), "cmd", "del", "-n", run_name, "--url",
                env.parts_to_url(self._codechecker_cfg)
            ]

            out = subprocess.check_output(rm_cmd,
                                          encoding="utf-8",
                                          errors="ignore")
コード例 #28
0
    def test_trim_path_prefix_store(self):
        """Trim the path prefix from the sored reports.

        The source file paths are converted to absolute with the
        temporary test directory, the test trims that temporary
        test directory from the source file path during the storage.
        """
        report_file = os.path.join(self.test_proj_dir, "divide_zero.plist")

        report_content = {}
        with open(report_file, mode="rb") as rf:
            report_content = plistlib.load(rf)

        trimmed_paths = [
            util.trim_path_prefixes(path, [self.test_proj_dir])
            for path in report_content["files"]
        ]

        run_name = "store_test"
        store_cmd = [
            env.codechecker_cmd(),
            "store",
            self.test_proj_dir,
            "--name",
            run_name,
            "--url",
            env.parts_to_url(self.codechecker_cfg),
            "--trim-path-prefix",
            self.test_proj_dir,
            "--verbose",
            "debug",
        ]

        try:
            out = subprocess.check_output(store_cmd,
                                          encoding="utf-8",
                                          errors="ignore")
            print(out)
        except subprocess.CalledProcessError as cerr:
            print(cerr.stdout)
            print(cerr.stderr)
            raise

        query_cmd = [
            env.codechecker_cmd(),
            "cmd",
            "results",
            run_name,
            # Use the 'Default' product.
            "--url",
            env.parts_to_url(self.codechecker_cfg),
            "-o",
            "json",
        ]

        out = subprocess.check_output(query_cmd,
                                      encoding="utf-8",
                                      errors="ignore")
        reports = json.loads(out)

        print(json.dumps(reports, indent=2))

        self.assertEqual(len(reports), 4)
        for report in reports:
            self.assertIn(report["checkedFile"], trimmed_paths)
コード例 #29
0
    def test_main_help(self):
        """ Main cmdline help. """

        main_help = [env.codechecker_cmd(), '--help']
        self.assertEqual(0, run_cmd(main_help))
コード例 #30
0
    def test_no_subcommand(self):
        """ Call CodeChecker without subcommand. """

        main_cmd = [env.codechecker_cmd()]
        self.assertEqual(0, run_cmd(main_cmd)[0])
コード例 #31
0
    def test_check_help(self):
        """ Get help for check subcmd. """

        check_help = [env.codechecker_cmd(), 'check', '--help']
        self.assertEqual(0, run_cmd(check_help))
コード例 #32
0
    def test_log_help(self):
        """ Get help for log subcmd. """

        log_help = [env.codechecker_cmd(), 'log', '--help']
        self.assertEqual(0, run_cmd(log_help)[0])
コード例 #33
0
    def test_checkers(self):
        """ Listing available checkers. """

        checkers_cmd = [env.codechecker_cmd(), 'checkers']
        self.assertEqual(0, run_cmd(checkers_cmd))
コード例 #34
0
    def test_analyze_help(self):
        """ Get help for analyze subcmd. """

        analyze_help = [env.codechecker_cmd(), 'analyze', '--help']
        self.assertEqual(0, run_cmd(analyze_help)[0])
コード例 #35
0
ファイル: test_instances.py プロジェクト: martong/codechecker
    def testShutdownTerminateByCmdline(self):
        """Tests that the command-line command actually kills the server,
        and that it does not kill anything else."""

        # NOTE: Yet again keep the lexicographical flow, no renames!

        test_cfg = env.import_test_cfg(self._test_workspace)
        codechecker_1 = test_cfg['codechecker_1']
        codechecker_2 = test_cfg['codechecker_2']
        EVENT_2.clear()
        start_server(codechecker_2, EVENT_2)

        # Kill the server, but yet again give a grace period.
        self.assertEqual(
            0,
            self.run_cmd([
                env.codechecker_cmd(), 'server', '--stop', '--view-port',
                str(codechecker_2['viewer_port']), '--workspace',
                self._test_workspace
            ]), "The stop command didn't return exit code 0.")
        time.sleep(5)

        # Check if the remaining server is still there,
        # we need to make sure that --stop only kills the specified server!
        instance_1 = [
            i for i in instance_manager.list(self.home)
            if i['port'] == codechecker_1['viewer_port']
            and i['workspace'] == self._test_workspace
        ]
        instance_2 = [
            i for i in instance_manager.list(self.home)
            if i['port'] == codechecker_2['viewer_port']
            and i['workspace'] == self._test_workspace
        ]

        self.assertNotEqual(
            instance_1, [], "The stopped server deleted another server's "
            "record from the instance list!")

        self.assertEqual(
            instance_2, [], "The stopped server did not disappear from the"
            " instance list.")

        # Kill the first server via cmdline too.
        self.assertEqual(
            0,
            self.run_cmd([
                env.codechecker_cmd(), 'server', '--stop', '--view-port',
                str(codechecker_1['viewer_port']), '--workspace',
                self._test_workspace
            ]), "The stop command didn't return exit code 0.")
        time.sleep(5)

        instance_1 = [
            i for i in instance_manager.list(self.home)
            if i['port'] == codechecker_1['viewer_port']
            and i['workspace'] == self._test_workspace
        ]
        instance_2 = [
            i for i in instance_manager.list(self.home)
            if i['port'] == codechecker_2['viewer_port']
            and i['workspace'] == self._test_workspace
        ]

        self.assertEqual(
            instance_1, [], "The stopped server did not disappear from the"
            " instance list.")

        self.assertEqual(
            instance_2, [], "The stopped server made another server's record "
            "appear in the instance list.")
コード例 #36
0
    def test_parse_help(self):
        """ Get help for parse subcmd. """

        parse_help = [env.codechecker_cmd(), 'parse', '--help']
        self.assertEqual(0, run_cmd(parse_help)[0])
コード例 #37
0
    def setUp(self):
        # TEST_WORKSPACE is automatically set by test package __init__.py .
        self.test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + self.test_workspace)

        self._codechecker_cfg = env.import_codechecker_cfg(self.test_workspace)

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()
        self._test_dir = os.path.join(self.test_workspace, 'test_files')

        try:
            os.makedirs(self._test_dir)
        except os.error:
            # Directory already exists.
            pass

        # Setup a viewer client to test viewer API calls.
        self._cc_client = env.setup_viewer_client(self.test_workspace)
        self.assertIsNotNone(self._cc_client)

        # Change working dir to testfile dir so CodeChecker can be run easily.
        self.__old_pwd = os.getcwd()
        os.chdir(self._test_dir)

        self._source_file = "main.cpp"

        # Init project dir.
        makefile = "all:\n\t$(CXX) -c main.cpp -Wno-division-by-zero " \
                   "-Wno-all -Wno-extra -o /dev/null\n"
        project_info = {"name": "hello", "clean_cmd": "", "build_cmd": "make"}

        with open(os.path.join(self._test_dir, 'Makefile'), 'w') as f:
            f.write(makefile)
        with open(os.path.join(self._test_dir, 'project_info.json'), 'w') as f:
            json.dump(project_info, f)

        self.sources = [
            """
int main()
{
  int i = 1 / 0;

  sizeof(42);
  sizeof(42);
  sizeof(42);
}""", """
int main()
{
  int i = 1 / 0;

  int* p = 0;

  i = *p + 42;

  sizeof(42);
  sizeof(42);
  sizeof(42);
}""", """
int main()
{
  int i = 1 / 2;

  int* p = 0;

  i = *p + 42;

  sizeof(42);
  sizeof(42);
  sizeof(42);
}""", """


int main()
{
  int i = 1 / 0;

  int* p = 0;

  i = *p + 42;

  sizeof(42);
  sizeof(42);
}"""
        ]
コード例 #38
0
    def setUp(self):
        # TEST_WORKSPACE is automatically set by test package __init__.py .
        self.test_workspace = os.environ['TEST_WORKSPACE']

        test_class = self.__class__.__name__
        print('Running ' + test_class + ' tests in ' + self.test_workspace)

        self._codechecker_cfg = env.import_codechecker_cfg(self.test_workspace)

        # Get the CodeChecker cmd if needed for the tests.
        self._codechecker_cmd = env.codechecker_cmd()
        self._test_dir = os.path.join(self.test_workspace, 'test_files')

        try:
            os.makedirs(self._test_dir)
        except os.error:
            # Directory already exists.
            pass

        # Setup a viewer client to test viewer API calls.
        self._cc_client = env.setup_viewer_client(self.test_workspace)
        self.assertIsNotNone(self._cc_client)

        # Change working dir to testfile dir so CodeChecker can be run easily.
        self.__old_pwd = os.getcwd()
        os.chdir(self._test_dir)

        self._source_file = "main.cpp"

        # Init project dir.
        makefile = "all:\n\t$(CXX) -c main.cpp -Wno-division-by-zero " \
                   "-Wno-all -Wno-extra -o /dev/null\n"
        project_info = {
            "name": "hello",
            "clean_cmd": "",
            "build_cmd": "make"
        }

        with open(os.path.join(self._test_dir, 'Makefile'), 'w') as f:
            f.write(makefile)
        with open(os.path.join(self._test_dir, 'project_info.json'), 'w') as f:
            json.dump(project_info, f)

        self.sources = ["""
int main()
{
  int i = 1 / 0;

  sizeof(42);
  sizeof(42);
  sizeof(42);
}""", """
int main()
{
  int i = 1 / 0;

  int* p = 0;

  i = *p + 42;

  sizeof(42);
  sizeof(42);
  sizeof(42);
}""", """
int main()
{
  int i = 1 / 2;

  int* p = 0;

  i = *p + 42;

  sizeof(42);
  sizeof(42);
  sizeof(42);
}""", """


int main()
{
  int i = 1 / 0;

  int* p = 0;

  i = *p + 42;

  sizeof(42);
  sizeof(42);
}"""]