Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dev: Refactoring to remove race conditions. #27

Merged
merged 1 commit into from
Feb 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 148 additions & 104 deletions lib/topology_docker_openswitch/openswitch_setup
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ from subprocess import check_call, check_output, call, CalledProcessError
from socket import AF_UNIX, SOCK_STREAM, socket, gethostname
from re import findall, MULTILINE
from yaml import load
from functools import partial

config_timeout = 1200
ops_switchd_active_timeout = 60
Expand All @@ -28,27 +29,83 @@ switchd_pid = '/var/run/openvswitch/ops-switchd.pid'
sock = None


def wait_check(function, wait_name, wait_error, *args):
info('Waiting for {}'.format(wait_name))

for i in range(0, config_timeout):
if not function(*args):
sleep(0.1)
else:
break
else:
raise Exception(
'The image did not boot correctly, '
'{} after waiting {} seconds.'.format(
wait_error, int(0.1 * config_timeout)
)
)


def start_restd():
output = ''
try:
output = check_output(
'systemctl status restd', shell=True
)
except CalledProcessError as e:
pass

if 'Active: active' not in output:
try:
def restd_started():
"""
Attempt to start restd and report back if successful.

:rtype: bool
:return: True if restd was started, False otherwise
"""
output = ''
try:
check_output(
'systemctl start restd', shell=True
)
output = check_output(
'systemctl status restd', shell=True
)
except CalledProcessError:
pass

return 'Active: active' in output

wait_check(
restd_started, 'restd service', 'restd did not start'
)

except CalledProcessError as e:
raise Exception(
'Failed to start restd: {}'.format(e.output)
)


def create_interfaces():
# Read ports from hardware description
with open('{}/ports.yaml'.format(hwdesc_dir), 'r') as fd:
ports_hwdesc = load(fd)
hwports = [str(p['name']) for p in ports_hwdesc['ports']]

netns = check_output("ls /var/run/netns", shell=True)
netns = check_output('ls /var/run/netns', shell=True)

# Get list of already created ports
not_in_netns = check_output(shsplit(
'ls /sys/class/net/'
)).split()

if "emulns" not in netns:
in_netns = check_output(shsplit(
'ip netns exec swns ls /sys/class/net/'
)).split()
not_in_netns = check_output(shsplit('ls /sys/class/net/')).split()

if 'emulns' not in netns:
in_netns = check_output(
shsplit('ip netns exec swns ls /sys/class/net/')
).split()
else:
in_netns = check_output(shsplit(
'ip netns exec emulns ls /sys/class/net/'
)).split()
in_netns = check_output(
shsplit('ip netns exec emulns ls /sys/class/net/')
).split()

info('Not in swns/emulns: {not_in_netns} '.format(**locals()))
info('In swns/emulns {in_netns} '.format(**locals()))
Expand Down Expand Up @@ -97,17 +154,14 @@ def create_interfaces():
shell=True
)

for i in range(0, config_timeout):
link_state = check_output(
wait_check(
lambda: 'UP' in check_output(
'{ns_exec} ip link show {hwport}'.format(**locals()),
shell=True
)
if "UP" in link_state:
break
else:
sleep(0.1)
else:
raise Exception('emulns interface did not came up.')
),
'{} to show up'.format(hwport),
'{} did not show up'.format(hwport)
)

out = check_output(
'{ns_exec} echo port_add {hwport} '
Expand All @@ -130,7 +184,8 @@ def create_interfaces():

if findall(regex, out, MULTILINE) is None:
raise Exception(
'Control utility for runtime P4 table failed.'
'Control utility for runtime'
' P4 table failed: {}'.format(out)
)

except CalledProcessError as error:
Expand Down Expand Up @@ -205,9 +260,11 @@ def cur_is_set(cur_key):
}

global sock

if sock is None:
sock = socket(AF_UNIX, SOCK_STREAM)
sock.connect(db_sock)

sock.send(dumps(queries[cur_key]))
response = loads(sock.recv(4096))

Expand All @@ -217,96 +274,83 @@ def cur_is_set(cur_key):
return 0


def ops_switchd_is_active():
is_active = call(["systemctl", "is-active", "switchd.service"])
return is_active == 0
SWNS_CHECK = partial(
wait_check,
exists,
swns_netns,
'{} was not present'.format(swns_netns),
swns_netns
)

HWDESC_CHECK = partial(
wait_check,
exists,
hwdesc_dir,
'{} was not present'.format(hwdesc_dir),
hwdesc_dir
)

SWITCHD_CHECK = partial(
wait_check,
exists,
switchd_pid,
'{} was not present'.format(switchd_pid),
switchd_pid
)

DB_SOCK_CHECK = partial(
wait_check,
exists,
db_sock,
'{} was not present'.format(db_sock),
db_sock
)

CUR_HW_CHECK = partial(
wait_check,
cur_is_set,
'cur_hw to be set to 1',
'cur_hw is not set to 1',
'cur_hw'
)

CUR_CFG_CHECK = partial(
wait_check,
cur_is_set,
'cur_hw to be set to 1',
'cur_hw is not set to 1',
'cur_hw'
)

OPS_SWITCHD_CHECK = partial(
wait_check,
lambda: 0 == call(['systemctl', 'is-active', 'switchd.service']),
'ops-switchd to be active',
'ops-switchd was not active'
)

HOSTNAME_CHECK = partial(
wait_check,
lambda: gethostname() == 'switch',
'final hostname',
'hostname was not set'
)


def main():

if '-d' in argv:
basicConfig(level=DEBUG)

def wait_check(function, wait_name, wait_error, *args):
info('Waiting for {}'.format(wait_name))

for i in range(0, config_timeout):
if not function(*args):
sleep(0.1)
else:
break
else:
raise Exception(
'The image did not boot correctly, '
'{} after waiting {} seconds.'.format(
wait_error, int(0.1 * config_timeout)
)
)

wait_check(
exists, swns_netns, '{} was not present'.format(swns_netns), swns_netns
)
wait_check(
exists, hwdesc_dir, '{} was not present'.format(hwdesc_dir), hwdesc_dir
)

info('Creating interfaces')
SWNS_CHECK()
HWDESC_CHECK()
SWITCHD_CHECK()
create_interfaces()

wait_check(
exists, db_sock, '{} was not present'.format(db_sock), db_sock
)
wait_check(
cur_is_set, 'cur_hw to be set to 1', 'cur_hw is not set to 1',
'cur_hw'
)
wait_check(
cur_is_set, 'cur_cfg to be set to 1', 'cur_cfg is not set to 1',
'cur_cfg'
)
wait_check(
exists, switchd_pid, '{} was not present'.format(switchd_pid),
switchd_pid
)
wait_check(
ops_switchd_is_active, 'ops-switchd to be active',
'ops-switchd was not active'
)
wait_check(
lambda: gethostname() == 'switch', 'final hostname',
'hostname was not set'
)

info('Checking restd service status...')
output = ''
try:
output = check_output(
'systemctl status restd', shell=True
)
except CalledProcessError as e:
pass
if 'Active: active' not in output:
try:
info('Starting restd daemon.')
check_output('systemctl start restd', shell=True)

info('Checking restd service started.')
for i in range(0, config_timeout):
output = ''
output = check_output(
'systemctl status restd', shell=True
)
if 'Active: active' not in output:
sleep(0.1)
else:
break
else:
raise Exception("Failed to start restd service")

except CalledProcessError as e:
raise Exception(
'Failed to start restd: {}'.format(e.output)
)
DB_SOCK_CHECK()
CUR_HW_CHECK()
CUR_CFG_CHECK()
OPS_SWITCHD_CHECK()
HOSTNAME_CHECK()


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,26 @@ def pytest_runtest_teardown(item):
FIXME: document the item argument
"""
test_suite = splitext(basename(item.parent.name))[0]
path_name = '/tmp/topology/docker/{}_{}_{}'.format(
test_suite, item.name, datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
)

# Being extra-prudent here
if exists(path_name):
rmtree(path_name)
from pytest import config

topology_log_dir = config.getoption('--topology-log-dir')

if not topology_log_dir:
return
else:
path_name = join(
topology_log_dir,
'{}_{}_{}'.format(
test_suite,
item.name,
datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
)
)

# Being extra-prudent here
if exists(path_name):
rmtree(path_name)

if 'topology' not in item.funcargs:
from topology_docker_openswitch.openswitch import LOG_PATHS
Expand Down Expand Up @@ -99,14 +112,14 @@ def pytest_runtest_teardown(item):
core_path = '/var/diagnostics/coredump'

bash_shell.send_command(
'ls -1 {}/core.* 2>/dev/null'.format(core_path), silent=True
'ls -1 {}/core* 2>/dev/null'.format(core_path), silent=True
)

core_files = bash_shell.get_response(silent=True).splitlines()

for core_file in core_files:
bash_shell.send_command(
'cp {core_path}/{core_file} /tmp'.format(**locals()),
'cp {core_file} /tmp'.format(**locals()),
silent=True
)
except:
Expand Down
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ def find_requirements(filename):

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