naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
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-l2tp-sanity-test
Petr Šabata d9a596
#   Description: Test basic ip l2tp 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
    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_address(self, address, interface):
Petr Šabata d9a596
        subprocess.check_output(['ip', 'address', 'add', address, 'dev', interface])
Petr Šabata d9a596
Petr Šabata d9a596
class IPL2tpTests(unittest.TestCase, IPLinkUtilities):
Petr Šabata d9a596
Petr Šabata d9a596
    def setUp(self):
Petr Šabata d9a596
        self.add_veth()
Petr Šabata d9a596
        self.link_exists('veth-test')
Petr Šabata d9a596
Petr Šabata d9a596
        self.add_address('192.168.11.12/24', 'veth-test')
Petr Šabata d9a596
        self.add_address('192.168.11.13/24', 'test-peer')
Petr Šabata d9a596
Petr Šabata d9a596
    def tearDown(self):
Petr Šabata d9a596
        self.del_veth()
Petr Šabata d9a596
Petr Šabata d9a596
    def test_add_l2tp_add_tunnel(self):
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip l2tp add tunnel tunnel_id 3000 peer_tunnel_id 4000 encap udp local 192.168.11.12 remote 192.168.11.13 udp_sport 5000 udp_dport 6000", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'l2tp', 'show', 'tunnel', 'tunnel_id', '3000']).rstrip().decode('utf-8')
Petr Šabata d9a596
        print(output)
Petr Šabata d9a596
        self.assertRegex(output, "From 192.168.11.12 to 192.168.11.13")
Petr Šabata d9a596
        self.assertRegex(output, "Peer tunnel 4000")
Petr Šabata d9a596
        self.assertRegex(output, "UDP source / dest ports: 5000/6000")
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip l2tp del tunnel tunnel_id 3000", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
    def test_add_l2tp_add_tunnel_session(self):
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip l2tp add tunnel tunnel_id 3000 peer_tunnel_id 4000 encap udp local 192.168.11.12 remote 192.168.11.13 udp_sport 5000 udp_dport 6000", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'l2tp', 'show', 'tunnel', 'tunnel_id', '3000']).rstrip().decode('utf-8')
Petr Šabata d9a596
        print(output)
Petr Šabata d9a596
        self.assertRegex(output, "From 192.168.11.12 to 192.168.11.13")
Petr Šabata d9a596
        self.assertRegex(output, "Peer tunnel 4000")
Petr Šabata d9a596
        self.assertRegex(output, "UDP source / dest ports: 5000/6000")
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call(" ip l2tp add session tunnel_id 3000 session_id 1000 peer_session_id 2000", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'l2tp', 'show', 'session', 'tunnel_id', '3000', 'session_id' ,'1000']).rstrip().decode('utf-8')
Petr Šabata d9a596
        print(output)
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip l2tp del session tunnel_id 3000 session_id 1000", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip l2tp del tunnel tunnel_id 3000", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
    def test_setup_l2tp(self):
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip l2tp add tunnel tunnel_id 3000 peer_tunnel_id 4000 encap udp local 192.168.11.12 remote 192.168.11.13 udp_sport 5000 udp_dport 6000", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'l2tp', 'show', 'tunnel', 'tunnel_id', '3000']).rstrip().decode('utf-8')
Petr Šabata d9a596
        print(output)
Petr Šabata d9a596
        self.assertRegex(output, "From 192.168.11.12 to 192.168.11.13")
Petr Šabata d9a596
        self.assertRegex(output, "Peer tunnel 4000")
Petr Šabata d9a596
        self.assertRegex(output, "UDP source / dest ports: 5000/6000")
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call(" ip l2tp add session tunnel_id 3000 session_id 1000 peer_session_id 2000", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'l2tp', 'show', 'session', 'tunnel_id', '3000', 'session_id' ,'1000']).rstrip().decode('utf-8')
Petr Šabata d9a596
        print(output)
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip l2tp add tunnel tunnel_id 4000 peer_tunnel_id 3000 encap udp local 192.168.11.13 remote 192.168.11.12 udp_sport 6000 udp_dport 5000", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'l2tp', 'show', 'tunnel', 'tunnel_id', '3000']).rstrip().decode('utf-8')
Petr Šabata d9a596
        print(output)
Petr Šabata d9a596
        self.assertRegex(output, "From 192.168.11.13 to 192.168.11.12")
Petr Šabata d9a596
        self.assertRegex(output, "Peer tunnel 4000")
Petr Šabata d9a596
        self.assertRegex(output, "UDP source / dest ports: 6000/5000")
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip l2tp add session tunnel_id 4000 session_id 2000 peer_session_id 1000", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'l2tp', 'show', 'session', 'tunnel_id', '4000', 'session_id' ,'2000']).rstrip().decode('utf-8')
Petr Šabata d9a596
        print(output)
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip link set l2tpeth0 up mtu 1488", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
        r = subprocess.call("ip link set l2tpeth0 up mtu 1488", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip addr add 10.42.1.1 peer 10.42.1.2 dev l2tpeth0", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
        r = subprocess.call("ip addr add 10.42.1.2 peer 10.42.1.1 dev l2tpeth1", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ip', 'link', 'show']).rstrip().decode('utf-8')
Petr Šabata d9a596
        print(output)
Petr Šabata d9a596
Petr Šabata d9a596
        output=subprocess.check_output(['ping', '10.42.1.2','-c', '5']).rstrip().decode('utf-8')
Petr Šabata d9a596
        print(output)
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip l2tp del session tunnel_id 3000 session_id 1000", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
        r = subprocess.call("ip l2tp del session tunnel_id 4000 session_id 2000", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
Petr Šabata d9a596
        r = subprocess.call("ip l2tp del tunnel tunnel_id 3000", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
Petr Šabata d9a596
        r = subprocess.call("ip l2tp del tunnel tunnel_id 4000", shell=True)
Petr Šabata d9a596
        self.assertEqual(r, 0)
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))