1+ from __future__ import annotations
2+
13import pytest
24
35import logging
46
7+ from lib .bond import Bond
58from lib .host import Host
69from lib .network import Network
10+ from lib .tunnel import Tunnel
11+ from lib .vlan import VLAN
712
813from typing import Generator
914
@@ -14,14 +19,7 @@ def host_no_sdn_controller(host: Host):
1419 pytest .skip ("This test requires an XCP-ng with no SDN controller" )
1520
1621
17- @pytest .fixture (scope = 'module' )
18- def empty_network (host : Host ) -> Generator [Network , None , None ]:
19- try :
20- net = host .create_network (label = "empty_network for tests" )
21- yield net
22- finally :
23- net .destroy ()
24-
22+ # ---- Bond ----
2523@pytest .fixture (params = [])
2624def bond_devices (request : pytest .FixtureRequest ) -> list [str ]:
2725 return request .param
@@ -31,7 +29,7 @@ def bond_mode(request: pytest.FixtureRequest) -> str:
3129 return request .param
3230
3331@pytest .fixture
34- def bond (host : Host , empty_network : Network , bond_devices : list [str ], bond_mode : str ):
32+ def bond (host : Host , empty_network : Network , bond_devices : list [str ], bond_mode : str ) -> Generator [ Bond , None , None ] :
3533 pifs = []
3634 logging .info (f"bond: resolve PIFs on { host .hostname_or_ip } using \
3735 { [(pif .network_uuid (), pif .param_get ('device' )) for pif in host .pifs ()]} " )
@@ -44,3 +42,72 @@ def bond(host: Host, empty_network: Network, bond_devices: list[str], bond_mode:
4442 yield bond
4543 finally :
4644 bond .destroy ()
45+
46+
47+ # ---- Network ----
48+ @pytest .fixture (scope = 'module' )
49+ def empty_network (host : Host ) -> Generator [Network , None , None ]:
50+ try :
51+ net = host .create_network (label = "empty_network for tests" )
52+ yield net
53+ finally :
54+ net .destroy ()
55+
56+
57+ # ---- Tunnel ----
58+ @pytest .fixture (params = ["eth0" ])
59+ def tunnel_device (request : pytest .FixtureRequest ) -> str :
60+ return request .param
61+
62+ @pytest .fixture (params = ["gre" , "vxlan" ])
63+ def tunnel_protocol (request : pytest .FixtureRequest ) -> str :
64+ return request .param
65+
66+ @pytest .fixture (params = [False , True ])
67+ def tunnel_encryption (request : pytest .FixtureRequest ) -> bool :
68+ return request .param
69+
70+ @pytest .fixture
71+ def tunnel (
72+ host : Host , empty_network : Network ,
73+ tunnel_device : str , tunnel_protocol : str , tunnel_encryption : bool ,
74+ ) -> Generator [Tunnel , None , None ]:
75+ logging .info (f"tunnel: resolve PIF on { host .hostname_or_ip } using \
76+ { [(pif .network_uuid (), pif .param_get ('device' )) for pif in host .pifs ()]} " )
77+
78+ [pif ] = host .pifs (device = tunnel_device )
79+ if pif .ip_configuration_mode () == "None" :
80+ pytest .skip (f"'tunnel' fixture requires tunnel_device={ tunnel_device } to have configured IP" )
81+
82+ tunnel = host .create_tunnel (empty_network , pif , tunnel_protocol )
83+ try :
84+ empty_network .param_set ('other-config' , tunnel_protocol , key = 'xo:sdn-controller:encapsulation' )
85+ empty_network .param_set ('other-config' , tunnel_encryption , key = 'xo:sdn-controller:encrypted' )
86+
87+ yield tunnel
88+ finally :
89+ empty_network .param_set ('other-config' , None , key = 'xo:sdn-controller:encrypted' )
90+ empty_network .param_set ('other-config' , None , key = 'xo:sdn-controller:encapsulation' )
91+ tunnel .destroy ()
92+
93+
94+ # ---- VLAN ----
95+ @pytest .fixture (params = ["eth0" ])
96+ def vlan_device (request : pytest .FixtureRequest ) -> str :
97+ return request .param
98+
99+ @pytest .fixture (params = [0 ])
100+ def vlan_tag (request : pytest .FixtureRequest ) -> int :
101+ return request .param
102+
103+ @pytest .fixture
104+ def vlan (host : Host , empty_network : Network , vlan_tag : int , vlan_device : str ) -> Generator [VLAN , None , None ]:
105+ logging .info (f"vlan: resolve PIF on { host .hostname_or_ip } using \
106+ { [(pif .network_uuid (), pif .param_get ('device' )) for pif in host .pifs ()]} " )
107+
108+ [pif ] = host .pifs (device = vlan_device )
109+ vlan = host .create_vlan (empty_network , pif , vlan_tag )
110+ try :
111+ yield vlan
112+ finally :
113+ vlan .destroy ()
0 commit comments