Blame tests/ip-tuntap-sanity-test/ip-tuntap-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-tuntap-sanity-test
Petr Šabata d9a596
#   Description: Test basic ip tuntap 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
        self.assertTrue(os.path.exists(os.path.join('/sys/class/net', link)))
Petr Šabata d9a596
Petr Šabata d9a596
class IPTuntapTests(unittest.TestCase, IPLinkUtilities):
Petr Šabata d9a596
Petr Šabata d9a596
    def test_add_tap(self):
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'tuntap', 'add', 'name', 'tap-test', 'mode', 'tap'])
Petr Šabata d9a596
        self.link_exists('tap-test')
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'tuntap', 'show', 'dev', 'tap-test']).rstrip().decode('utf-8')
Petr Šabata d9a596
        self.assertRegex(output, "tap-test: tap")
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'link', 'del', 'tap-test'])
Petr Šabata d9a596
Petr Šabata d9a596
    def test_add_tun(self):
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'tuntap', 'add', 'name', 'tun-test', 'mode', 'tun'])
Petr Šabata d9a596
        self.link_exists('tun-test')
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'tuntap', 'show', 'dev', 'tun-test']).rstrip().decode('utf-8')
Petr Šabata d9a596
        self.assertRegex(output, "tun-test: tun")
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'link', 'del', 'tun-test'])
Petr Šabata d9a596
Petr Šabata d9a596
    def test_add_tun_user_group(self):
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'tuntap', 'add', 'name', 'tun-test', 'mode', 'tun', 'user', 'root', 'group', 'root'])
Petr Šabata d9a596
        self.link_exists('tun-test')
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'tuntap', 'show', 'dev', 'tun-test']).rstrip().decode('utf-8')
Petr Šabata d9a596
        self.assertRegex(output, "tun-test: tun")
Petr Šabata d9a596
        self.assertRegex(output, "user 0 group 0")
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'link', 'del', 'tun-test'])
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))