Beispiel #1
0
def test_tracer():
    tracer = TraceHelper()
    try:
        tracer.enable_logging("sys/tg_test/1", LogLevel.LOG_DEBUG)
        tracer.wait_until_message_received("DataGenerator::generating data", 20)
        n_msg = len(tracer.get_messages())
        assert n_msg > 0
    finally:
        tracer.disable_logging("sys/tg_test/1")
    
    # test that we didn't get any additional messages after disabling the logging
    sleep(1)
    assert n_msg == len(tracer.get_messages())
Beispiel #2
0
 def __init__(self):
     self.tracer=TraceHelper()
    # self.tracer.disable_logging()
     self.tracer.reset_messages()
     self.traces=[]
     self.log_level=LogLevel.LOG_DEBUG
Beispiel #3
0
class DeviceLoggingImplWithTraceHelper():
    
    def __init__(self):
        self.tracer=TraceHelper()
       # self.tracer.disable_logging()
        self.tracer.reset_messages()
        self.traces=[]
        self.log_level=LogLevel.LOG_DEBUG
    
    def set_logging_level(self,level):
        mapping = {'DEBUG' : LogLevel.LOG_DEBUG,
                   'INFO' : LogLevel.LOG_INFO,
                   'WARNING': LogLevel.LOG_WARN,
                   'OFF': LogLevel.LOG_OFF,
                   'FATAL':LogLevel.LOG_FATAL}
        self.log_level=mapping[level]
    
    def update_traces(self,traces):
        if type(traces) == list:
            self.traces.extend(traces)
        if type(traces) == str:
            self.traces.append(traces)

    def start_tracing(self):
        for trace in self.traces:
            LOGGER.debug('setting traces for %s',trace)
            self.tracer.enable_logging(trace, self.log_level)

    def stop_tracing(self):
        for trace in self.traces:
            LOGGER.debug('stopping traces for %s',trace)
            self.tracer.disable_logging(trace)
    
    def get_logging(self):
        return self.tracer.get_messages()

    def wait_until_message_received(self,message, timeout):
        self.tracer.wait_until_message_received(message, timeout)

    def _format_event_data(self,e,format='string'):
        if format=='string':
            message =" reception date: {} message: '{}' error:{}".\
                format(
                    e.reception_date,
                    e.attr_value.value,
                    e.err
                )
        elif format=='dict':
            message = {
                'reception date':e.reception_date,
                'message':e.attr_value.value,
                'error': e.err}
        return message

    def get_printable_messages(self):
        messages = self.tracer.get_messages()
        msg_counter = 0
        printout = ''
        for message in messages:
            msg_counter +=1
            printout += str(msg_counter) + self._format_event_data(message) + "\n"
        return printout

    def get_messages_as_list_dict(self):
        messages = self.tracer.get_messages()
        return [self._format_event_data(message,format="dict") for message in messages]
Beispiel #4
0
 def is_local(self,port=9200,elastic_name='elastic-logging'):
     HELM_RELEASE = os.environ.get('HELM_RELEASE')
     elastic_host = '{}-{}'.format(elastic_name,HELM_RELEASE)
     tracer = TraceHelper()
     return tracer.check_port(elastic_host, port) == 0
Beispiel #5
0
def test_logging_namespace(run_context):
    """Test that we only get logs from our namespace"""

    ES_HOST = "elastic-logging-{}".format(run_context.HELM_RELEASE)
    ES_PORT = "9200"
    NAMESPACE = run_context.KUBE_NAMESPACE
    INDEX_MATCH = "lo*-*"
    local = True
    tracer = TraceHelper()
    if tracer.check_port(ES_HOST, ES_PORT) != 0:
        ES_HOST = "192.168.93.94"
        ES_PORT = "9200"
        NAMESPACE = run_context.KUBE_NAMESPACE
        INDEX_MATCH = "fi*-*"
        local = False

    es = Elasticsearch(["{}:{}".format(ES_HOST, ES_PORT)],
                       use_ssl=False,
                       verify_certs=False,
                       ssl_show_warn=False)

    indexes = es.indices.get(INDEX_MATCH)

    assert indexes, "No indexes found that match [{}]".format(INDEX_MATCH)
    last_index = sorted(
        indexes,
        key=lambda i: indexes[i]['settings']['index']['creation_date'],
        reverse=True)[0]

    label_namespace = "container.labels.io_kubernetes_pod_namespace"

    if (local):
        label_namespace = "kubernetes_namespace"

    search_namespace = {
        "query": {
            "match": {
                label_namespace: {
                    "query": NAMESPACE
                }
            }
        }
    }

    search_not_namespace = {
        "query": {
            "bool": {
                "must_not": [{
                    "term": {
                        label_namespace: NAMESPACE
                    }
                }],
                "must": [{
                    "exists": {
                        "field": label_namespace
                    }
                }]
            }
        }
    }

    res = es.search(index=last_index, body=search_namespace)
    assert res['hits']['total']['value'], (
        "Found no matches for namespace [{}] using"
        " index [{}]".format(NAMESPACE, last_index))
    if (local):
        res = es.search(index=last_index, body=search_not_namespace)
        assert not res['hits']['total'][
            'value'], "Found matches on other namespaces"
Beispiel #6
0
class DeviceLoggingImplWithTraceHelper:
    def __init__(self):
        self.tracer = TraceHelper()
        # self.tracer.disable_logging()
        self.tracer.reset_messages()
        self.traces = []
        self.log_level = LogLevel.LOG_DEBUG

    def set_logging_level(self, level):
        mapping = {
            "DEBUG": LogLevel.LOG_DEBUG,
            "INFO": LogLevel.LOG_INFO,
            "WARNING": LogLevel.LOG_WARN,
            "OFF": LogLevel.LOG_OFF,
            "FATAL": LogLevel.LOG_FATAL,
        }
        self.log_level = mapping[level]

    def update_traces(self, traces):
        if type(traces) == list:
            self.traces.extend(traces)
        if type(traces) == str:
            self.traces.append(traces)

    def start_tracing(self):
        for trace in self.traces:
            LOGGER.debug("setting traces for %s", trace)
            self.tracer.enable_logging(trace, self.log_level)

    def stop_tracing(self):
        for trace in self.traces:
            LOGGER.debug("stopping traces for %s", trace)
            self.tracer.disable_logging(trace)

    def get_logging(self):
        return self.tracer.get_messages()

    def wait_until_message_received(self, message, timeout):
        self.tracer.wait_until_message_received(message, timeout)

    def _format_event_data(self, e, format="string"):
        if format == "string":
            message = " reception date: {} message: '{}' error:{}".format(
                e.reception_date, e.attr_value.value, e.err)
        elif format == "dict":
            message = {
                "reception date": e.reception_date,
                "message": e.attr_value.value,
                "error": e.err,
            }
        return message

    def get_printable_messages(self):
        messages = self.tracer.get_messages()
        msg_counter = 0
        printout = ""
        for message in messages:
            msg_counter += 1
            printout += str(msg_counter) + self._format_event_data(
                message) + "\n"
        return printout

    def get_messages_as_list_dict(self):
        messages = self.tracer.get_messages()
        return [
            self._format_event_data(message, format="dict")
            for message in messages
        ]
Beispiel #7
0
 def is_local(self, port=9200, elastic_name="elastic-logging"):
     HELM_RELEASE = os.environ.get("HELM_RELEASE")
     elastic_host = "{}-{}".format(elastic_name, HELM_RELEASE)
     tracer = TraceHelper()
     return tracer.check_port(elastic_host, port) == 0
Beispiel #8
0
def test_tracer_update():
    logging.info("instantiating TracerHelper")
    tracer = TraceHelper()
    try:
        logging.info("enable logging on sys/tg_test/1")
        tracer.enable_logging("sys/tg_test/1", LogLevel.LOG_DEBUG)
        tracer.wait_until_message_received("DataGenerator::generating data", 20)
    finally:
        tracer.disable_logging("sys/tg_test/1")
        logging.info("disabled logging on sys/tg_test/1")
    old_messages = tracer.get_messages()
    tracer = TraceHelper()
    tracer.reset_messages()
    logging.info("enable logging on ska_mid/tm_central/central_node")
    tracer.enable_logging("ska_mid/tm_central/central_node", LogLevel.LOG_DEBUG)
    sleep(1)
    new_messages = tracer.get_messages()
    try:
        logging.info("new messages")
        for new_message in new_messages:
            logging.info("date: %s message: %s",new_message.reception_date,new_message.attr_value.value)
            logging.info("old messages")
            for old_message in old_messages:
                logging.info("date: %s message: %s",old_message.reception_date,old_message.attr_value.value)
                assert_that(new_message).is_not_equal_to(old_message)
    finally:
        tracer.disable_logging("ska_mid/tm_central/central_node")
        logging.info("disabled logging on ska_mid/tm_central/central_node")