iproute/tests/ip-netns-sanity-test/ip-netns-tests.py

119 lines
4.4 KiB
Python
Raw Normal View History

CI: Add netns to test Sample run: ``` :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: Test :: Test :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: [ 15:37:14 ] :: [ LOG ] :: ip netns tests :: [ 15:37:14 ] :: [ LOG ] :: ip netns tests :: [ 15:37:14 ] :: [ BEGIN ] :: Running '/usr/bin/python3 /usr/bin/ip-netns-tests.py' test_add_dummy_interface_to_ns (__main__.IPNsTests) ... 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: gre0@NONE: <NOARP> mtu 1476 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/gre 0.0.0.0 brd 0.0.0.0 3: gretap0@NONE: <BROADCAST,MULTICAST> mtu 1462 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff 4: erspan0@NONE: <BROADCAST,MULTICAST> mtu 1446 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff 4: erspan0@NONE: <BROADCAST,MULTICAST> mtu 1446 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff 5: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/tunnel6 :: brd :: 6: ip6gre0@NONE: <NOARP> mtu 1448 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/gre6 :: brd :: 7: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ipip 0.0.0.0 brd 0.0.0.0 8: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/sit 0.0.0.0 brd 0.0.0.0 9: ip_vti0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ipip 0.0.0.0 brd 0.0.0.0 143: dummy-test: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 6e:d1:ba:73:59:c5 brd ff:ff:ff:ff:ff:ff ok test_add_ns (__main__.IPNsTests) ... ok test_add_veth_interface_to_ns (__main__.IPNsTests) ... 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: gre0@NONE: <NOARP> mtu 1476 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/gre 0.0.0.0 brd 0.0.0.0 3: gretap0@NONE: <BROADCAST,MULTICAST> mtu 1462 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff 4: erspan0@NONE: <BROADCAST,MULTICAST> mtu 1446 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff 5: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/tunnel6 :: brd :: 6: ip6gre0@NONE: <NOARP> mtu 1448 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/gre6 :: brd :: 7: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ipip 0.0.0.0 brd 0.0.0.0 8: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/sit 0.0.0.0 brd 0.0.0.0 9: ip_vti0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ipip 0.0.0.0 brd 0.0.0.0 144: test-peer@if145: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 62:ce:d1:9f:bc:de brd ff:ff:ff:ff:ff:ff link-netnsid 0 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: gre0@NONE: <NOARP> mtu 1476 qdisc noop state DOWN group default qlen 1000 link/gre 0.0.0.0 brd 0.0.0.0 3: gretap0@NONE: <BROADCAST,MULTICAST> mtu 1462 qdisc noop state DOWN group default qlen 1000 link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff 4: erspan0@NONE: <BROADCAST,MULTICAST> mtu 1446 qdisc noop state DOWN group default qlen 1000 link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff 5: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN group default qlen 1000 link/tunnel6 :: brd :: 6: ip6gre0@NONE: <NOARP> mtu 1448 qdisc noop state DOWN group default qlen 1000 link/gre6 :: brd :: 7: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000 link/ipip 0.0.0.0 brd 0.0.0.0 8: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000 link/sit 0.0.0.0 brd 0.0.0.0 9: ip_vti0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000 link/ipip 0.0.0.0 brd 0.0.0.0 144: test-peer@if145: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 62:ce:d1:9f:bc:de brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 10.200.1.2/24 scope global test-peer valid_lft forever preferred_lft forever inet6 fe80::60ce:d1ff:fe9f:bcde/64 scope link tentative valid_lft forever preferred_lft forever PING 10.200.1.1 (10.200.1.1) 56(84) bytes of data. 64 bytes from 10.200.1.1: icmp_seq=1 ttl=64 time=0.063 ms 64 bytes from 10.200.1.1: icmp_seq=2 ttl=64 time=0.042 ms 64 bytes from 10.200.1.1: icmp_seq=3 ttl=64 time=0.054 ms 64 bytes from 10.200.1.1: icmp_seq=4 ttl=64 time=0.059 ms 64 bytes from 10.200.1.1: icmp_seq=5 ttl=64 time=0.064 ms --- 10.200.1.1 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4073ms rtt min/avg/max/mdev = 0.042/0.056/0.064/0.010 ms ok ---------------------------------------------------------------------- Ran 3 tests in 4.431s Ran 3 tests in 4.431s OK :: [ 15:37:18 ] :: [ PASS ] :: Command '/usr/bin/python3 /usr/bin/ip-netns-tests.py' (Expected 0, got 0) :: [ 15:37:18 ] :: [ PASS ] :: Command '/usr/bin/python3 /usr/bin/ip-netns-tests.py' (Expected 0, got 0) :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: Duration: 4s :: Duration: 4s :: Assertions: 1 good, 0 bad :: Assertions: 1 good, 0 bad :: RESULT: PASS :: RESULT: PASS :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :: Cleanup :: Cleanup ```
2018-07-31 10:06:29 +00:00
#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1+
# ~~~
# runtest.sh of /CoreOS/iproute/Sanity/ip-netns-sanity-test
# Description: Test basic ip netns funcionality
#
# Author: Susant Sahani <susant@redhat.com>
# Copyright (c) 2018 Red Hat, Inc.
# ~~~
import errno
import os
import sys
import time
import unittest
import subprocess
import signal
import shutil
def setUpModule():
if shutil.which('ip') is None:
raise OSError(errno.ENOENT, 'ip not found')
class GenericUtilities():
def path_exists(self, path):
self.assertTrue(os.path.exists(os.path.join('/var/run/netns', path)))
def link_exists(self, link):
self.assertTrue(os.path.exists(os.path.join('/sys/class/net', link)))
def add_veth(self):
subprocess.check_output(['ip', 'link', 'add', 'veth-test', 'type', 'veth', 'peer', 'name', 'test-peer'])
def del_veth(self):
subprocess.check_output(['ip', 'link', 'del', 'veth-test'])
def add_dummy(self):
subprocess.check_output(['ip', 'link', 'add', 'dummy-test', 'type', 'dummy'])
def del_dummy(self):
subprocess.check_output(['ip', 'link', 'del', 'dummy-test'])
class IPNsTests(unittest.TestCase, GenericUtilities):
def test_add_ns(self):
subprocess.check_output(['ip', 'netns', 'add', 'net-ns-test'])
self.path_exists('net-ns-test')
output=subprocess.check_output(['ip', 'netns', 'list']).rstrip().decode('utf-8')
self.assertRegex(output, "net-ns-test")
self.addCleanup(subprocess.call, ['ip', 'netns', 'del', 'net-ns-test'])
def test_add_dummy_interface_to_ns(self):
self.add_dummy()
self.link_exists('dummy-test')
subprocess.check_output(['ip', 'netns', 'add', 'net-ns-test'])
self.path_exists('net-ns-test')
output=subprocess.check_output(['ip', 'netns', 'list']).rstrip().decode('utf-8')
self.assertRegex(output, "net-ns-test")
subprocess.check_output(['ip', 'link', 'set', 'dev', 'dummy-test', 'netns', 'net-ns-test'])
output=subprocess.check_output(['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'link', 'show']).rstrip().decode('utf-8')
print(output)
self.assertRegex(output, "dummy-test")
self.addCleanup(subprocess.call, ['ip', 'netns', 'del', 'net-ns-test'])
self.addCleanup(subprocess.call, ['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'link', 'del', 'dummy-test'])
def test_add_veth_interface_to_ns(self):
self.add_veth()
self.link_exists('veth-test')
subprocess.check_output(['ip', 'netns', 'add', 'net-ns-test'])
self.path_exists('net-ns-test')
output=subprocess.check_output(['ip', 'netns', 'list']).rstrip().decode('utf-8')
self.assertRegex(output, "net-ns-test")
subprocess.check_output(['ip', 'link', 'set', 'dev', 'test-peer', 'netns', 'net-ns-test'])
output=subprocess.check_output(['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'link', 'show']).rstrip().decode('utf-8')
print(output)
self.assertRegex(output, "test-peer")
# Setup IP address of veth-test.
subprocess.check_output(['ip', 'addr', 'add', '10.200.1.1/24', 'dev', 'veth-test'])
subprocess.check_output(['ip', 'link', 'set', 'veth-test', 'up'])
# Setup IP address of v-peer1.
subprocess.check_output(['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'addr', 'add',' 10.200.1.2/24', 'dev', 'test-peer'])
subprocess.check_output(['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'link', 'set', 'test-peer', 'up'])
subprocess.check_output(['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'link', 'set', 'lo', 'up'])
output=subprocess.check_output(['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'addr', 'show']).rstrip().decode('utf-8')
print(output)
self.assertRegex(output, "test-peer")
self.assertRegex(output, "lo: <LOOPBACK,UP,LOWER_UP>")
self.assertRegex(output, "inet 10.200.1.2/24")
output=subprocess.check_output(['ip', 'netns', 'exec', 'net-ns-test', 'ping', '10.200.1.1', '-c', '5']).rstrip().decode('utf-8')
print(output)
self.addCleanup(subprocess.call, ['ip', 'netns', 'del', 'net-ns-test'])
self.addCleanup(subprocess.call, ['ip', 'netns', 'exec', 'net-ns-test', 'ip', 'link', 'del', 'test-peer'])
if __name__ == '__main__':
unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout,
verbosity=2))