def test_comment(self): """ Test commenting of multiple bugs. """ runid = self._test_runs[0].runId logging.debug('Get all run results from the db for runid: ' + str(runid)) run_results = get_all_run_results(self._cc_client, runid) self.assertIsNotNone(run_results) self.assertNotEqual(len(run_results), 0) bug = run_results[0] # There are no comments available for the first bug comments = self._cc_client.getComments(bug.reportId) self.assertEqual(len(comments), 0) num_comment = self._cc_client.getCommentCount(bug.reportId) self.assertEqual(num_comment, 0) # Try to add a new comment for the first bug comment1 = CommentData(author='anybody', message='First msg') success = self._cc_client.addComment(bug.reportId, comment1) self.assertTrue(success) logging.debug('Bug commented successfully') # Try to add another new comment for the first bug comment2 = CommentData(author='anybody', message='Second msg') success = self._cc_client.addComment(bug.reportId, comment2) self.assertTrue(success) logging.debug('Bug commented successfully') # Try to add another new comment with empty message. with self.assertRaises(RequestFailed): self._cc_client.addComment(bug.reportId, CommentData(author='anybody')) # Add new comment for a second bug with a different hash! bug2 = None for b in run_results: if b.bugHash != bug.bugHash: bug2 = b comment3 = CommentData(author='anybody', message='Third msg') success = self._cc_client.addComment(bug2.reportId, comment3) self.assertTrue(success) logging.debug('Bug commented successfully') # There are two comments available for the first bug comments = self._cc_client.getComments(bug.reportId) self.assertEqual(len(comments), 2) for c in comments: self.assertEqual(c.author, 'cc') num_comment = self._cc_client.getCommentCount(bug.reportId) self.assertEqual(num_comment, 2) # Check the order of comments (first comment is the earliest) self.assertGreater(comments[0].createdAt, comments[1].createdAt) # Remove the first comment print("removing comment:" + str(comments[0].id)) success = self._cc_client.removeComment(comments[0].id) self.assertTrue(success) logging.debug('Comment removed successfully') # Remove the second comment as john should be unsuccessful print("removing comment:" + str(comments[1].id)) with self.assertRaises(RequestFailed): self._cc_client_john.removeComment(comments[1].id) logging.debug('Comment was removed by another user') comments = self._cc_client.getComments(bug.reportId) self.assertEqual(len(comments), 1) num_comment = self._cc_client.getCommentCount(bug.reportId) self.assertEqual(num_comment, 1) # Edit the message of the first remaining comment new_msg = 'New msg' success = self._cc_client.updateComment(comments[0].id, new_msg) self.assertTrue(success) logging.debug('Comment edited successfully') # Update comment with empty message. with self.assertRaises(RequestFailed): self._cc_client.updateComment(comments[0].id, ' ') john_msg = 'John cannot edit' with self.assertRaises(RequestFailed): self._cc_client_john.updateComment(comments[0].id, john_msg) logging.debug('Comment was edited by john') comments = self._cc_client.getComments(bug.reportId) user_comments, system_comments = separate_comments(comments) self.assertEqual(len(user_comments), 1) self.assertEqual(user_comments[0].message, new_msg) self.assertEqual(len(system_comments), 1) # Remove the last comment for the first bug success = self._cc_client.removeComment(user_comments[0].id) self.assertTrue(success) logging.debug('Comment removed successfully') comments = self._cc_client.getComments(bug.reportId) self.assertEqual(len(comments), 1) self.assertEqual(comments[0].kind, CommentKind.SYSTEM) num_comment = self._cc_client.getCommentCount(bug.reportId) self.assertEqual(num_comment, 1) # Remove the last comment for the second bug. comments = self._cc_client.getComments(bug2.reportId) self.assertEqual(len(comments), 1) success = self._cc_client.removeComment(comments[0].id) self.assertTrue(success) logging.debug('Comment removed successfully') comments = self._cc_client.getComments(bug2.reportId) self.assertEqual(len(comments), 0) num_comment = self._cc_client.getCommentCount(bug2.reportId) self.assertEqual(num_comment, 0)
def test_export_import(self): """ Test exporting and importing feature """ run_filter = RunFilter() run_filter.names = [self.test_runs[0].name] logging.debug('Get all run results from the db for runid: ' + str(self.test_runs[0].runId)) run_results = get_all_run_results(self._cc_client, self.test_runs[0].runId) self.assertIsNotNone(run_results) self.assertNotEqual(len(run_results), 0) bug = run_results[0] # There are no comments available for the first bug comments = self._cc_client.getComments(bug.reportId) self.assertEqual(len(comments), 0) comment1 = CommentData(author='anybody', message='First msg') success = self._cc_client.addComment(bug.reportId, comment1) self.assertTrue(success) logging.debug('Bug commented successfully') # Try to add another new comment for the first bug comment2 = CommentData(author='anybody', message='Second msg') success = self._cc_client.addComment(bug.reportId, comment2) self.assertTrue(success) logging.debug('Bug commented successfully') # Change review status review_comment = 'This is really a bug' status = ReviewStatus.CONFIRMED success = self._cc_client.changeReviewStatus(bug.reportId, status, review_comment) self.assertTrue(success) logging.debug('Bug review status changed successfully') # Export the comments and reviews exported = self._cc_client.exportData(run_filter) exported_comments = exported.comments[bug.bugHash] exported_review = exported.reviewData[bug.bugHash] # Check if the comments are same user_comments = get_user_comments(exported_comments) self.assertEqual(user_comments[0].message, comment2.message) self.assertEqual(user_comments[1].message, comment1.message) # Check for the review status self.assertEqual(exported_review.status, status) self.assertEqual(exported_review.comment, review_comment) new_export_command = [ self._codechecker_cmd, 'cmd', 'export', '-n', self.test_runs[0].name, '--url', str(self.server_url) ] print(new_export_command) out_json = subprocess.check_output(new_export_command, encoding="utf-8", errors="ignore") resolved_results = json.loads(out_json) print(resolved_results) with tempfile.NamedTemporaryFile() as component_f: component_f.write(out_json.encode('utf-8')) component_f.flush() component_f.seek(0) self._cc_client.removeComment(user_comments[0].id) updated_message = "The new comment for testing" self._cc_client.updateComment(user_comments[1].id, updated_message) new_import_command = [ self._codechecker_cmd, 'cmd', 'import', '-i', component_f.name, '--url', str(self.server_url) ] print(new_import_command) subprocess.check_output(new_import_command, encoding="utf-8", errors="ignore") exported = self._cc_client.exportData(run_filter) new_comments = exported.comments[bug.bugHash] print(new_comments) # Check if the last and second last comments self.assertEqual(new_comments[-1].message, comment1.message) self.assertEqual(new_comments[-2].message, updated_message)
def test_same_bug_hash(self): """ Test that different report ID's referring the same bug hash can query each other's comments. """ # Get run results for the first run. runid_base = self._test_runs[0].runId logging.debug('Get all run results from the db for runid: ' + str(runid_base)) run_results_base = get_all_run_results(self._cc_client, runid_base) self.assertIsNotNone(run_results_base) self.assertNotEqual(len(run_results_base), 0) bug_base = run_results_base[0] # Get run results for the second run. runid_new = self._test_runs[1].runId logging.debug('Get all run results from the db for runid: ' + str(runid_new)) run_results_new = get_all_run_results(self._cc_client, runid_new) self.assertIsNotNone(run_results_new) self.assertNotEqual(len(run_results_new), 0) bug_new = run_results_new[0] # Both bug have the same bug hash. self.assertEqual(bug_base.bugHash, bug_new.bugHash) # There are no comments available for the bug. comments = self._cc_client.getComments(bug_base.reportId) user_comments, _ = separate_comments(comments) self.assertEqual(len(user_comments), 0) comments = self._cc_client.getComments(bug_new.reportId) user_comments, _ = separate_comments(comments) self.assertEqual(len(user_comments), 0) # Try to add a new comment for the first bug comment = CommentData(author='Anonymous', message='First msg') success = self._cc_client.addComment(bug_base.reportId, comment) self.assertTrue(success) logging.debug('Bug commented successfully') comments = self._cc_client.getComments(bug_base.reportId) user_comments, _ = separate_comments(comments) self.assertEqual(len(user_comments), 1) comments = self._cc_client.getComments(bug_new.reportId) user_comments, _ = separate_comments(comments) self.assertEqual(len(user_comments), 1) # Remove the comment for the bug. success = self._cc_client.removeComment(comments[0].id) self.assertTrue(success) logging.debug('Comment removed successfully') comments = self._cc_client.getComments(bug_base.reportId) user_comments, _ = separate_comments(comments) self.assertEqual(len(user_comments), 0) comments = self._cc_client.getComments(bug_new.reportId) user_comments, _ = separate_comments(comments) self.assertEqual(len(user_comments), 0)
def test_garbage_file_collection(self): event = multiprocessing.Event() event.clear() self.codechecker_cfg['viewer_port'] = env.get_free_port() env.export_test_cfg(self.test_workspace, {'codechecker_cfg': self.codechecker_cfg}) env.enable_auth(self.test_workspace) server_access = codechecker.start_server(self.codechecker_cfg, event) server_access['viewer_port'] \ = self.codechecker_cfg['viewer_port'] server_access['viewer_product'] \ = self.codechecker_cfg['viewer_product'] codechecker.add_test_package_product(server_access, self.test_workspace) self._cc_client = env.setup_viewer_client(self.test_workspace) self.assertIsNotNone(self._cc_client) run_name1 = 'db_cleanup_test' run_name2 = f'{run_name1}2' self.__create_test_dir() # Store the results. codechecker.check_and_store(self.codechecker_cfg, run_name1, self.test_dir) # Store the results to a different run too to see if we remove only one # run, comments and review statuses not cleared. codechecker.check_and_store(self.codechecker_cfg, run_name2, self.test_dir) run_id1 = self.__get_run_id([run_name1]) report = self._cc_client.getRunResults(None, 1, 0, [], None, None, False)[0] report_hash = report.bugHash report_id = report.reportId # Add a new comment. comment = CommentData(author='anybody', message='Msg') success = self._cc_client.addComment(report_id, comment) self.assertTrue(success) # Change review status. success = self._cc_client.changeReviewStatus(report_id, ReviewStatus.CONFIRMED, 'Real bug') self.assertTrue(success) # Remove the first storage. self._cc_client.removeRun(run_id1, None) # Comments and review statuses are not cleared, because the second # run results still reference them. run_id2 = self.__get_run_id([run_name2]) r_filter = ReportFilter(reviewStatus=[ReviewStatus.CONFIRMED]) run_results = self._cc_client.getRunResults([run_id2], 1, 0, None, r_filter, None, False) self.assertTrue(run_results) comments = self._cc_client.getComments(run_results[0].reportId) self.assertTrue(comments) # Remove the second run too, so it will cleanup the unused commments # and review statuses. self._cc_client.removeRun(run_id2, None) # Store results again and check that previous comments and review # statuses are gone. files_in_report_before = self.__get_files_in_report(run_name1) r_filter = ReportFilter(reportHash=[report_hash]) report = self._cc_client.getRunResults(None, 1, 0, None, r_filter, None, False)[0] report_id = report.reportId comments = self._cc_client.getComments(report_id) self.assertFalse(comments) r_filter = ReportFilter(reviewStatus=[ReviewStatus.CONFIRMED]) run_results = self._cc_client.getRunResults(None, 1, 0, None, r_filter, None, False) self.assertFalse(run_results) # Checker severity levels. self.__check_serverity_of_reports(run_name1) self.__rename_project_dir() # Delete previous analysis report directory. rmtree(self.codechecker_cfg['reportdir']) files_in_report_after = self.__get_files_in_report(run_name1) event.set() event.clear() # Change severity level of core.DivideZero to LOW. with open(self.workspace_severity_cfg, 'r+', encoding='utf-8', errors='ignore') as severity_cgf_file: severity_map = json.load(severity_cgf_file) severity_map['core.DivideZero'] = 'LOW' severity_cgf_file.seek(0) severity_cgf_file.truncate() severity_cgf_file.write(str(json.dumps(severity_map))) self.codechecker_cfg['viewer_port'] = env.get_free_port() env.export_test_cfg(self.test_workspace, {'codechecker_cfg': self.codechecker_cfg}) codechecker.start_server(self.codechecker_cfg, event) codechecker.login(self.codechecker_cfg, self.test_workspace, 'cc', 'test') self._cc_client = env.setup_viewer_client(self.test_workspace) self.assertIsNotNone(self._cc_client) self.assertEqual(len(files_in_report_before & files_in_report_after), 0) for file_id in files_in_report_before: f = self._cc_client.getSourceFileData(file_id, False, None) self.assertIsNone(f.fileId) # Checker severity levels. self.__check_serverity_of_reports(run_name1) event.set()