Add wrapper for IP command
Change-Id: I5bbd4f1fa0c4f495228efbf5796d8a03959f19f2changes/85/688385/12
parent
9cb3538d4c
commit
9fc9eb4112
@ -0,0 +1,79 @@
|
||||
# Copyright (c) 2019 Red Hat, Inc.
|
||||
#
|
||||
# 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.
|
||||
from __future__ import absolute_import
|
||||
|
||||
import netaddr
|
||||
|
||||
import tobiko
|
||||
from tobiko.shell import sh
|
||||
|
||||
|
||||
class IpError(tobiko.TobikoException):
|
||||
message = ("Unable to get IP addresses from host "
|
||||
"(exit_status={exit_status!r}): {error!s}")
|
||||
|
||||
|
||||
INETS = {
|
||||
4: ['inet'],
|
||||
6: ['inet6'],
|
||||
None: ['inet', 'inet6']
|
||||
}
|
||||
|
||||
|
||||
def list_ip_addresses(ip_version=None, **execute_params):
|
||||
inets = INETS.get(ip_version)
|
||||
if inets is None:
|
||||
error = "invalid IP version: {!r}".format(ip_version)
|
||||
raise IpError(error=error)
|
||||
|
||||
output = execute_ip(['-o', 'address', 'list'], **execute_params)
|
||||
|
||||
ips = tobiko.Selection()
|
||||
if output:
|
||||
for line in output.splitlines():
|
||||
fields = line.strip().split()
|
||||
inet = fields[2]
|
||||
if inet in inets:
|
||||
address = fields[3]
|
||||
if '/' in address:
|
||||
address, _ = address.split('/', 1)
|
||||
ips.append(netaddr.IPAddress(address))
|
||||
return ips
|
||||
|
||||
|
||||
def list_network_namespaces(**execute_params):
|
||||
output = execute_ip(['-o', 'netns', 'list'], **execute_params)
|
||||
namespaces = list()
|
||||
if output:
|
||||
for line in output.splitlines():
|
||||
fields = line.strip().split()
|
||||
namespace = fields[0]
|
||||
namespaces.append(namespace)
|
||||
return namespaces
|
||||
|
||||
|
||||
def execute_ip(ifconfig_args, network_namespace=None, sudo=None,
|
||||
**execute_params):
|
||||
command = ['/sbin/ip'] + ifconfig_args
|
||||
if network_namespace:
|
||||
if sudo is None:
|
||||
sudo = True
|
||||
command = ['/sbin/ip', 'netns', 'exec', network_namespace] + command
|
||||
result = sh.execute(command, stdin=False, stdout=True, stderr=True,
|
||||
expect_exit_status=None, sudo=sudo, **execute_params)
|
||||
if result.exit_status:
|
||||
raise IpError(error=result.stderr, exit_status=result.exit_status)
|
||||
return result.stdout
|
@ -0,0 +1,85 @@
|
||||
# Copyright (c) 2019 Red Hat, Inc.
|
||||
#
|
||||
# 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.
|
||||
from __future__ import absolute_import
|
||||
|
||||
import netaddr
|
||||
import six
|
||||
import testtools
|
||||
|
||||
import tobiko
|
||||
from tobiko.shell import ip
|
||||
from tobiko.shell import ssh
|
||||
from tobiko.openstack import stacks
|
||||
|
||||
|
||||
class IpTest(testtools.TestCase):
|
||||
|
||||
centos_stack = tobiko.required_setup_fixture(
|
||||
stacks.CentosServerStackFixture)
|
||||
|
||||
cirros_stack = tobiko.required_setup_fixture(
|
||||
stacks.CirrosServerStackFixture)
|
||||
|
||||
ubuntu_stack = tobiko.required_setup_fixture(
|
||||
stacks.UbuntuServerStackFixture)
|
||||
|
||||
def test_list_ip_addresses(self, ip_version=None, **execute_params):
|
||||
ips = ip.list_ip_addresses(ip_version=ip_version, **execute_params)
|
||||
self.assertIsInstance(ips, tobiko.Selection)
|
||||
for ip_address in ips:
|
||||
self.assertIsInstance(ip_address, netaddr.IPAddress)
|
||||
if ip_version:
|
||||
self.assertEqual(ips.with_attributes(version=ip_version), ips)
|
||||
|
||||
def test_list_ip_addresses_with_ipv4(self):
|
||||
self.test_list_ip_addresses(ip_version=4)
|
||||
|
||||
def test_list_ip_addresses_with_ipv6(self):
|
||||
self.test_list_ip_addresses(ip_version=6)
|
||||
|
||||
def test_list_ip_addresses_with_centos_server(self):
|
||||
self.test_list_ip_addresses(ssh_client=self.centos_stack.ssh_client)
|
||||
|
||||
def test_list_ip_addresses_with_cirros_server(self):
|
||||
self.test_list_ip_addresses(ssh_client=self.cirros_stack.ssh_client)
|
||||
|
||||
def test_list_ip_addresses_with_ubuntu_server(self):
|
||||
self.test_list_ip_addresses(ssh_client=self.ubuntu_stack.ssh_client)
|
||||
|
||||
def test_list_ip_addresses_with_proxy_ssh_client(self):
|
||||
ssh_client = ssh.ssh_proxy_client()
|
||||
if ssh_client is None:
|
||||
self.skip('SSH proxy server not configured')
|
||||
self.test_list_ip_addresses(ssh_client=ssh_client)
|
||||
|
||||
def test_list_namespaces(self, **execute_params):
|
||||
namespaces = ip.list_network_namespaces(**execute_params)
|
||||
self.assertIsInstance(namespaces, list)
|
||||
for namespace in namespaces:
|
||||
self.assertIsInstance(namespace, six.string_types)
|
||||
self.test_list_ip_addresses(network_namespace=namespace)
|
||||
|
||||
def test_list_namespaces_with_centos_server(self):
|
||||
self.test_list_namespaces(ssh_client=self.centos_stack.ssh_client)
|
||||
|
||||
def test_list_namespaces_with_ubuntu_server(self):
|
||||
self.test_list_namespaces(ssh_client=self.ubuntu_stack.ssh_client)
|
||||
|
||||
def test_list_namespaces_with_proxy_ssh_client(self):
|
||||
ssh_client = ssh.ssh_proxy_client()
|
||||
if ssh_client is None:
|
||||
self.skip('SSH proxy server not configured')
|
||||
self.test_list_namespaces(ssh_client=ssh_client)
|
Loading…
Reference in New Issue