Skip to content

Commit

Permalink
REMOVEME: log_file_fmt arg
Browse files Browse the repository at this point in the history
  • Loading branch information
miri64 committed Jul 31, 2020
1 parent 97aa394 commit 5d4bc83
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 10 deletions.
5 changes: 4 additions & 1 deletion 04-single-hop-6lowpan-icmp/test_spec04.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ def test_task04(riot_ctrl):
pinger.ifconfig_set(pinger_netif, "channel", 26)

res = ping6(pinger, pinged_addr,
count=10000, interval=100, packet_size=100)
count=1000, interval=100, packet_size=100)
assert res['stats']['packet_loss'] < 10

# enforce reconnect to pinged's terminal as connection to it get's lost
# in some instances
pinged.stop_term()
assert pktbuf(pinged).is_empty()
assert pktbuf(pinger).is_empty()

Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,25 @@ be a good reference.

```
usage: pytest [--boards] [--hide-output] [--local] [--non-RC] [--self-test]
[--log-file-fmt=[LOG_FILE_FMT]]
optional arguments:
--boards String list of boards to use for the test, can be
IOTLAB_NODE or RIOT BOARDs.
--hide-output Do not log output from nodes
--local Use local boards, default=False (will use IoT-LAB unless
all boards are native)
--local use local boards
--hide-output Don't log output of nodes
--boards=BOARDS list of BOARD's or IOTLAB_NODEs for the test
--non-RC Runs test even if RIOT version under test is not an RC
--self-test Tests the testutils rather than running the release
tests
--log-file-fmt=[LOG_FILE_FMT]
Format for the log file name. The available variables
are: `module`: The module (=specXX) of the test,
`function`: The function (=taskXX) of the test, `node`:
Name of the node (on IoT-LAB the URL of the node,
locally board name + port), `time`: UNIX timestamp at
creation time. If the provided argument is an empty
string the format will be
'{module}-{function}-{node}-{time}.log' and stored in
the current work directory
```

Running `tox` will do most of that for you
Expand Down
49 changes: 45 additions & 4 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
See https://docs.pytest.org/en/stable/fixture.html#conftest-py-sharing-fixture-functions
""" # noqa: E501

import re
import os
import subprocess
import sys
import time
from collections.abc import Iterable

import pytest
Expand Down Expand Up @@ -51,6 +53,19 @@ def pytest_addoption(parser):
"--self-test", action="store_true", default=False,
help="Tests the testutils rather than running the release tests",
)
parser.addoption(
"--log-file-fmt", nargs="?", default=None,
type=testutils.pytest.log_file_fmt,
help="Format for the log file name. The available variables are: "
"`module`: The module (=specXX) of the test, "
"`function`: The function (=taskXX) of the test, "
"`node`: Name of the node (on IoT-LAB the URL of the node, "
"locally board name + port), "
"`time`: UNIX timestamp at creation time. "
"If the provided argument is an empty string the format will be "
"'{module}-{function}-{node}-{time}.log' and stored in the "
"current work directory"
)


def pytest_ignore_collect(path, config):
Expand Down Expand Up @@ -124,6 +139,17 @@ def log_nodes(request):
return not request.config.getoption("--hide-output")


@pytest.fixture
def log_file_fmt(request):
"""
Show output of nodes
:return: True if output of nodes should be shown, False otherwise
"""
# use reverse, since from outside we most of the time _want_ to log
return request.config.getoption("--log-file-fmt")


@pytest.fixture
def local(request):
"""
Expand Down Expand Up @@ -154,10 +180,10 @@ def boards(request):
def get_namefmt(request):
name_fmt = {}
if request.module:
name_fmt["module"] = request.module.__name__.replace("test_", "-")
name_fmt["module"] = request.module.__name__.replace("test_", "")
if request.function:
name_fmt["function"] = request.function.__name__ \
.replace("test_", "-")
.replace("test_", "")
return name_fmt


Expand Down Expand Up @@ -185,7 +211,7 @@ def nodes(local, request, boards):
name_fmt = get_namefmt(request)
# Start IoT-LAB experiment if requested
exp = IoTLABExperiment(
name="RIOT-release-test{module}{function}".format(**name_fmt),
name="RIOT-release-test-{module}-{function}".format(**name_fmt),
ctrls=ctrls,
site=os.environ.get("IOTLAB_SITE", DEFAULT_SITE))
RUNNING_EXPERIMENTS.append(exp)
Expand Down Expand Up @@ -216,7 +242,7 @@ def update_env(node, modules=None, cflags=None, port=None, termflags=None):


@pytest.fixture
def riot_ctrl(log_nodes, nodes, riotbase):
def riot_ctrl(log_nodes, log_file_fmt, nodes, riotbase, request):
"""
Factory to create RIOTCtrl objects from list nodes provided by nodes
fixture
Expand All @@ -232,6 +258,21 @@ def ctrl(nodes_idx, application_dir, shell_interaction_cls,
else:
node = nodes[nodes_idx]
update_env(node, modules, cflags, port, termflags)
# if the nodes are not logged, there is no sense in logging to a file
# so check if nodes are logged as well as if they should be logged to a
# file
if log_nodes and log_file_fmt:
if node.env.get("IOTLAB_NODE"):
node_name = node.env["IOTLAB_NODE"]
else:
node_name = "{}-{}".format(
node.board(), re.sub(r'\W+', '-', node.env["PORT"]))
node.env["TERMLOG"] = os.path.join(
os.getcwd(), log_file_fmt.format(
node=node_name, time=int(time.time()),
**get_namefmt(request)
)
)
# need to access private member here isn't possible otherwise sadly :(
# pylint: disable=W0212
node._application_directory = os.path.join(riotbase, application_dir)
Expand Down
20 changes: 20 additions & 0 deletions testutils/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ def list_from_string(list_str=None):
return [v for v in value if v]


def log_file_fmt(fmt_str=None):
"""Get defaulted format string
>>> import os
>>> log_file_fmt(None)
>>> os.path.basename(log_file_fmt(''))
'{module}-{function}-{node}-{time}.log'
>>> log_file_fmt('foobar')
'foobar'
>>> log_file_fmt('{module}-{function}-{time}-{node}')
'{module}-{function}-{time}-{node}'
"""
if fmt_str is not None:
if len(fmt_str) > 0:
return fmt_str
return os.path.join(os.getcwd(),
"{module}-{function}-{node}-{time}.log")
return None


def check_ssh():
user, _ = IoTLABExperiment.user_credentials()
if user is None:
Expand Down

0 comments on commit 5d4bc83

Please # to comment.