Commit 908d37c1 authored by Matous Holinka's avatar Matous Holinka Committed by Ondřej Zajíček

netlab/tests/ updated:

+ function 'test_logs' allows some types of messages (DBG, TRACE, INFO),
  but the rest of the log messages should rase exceptions

+ function 'check_expected_logs_timeout' periodically calls function
  'check_expected_logs' which watches log messages every second

- functions 'modify_command', 'save_stdout', 'get_attributes_from_output',
  'check_running_protocols' and 'check_protocol_state' were removed
parent 1ca0e00a
......@@ -2,6 +2,7 @@ import os
import sys
import time
import subprocess
from typing import List
import tests.config as cf
......@@ -83,39 +84,39 @@ def read_file(name: str) -> None:
def modify_command(dev: str) -> str:
cmd = f"cd {dev} && sudo ./birdc -l show protocols"
return cmd
def test_logs(dev: str, log_messages: List[str],
filename: str = "bird.log") -> None:
pattern = "DBG|TRACE|INFO"
logfile = f"{dev}/{filename}"
if not log_messages:
if not os.system(f"egrep -v '{pattern}' {logfile}"):
assert False, "Log file contains incorrect message"
def save_stdout(cmd: str) -> str:
"""Run the command :cmd: and return it as variable (stdout)"""
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
proc_stdout = process.communicate()[0].strip()
elif log_messages:
check_expected_logs_timeout(log_messages, logfile)
pattern = f"{pattern}|{'|'.join(log_messages)}"
return proc_stdout.decode("utf-8")
if not os.system(f"egrep -v '{pattern}' {logfile}"):
assert False, "Log file contains incorrect message"
def get_attributes_from_output(data: str, protocol: str) -> list:
"""Parse the string from parameter and return list"""
return [line.split() for line in data.split("\n") if line.startswith(protocol)]
def check_running_protocols(data: list) -> None:
"""Assertion that all protocols are running properly"""
assert "Running" in data
def check_expected_logs_timeout(log_messages: str, logfile: str) -> None:
for _ in range(61):
if check_expected_logs(log_messages, logfile):
assert False, "Log file contains incorrect message"
def check_protocol_state(dev: str, protocol: str) -> None:
def check_expected_logs(log_messages: List[str], logfile: str) -> bool:
1. Modify the specified bash command with proper variable
2. Send bash command into the function and return output as str
3. Collect all desirable lines into single list
4. If every single line contains "Running" as a state --> pass
TRUE -> if there IS NOT variable `message` in variable `logfile`
FALSE -> if there IS variable `message` in variable `logfile`
command = modify_command(dev)
decoded = save_stdout(command)
clean_str = get_attributes_from_output(decoded, protocol)
for message in log_messages:
if os.system(f"egrep '{message}' {logfile}"):
return False
return True
for line in clean_str:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment