コード例 #1
0
 def run_test_build_journal(self, rule_id, test, journal_file, tablename):
     """
     Build journal entries from a test within a specified rule_id
     Pass in the rule_id, test object, and path to journal_file 
     DB MUST already be instantiated from util.instantiate_database()
     """
     conn = sqlite3.connect(journal_file)
     conn.text_factory = str
     cur = conn.cursor()
     for i, stage in enumerate(test.stages):
         response = None
         status = None
         try:
             print 'Running test %s from rule file %s' % (test.test_title,
                                                          rule_id)
             http_ua = http.HttpUA()
             start = datetime.datetime.now()
             http_ua.send_request(stage.input)
             response = http_ua.response_object.response
             status = http_ua.response_object.status
         except errors.TestError as e:
             print '%s got error. %s' % (test.test_title, str(e))
             response = str(e)
             status = -1
         finally:
             end = datetime.datetime.now()
             ins_q = util.get_insert_statement(tablename)
             cur.execute(ins_q, (rule_id, test.test_title, start, end,
                                 response, status, i))
             conn.commit()
コード例 #2
0
    def run_stage(self, stage, logger_obj=None, http_ua=None):
        """
        Runs a stage in a test by building an httpua object with the stage
        input, waits for output then compares expected vs actual output
        http_ua can be passed in to persist cookies
        """

        # Send our request (exceptions caught as needed)
        if stage.output.expect_error:
            with pytest.raises(errors.TestError) as excinfo:
                if not http_ua:
                    http_ua = http.HttpUA()
                start = datetime.datetime.now()
                http_ua.send_request(stage.input)
                end = datetime.datetime.now()
            print '\nExpected Error: %s' % str(excinfo)
        else:
            if not http_ua:
                http_ua = http.HttpUA()
            start = datetime.datetime.now()
            http_ua.send_request(stage.input)
            end = datetime.datetime.now()
        if (stage.output.log_contains_str or stage.output.no_log_contains_str) \
        and logger_obj is not None:
            logger_obj.set_times(start, end)
            lines = logger_obj.get_logs()
            if stage.output.log_contains_str:
                self.test_log(lines, stage.output.log_contains_str, False)
            if stage.output.no_log_contains_str:
                # The last argument means that we should negate the resp
                self.test_log(lines, stage.output.no_log_contains_str, True)
        if stage.output.response_contains_str:
            self.test_response(http_ua.response_object,
                               stage.output.response_contains_str)
        if stage.output.status:
            self.test_status(stage.output.status,
                             http_ua.response_object.status)
コード例 #3
0
ファイル: testrunner.py プロジェクト: vitty8484/ftw-1
 def run_test_build_journal(self,
                            rule_id,
                            test,
                            journal_file,
                            tablename,
                            destaddr,
                            callback,
                            headers={}):
     """
     Build journal entries from a test within a specified rule_id
     Pass in the rule_id, test object, and path to journal_file 
     DB MUST already be instantiated from util.instantiate_database()
     """
     conn = sqlite3.connect(journal_file)
     conn.text_factory = str
     cur = conn.cursor()
     for i, stage in enumerate(test.stages):
         response = None
         status = None
         try:
             if callback is not None:
                 callback(test, rule_id)
             if destaddr is not None:
                 stage.input.dest_addr = destaddr
             '''
                 Merge in/override the headers that were passed in by
                 the caller.
             '''
             stage.input.headers.update(headers)
             http_ua = http.HttpUA()
             start = datetime.datetime.now()
             http_ua.send_request(stage.input)
             response = http_ua.response_object.response
             status = http_ua.response_object.status
         except errors.TestError as e:
             print '%s got error. %s' % (test.test_title, str(e))
             response = str(e)
             status = -1
         finally:
             end = datetime.datetime.now()
             ins_q = util.get_insert_statement(tablename)
             cur.execute(ins_q, (rule_id, test.test_title, start, end,
                                 response, status, i))
             conn.commit()
コード例 #4
0
 def run_stage(self, stage, logger_obj=None):
     """
     Runs a stage in a test by building an httpua object with the stage
     input, waits for output then compares expected vs actual output
     """
     http_ua = http.HttpUA(stage.input)
     if stage.output.log_contains_str and logger_obj is not None:
         start = datetime.datetime.now()
         http_ua.send_request()
         end = datetime.datetime.now()
         logger_obj.set_times(start, end)
         lines = logger_obj.get_logs()
         self.test_log(lines, stage.output.log_contains_str)
     else:
         http_ua.send_request()
     if stage.output.html_contains_str:
         self.test_response(http_ua.response_object,
                            stage.output.html_contains_str)
     if stage.output.status:
         self.test_status(stage.output.status,
                          http_ua.request_object.status)