1212
1313import functools
1414import json
15- import re
1615
17- from testinfra .modules .base import Module
16+ from testinfra .modules .base import InstanceModule
1817
19- class IP (Module ):
20- """Test network configuration via ip commands
18+
19+ class IP (InstanceModule ):
20+ """Test network configuration via iproute2 commands
2121
2222 >>> host.ip.rules()
2323
@@ -27,7 +27,7 @@ class IP(Module):
2727 host.ip.addresses()
2828 host.ip.tunnels()
2929
30- Optionally, the protocol family can be provided:
30+ Optionally, the protocol family can be provided to reduce the number of routes returned :
3131 >>> host.ip.routes("inet6", table="main")
3232 ...FIX
3333
@@ -36,94 +36,67 @@ class IP(Module):
3636 ...FIX
3737 """
3838
39- def __init__ (self , family = None , netns = None ):
39+ def __init__ (self , family = None , namespace = None ):
4040 self .family = family
41- self .netns = netns
41+ self .namespace = namespace
4242 super ().__init__ ()
4343
44- @property
45- def exists (self ):
46- raise NotImplementedError
47-
48- def addresses (self ):
49- """Return the addresses associated with interfaces
50- """
51- raise NotImplementedError
52-
53- def links (self ):
54- """Return links and their state
55- """
56- raise NotImplementedError
57-
58- def routes (self ):
59- """Return the routes associated with the routing table
60- """
61- raise NotImplementedError
62-
63- def rules (self ):
64- """Return all configured ip rules
65- """
66- raise NotImplementedError
67-
68- def tunnels (self ):
69- """Return all configured tunnels
70- """
71- raise NotImplementedError
72-
7344 def __repr__ (self ):
7445 return "<ip>"
7546
76- @classmethod
77- def get_module_class (cls , host ):
78- if host .system_info .type == "linux" :
79- return LinuxIP
80- raise NotImplementedError
81-
82- class LinuxIP (IP ):
8347 @functools .cached_property
8448 def _ip (self ):
8549 ip_cmd = self .find_command ("ip" )
86- if self .netns is not None :
87- ip_cmd = f"{ ip_cmd } netns exec { self .netns } { ip_cmd } "
50+ if self .namespace is not None :
51+ ip_cmd = f"{ ip_cmd } netns exec { self .namespace } { ip_cmd } "
8852 if self .family is not None :
8953 ip_cmd = f"{ ip_cmd } -f { self .family } "
9054 return ip_cmd
9155
9256 @property
9357 def exists (self ):
94- return self .run_test ("{} -V" .format (self ._ip ), self . name ).rc == 0
58+ return self .run_test ("{} -V" .format (self ._ip )).rc == 0
9559
9660 def addresses (self ):
97- """Return the addresses associated with interfaces
98- """
61+ """Return the addresses associated with interfaces"""
9962 cmd = f"{ self ._ip } --json address show"
10063 out = self .check_output (cmd )
10164 return json .loads (out )
10265
10366 def links (self ):
104- """Return links and their state
105- """
67+ """Return links and their state"""
10668 cmd = f"{ self ._ip } --json link show"
10769 out = self .check_output (cmd )
10870 return json .loads (out )
10971
11072 def routes (self ):
111- """Return the routes installed
112- """
73+ """Return the routes installed"""
11374 cmd = f"{ self ._ip } --json route show table all"
11475 out = self .check_output (cmd )
11576 return json .loads (out )
11677
11778 def rules (self ):
118- """Return the rules our routing policy consists of
119- """
79+ """Return the rules our routing policy consists of"""
12080 cmd = f"{ self ._ip } --json rule show"
12181 out = self .check_output (cmd )
12282 return json .loads (out )
12383
12484 def tunnels (self ):
125- """Return all configured tunnels
126- """
85+ """Return all configured tunnels"""
12786 cmd = f"{ self ._ip } --json tunnel show"
12887 out = self .check_output (cmd )
12988 return json .loads (out )
89+
90+ def vrfs (self ):
91+ """Return all configured vrfs"""
92+ cmd = f"{ self ._ip } --json vrf show"
93+ out = self .check_output (cmd )
94+ return json .loads (out )
95+
96+ def netns (self ):
97+ """Return all configured network namespaces"""
98+ cmd = f"{ self ._ip } --json netns show"
99+ out = self .check_output (cmd )
100+ if out is None : # ip netns returns null instead of [] in json mode
101+ return json .loads ("[]" )
102+ return json .loads (out )
0 commit comments