1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
import errno
import os
import platform
import pwd
import re
import stat
import subprocess
import sys
import uuid
from socket import AF_INET, AF_INET6
import netaddr
import pytest
from pyroute2 import config
from pyroute2.iproute.linux import IPRoute
try:
import httplib
except ImportError:
import http.client as httplib
dtcd_uuid = str(uuid.uuid4())
# check the dtcd
try:
cx = httplib.HTTPConnection('localhost:7623')
cx.request('GET', '/v1/network/')
cx.getresponse()
has_dtcd = True
except:
has_dtcd = False
supernet = {
AF_INET: netaddr.IPNetwork('172.16.0.0/12'),
AF_INET6: netaddr.IPNetwork('fdb3:84e5:4ff4::/48'),
}
network_pool = {
AF_INET: list(supernet[AF_INET].subnet(24)),
AF_INET6: list(supernet[AF_INET6].subnet(64)),
}
allocations = {}
family_url = {AF_INET: 'ipv4', AF_INET6: 'ipv6'}
def allocate_network(family=AF_INET):
global dtcd_uuid
global network_pool
global allocations
network = None
try:
cx = httplib.HTTPConnection('localhost:7623')
cx.request(
'POST', '/v1/network/%s/' % family_url[family], body=dtcd_uuid
)
resp = cx.getresponse()
if resp.status == 200:
network = netaddr.IPNetwork(resp.read().decode('utf-8'))
cx.close()
except Exception:
pass
if network is None:
network = network_pool[family].pop()
allocations[network] = True
return network
def free_network(network, family=AF_INET):
global network_pool
global allocations
if network in allocations:
allocations.pop(network)
network_pool[family].append(network)
else:
cx = httplib.HTTPConnection('localhost:7623')
cx.request(
'DELETE', '/v1/network/%s/' % family_url[family], body=str(network)
)
cx.getresponse()
cx.close()
def conflict_arch(arch):
if platform.machine().find(arch) >= 0:
pytest.skip('conflict with architecture %s' % (arch))
def kernel_version_ge(major, minor):
# True if running kernel is >= X.Y
if config.kernel[0] > major:
return True
if config.kernel[0] < major:
return False
if minor and config.kernel[1] < minor:
return False
return True
def require_kernel(major, minor=None):
if not kernel_version_ge(major, minor):
pytest.skip('incompatible kernel version')
def require_python(target):
if sys.version_info[0] != target:
pytest.skip('test requires Python %i' % target)
def require_8021q():
try:
os.stat('/proc/net/vlan/config')
except OSError as e:
# errno 2 'No such file or directory'
if e.errno == 2:
pytest.skip('missing 8021q support, or module is not loaded')
raise
def require_bridge():
with IPRoute() as ip:
try:
ip.link('add', ifname='test_req', kind='bridge')
except Exception:
pytest.skip('can not create <bridge>')
idx = ip.link_lookup(ifname='test_req')
if not idx:
pytest.skip('can not create <bridge>')
ip.link('del', index=idx)
def require_bond():
with IPRoute() as ip:
try:
ip.link('add', ifname='test_req', kind='bond')
except Exception:
pytest.skip('can not create <bond>')
idx = ip.link_lookup(ifname='test_req')
if not idx:
pytest.skip('can not create <bond>')
ip.link('del', index=idx)
def require_user(user):
if bool(os.environ.get('PYROUTE2_TESTS_RO', False)):
pytest.skip('read-only tests requested')
if pwd.getpwuid(os.getuid()).pw_name != user:
pytest.skip('required user %s' % (user))
def require_executable(name):
try:
with open(os.devnull, 'w') as fnull:
subprocess.check_call(['which', name], stdout=fnull, stderr=fnull)
except Exception:
pytest.skip('required %s not found' % (name))
def remove_link(name):
if os.getuid() != 0:
return
with open(os.devnull, 'w') as fnull:
subprocess.call(
['ip', 'link', 'del', 'dev', name], stdout=fnull, stderr=fnull
)
while True:
links = get_ip_link()
if name not in links:
break
def create_link(name, kind):
if os.getuid() != 0:
return
subprocess.call(['ip', 'link', 'add', 'dev', name, 'type', kind])
for i in range(20):
links = get_ip_link()
if name in links:
return
raise Exception("interface not created")
def _check_output(*argv):
# we can not use check_output, as it does not exist in 2.6
process = subprocess.Popen(argv, stdout=subprocess.PIPE)
ret = process.communicate()
return ret[0].decode('utf-8').split('\n')
def grep(command, pattern=None):
out = _check_output(*command.split())
ret = []
reg = re.compile(pattern)
for string in out:
if reg.search(string):
ret.append(string)
return ret
def get_ip_addr(interface=None):
argv = ['ip', '-o', 'ad']
if interface:
argv.extend(['li', 'dev', interface])
out = _check_output(*argv)
ret = []
for string in out:
fields = string.split()
if len(fields) >= 5 and fields[2][:4] == 'inet':
ret.append(fields[3])
return ret
def get_ip_brd(interface=None):
argv = ['ip', '-o', 'ad']
if interface:
argv.extend(['li', 'dev', interface])
out = _check_output(*argv)
ret = []
for string in out:
fields = string.split()
if len(fields) >= 5 and fields[4] == 'brd':
ret.append(fields[5])
return ret
def get_ip_link():
ret = []
out = _check_output('ip', '-o', 'li')
for string in out:
fields = string.split()
if len(fields) >= 2:
ret.append(fields[1][:-1].split('@')[0])
return ret
def get_ip_default_routes():
ret = []
out = _check_output('ip', '-4', 'ro')
for string in out:
if 'default' in string:
ret.append(string)
return ret
def get_ip_rules(proto='-4'):
ret = []
out = _check_output('ip', proto, 'rule', 'show')
for string in out:
if len(string):
ret.append(string)
return ret
def count_socket_fds():
pid_fd = '/proc/%s/fd' % os.getpid()
sockets = 0
for fd in os.listdir(pid_fd):
try:
if stat.S_ISSOCK(os.stat(os.path.join(pid_fd, fd)).st_mode):
sockets += 1
except OSError as e:
if e.errno != errno.ENOENT:
raise
return sockets
|