Skip to content

Commit

Permalink
Merge pull request #6 from baraserg/master
Browse files Browse the repository at this point in the history
Merge log plugin
  • Loading branch information
saenzpa authored Jun 21, 2016
2 parents 97dafda + 7b08cf2 commit 3cb152a
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 4 deletions.
11 changes: 7 additions & 4 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 @@ -118,7 +118,8 @@ def create_interfaces():
raise Exception('Failed to map ports with port labels')
# Writting mapping to file
with open('/tmp/port_mapping.json', 'w') as json_file:
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))
for hwport in hwports:
Expand Down Expand Up @@ -318,12 +319,14 @@ def _setup_system(self):
fd.write(SETUP_SCRIPT)

try:
self._docker_exec('python /tmp/openswitch_setup.py -d')
self._docker_exec('python {}/openswitch_setup.py -d'
.format(self.shared_dir_mount))
except Exception as e:
check_call('touch {}/logs'.format(self.shared_dir), shell=True)
check_call('chmod 766 {}/logs'.format(self.shared_dir),
shell=True)
self._docker_exec('/bin/bash /tmp/process_log.sh')
self._docker_exec('/bin/bash {}/process_log.sh'
.format(self.shared_dir_mount))
check_call(
'tail -n 2000 /var/log/syslog > {}/syslog'.format(
self.shared_dir
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 3cb152a

Please sign in to comment.