naccyde / rpms / iproute

Forked from rpms/iproute 9 months ago
Clone

Blame tests/ip-tunnel-sanity-test/ip-tunnel-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-tunnel-sanity-test
Petr Šabata d9a596
#   Description: Test basic ip tunnel 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 add_dummy(self):
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
    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 IPTunnelTests(unittest.TestCase, IPLinkUtilities):
Petr Šabata d9a596
Petr Šabata d9a596
    def setUp(self):
Petr Šabata d9a596
        self.add_dummy()
Petr Šabata d9a596
        self.link_exists('dummy-test')
Petr Šabata d9a596
Petr Šabata d9a596
    def tearDown(self):
Petr Šabata d9a596
        self.del_dummy()
Petr Šabata d9a596
Petr Šabata d9a596
    def test_add_ipip(self):
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'tunnel', 'add', 'ipiptun-test', 'mode', 'ipip', 'local', '10.3.3.3', 'remote', '10.4.4.4', 'ttl', '64', 'dev', 'dummy-test'])
Petr Šabata d9a596
        self.link_exists('ipiptun-test')
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'tunnel']).rstrip().decode('utf-8')
Petr Šabata d9a596
        print(output)
Petr Šabata d9a596
        self.assertRegex(output, "ipiptun-test: ip/ip remote 10.4.4.4 local 10.3.3.3 dev dummy-test ttl 64")
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'link', 'del', 'ipiptun-test'])
Petr Šabata d9a596
Petr Šabata d9a596
    def test_add_gre(self):
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'tunnel', 'add', 'gretun-test', 'mode', 'gre', 'local', '10.3.3.3', 'remote', '10.4.4.4', 'ttl', '64', 'dev', 'dummy-test'])
Petr Šabata d9a596
        self.link_exists('gretun-test')
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'tunnel']).rstrip().decode('utf-8')
Petr Šabata d9a596
        print(output)
Petr Šabata d9a596
        self.assertRegex(output, "gretun-test: gre/ip remote 10.4.4.4 local 10.3.3.3 dev dummy-test ttl 64")
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'link', 'del', 'gretun-test'])
Petr Šabata d9a596
Petr Šabata d9a596
    def test_add_sit(self):
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'tunnel', 'add', 'sittun-test', 'mode', 'sit', 'local', '10.3.3.3', 'remote', '10.4.4.4', 'ttl', '64', 'dev', 'dummy-test'])
Petr Šabata d9a596
        self.link_exists('sittun-test')
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'tunnel']).rstrip().decode('utf-8')
Petr Šabata d9a596
        print(output)
Petr Šabata d9a596
        self.assertRegex(output, "sittun-test: ipv6/ip remote 10.4.4.4 local 10.3.3.3 dev dummy-test ttl 64")
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'link', 'del', 'sittun-test'])
Petr Šabata d9a596
Petr Šabata d9a596
    def test_add_isatap(self):
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'tunnel', 'add', 'isatap-test', 'mode', 'sit', 'local', '10.3.3.3', 'remote', '10.4.4.4', 'ttl', '64', 'dev', 'dummy-test'])
Petr Šabata d9a596
        self.link_exists('isatap-test')
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'tunnel']).rstrip().decode('utf-8')
Petr Šabata d9a596
        print(output)
Petr Šabata d9a596
        self.assertRegex(output, "isatap-test: ipv6/ip remote 10.4.4.4 local 10.3.3.3 dev dummy-test ttl 64")
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'link', 'del', 'isatap-test'])
Petr Šabata d9a596
Petr Šabata d9a596
    def test_add_vti(self):
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'tunnel', 'add', 'vti-test', 'mode', 'sit', 'local', '10.3.3.3', 'remote', '10.4.4.4', 'ttl', '64', 'dev', 'dummy-test'])
Petr Šabata d9a596
        self.link_exists('vti-test')
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'tunnel']).rstrip().decode('utf-8')
Petr Šabata d9a596
        print(output)
Petr Šabata d9a596
        self.assertRegex(output, "vti-test: ipv6/ip remote 10.4.4.4 local 10.3.3.3 dev dummy-test ttl 64")
Petr Šabata d9a596
Petr Šabata d9a596
        subprocess.check_output(['ip', 'link', 'del', 'vti-test'])
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))