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
12+ from lib .xo import xo_cli
713
814from typing import Generator
915
@@ -14,14 +20,7 @@ def host_no_sdn_controller(host: Host):
1420 pytest .skip ("This test requires an XCP-ng with no SDN controller" )
1521
1622
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-
23+ # ---- Bond ----
2524@pytest .fixture (params = [])
2625def bond_devices (request : pytest .FixtureRequest ) -> list [str ]:
2726 return request .param
@@ -31,7 +30,7 @@ def bond_mode(request: pytest.FixtureRequest) -> str:
3130 return request .param
3231
3332@pytest .fixture
34- def bond (host : Host , empty_network : Network , bond_devices : list [str ], bond_mode : str ):
33+ def bond (host : Host , empty_network : Network , bond_devices : list [str ], bond_mode : str ) -> Generator [ Bond , None , None ] :
3534 pifs = []
3635 logging .info (f"bond: resolve PIFs on { host .hostname_or_ip } using \
3736 { [(pif .network_uuid (), pif .param_get ('device' )) for pif in host .pifs ()]} " )
@@ -44,3 +43,106 @@ def bond(host: Host, empty_network: Network, bond_devices: list[str], bond_mode:
4443 yield bond
4544 finally :
4645 bond .destroy ()
46+
47+
48+ # ---- Network ----
49+ @pytest .fixture (scope = 'module' )
50+ def empty_network (host : Host ) -> Generator [Network , None , None ]:
51+ try :
52+ net = host .create_network (label = "empty_network for tests" )
53+ yield net
54+ finally :
55+ net .destroy ()
56+
57+
58+ # ---- Tunnel ----
59+ @pytest .fixture (params = ["eth0" ])
60+ def tunnel_device (request : pytest .FixtureRequest ) -> str :
61+ return request .param
62+
63+ @pytest .fixture (params = ["gre" , "vxlan" ])
64+ def tunnel_protocol (request : pytest .FixtureRequest ) -> str :
65+ return request .param
66+
67+ @pytest .fixture (params = [False , True ])
68+ def tunnel_encryption (request : pytest .FixtureRequest ) -> bool :
69+ return request .param
70+
71+ @pytest .fixture
72+ def tunnel (
73+ connected_hosts_with_xo : list [Host ],
74+ tunnel_device : str , tunnel_protocol : str , tunnel_encryption : bool ,
75+ ) -> Generator [Tunnel , None , None ]:
76+ host = connected_hosts_with_xo [0 ]
77+
78+ # check system requirements
79+ if host .ssh_with_result ("rpm -q openvswitch-ipsec" ).returncode != 0 :
80+ pytest .skip ("'tunnel' fixture requires configuration, see https://docs.xen-orchestra.com/sdn_controller" )
81+
82+ logging .info (f"tunnel: resolve PIF on { host .hostname_or_ip } using \
83+ { [(pif .network_uuid (), pif .param_get ('device' )) for pif in host .pifs ()]} " )
84+
85+ [pif ] = host .pifs (device = tunnel_device )
86+ if pif .ip_configuration_mode () == "None" :
87+ pytest .skip (f"'tunnel' fixture requires tunnel_device={ tunnel_device } to have configured IP" )
88+
89+ xo_cli ('sdnController.createPrivateNetwork' , {
90+ 'poolIds' : f"json:[\" { host .pool .uuid } \" ]" ,
91+ 'pifIds' : f"json:[\" { pif .uuid } \" ]" ,
92+ 'name' : 'test-tunnel' ,
93+ 'description' : 'tunnel for test' ,
94+ 'encapsulation' : tunnel_protocol ,
95+ 'encrypted' : 'true' if tunnel_encryption else 'false' ,
96+ })
97+ tunnel = None
98+ network = None
99+ try :
100+ # get Tunnel from PIF
101+ tunnel_uuid = host .xe ('tunnel-list' , {
102+ 'transport-PIF' : pif .uuid ,
103+ }, minimal = True )
104+ tunnel = Tunnel (host , tunnel_uuid )
105+
106+ # get Network from Tunnel
107+ network_uuid = tunnel .access_PIF ().network_uuid ()
108+ network = Network (host , network_uuid )
109+
110+ yield tunnel
111+ finally :
112+ if tunnel is not None :
113+ tunnel .destroy ()
114+
115+ if network is not None :
116+ # sdnController.createPrivateNetwork might have create several Tunnel (one per host)
117+ # so get all Tunnel attached to Network and destroy them
118+ for pif_uuid in network .pif_uuids ():
119+ tunnel_uuid = host .xe ('tunnel-list' , {
120+ 'access-PIF' : pif_uuid ,
121+ }, minimal = True )
122+ tunnel = Tunnel (host , tunnel_uuid )
123+ tunnel .destroy ()
124+
125+ # finally destroy the Network
126+ network .destroy ()
127+
128+
129+ # ---- VLAN ----
130+ @pytest .fixture (params = ["eth0" ])
131+ def vlan_device (request : pytest .FixtureRequest ) -> str :
132+ return request .param
133+
134+ @pytest .fixture (params = [0 ])
135+ def vlan_tag (request : pytest .FixtureRequest ) -> int :
136+ return request .param
137+
138+ @pytest .fixture
139+ def vlan (host : Host , empty_network : Network , vlan_tag : int , vlan_device : str ) -> Generator [VLAN , None , None ]:
140+ logging .info (f"vlan: resolve PIF on { host .hostname_or_ip } using \
141+ { [(pif .network_uuid (), pif .param_get ('device' )) for pif in host .pifs ()]} " )
142+
143+ [pif ] = host .pifs (device = vlan_device )
144+ vlan = host .create_vlan (empty_network , pif , vlan_tag )
145+ try :
146+ yield vlan
147+ finally :
148+ vlan .destroy ()
0 commit comments