Skip to content

Commit

Permalink
fixes logging with examples and templates
Browse files Browse the repository at this point in the history
Signed-off-by: Beraldo Leal <[email protected]>
  • Loading branch information
beraldoleal authored and richtja committed Oct 12, 2021
1 parent aa044c8 commit 6b9a6eb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
5 changes: 4 additions & 1 deletion examples/tests/guest_hostname.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import logging


LOG = logging.getLogger('avocado.vt.examples.guest_hostname')


def run(test, params, env):
"""
Logs guest's hostname.
Expand All @@ -30,4 +33,4 @@ def run(test, params, env):
output = session.cmd_output("hostname")

# 4) log the output
logging.info("The output of 'hostname' command from guest is '%s'", output)
LOG.info("The output of 'hostname' command from guest is '%s'", output)
7 changes: 5 additions & 2 deletions examples/tests/hostname.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from avocado.utils import process


LOG = logging.getLogger('avocado.vt.examples.hostname')


def run(test, params, env):
"""
Logs the host name and exits
Expand All @@ -20,5 +23,5 @@ def run(test, params, env):
:param env: Dictionary with test environment.
"""
result = process.run("hostname")
logging.info("Output of 'hostname' cmd is '%s'",
result.stdout_text)
LOG.info("Output of 'hostname' cmd is '%s'",
result.stdout_text)
7 changes: 5 additions & 2 deletions examples/tests/ls_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import logging


LOG = logging.getLogger('avocado.vt.examples.lsdisk')


def run(test, params, env):
"""
Logs guest's disk partitions
Expand All @@ -26,7 +29,7 @@ def run(test, params, env):
vm = env.get_vm(params["main_vm"])
session = vm.wait_for_login()
output = session.cmd_output("ls /dev/[hsv]d* -1")
logging.info("Guest disks are:\n%s", output)
LOG.info("Guest disks are:\n%s", output)

# Let's get some monitor data
monitor = vm.monitor
Expand All @@ -35,4 +38,4 @@ def run(test, params, env):
# output = monitor.info("block", debug=False)
# Following command unifies the response no matter which monitor is used
output = monitor.info_block(debug=False)
logging.info("info block:\n%s", output)
LOG.info("info block:\n%s", output)
17 changes: 10 additions & 7 deletions examples/tests/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
from virttest import error_context


LOG = logging.getLogger('avocado.vt.examples.service')


# error_context.context_aware decorator initializes context, which provides additional
# information on exceptions.
@error_context.context_aware
Expand All @@ -37,37 +40,37 @@ def run(test, params, env):
if params.get('test_on_guest') == "yes":
# error_context.context() is common method to log test steps used to verify
# what exactly was tested.
error_context.context("Using guest.", logging.info)
error_context.context("Using guest.", LOG.info)
vm = env.get_vm(params["main_vm"])
session = vm.wait_for_login()
# RemoteRunner is object, which simulates the utils.run() behavior
# on remote consoles
runner = remote.RemoteRunner(session=session).run
else:
error_context.context("Using host", logging.info)
error_context.context("Using host", LOG.info)
runner = process.run

error_context.context("Initialize service manager", logging.info)
error_context.context("Initialize service manager", LOG.info)
service = SpecificServiceManager(params["test_service"], runner)

error_context.context("Testing service %s" %
params["test_service"], logging.info)
params["test_service"], LOG.info)
original_status = service.status()
logging.info("Original status=%s", original_status)
LOG.info("Original status=%s", original_status)

if original_status is True:
service.stop()
time.sleep(5)
if service.status() is not False:
logging.error("Fail to stop service")
LOG.error("Fail to stop service")
service.start()
raise exceptions.TestFail("Fail to stop service")
service.start()
else:
service.start()
time.sleep(5)
if service.status() is not True:
logging.error("Fail to start service")
LOG.error("Fail to start service")
service.stop()
raise exceptions.TestFail("Fail to start service")
service.start()
Expand Down
5 changes: 4 additions & 1 deletion examples/tests/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import logging


LOG = logging.getLogger('avocado.test')


def run(test, params, env):
"""
Docstring describing template.
Expand All @@ -23,4 +26,4 @@ def run(test, params, env):
timeout = float(params.get("login_timeout", 240))
session = vm.wait_for_login(timeout=timeout)
uptime = session.cmd("uptime")
logging.info("Guest uptime result is: %s", uptime)
LOG.info("Guest uptime result is: %s", uptime)

0 comments on commit 6b9a6eb

Please sign in to comment.