Blame tests/ip-address-sanity-test/ip-address-tests.py

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-address-sanity-test
Petr Šabata d9a596
#   Description: Test basic ip address 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 IPLinkUtilities():
Petr Šabata d9a596
Petr Šabata d9a596
    def link_exists(self, link):
Petr Šabata d9a596
Petr Šabata d9a596
        self.assertTrue(os.path.exists(os.path.join('/sys/class/net', link)))
Petr Šabata d9a596
Petr Šabata d9a596
    def add_dummy(self):
Petr Šabata d9a596
        """ Setup """
Petr Šabata d9a596
        subprocess.check_output(['ip', 'link', 'add', 'dummy-test', 'type', 'dummy'])
Petr Šabata d9a596
        self.link_exists('dummy-test')
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 IPAddressTests(unittest.TestCase, IPLinkUtilities):
Petr Šabata d9a596
Petr Šabata d9a596
    def setUp(self):
Petr Šabata d9a596
        self.add_dummy()
Petr Šabata d9a596
Petr Šabata d9a596
    def tearDown(self):
Petr Šabata d9a596
        self.del_dummy()
Petr Šabata d9a596
Petr Šabata d9a596
    def test_add_address(self):
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip address add 192.168.1.200/24 dev dummy-test", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'address', 'show', 'dev', 'dummy-test']).rstrip().decode('utf-8')
Petr Šabata d9a596
        self.assertRegex(output, "192.168.1.200")
Petr Šabata d9a596
Petr Šabata d9a596
    def test_add_broadcast_address_label(self):
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip addr add 192.168.1.50/24 brd + dev dummy-test label dummy-test-Home", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'address', 'show', 'dev', 'dummy-test']).rstrip().decode('utf-8')
Petr Šabata d9a596
        self.assertRegex(output, "192.168.1.50")
Petr Šabata d9a596
        self.assertRegex(output, "192.168.1.255")
Petr Šabata d9a596
        self.assertRegex(output, "dummy-test-Home")
Petr Šabata d9a596
Petr Šabata d9a596
    def test_del_address(self):
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip address add 192.168.1.200/24 dev dummy-test", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'address', 'show', 'dev', 'dummy-test']).rstrip().decode('utf-8')
Petr Šabata d9a596
        self.assertRegex(output, "192.168.1.200")
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip address del 192.168.1.200/24 dev dummy-test", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'address', 'show', 'dev', 'dummy-test']).rstrip().decode('utf-8')
Petr Šabata d9a596
        self.assertNotRegex(output, "192.168.1.200")
Petr Šabata d9a596
Petr Šabata d9a596
    def test_add_address_scope(self):
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip address add 192.168.1.200/24 dev dummy-test scope host", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'address', 'show', 'dev', 'dummy-test']).rstrip().decode('utf-8')
Petr Šabata d9a596
        self.assertRegex(output, "192.168.1.200")
Petr Šabata d9a596
        self.assertRegex(output, "host")
Petr Šabata d9a596
Petr Šabata d9a596
    def test_add_address_lifetime(self):
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip address add 192.168.1.200/24 dev dummy-test valid_lft 1000 preferred_lft 500", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'address', 'show', 'dev', 'dummy-test']).rstrip().decode('utf-8')
Petr Šabata d9a596
        self.assertRegex(output, "192.168.1.200")
Petr Šabata d9a596
        self.assertRegex(output, "1000sec")
Petr Šabata d9a596
        self.assertRegex(output, "500sec")
Petr Šabata d9a596
Petr Šabata d9a596
    def test_add_ipv6_address(self):
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip -6 addr add 2001:0db8:0:f101::1/64 dev dummy-test", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'address', 'show', 'dev', 'dummy-test']).rstrip().decode('utf-8')
Petr Šabata d9a596
        self.assertRegex(output, "2001:db8:0:f101::1")
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))