예제 #1
0
    def test_custom_format(self):
        """Initializing an Analyzer with custom log format."""
        # this is essentially the nginx pattern and time format
        pattern = r'''
            ^(?P<remote_addr>\S+)\s-\s(?P<remote_user>\S+)\s
            \[(?P<timestamp>.*?)\]\s
            "(?P<verb>[A-Z]+)\s(?P<path>[^?]+)(?:\?.+)?\sHTTP/(?:[\d.]+)"\s
            (?P<status>\d+?)\s
            (?P<body_bytes_sent>\d+?)\s
            "(?P<http_referer>[^"]+?)"\s
            "(?P<http_user_agent>[^"]+?)"\s
            "(?P<http_x_forwarded_for>[^"]+?)"\s
            (?P<request_time>[\d\.]+)\s
            (?P<upstream_response_time>[\d\.]+)\s?
            (?P<pipe>\S+)?$'''
        time_format = '%d/%b/%Y:%H:%M:%S +0000'

        analyzer.Analyzer(log=self.log,
                          format='custom',
                          pattern=pattern,
                          time_format=time_format,
                          verbs=analyzer.DEFAULT_VERBS,
                          status_codes=analyzer.DEFAULT_STATUS_CODES,
                          paths=analyzer.DEFAULT_PATHS,
                          max_age=None,
                          path_stats=False)
예제 #2
0
    def setup(self):
        """Define analyzer for Analyzer tests."""
        self.now = datetime.datetime.now()
        self.log1_date = self.now - datetime.timedelta(minutes=45)
        self.log2_date = self.now - datetime.timedelta(minutes=10)
        with tempfile.NamedTemporaryFile(mode='w',
                                         delete=False,
                                         **TMPFILE_ARGS) as log:
            log.write('123.123.123.123 - test_client [{}] '
                      '"POST /auth/token HTTP/1.1" 200 174 "-" '
                      '"OAuthClient 0.2.3" "-" 0.633 0.633\n'.format(
                          self.log1_date.strftime(NGINX.time_format)))
            log.write('234.234.234.234 - - [{}] '
                      '"GET /sub/folder HTTP/1.1" 200 110 "-" '
                      '"UAString" "-" 0.312 0.312\n'.format(
                          self.log2_date.strftime(NGINX.time_format)))
            self._log_name = log.name
        with open(self._log_name, mode='r') as log:
            self.log = log.readlines()

        self.analyzer = analyzer.Analyzer(
            log=self.log,
            format='nginx',
            pattern=None,
            time_format=None,
            verbs=analyzer.DEFAULT_VERBS,
            status_codes=analyzer.DEFAULT_STATUS_CODES,
            paths=analyzer.DEFAULT_PATHS,
            max_age=None,
            path_stats=False)
예제 #3
0
 def test_execute_non_match(self):
     """All entries that do not match the log format are skipped."""
     log = list(self.log)
     log.append('malformatted entry')
     amalformatted = analyzer.Analyzer(log, format='nginx', path_stats=True)
     report = amalformatted()
     assert report.requests == 2
     assert (sorted(
         report._path_requests.keys()) == ['/auth/token', '/sub/folder'])
예제 #4
0
 def test_execute_max_age(self):
     """All entries older than max-age are skipped."""
     amaxage = analyzer.Analyzer(self.log,
                                 format='nginx',
                                 max_age=15,
                                 path_stats=True)
     report = amaxage()
     assert report.requests == 1
     assert sorted(report._path_requests.keys()) == ['/sub/folder']
예제 #5
0
 def test_execute_path_mismatch(self):
     """All entries not matching the defined paths are skipped."""
     log = list(self.log)
     log.append(self.log[0].replace('/auth/token', '/not/interesting'))
     apathmismatch = analyzer.Analyzer(log,
                                       format='nginx',
                                       paths=['/auth', '/sub/folder'])
     report = apathmismatch()
     assert report.requests == 2
     assert (sorted(
         report._path_requests.keys()) == ['/auth', '/sub/folder'])
예제 #6
0
 def test_execute_too_new(self):
     """All entries added after starting the analysis are skipped."""
     log = list(self.log)
     date_too_new = self.now + datetime.timedelta(minutes=3)
     log.append('123.123.123.123 - test_client [{}] '
                '"POST /auth/token HTTP/1.1" 200 174 "-" '
                '"OAuthClient 0.2.3" "-" 0.633 0.633\n'.format(
                    date_too_new.strftime(NGINX.time_format)))
     atoonew = analyzer.Analyzer(log,
                                 format='nginx',
                                 max_age=50,
                                 path_stats=True)
     report = atoonew()
     assert report.requests == 2
     assert (sorted(
         report._path_requests.keys()) == ['/auth/token', '/sub/folder'])
예제 #7
0
 def test_missing_format(self):
     """For unknown formats, a MissingFormatError is raised."""
     with pytest.raises(MissingFormatError):
         analyzer.Analyzer(log=self.log, format='unknown')