Files
deb-neutron-fwaas/neutron_fwaas/privileged/tests/functional/utils.py
Cedric Brandily 094d2d5f01 Define in_namespace contextmanager
This change defines the contextmanager in_namespace[1]. It moves current
process in a network namespace (in __enter__) and moves it back in its
original network namespace (in _exit__) or kills current process if
__exit__ fails in order to ensure following commands will be executed
in the correct network namespace.

This change is an enabler to the Netlink solution to clean conntrack
entries.

[1] neutron_fwaas.privileged.utils

Partial-Bug: #1664294
Change-Id: I587257db8e1fce56a95f0db3dc4e0752751fdd81
2017-02-28 10:18:07 +07:00

37 lines
1.2 KiB
Python

# Copyright (c) 2017 Thales Services SAS
# All Rights Reserved.
#
# 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.
import os
import re
from neutron_fwaas import privileged
from neutron_fwaas.privileged import utils
def get_my_netns_inode():
link = os.readlink(utils.PROCESS_NETNS)
# NOTE(cby): link respects the format "net:[<inode>]"
return int(re.match('net:\[(\d+)\]', link).group(1))
@privileged.default.entrypoint
def get_in_namespace_netns_inodes(namespace):
before = get_my_netns_inode()
with utils.in_namespace(namespace):
inside = get_my_netns_inode()
after = get_my_netns_inode()
return before, inside, after