Resolve ifconfig/bird6/dnsmasq BSD -> Linux-isms.

This commit is contained in:
Ryan Petrello 2014-08-06 15:01:27 -07:00
parent b4d1e70a1c
commit 57bc76c54c
7 changed files with 53 additions and 21 deletions

View File

@ -42,11 +42,11 @@ class BirdManager(base.Manager):
def restart(self):
try:
utils.execute(['/etc/rc.d/bird6', 'check'], self.root_helper)
utils.execute(['/etc/init.d/bird6', 'status'], self.root_helper)
except: # pragma no cover
utils.execute(['/etc/rc.d/bird6', 'start'], self.root_helper)
utils.execute(['/etc/init.d/bird6', 'start'], self.root_helper)
else: # pragma no cover
utils.execute(['/etc/rc.d/bird6', 'reload'], self.root_helper)
utils.execute(['/etc/init.d/bird6', 'reload'], self.root_helper)
def build_config(config, interface_map):

View File

@ -25,7 +25,7 @@ from akanda.router import utils
LOG = logging.getLogger(__name__)
CONF_DIR = '/etc/dnsmasq.d'
RC_PATH = '/etc/rc.d/dnsmasq'
RC_PATH = '/etc/init.d/dnsmasq'
DEFAULT_LEASE = 86400
@ -90,7 +90,7 @@ class DHCPManager(base.Manager):
def restart(self):
try:
utils.execute(['/etc/rc.d/dnsmasq', 'stop'], self.root_helper)
utils.execute(['/etc/init.d/dnsmasq', 'stop'], self.root_helper)
except:
pass
@ -99,7 +99,7 @@ class DHCPManager(base.Manager):
while remaining:
remaining -= 1
try:
utils.execute(['/etc/rc.d/dnsmasq', 'start'], self.root_helper)
utils.execute(['/etc/init.d/dnsmasq', 'start'], self.root_helper)
return
except Exception:
if remaining <= 0:

View File

@ -15,6 +15,7 @@
# under the License.
import functools
import re
import netaddr
@ -111,14 +112,17 @@ class InterfaceManager(base.Manager):
self.sudo(real_ifname, 'description', interface.description)
def _update_addresses(self, real_ifname, interface, old_interface):
family = {4: 'inet', 6: 'inet6'}
add = lambda a: (
real_ifname, family[a[0].version], 'add', '%s/%s' % (a[0], a[1])
)
delete = lambda a: (
real_ifname, family[a[0].version], 'del', '%s/%s' % (a[0], a[1])
)
def _gen_cmd(cmd, address):
family = {4: 'inet', 6: 'inet6'}[address[0].version]
args = [real_ifname, family]
if family == 'inet6':
args.append(cmd)
args.append('%s/%s' % (address[0], address[1]))
return args
add = functools.partial(_gen_cmd, 'add')
delete = functools.partial(_gen_cmd, 'del')
mutator = lambda a: (a.ip, a.prefixlen)
self._update_set(real_ifname, interface, old_interface,

View File

@ -55,7 +55,7 @@ class MetadataManager(base.Manager):
def ensure_started(self):
try:
execute(['/etc/rc.d/metadata', 'check'], self.root_helper)
execute(['/etc/rc.d/metadata', 'status'], self.root_helper)
except:
execute(['/etc/rc.d/metadata', 'start'], self.root_helper)

View File

@ -96,17 +96,17 @@ class BirdTestCase(TestCase):
def test_restart(self):
self.mgr.restart()
self.mock_execute.assert_has_calls([
mock.call(['/etc/rc.d/bird6', 'check'], 'sudo'),
mock.call(['/etc/rc.d/bird6', 'reload'], 'sudo'),
mock.call(['/etc/init.d/bird6', 'status'], 'sudo'),
mock.call(['/etc/init.d/bird6', 'reload'], 'sudo'),
])
def test_restart_failure(self):
with mock.patch('akanda.router.utils.execute') as execute:
execute.side_effect = [Exception('check failed!'), None]
execute.side_effect = [Exception('status failed!'), None]
self.mgr.restart()
execute.assert_has_calls([
mock.call(['/etc/rc.d/bird6', 'check'], 'sudo'),
mock.call(['/etc/rc.d/bird6', 'start'], 'sudo'),
mock.call(['/etc/init.d/bird6', 'status'], 'sudo'),
mock.call(['/etc/init.d/bird6', 'start'], 'sudo'),
])
def test_build_config(self):

View File

@ -136,6 +136,6 @@ class DnsmasqTestCase(TestCase):
def test_restart(self):
self.mgr.restart()
self.mock_execute.assert_has_calls([
mock.call(['/etc/rc.d/dnsmasq', 'stop'], 'sudo'),
mock.call(['/etc/rc.d/dnsmasq', 'start'], 'sudo')
mock.call(['/etc/init.d/dnsmasq', 'stop'], 'sudo'),
mock.call(['/etc/init.d/dnsmasq', 'start'], 'sudo')
])

View File

@ -269,6 +269,34 @@ class IfconfigTestCase(TestCase):
mock.ANY
)
def test_address_add(self):
cmd = '/sbin/ifconfig'
v4 = netaddr.IPNetwork('172.16.27.13/24')
v6 = netaddr.IPNetwork('fdca:3ba5:a17a:acda:20c:29ff:fe94:723d/64')
iface = mock.Mock(all_addresses=[v4, v6])
old_iface = mock.Mock(all_addresses=[])
mgr = ifconfig.InterfaceManager()
mgr._update_addresses('em0', iface, old_iface)
assert self.mock_execute.call_args_list == [
mock.call([cmd, 'em0', 'inet', str(v4)], 'sudo'),
mock.call([cmd, 'em0', 'inet6', 'add', str(v6)], 'sudo'),
]
def test_address_remove(self):
cmd = '/sbin/ifconfig'
v4 = netaddr.IPNetwork('172.16.27.13/24')
v6 = netaddr.IPNetwork('fdca:3ba5:a17a:acda:20c:29ff:fe94:723d/64')
iface = mock.Mock(all_addresses=[])
old_iface = mock.Mock(all_addresses=[v4, v6])
mgr = ifconfig.InterfaceManager()
mgr._update_addresses('em0', iface, old_iface)
assert self.mock_execute.call_args_list == [
mock.call([cmd, 'em0', 'inet', str(v4)], 'sudo'),
mock.call([cmd, 'em0', 'inet6', 'del', str(v6)], 'sudo'),
]
def test_update_set(self):
iface = mock.Mock()
iface.all_addresses = ['a', 'b']