| 1 |  |  | import json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import os | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import re | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import tempfile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | import pytest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class TestTools(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     def find_files(file_name, search_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         result = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         # root, directory, files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         for root, _, files in os.walk(search_path): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |             for filename in files: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |                 if file_name in filename: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |                     result.append(os.path.abspath(os.path.join(root, filename))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         return result | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     @staticmethod | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     def get_text_file(src): | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         top_patch = os.path.dirname(os.path.realpath(__file__)) | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         patch = os.path.join(top_patch, src) | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         with open(patch, 'r') as data: | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |             return data.readlines() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     def get_random_dir_in_tmp(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         return os.path.join(tempfile.gettempdir(), str(uuid.uuid4())) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 32 |  | View Code Duplication |     @staticmethod | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     def compare_results_html(result): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         result_ = TestTools.get_text_file(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         reference_pattern = TestTools.get_text_file( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             'test_commands/test_data/referenc_pattern_html_report.txt') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         prefix_start = '<script>var data_of_tree = ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         prefix_end = ';</script><div>\n' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         data_in_html = "" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         matched = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         for row in result_: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             if prefix_start in row and prefix_end in row: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 matched = True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 data_in_html = row | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         assert matched | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         tmp_json_str = data_in_html.replace(prefix_start, '').replace(prefix_end, '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         tmp_json = json.loads(tmp_json_str) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         data_in_html = prefix_start + json.dumps(tmp_json, indent=4, sort_keys=False) + prefix_end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         count_row = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         rule_name = 'xccdforgssgprojectcontentrulepackageabrtremoved' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         for row in reference_pattern: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             if row in data_in_html or rule_name in row: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 count_row += 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         assert count_row == len(reference_pattern) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     def get_data_json(src): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         top_patch = os.path.dirname(os.path.realpath(__file__)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         patch = os.path.join(top_patch, src) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         with open(patch, 'r') as data: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             return json.load(data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     def compare_results_json(result): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         result = TestTools.get_data_json(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         reference_result = TestTools.get_data_json( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             'test_commands/test_data/referenc_result_data_json.json') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         rule_name = "xccdf_org.ssgproject.content_rule_package_abrt_removed" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         result_rule_name = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             x for x in result.keys() if re.search( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 rule_name, x)] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         assert result[result_rule_name[0]] == reference_result[rule_name] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     def find_all_in_string(regex, count, string): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         assert len(re.findall(regex, string)) == count | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     def get_questions_not_selected(capsys, client, result): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         out = client.get_questions()[0].choices | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         assert out == result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         captured = capsys.readouterr() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         regex = r'rule_package_\w+_removed +\(Not selected\)' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         TestTools.find_all_in_string(regex, 6, captured.out) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     def get_questions_with_option_show_failed_rules(client): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         out = client.get_questions()[0].choices | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         rule1 = 'xccdf_org.ssgproject.content_rule_package_abrt_removed' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         assert out[0] == rule1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         with pytest.raises(Exception, match="list index out of range"): | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  |             assert out[2] is None | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |  |