def test_get_all_url_title_data(self): test_all_url_title_object = ApacheAnalysis(server_ip) test_all_url_title_object.get_all_url_title_data( test_check_true_data_set) check_all_url_title_data = {"/coding/miniprj/material.html": None} self.assertEqual(check_all_url_title_data, test_all_url_title_object.all_url_title_data)
def test_get_html_title(self): test_html_title_object = ApacheAnalysis(server_ip) test_url = 'http://' + server_ip + test_true_url test_true_title = test_html_title_object.get_html_title(test_url) check_true_title = None self.assertEqual(check_true_title, test_true_title) test_false_title = test_html_title_object.get_html_title( test_false_url) check_false_title = None self.assertEqual(check_false_title, test_false_title)
def test_get_ip_url(self): test_data = '200.200.76.130 - - [16/Feb/2019:11:27:20 +0800] "GET /coding/miniprj/material.html HTTP/1.1" 200 38093' test_url_object = ApacheAnalysis(server_ip) check_data = { "ip": '200.200.76.130', "datetime": '16/Feb/2019:11:27:20 +0800', "url": '/coding/miniprj/material.html', "method": 'GET' } test_obj = test_url_object.get_ip_url(test_data) self.assertEqual(test_obj, check_data) test_obj = test_url_object.get_ip_url("xxxxxxxxxx") self.assertEqual(None, test_obj)
def test_get_detailed_data(self): test_get_detailed_data_object = ApacheAnalysis(server_ip) detailed_data = test_get_detailed_data_object.get_detailed_data( test_log_file_data) check_detailed_data = [{ 'url': '/coding/miniprj/material.html', 'ip': '200.200.76.130', 'method': 'GET', 'datetime': '16/Feb/2019:11:27:20 +0800' }, { 'url': '/coding/gitbook/gitbook-plugin-search-plus/search.css', 'ip': '200.200.76.130', 'method': 'GET', 'datetime': '16/Feb/2019:11:27:20 +0800' }] self.assertEqual(check_detailed_data, detailed_data)
def test_all_report(self): test_data_base = DataBase() log_file_data = test_data_base.read_log_file(log_file) test_apache_analysis = ApacheAnalysis(server_ip) detailed_data_list = test_apache_analysis.get_detailed_data( log_file_data) data_set = test_apache_analysis.get_ip_url_set_data(detailed_data_list) test_apache_analysis.get_all_url_title_data(data_set) object_all = test_apache_analysis.get_all_report( detailed_data_list, data_set) # 测试文章报表 object_title = object_all['object_title'] title_report_rows = object_title._rows self.assertEquals(len(title_report_rows), 6) for url_info in title_report_rows: self.assertIn(url_info[0], mock_title_urls) #self.assertIn(url_info[1], mock_title_arttitle) self.assertIn(url_info[2], mock_title_visit_count) self.assertIn(url_info[3], mock_title_ip_count) # 测试完整报表 object_full = object_all['object_full'] full_report_rows = object_full._rows self.assertEquals(len(full_report_rows), 6) for url_info in full_report_rows: self.assertIn(url_info[0], mock_full_ips) self.assertIn(url_info[1], mock_full_urls) self.assertIn(url_info[2], mock_title_visit_count) # 测试ip报表的正确性 object_ip = object_all['object_ip'] ip_report_rows = object_ip._rows self.assertEquals(len(ip_report_rows), 2) for url_info in ip_report_rows: self.assertIn(url_info[0], mock_ip_ips) self.assertIn(url_info[1], mock_ip_visit_count) self.assertIn(url_info[2], mock_ip_article_count)
def test_get_ip_url_set_data(self): test_data_set_object = ApacheAnalysis(server_ip) test_data_set = test_data_set_object.get_ip_url_set_data( test_detailed_data_list) self.assertNotEqual(test_check_false_data_set, test_data_set) self.assertEqual(test_check_true_data_set, test_data_set)
def test_get_url_html(self): test_html_object = ApacheAnalysis(server_ip) #test_html_object.get_url_html(test_true_url) test_false_object = test_html_object.get_url_html(test_false_url) self.assertEqual(None, test_false_object)
# -*- coding: utf-8 -*- from common.data_analysis import ApacheAnalysis from common.data_load import DataBase from tool.param_analysis import apache_param_analysis if __name__ == '__main__': # 参数检查 param = apache_param_analysis() # 加载数据类 data_base = DataBase() # 获取log文件中的数据 log_file_data = data_base.read_log_file(param['log_file_path']) # 生成日志分析类,设定服务器ip apache_analysis = ApacheAnalysis(param['server_ip']) detailed_data_list = apache_analysis.get_detailed_data(log_file_data) data_set = apache_analysis.get_ip_url_set_data(detailed_data_list) apache_analysis.get_all_url_title_data(data_set) if param['report_type'] == 'ip' : apache_analysis.get_ip_report(detailed_data_list,data_set) if param['report_type'] == 'title' : apache_analysis.get_title_report(detailed_data_list,data_set) if param['report_type'] == 'full' : apache_analysis.get_full_report(detailed_data_list,data_set) if param['report_type'] == 'all' : apache_analysis.get_all_report(detailed_data_list,data_set)