12
12
13
13
import functools
14
14
import json
15
- import re
16
15
17
- from testinfra .modules .base import Module
16
+ from testinfra .modules .base import InstanceModule
18
17
19
- class IP (Module ):
20
- """Test network configuration via ip commands
18
+
19
+ class IP (InstanceModule ):
20
+ """Test network configuration via iproute2 commands
21
21
22
22
>>> host.ip.rules()
23
23
@@ -27,7 +27,7 @@ class IP(Module):
27
27
host.ip.addresses()
28
28
host.ip.tunnels()
29
29
30
- Optionally, the protocol family can be provided:
30
+ Optionally, the protocol family can be provided to reduce the number of routes returned :
31
31
>>> host.ip.routes("inet6", table="main")
32
32
...FIX
33
33
@@ -36,94 +36,67 @@ class IP(Module):
36
36
...FIX
37
37
"""
38
38
39
- def __init__ (self , family = None , netns = None ):
39
+ def __init__ (self , family = None , namespace = None ):
40
40
self .family = family
41
- self .netns = netns
41
+ self .namespace = namespace
42
42
super ().__init__ ()
43
43
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
-
73
44
def __repr__ (self ):
74
45
return "<ip>"
75
46
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 ):
83
47
@functools .cached_property
84
48
def _ip (self ):
85
49
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 } "
88
52
if self .family is not None :
89
53
ip_cmd = f"{ ip_cmd } -f { self .family } "
90
54
return ip_cmd
91
55
92
56
@property
93
57
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
95
59
96
60
def addresses (self ):
97
- """Return the addresses associated with interfaces
98
- """
61
+ """Return the addresses associated with interfaces"""
99
62
cmd = f"{ self ._ip } --json address show"
100
63
out = self .check_output (cmd )
101
64
return json .loads (out )
102
65
103
66
def links (self ):
104
- """Return links and their state
105
- """
67
+ """Return links and their state"""
106
68
cmd = f"{ self ._ip } --json link show"
107
69
out = self .check_output (cmd )
108
70
return json .loads (out )
109
71
110
72
def routes (self ):
111
- """Return the routes installed
112
- """
73
+ """Return the routes installed"""
113
74
cmd = f"{ self ._ip } --json route show table all"
114
75
out = self .check_output (cmd )
115
76
return json .loads (out )
116
77
117
78
def rules (self ):
118
- """Return the rules our routing policy consists of
119
- """
79
+ """Return the rules our routing policy consists of"""
120
80
cmd = f"{ self ._ip } --json rule show"
121
81
out = self .check_output (cmd )
122
82
return json .loads (out )
123
83
124
84
def tunnels (self ):
125
- """Return all configured tunnels
126
- """
85
+ """Return all configured tunnels"""
127
86
cmd = f"{ self ._ip } --json tunnel show"
128
87
out = self .check_output (cmd )
129
88
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