-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tunnel_manager.py
91 lines (74 loc) · 3.99 KB
/
test_tunnel_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import socket
import subprocess
import unittest
from unittest.mock import MagicMock, mock_open, patch
from tunnel_manager import TunnelManager, TunnelManagerError, TunnelType
class TestTunnelManager(unittest.TestCase):
def setUp(self):
# Create a VXLAN tunnel manager
self.vxlan_manager = TunnelManager(TunnelType.VXLAN)
# Create a Geneve tunnel manager
self.geneve_manager = TunnelManager(TunnelType.GENEVE)
# Test cases for creating tunnels
@patch("tunnel_manager.subprocess.run")
def test_create_vxlan_interface_success(self, mock_run):
self.vxlan_manager.create(1001, "192.168.1.1", "192.168.1.2", "br0")
mock_run.assert_called()
@patch("tunnel_manager.subprocess.run")
def test_create_vxlan_interface_failure(self, mock_run):
mock_run.side_effect = subprocess.CalledProcessError(1, "ip")
with self.assertRaises(TunnelManagerError):
self.vxlan_manager.create(1001, "192.168.1.1", "192.168.1.2", "br0")
@patch("tunnel_manager.subprocess.run")
def test_create_geneve_interface_success(self, mock_run):
self.geneve_manager.create(1001, "192.168.1.1", "192.168.1.2", "br0")
mock_run.assert_called()
@patch("tunnel_manager.subprocess.run")
def test_create_geneve_interface_failure(self, mock_run):
mock_run.side_effect = subprocess.CalledProcessError(1, "ip")
with self.assertRaises(TunnelManagerError):
self.geneve_manager.create(1001, "192.168.1.1", "192.168.1.2", "br0")
# Test cases for cleaning up tunnels
@patch("tunnel_manager.subprocess.run")
def test_cleanup_vxlan_interface_success(self, mock_run):
self.vxlan_manager.cleanup(1001, "br0")
mock_run.assert_called()
@patch("tunnel_manager.subprocess.run")
def test_cleanup_vxlan_interface_failure(self, mock_run):
mock_run.side_effect = subprocess.CalledProcessError(1, "ip")
with self.assertRaises(TunnelManagerError):
self.vxlan_manager.cleanup(1001, "br0")
@patch("tunnel_manager.subprocess.run")
def test_cleanup_geneve_interface_success(self, mock_run):
self.geneve_manager.cleanup(1001, "br0")
mock_run.assert_called()
@patch("tunnel_manager.subprocess.run")
def test_cleanup_geneve_interface_failure(self, mock_run):
mock_run.side_effect = subprocess.CalledProcessError(1, "ip")
with self.assertRaises(TunnelManagerError):
self.geneve_manager.cleanup(1001, "br0")
# Test cases for validating tunnel connectivity
@patch("tunnel_manager.socket.socket")
def test_validate_vxlan_connectivity_success(self, mock_socket):
mock_socket_instance = mock_socket.return_value.__enter__.return_value
self.vxlan_manager.validate("192.168.1.1", "192.168.1.2", 1001)
mock_socket_instance.connect.assert_called_with(("192.168.1.2", 4789))
@patch("tunnel_manager.socket.socket")
def test_validate_vxlan_connectivity_failure(self, mock_socket):
mock_socket_instance = mock_socket.return_value.__enter__.return_value
mock_socket_instance.connect.side_effect = socket.error
with self.assertRaises(TunnelManagerError):
self.vxlan_manager.validate("192.168.1.1", "192.168.1.2", 1001)
@patch("tunnel_manager.socket.socket")
def test_validate_geneve_connectivity_success(self, mock_socket):
mock_socket_instance = mock_socket.return_value.__enter__.return_value
self.geneve_manager.validate("192.168.1.1", "192.168.1.2", 1001)
mock_socket_instance.connect.assert_called_with(("192.168.1.2", 6081))
@patch("tunnel_manager.socket.socket")
def test_validate_geneve_connectivity_failure(self, mock_socket):
mock_socket_instance = mock_socket.return_value.__enter__.return_value
mock_socket_instance.connect.side_effect = socket.error
with self.assertRaises(TunnelManagerError):
self.geneve_manager.validate("192.168.1.1", "192.168.1.2", 1001)
if __name__ == "__main__":
unittest.main()