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,76 @@ 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+ # XXX doesn't trigger XO-side for proper PrivateNetwork configuration
79+
80+ [pif ] = host .pifs (device = tunnel_device )
81+ if pif .ip_configuration_mode () == "None" :
82+ pytest .skip (f"'tunnel' fixture requires tunnel_device={ tunnel_device } to have configured IP" )
83+
84+ tunnel = host .create_tunnel (empty_network , pif , tunnel_protocol )
85+ try :
86+ empty_network .param_set ('other-config' , tunnel .uuid , key = 'xo:sdn-controller:private-network-uuid' )
87+ empty_network .param_set ('other-config' , tunnel_protocol , key = 'xo:sdn-controller:encapsulation' )
88+ empty_network .param_set ('other-config' , tunnel_encryption , key = 'xo:sdn-controller:encrypted' )
89+
90+ yield tunnel
91+ finally :
92+ empty_network .param_set ('other-config' , None , key = 'xo:sdn-controller:encrypted' )
93+ empty_network .param_set ('other-config' , None , key = 'xo:sdn-controller:encapsulation' )
94+ empty_network .param_set ('other-config' , None , key = 'xo:sdn-controller:private-network-uuid' )
95+ tunnel .destroy ()
96+
97+
98+ # ---- VLAN ----
99+ @pytest .fixture (params = ["eth0" ])
100+ def vlan_device (request : pytest .FixtureRequest ) -> str :
101+ return request .param
102+
103+ @pytest .fixture (params = [0 ])
104+ def vlan_tag (request : pytest .FixtureRequest ) -> int :
105+ return request .param
106+
107+ @pytest .fixture
108+ def vlan (host : Host , empty_network : Network , vlan_tag : int , vlan_device : str ) -> Generator [VLAN , None , None ]:
109+ logging .info (f"vlan: resolve PIF on { host .hostname_or_ip } using \
110+ { [(pif .network_uuid (), pif .param_get ('device' )) for pif in host .pifs ()]} " )
111+
112+ [pif ] = host .pifs (device = vlan_device )
113+ vlan = host .create_vlan (empty_network , pif , vlan_tag )
114+ try :
115+ yield vlan
116+ finally :
117+ vlan .destroy ()
0 commit comments