Skip to content

Commit

Permalink
chg: dev: Add pytest plugin to move container logs to test name folder
Browse files Browse the repository at this point in the history
  • Loading branch information
baraserg committed Jun 18, 2016
1 parent e71dc1e commit 7b08cf2
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/topology_docker_openswitch/openswitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import logging
from sys import argv
from time import sleep
from os.path import exists
from os.path import exists, split
from json import dumps, loads
from shlex import split as shsplit
from subprocess import check_call, check_output
Expand Down Expand Up @@ -103,7 +103,7 @@ def create_interfaces():
raise Exception('Failed to map ports with port labels')
# Writting mapping to file
shared_dir_tmp = str(__file__).split('openswitch_setup.py')[0]
shared_dir_tmp = split(__file__)[0]
with open('{}/port_mapping.json'.format(shared_dir_tmp), 'w') as json_file:
json_file.write(dumps(mapping_ports))
Expand Down
20 changes: 20 additions & 0 deletions lib/topology_docker_openswitch/plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

"""
topology_docker_openswitch module entry point.
"""
72 changes: 72 additions & 0 deletions lib/topology_docker_openswitch/plugin/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2016 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from os.path import exists, basename, splitext
from os import makedirs
from shutil import copytree, Error
from logging import warning


def pytest_runtest_teardown(item):
"""
pytest hook to get the name of the test executed, it creates a folder with
the name, then copies the folders defined in the shared_dir_mount attribute
of each openswitch container, additionally the /var/log/messages of the
container is copied to the same folder.
"""
if 'topology' in item.funcargs:
topology = item.funcargs['topology']
if topology.engine == 'docker':
logs_path = '/var/log/messages'
for node in topology.nodes:
node_obj = topology.get(node)
if node_obj.metadata['type'] == 'openswitch':
shared_dir = node_obj.shared_dir
try:
node_obj.send_command(
'cat {} > {}/var_messages.log'.format(
logs_path, node_obj.shared_dir_mount
),
shell='bash',
silent=True
)
except Error:
warning(
'Unable to get {} from container'.format(logs_path)
)
test_suite = splitext(basename(item.parent.name))[0]
path_name = '/tmp/{}_{}_{}'.format(
test_suite, item.name, str(id(item))
)
if not exists(path_name):
makedirs(path_name)
try:
copytree(
shared_dir, '{}/{}'.format(
path_name,
basename(shared_dir)
)
)
except Error as err:
errors = err.args[0]
for error in errors:
src, dest, msg = error
warning(
'Unable to copy file {}, Error {}'.format(
src, msg
)
)
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ def find_requirements(filename):

# Entry points
entry_points={
'pytest11': ['topology_docker_openswitch '
'= topology_docker_openswitch.plugin.plugin'],
'topology_docker_node_10': [
'openswitch = topology_docker_openswitch.openswitch:OpenSwitchNode'
]
Expand Down

0 comments on commit 7b08cf2

Please sign in to comment.