Logging changes.

This commit is contained in:
Roland Hedberg
2013-08-26 09:47:02 +02:00
parent edda40422c
commit d11a63d643
3 changed files with 75 additions and 36 deletions

View File

@@ -3,25 +3,55 @@ import argparse
import sys
from importlib import import_module
from idp_test import Trace, SCHEMA
from idp_test import SCHEMA
from saml2 import root_logger
from saml2.mdstore import MetadataStore, MetaData
from saml2.saml import NAME_FORMAT_UNSPECIFIED
from saml2.server import Server
from saml2.config import IdPConfig
from saml2.config import logging
from sp_test.base import Conversation
from srtest import FatalError, CheckError
from srtest import exception_trace
from saml2test import FatalError
from saml2test import CheckError
from saml2test import ContextFilter
from saml2test import exception_trace
__author__ = 'rolandh'
formatter_2 = logging.Formatter(
"%(delta).6f - %(levelname)s - [%(name)s] %(message)s")
cf = ContextFilter()
cf.start()
memoryhandler = logging.handlers.MemoryHandler(1024*10, logging.DEBUG)
pys_streamhandler = logging.StreamHandler(sys.stderr)
pys_streamhandler.setFormatter(formatter_2)
pys_memoryhandler = logging.handlers.MemoryHandler(1024*10, logging.DEBUG)
pys_memoryhandler.setFormatter(formatter_2)
pys_memoryhandler.addFilter(cf)
root_logger.addHandler(pys_memoryhandler)
root_logger.setLevel(logging.DEBUG)
tracelog = logging.getLogger(__name__)
t_memoryhandler = logging.handlers.MemoryHandler(1024*10, logging.DEBUG)
t_memoryhandler.addFilter(cf)
tracelog.addHandler(t_memoryhandler)
tracelog.setLevel(logging.DEBUG)
saml2testlog = logging.getLogger("saml2test")
saml2testlog.addHandler(t_memoryhandler)
saml2testlog.setLevel(logging.DEBUG)
class Client(object):
def __init__(self, operations, check_factory):
self.trace = Trace()
self.operations = operations
self.tests = None
self.check_factory = check_factory
@@ -92,6 +122,16 @@ class Client(object):
return info
def output_log(self, memhndlr, hndlr2):
"""
"""
print >> sys.stderr, 80 * ":"
hndlr2.setFormatter(formatter_2)
memhndlr.setTarget(hndlr2)
memhndlr.flush()
memhndlr.close()
def run(self):
self.args = self._parser.parse_args()
@@ -124,7 +164,7 @@ class Client(object):
opers = oper["sequence"]
conv = Conversation(self.idp, self.idp_config, self.trace,
conv = Conversation(self.idp, self.idp_config,
self.interactions, self.json_config,
check_factory=self.check_factory,
entity_id=self.entity_id,
@@ -134,13 +174,11 @@ class Client(object):
self.test_log = conv.test_output
tsum = self.test_summation(self.args.oper)
print >>sys.stdout, json.dumps(tsum)
if tsum["status"] > 1 or self.args.debug:
print >> sys.stderr, self.trace
err = None
except CheckError, err:
self.test_log = conv.test_output
tsum = self.test_summation(self.args.oper)
print >>sys.stdout, json.dumps(tsum)
print >> sys.stderr, self.trace
except FatalError, err:
if conv:
self.test_log = conv.test_output
@@ -149,7 +187,6 @@ class Client(object):
self.test_log = exception_trace("RUN", err)
tsum = self.test_summation(self.args.oper)
print >>sys.stdout, json.dumps(tsum)
print >> sys.stderr, self.trace
except Exception, err:
if conv:
self.test_log = conv.test_output
@@ -159,6 +196,12 @@ class Client(object):
tsum = self.test_summation(self.args.oper)
print >>sys.stdout, json.dumps(tsum)
if self.args.pysamllog:
self.output_log(pys_memoryhandler, pys_streamhandler)
if tsum["status"] > 1 or self.args.debug or err:
self.output_log(t_memoryhandler, pys_streamhandler)
def setup(self):
self.json_config = self.json_config_file()

View File

@@ -11,14 +11,14 @@ from saml2 import BINDING_HTTP_REDIRECT
from saml2 import BINDING_HTTP_POST
from saml2.request import SERVICE2REQUEST
from srtest import CheckError
from srtest.check import Check
from srtest.check import ExpectedError
from srtest.check import INTERACTION
from srtest.check import STATUSCODE
from srtest.interaction import Action
from srtest.interaction import Interaction
from srtest.interaction import InteractionNeeded
from saml2test import CheckError
from saml2test.check import Check
from saml2test.check import ExpectedError
from saml2test.check import INTERACTION
from saml2test.check import STATUSCODE
from saml2test.interaction import Action
from saml2test.interaction import Interaction
from saml2test.interaction import InteractionNeeded
from sp_test.tests import ErrorResponse
@@ -32,13 +32,12 @@ camel2underscore = re.compile('((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))')
class Conversation():
def __init__(self, instance, config, trace, interaction, json_config,
def __init__(self, instance, config, interaction, json_config,
check_factory, entity_id, msg_factory=None,
features=None, verbose=False, constraints=None,
expect_exception=None):
self.instance = instance
self._config = config
self.trace = trace
self.test_output = []
self.features = features
self.verbose = verbose
@@ -74,14 +73,14 @@ class Conversation():
def check_severity(self, stat):
if stat["status"] >= 4:
self.trace.error("WHERE: %s" % stat["id"])
self.trace.error("STATUS:%s" % STATUSCODE[stat["status"]])
logger.error("WHERE: %s" % stat["id"])
logger.error("STATUS:%s" % STATUSCODE[stat["status"]])
try:
self.trace.error("HTTP STATUS: %s" % stat["http_status"])
logger.error("HTTP STATUS: %s" % stat["http_status"])
except KeyError:
pass
try:
self.trace.error("INFO: %s" % stat["message"])
logger.error("INFO: %s" % stat["message"])
except KeyError:
pass
@@ -199,8 +198,7 @@ class Conversation():
raise FatalError(
"Too long sequence of redirects: %s" % rdseq)
self.trace.reply("REDIRECT TO: %s" % url)
logger.debug("REDIRECT TO: %s" % url)
logger.info("--> REDIRECT TO: %s" % url)
# If back to me
for_me = False
try:
@@ -343,8 +341,6 @@ class Conversation():
raise FatalError(
"Too long sequence of redirects: %s" % rdseq)
self.trace.reply("REDIRECT TO: %s" % url)
logger.debug("REDIRECT TO: %s" % url)
# If back to me
for_me = False
try:
@@ -363,7 +359,7 @@ class Conversation():
raise FatalError("%s" % err)
content = _response.text
self.trace.reply("CONTENT: %s" % content)
logger.info("<-- CONTENT: %s" % content)
self.position = url
self.last_content = content
self.response = _response
@@ -381,11 +377,11 @@ class Conversation():
_spec = self.interaction.pick_interaction(_base, content)
except InteractionNeeded:
self.position = url
self.trace.error("Page Content: %s" % content)
logger.error("Page Content: %s" % content)
raise
except KeyError:
self.position = url
self.trace.error("Page Content: %s" % content)
logger.error("Page Content: %s" % content)
self.err_check("interaction-needed")
if _spec == _last_action:
@@ -396,14 +392,14 @@ class Conversation():
_last_action = _spec
if len(_spec) > 2:
self.trace.info(">> %s <<" % _spec["page-type"])
logger.info(">> %s <<" % _spec["page-type"])
if _spec["page-type"] == "login":
self.login_page = content
_op = Action(_spec["control"])
try:
_response = _op(self.instance, self, self.trace, url,
_response = _op(self.instance, self, logger, url,
_response, content, self.features)
if isinstance(_response, dict):
self.last_response = _response

View File

@@ -1,10 +1,10 @@
import inspect
import sys
from srtest.check import Check
from srtest.check import CRITICAL
from srtest import check
from srtest.interaction import Interaction
from saml2test.check import Check
from saml2test.check import CRITICAL
from saml2test import check
from saml2test.interaction import Interaction
__author__ = 'rolandh'