|
Petr Šabata |
d9a596 |
#!/usr/bin/env python3
|
|
Petr Šabata |
d9a596 |
# SPDX-License-Identifier: LGPL-2.1+
|
|
Petr Šabata |
d9a596 |
# ~~~
|
|
Petr Šabata |
d9a596 |
# runtest.sh of /CoreOS/iproute/Sanity/ip-netns-sanity-test
|
|
Petr Šabata |
d9a596 |
# Description: Test basic ip netns funcionality
|
|
Petr Šabata |
d9a596 |
#
|
|
Petr Šabata |
d9a596 |
# Author: Susant Sahani <susant@redhat.com>
|
|
Petr Šabata |
d9a596 |
# Copyright (c) 2018 Red Hat, Inc.
|
|
Petr Šabata |
d9a596 |
# ~~~
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
import errno
|
|
Petr Šabata |
d9a596 |
import os
|
|
Petr Šabata |
d9a596 |
import sys
|
|
Petr Šabata |
d9a596 |
import time
|
|
Petr Šabata |
d9a596 |
import unittest
|
|
Petr Šabata |
d9a596 |
import subprocess
|
|
Petr Šabata |
d9a596 |
import signal
|
|
Petr Šabata |
d9a596 |
import shutil
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
def setUpModule():
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
if shutil.which('ip') is None:
|
|
Petr Šabata |
d9a596 |
raise OSError(errno.ENOENT, 'ip not found')
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
class GenericUtilities():
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
def path_exists(self, path):
|
|
Petr Šabata |
d9a596 |
self.assertTrue(os.path.exists(os.path.join('/var/run/netns', path)))
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
def link_exists(self, link):
|
|
Petr Šabata |
d9a596 |
self.assertTrue(os.path.exists(os.path.join('/sys/class/net', link)))
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
def add_veth(self):
|
|
Petr Šabata |
d9a596 |
subprocess.check_output(['ip', 'link', 'add', 'veth-test', 'type', 'veth', 'peer', 'name', 'test-peer'])
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
def del_veth(self):
|
|
Petr Šabata |
d9a596 |
subprocess.check_output(['ip', 'link', 'del', 'veth-test'])
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
def add_dummy(self):
|
|
Petr Šabata |
d9a596 |
subprocess.check_output(['ip', 'link', 'add', 'dummy-test', 'type', 'dummy'])
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
def del_dummy(self):
|
|
Petr Šabata |
d9a596 |
subprocess.check_output(['ip', 'link', 'del', 'dummy-test'])
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
class IPNsTests(unittest.TestCase, GenericUtilities):
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
def test_add_ns(self):
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
subprocess.check_output(['ip', 'netns', 'add', 'net-ns-test'])
|
|
Petr Šabata |
d9a596 |
self.path_exists('net-ns-test')
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
output=subprocess.check_output(['ip', 'netns', 'list']).rstrip().decode('utf-8')
|
|
Petr Šabata |
d9a596 |
self.assertRegex(output, "net-ns-test")
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
self.addCleanup(subprocess.call, ['ip', 'netns', 'del', 'net-ns-test'])
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
def test_add_dummy_interface_to_ns(self):
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
self.add_dummy()
|
|
Petr Šabata |
d9a596 |
self.link_exists('dummy-test')
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
subprocess.check_output(['ip', 'netns', 'add', 'net-ns-test'])
|
|
Petr Šabata |
d9a596 |
self.path_exists('net-ns-test')
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
output=subprocess.check_output(['ip', 'netns', 'list']).rstrip().decode('utf-8')
|
|
Petr Šabata |
d9a596 |
self.assertRegex(output, "net-ns-test")
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
subprocess.check_output(['ip', 'link', 'set', 'dev', 'dummy-test', 'netns', 'net-ns-test'])
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
output=subprocess.check_output(['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'link', 'show']).rstrip().decode('utf-8')
|
|
Petr Šabata |
d9a596 |
print(output)
|
|
Petr Šabata |
d9a596 |
self.assertRegex(output, "dummy-test")
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
self.addCleanup(subprocess.call, ['ip', 'netns', 'del', 'net-ns-test'])
|
|
Petr Šabata |
d9a596 |
self.addCleanup(subprocess.call, ['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'link', 'del', 'dummy-test'])
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
def test_add_veth_interface_to_ns(self):
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
self.add_veth()
|
|
Petr Šabata |
d9a596 |
self.link_exists('veth-test')
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
subprocess.check_output(['ip', 'netns', 'add', 'net-ns-test'])
|
|
Petr Šabata |
d9a596 |
self.path_exists('net-ns-test')
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
output=subprocess.check_output(['ip', 'netns', 'list']).rstrip().decode('utf-8')
|
|
Petr Šabata |
d9a596 |
self.assertRegex(output, "net-ns-test")
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
subprocess.check_output(['ip', 'link', 'set', 'dev', 'test-peer', 'netns', 'net-ns-test'])
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
output=subprocess.check_output(['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'link', 'show']).rstrip().decode('utf-8')
|
|
Petr Šabata |
d9a596 |
print(output)
|
|
Petr Šabata |
d9a596 |
self.assertRegex(output, "test-peer")
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
# Setup IP address of veth-test.
|
|
Petr Šabata |
d9a596 |
subprocess.check_output(['ip', 'addr', 'add', '10.200.1.1/24', 'dev', 'veth-test'])
|
|
Petr Šabata |
d9a596 |
subprocess.check_output(['ip', 'link', 'set', 'veth-test', 'up'])
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
# Setup IP address of v-peer1.
|
|
Petr Šabata |
d9a596 |
subprocess.check_output(['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'addr', 'add',' 10.200.1.2/24', 'dev', 'test-peer'])
|
|
Petr Šabata |
d9a596 |
subprocess.check_output(['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'link', 'set', 'test-peer', 'up'])
|
|
Petr Šabata |
d9a596 |
subprocess.check_output(['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'link', 'set', 'lo', 'up'])
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
output=subprocess.check_output(['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'addr', 'show']).rstrip().decode('utf-8')
|
|
Petr Šabata |
d9a596 |
print(output)
|
|
Petr Šabata |
d9a596 |
self.assertRegex(output, "test-peer")
|
|
Petr Šabata |
d9a596 |
self.assertRegex(output, "lo: <LOOPBACK,UP,LOWER_UP>")
|
|
Petr Šabata |
d9a596 |
self.assertRegex(output, "inet 10.200.1.2/24")
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
output=subprocess.check_output(['ip', 'netns', 'exec', 'net-ns-test', 'ping', '10.200.1.1', '-c', '5']).rstrip().decode('utf-8')
|
|
Petr Šabata |
d9a596 |
print(output)
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
self.addCleanup(subprocess.call, ['ip', 'netns', 'del', 'net-ns-test'])
|
|
Petr Šabata |
d9a596 |
self.addCleanup(subprocess.call, ['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'link', 'del', 'test-peer'])
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
|
|
Petr Šabata |
d9a596 |
if __name__ == '__main__':
|
|
Petr Šabata |
d9a596 |
unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout,
|
|
Petr Šabata |
d9a596 |
verbosity=2))
|