Merge "Execute ipset command using check_exit_code"

This commit is contained in:
Jenkins 2015-10-06 11:20:15 +00:00 committed by Gerrit Code Review
commit 7554106385
3 changed files with 19 additions and 12 deletions

View File

@ -124,7 +124,7 @@ class IpsetManager(object):
def _del_member_from_set(self, set_name, member_ip): def _del_member_from_set(self, set_name, member_ip):
cmd = ['ipset', 'del', set_name, member_ip] cmd = ['ipset', 'del', set_name, member_ip]
self._apply(cmd) self._apply(cmd, fail_on_errors=False)
self.ipset_sets[set_name].remove(member_ip) self.ipset_sets[set_name].remove(member_ip)
def _create_set(self, set_name, ethertype): def _create_set(self, set_name, ethertype):
@ -133,13 +133,14 @@ class IpsetManager(object):
self._apply(cmd) self._apply(cmd)
self.ipset_sets[set_name] = [] self.ipset_sets[set_name] = []
def _apply(self, cmd, input=None): def _apply(self, cmd, input=None, fail_on_errors=True):
input = '\n'.join(input) if input else None input = '\n'.join(input) if input else None
cmd_ns = [] cmd_ns = []
if self.namespace: if self.namespace:
cmd_ns.extend(['ip', 'netns', 'exec', self.namespace]) cmd_ns.extend(['ip', 'netns', 'exec', self.namespace])
cmd_ns.extend(cmd) cmd_ns.extend(cmd)
self.execute(cmd_ns, run_as_root=True, process_input=input) self.execute(cmd_ns, run_as_root=True, process_input=input,
check_exit_code=fail_on_errors)
def _get_new_set_ips(self, set_name, expected_ips): def _get_new_set_ips(self, set_name, expected_ips):
new_member_ips = (set(expected_ips) - new_member_ips = (set(expected_ips) -
@ -175,5 +176,5 @@ class IpsetManager(object):
def _destroy(self, set_name, forced=False): def _destroy(self, set_name, forced=False):
if set_name in self.ipset_sets or forced: if set_name in self.ipset_sets or forced:
cmd = ['ipset', 'destroy', set_name] cmd = ['ipset', 'destroy', set_name]
self._apply(cmd) self._apply(cmd, fail_on_errors=False)
self.ipset_sets.pop(set_name, None) self.ipset_sets.pop(set_name, None)

View File

@ -97,6 +97,5 @@ class IpsetManagerTestCase(IpsetBase):
self.source.assert_ping(self.destination.ip) self.source.assert_ping(self.destination.ip)
def test_destroy_ipset_set(self): def test_destroy_ipset_set(self):
self.assertRaises(RuntimeError, self.ipset._destroy, self.ipset_name)
self._remove_iptables_ipset_rules() self._remove_iptables_ipset_rules()
self.ipset._destroy(self.ipset_name) self.ipset._destroy(self.ipset_name)

View File

@ -67,19 +67,23 @@ class BaseIpsetManagerTest(base.BaseTestCase):
self.expected_calls.extend([ self.expected_calls.extend([
mock.call(['ipset', 'restore', '-exist'], mock.call(['ipset', 'restore', '-exist'],
process_input=input, process_input=input,
run_as_root=True), run_as_root=True,
check_exit_code=True),
mock.call(['ipset', 'swap', TEST_SET_NAME_NEW, TEST_SET_NAME], mock.call(['ipset', 'swap', TEST_SET_NAME_NEW, TEST_SET_NAME],
process_input=None, process_input=None,
run_as_root=True), run_as_root=True,
check_exit_code=True),
mock.call(['ipset', 'destroy', TEST_SET_NAME_NEW], mock.call(['ipset', 'destroy', TEST_SET_NAME_NEW],
process_input=None, process_input=None,
run_as_root=True)]) run_as_root=True,
check_exit_code=False)])
def expect_add(self, addresses): def expect_add(self, addresses):
self.expected_calls.extend( self.expected_calls.extend(
mock.call(['ipset', 'add', '-exist', TEST_SET_NAME, ip], mock.call(['ipset', 'add', '-exist', TEST_SET_NAME, ip],
process_input=None, process_input=None,
run_as_root=True) run_as_root=True,
check_exit_code=True)
for ip in self.ipset._sanitize_addresses(addresses)) for ip in self.ipset._sanitize_addresses(addresses))
def expect_del(self, addresses): def expect_del(self, addresses):
@ -87,7 +91,8 @@ class BaseIpsetManagerTest(base.BaseTestCase):
self.expected_calls.extend( self.expected_calls.extend(
mock.call(['ipset', 'del', TEST_SET_NAME, ip], mock.call(['ipset', 'del', TEST_SET_NAME, ip],
process_input=None, process_input=None,
run_as_root=True) run_as_root=True,
check_exit_code=False)
for ip in self.ipset._sanitize_addresses(addresses)) for ip in self.ipset._sanitize_addresses(addresses))
def expect_create(self): def expect_create(self):
@ -95,13 +100,15 @@ class BaseIpsetManagerTest(base.BaseTestCase):
mock.call(['ipset', 'create', '-exist', TEST_SET_NAME, mock.call(['ipset', 'create', '-exist', TEST_SET_NAME,
'hash:net', 'family', 'inet'], 'hash:net', 'family', 'inet'],
process_input=None, process_input=None,
run_as_root=True)) run_as_root=True,
check_exit_code=True))
def expect_destroy(self): def expect_destroy(self):
self.expected_calls.append( self.expected_calls.append(
mock.call(['ipset', 'destroy', TEST_SET_NAME], mock.call(['ipset', 'destroy', TEST_SET_NAME],
process_input=None, process_input=None,
run_as_root=True)) run_as_root=True,
check_exit_code=False))
def add_first_ip(self): def add_first_ip(self):
self.expect_set([FAKE_IPS[0]]) self.expect_set([FAKE_IPS[0]])