<s> import os <EOL> import sys <EOL> import mock <EOL> from oslo_config import cfg <EOL> import testtools <EOL> from neutron . _i18n import _ <EOL> from neutron . agent . linux import iptables_comments as ic <EOL> from neutron . agent . linux import iptables_manager <EOL> from neutron . common import constants <EOL> from neutron . common import exceptions as n_exc <EOL> from neutron . tests import base <EOL> from neutron . tests import tools <EOL> IPTABLES_ARG = { '<STR_LIT>' : iptables_manager . binary_name , <EOL> '<STR_LIT>' : ic . SNAT_OUT , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : constants . ROUTER_MARK_MASK } <EOL> NAT_TEMPLATE = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> NAT_DUMP = NAT_TEMPLATE % IPTABLES_ARG <EOL> FILTER_TEMPLATE = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> FILTER_DUMP = FILTER_TEMPLATE % IPTABLES_ARG <EOL> FILTER_WITH_RULES_TEMPLATE = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> COMMENTED_NAT_DUMP = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % IPTABLES_ARG ) <EOL> TRAFFIC_COUNTERS_DUMP = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class IptablesTestCase ( base . BaseTestCase ) : <EOL> def test_get_binary_name_in_unittest ( self ) : <EOL> with mock . patch ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:class>' ] ) : <EOL> binary_name = iptables_manager . get_binary_name ( ) <EOL> self . assertEqual ( '<STR_LIT>' , binary_name ) <EOL> class IptablesCommentsTestCase ( base . BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( IptablesCommentsTestCase , self ) . setUp ( ) <EOL> cfg . CONF . set_override ( '<STR_LIT>' , True , '<STR_LIT>' ) <EOL> self . iptables = iptables_manager . IptablesManager ( ) <EOL> self . execute = mock . patch . object ( self . iptables , "<STR_LIT>" ) . start ( ) <EOL> def test_comments_short_enough ( self ) : <EOL> for attr in dir ( ic ) : <EOL> if not attr . startswith ( '<STR_LIT>' ) and len ( getattr ( ic , attr ) ) > <NUM_LIT:255> : <EOL> self . fail ( "<STR_LIT>" <EOL> % attr ) <EOL> def test_reordering_of_jump_rule_comments ( self ) : <EOL> self . assertEqual ( <EOL> '<STR_LIT>' , <EOL> iptables_manager . comment_rule ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . assertEqual ( <EOL> '<STR_LIT>' , <EOL> iptables_manager . comment_rule ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . assertEqual ( <EOL> '<STR_LIT>' , <EOL> iptables_manager . comment_rule ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_add_filter_rule ( self ) : <EOL> iptables_args = { } <EOL> iptables_args . update ( IPTABLES_ARG ) <EOL> filter_rules = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % iptables_args ) <EOL> iptables_args [ '<STR_LIT>' ] = filter_rules <EOL> filter_dump_mod = FILTER_WITH_RULES_TEMPLATE % iptables_args <EOL> raw_dump = _generate_raw_dump ( IPTABLES_ARG ) <EOL> mangle_dump = _generate_mangle_dump ( IPTABLES_ARG ) <EOL> expected_calls_and_values = [ <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( filter_dump_mod + mangle_dump + <EOL> COMMENTED_NAT_DUMP + raw_dump ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( FILTER_DUMP + mangle_dump + <EOL> COMMENTED_NAT_DUMP + raw_dump ) , <EOL> run_as_root = True <EOL> ) , <EOL> None ) , <EOL> ] <EOL> tools . setup_mock_calls ( self . execute , expected_calls_and_values ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_chain ( '<STR_LIT>' ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_rule ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_rule ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % IPTABLES_ARG ) <EOL> self . iptables . apply ( ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_rule ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_rule ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % IPTABLES_ARG ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_chain ( '<STR_LIT>' ) <EOL> self . iptables . apply ( ) <EOL> tools . verify_mock_calls ( self . execute , expected_calls_and_values ) <EOL> def _generate_mangle_dump ( iptables_args ) : <EOL> return ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % iptables_args ) <EOL> def _generate_mangle_dump_v6 ( iptables_args ) : <EOL> return ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % iptables_args ) <EOL> def _generate_raw_dump ( iptables_args ) : <EOL> return ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % iptables_args ) <EOL> MANGLE_DUMP = _generate_mangle_dump ( IPTABLES_ARG ) <EOL> MANGLE_DUMP_V6 = _generate_mangle_dump_v6 ( IPTABLES_ARG ) <EOL> RAW_DUMP = _generate_raw_dump ( IPTABLES_ARG ) <EOL> class IptablesManagerStateFulTestCase ( base . BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( IptablesManagerStateFulTestCase , self ) . setUp ( ) <EOL> cfg . CONF . set_override ( '<STR_LIT>' , False , '<STR_LIT>' ) <EOL> self . iptables = iptables_manager . IptablesManager ( ) <EOL> self . execute = mock . patch . object ( self . iptables , "<STR_LIT>" ) . start ( ) <EOL> def test_binary_name ( self ) : <EOL> expected = os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) [ : <NUM_LIT:16> ] <EOL> self . assertEqual ( expected , iptables_manager . binary_name ) <EOL> def test_get_chain_name ( self ) : <EOL> name = '<STR_LIT>' * <NUM_LIT:5> <EOL> self . assertEqual ( iptables_manager . get_chain_name ( name , wrap = False ) , <EOL> name [ : <NUM_LIT> ] ) <EOL> self . assertEqual ( iptables_manager . get_chain_name ( name , wrap = True ) , <EOL> name [ : <NUM_LIT:11> ] ) <EOL> def test_defer_apply_with_exception ( self ) : <EOL> self . iptables . _apply = mock . Mock ( side_effect = Exception ) <EOL> with testtools . ExpectedException ( n_exc . IpTablesApplyException ) : <EOL> with self . iptables . defer_apply ( ) : <EOL> pass <EOL> def _extend_with_ip6tables_filter ( self , expected_calls , filter_dump ) : <EOL> expected_calls . insert ( <NUM_LIT:2> , ( <EOL> mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) ) <EOL> expected_calls . insert ( <NUM_LIT:3> , ( <EOL> mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = filter_dump , <EOL> run_as_root = True ) , <EOL> None ) ) <EOL> expected_calls . extend ( [ <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = filter_dump , <EOL> run_as_root = True ) , <EOL> None ) ] ) <EOL> def _test_add_and_remove_chain_custom_binary_name_helper ( self , use_ipv6 ) : <EOL> bn = ( "<STR_LIT>" * <NUM_LIT:5> ) <EOL> self . iptables = iptables_manager . IptablesManager ( <EOL> binary_name = bn , <EOL> use_ipv6 = use_ipv6 ) <EOL> self . execute = mock . patch . object ( self . iptables , "<STR_LIT>" ) . start ( ) <EOL> iptables_args = { '<STR_LIT>' : bn [ : <NUM_LIT:16> ] , '<STR_LIT>' : '<STR_LIT>' } <EOL> filter_dump = FILTER_WITH_RULES_TEMPLATE % iptables_args <EOL> filter_dump_ipv6 = FILTER_TEMPLATE % iptables_args <EOL> filter_dump_mod = filter_dump <EOL> nat_dump = NAT_TEMPLATE % iptables_args <EOL> raw_dump = _generate_raw_dump ( iptables_args ) <EOL> mangle_dump = _generate_mangle_dump ( iptables_args ) <EOL> expected_calls_and_values = [ <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( filter_dump_mod + mangle_dump + <EOL> nat_dump + raw_dump ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( filter_dump + mangle_dump + <EOL> nat_dump + raw_dump ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ] <EOL> if use_ipv6 : <EOL> mangle_dump_v6 = _generate_mangle_dump_v6 ( iptables_args ) <EOL> self . _extend_with_ip6tables_filter ( <EOL> expected_calls_and_values , <EOL> filter_dump_ipv6 + mangle_dump_v6 + raw_dump ) <EOL> tools . setup_mock_calls ( self . execute , expected_calls_and_values ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_chain ( '<STR_LIT>' ) <EOL> self . iptables . apply ( ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . empty_chain ( '<STR_LIT>' ) <EOL> self . iptables . apply ( ) <EOL> tools . verify_mock_calls ( self . execute , expected_calls_and_values ) <EOL> def test_add_and_remove_chain_custom_binary_name ( self ) : <EOL> self . _test_add_and_remove_chain_custom_binary_name_helper ( False ) <EOL> def test_add_and_remove_chain_custom_binary_name_with_ipv6 ( self ) : <EOL> self . _test_add_and_remove_chain_custom_binary_name_helper ( True ) <EOL> def _test_empty_chain_custom_binary_name_helper ( self , use_ipv6 ) : <EOL> bn = ( "<STR_LIT>" * <NUM_LIT:5> ) [ : <NUM_LIT:16> ] <EOL> self . iptables = iptables_manager . IptablesManager ( <EOL> binary_name = bn , <EOL> use_ipv6 = use_ipv6 ) <EOL> self . execute = mock . patch . object ( self . iptables , "<STR_LIT>" ) . start ( ) <EOL> iptables_args = { '<STR_LIT>' : bn } <EOL> filter_dump = FILTER_TEMPLATE % iptables_args <EOL> filter_rules = ( '<STR_LIT>' <EOL> % iptables_args ) <EOL> iptables_args [ '<STR_LIT>' ] = filter_rules <EOL> filter_dump_mod = FILTER_WITH_RULES_TEMPLATE % iptables_args <EOL> nat_dump = NAT_TEMPLATE % iptables_args <EOL> raw_dump = _generate_raw_dump ( iptables_args ) <EOL> mangle_dump = _generate_mangle_dump ( iptables_args ) <EOL> expected_calls_and_values = [ <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( filter_dump_mod + mangle_dump + <EOL> nat_dump + raw_dump ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( filter_dump + mangle_dump + <EOL> nat_dump + raw_dump ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ] <EOL> if use_ipv6 : <EOL> mangle_dump_v6 = _generate_mangle_dump_v6 ( iptables_args ) <EOL> self . _extend_with_ip6tables_filter ( <EOL> expected_calls_and_values , <EOL> filter_dump + mangle_dump_v6 + raw_dump ) <EOL> tools . setup_mock_calls ( self . execute , expected_calls_and_values ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_chain ( '<STR_LIT>' ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_rule ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . iptables . apply ( ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_chain ( '<STR_LIT>' ) <EOL> self . iptables . apply ( ) <EOL> tools . verify_mock_calls ( self . execute , expected_calls_and_values ) <EOL> def test_empty_chain_custom_binary_name ( self ) : <EOL> self . _test_empty_chain_custom_binary_name_helper ( False ) <EOL> def test_empty_chain_custom_binary_name_with_ipv6 ( self ) : <EOL> self . _test_empty_chain_custom_binary_name_helper ( True ) <EOL> def _test_add_and_remove_chain_helper ( self , use_ipv6 ) : <EOL> self . iptables = iptables_manager . IptablesManager ( <EOL> use_ipv6 = use_ipv6 ) <EOL> self . execute = mock . patch . object ( self . iptables , "<STR_LIT>" ) . start ( ) <EOL> filter_dump_mod = FILTER_WITH_RULES_TEMPLATE % IPTABLES_ARG <EOL> expected_calls_and_values = [ <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( filter_dump_mod + MANGLE_DUMP + <EOL> NAT_DUMP + RAW_DUMP ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( FILTER_DUMP + MANGLE_DUMP + NAT_DUMP + <EOL> RAW_DUMP ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ] <EOL> if use_ipv6 : <EOL> self . _extend_with_ip6tables_filter ( <EOL> expected_calls_and_values , <EOL> FILTER_DUMP + MANGLE_DUMP_V6 + RAW_DUMP ) <EOL> tools . setup_mock_calls ( self . execute , expected_calls_and_values ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_chain ( '<STR_LIT>' ) <EOL> self . iptables . apply ( ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_chain ( '<STR_LIT>' ) <EOL> self . iptables . apply ( ) <EOL> tools . verify_mock_calls ( self . execute , expected_calls_and_values ) <EOL> def test_add_and_remove_chain ( self ) : <EOL> self . _test_add_and_remove_chain_helper ( False ) <EOL> def test_add_and_remove_chain_with_ipv6 ( self ) : <EOL> self . _test_add_and_remove_chain_helper ( True ) <EOL> def _test_add_filter_rule_helper ( self , use_ipv6 ) : <EOL> self . iptables = iptables_manager . IptablesManager ( <EOL> use_ipv6 = use_ipv6 ) <EOL> self . execute = mock . patch . object ( self . iptables , "<STR_LIT>" ) . start ( ) <EOL> iptables_args = { } <EOL> iptables_args . update ( IPTABLES_ARG ) <EOL> filter_rules = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % iptables_args ) <EOL> iptables_args [ '<STR_LIT>' ] = filter_rules <EOL> filter_dump_mod = FILTER_WITH_RULES_TEMPLATE % iptables_args <EOL> raw_dump = RAW_DUMP % IPTABLES_ARG <EOL> expected_calls_and_values = [ <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( filter_dump_mod + MANGLE_DUMP + <EOL> NAT_DUMP + RAW_DUMP ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( FILTER_DUMP + MANGLE_DUMP + NAT_DUMP + <EOL> RAW_DUMP ) , <EOL> run_as_root = True <EOL> ) , <EOL> None ) , <EOL> ] <EOL> if use_ipv6 : <EOL> self . _extend_with_ip6tables_filter ( <EOL> expected_calls_and_values , <EOL> FILTER_DUMP + MANGLE_DUMP_V6 + raw_dump ) <EOL> tools . setup_mock_calls ( self . execute , expected_calls_and_values ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_chain ( '<STR_LIT>' ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_rule ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_rule ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % IPTABLES_ARG ) <EOL> self . iptables . apply ( ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_rule ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_rule ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % IPTABLES_ARG ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_chain ( '<STR_LIT>' ) <EOL> self . iptables . apply ( ) <EOL> tools . verify_mock_calls ( self . execute , expected_calls_and_values ) <EOL> def test_add_filter_rule ( self ) : <EOL> self . _test_add_filter_rule_helper ( False ) <EOL> def test_add_filter_rule_with_ipv6 ( self ) : <EOL> self . _test_add_filter_rule_helper ( True ) <EOL> def _test_rule_with_wrap_target_helper ( self , use_ipv6 ) : <EOL> self . iptables = iptables_manager . IptablesManager ( <EOL> use_ipv6 = use_ipv6 ) <EOL> self . execute = mock . patch . object ( self . iptables , "<STR_LIT>" ) . start ( ) <EOL> name = '<STR_LIT>' * <NUM_LIT:5> <EOL> wrap = "<STR_LIT>" % ( iptables_manager . binary_name , <EOL> iptables_manager . get_chain_name ( name ) ) <EOL> iptables_args = { '<STR_LIT>' : iptables_manager . binary_name , <EOL> '<STR_LIT>' : wrap } <EOL> filter_dump_mod = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % iptables_args ) <EOL> raw_dump = RAW_DUMP % IPTABLES_ARG <EOL> expected_calls_and_values = [ <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( filter_dump_mod + MANGLE_DUMP + <EOL> NAT_DUMP + RAW_DUMP ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( FILTER_DUMP + MANGLE_DUMP + <EOL> NAT_DUMP + RAW_DUMP ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ] <EOL> if use_ipv6 : <EOL> self . _extend_with_ip6tables_filter ( <EOL> expected_calls_and_values , <EOL> FILTER_DUMP + MANGLE_DUMP_V6 + raw_dump ) <EOL> tools . setup_mock_calls ( self . execute , expected_calls_and_values ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_chain ( name ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_rule ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % name ) <EOL> self . iptables . apply ( ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_rule ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % name ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_chain ( name ) <EOL> self . iptables . apply ( ) <EOL> tools . verify_mock_calls ( self . execute , expected_calls_and_values ) <EOL> def test_rule_with_wrap_target ( self ) : <EOL> self . _test_rule_with_wrap_target_helper ( False ) <EOL> def test_rule_with_wrap_target_with_ipv6 ( self ) : <EOL> self . _test_rule_with_wrap_target_helper ( True ) <EOL> def _test_add_mangle_rule_helper ( self , use_ipv6 ) : <EOL> self . iptables = iptables_manager . IptablesManager ( <EOL> use_ipv6 = use_ipv6 ) <EOL> self . execute = mock . patch . object ( self . iptables , "<STR_LIT>" ) . start ( ) <EOL> mangle_dump_mod = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % IPTABLES_ARG ) <EOL> expected_calls_and_values = [ <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( FILTER_DUMP + mangle_dump_mod + <EOL> NAT_DUMP + RAW_DUMP ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( FILTER_DUMP + MANGLE_DUMP + <EOL> NAT_DUMP + RAW_DUMP ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ] <EOL> if use_ipv6 : <EOL> self . _extend_with_ip6tables_filter ( <EOL> expected_calls_and_values , <EOL> FILTER_DUMP + MANGLE_DUMP_V6 + RAW_DUMP ) <EOL> tools . setup_mock_calls ( self . execute , expected_calls_and_values ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_chain ( '<STR_LIT>' ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_rule ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % constants . ROUTER_MARK_MASK ) <EOL> self . iptables . apply ( ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_rule ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % constants . ROUTER_MARK_MASK ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_chain ( '<STR_LIT>' ) <EOL> self . iptables . apply ( ) <EOL> tools . verify_mock_calls ( self . execute , expected_calls_and_values ) <EOL> def test_add_mangle_rule ( self ) : <EOL> self . _test_add_mangle_rule_helper ( False ) <EOL> def test_add_mangle_rule_with_ipv6 ( self ) : <EOL> self . _test_add_mangle_rule_helper ( True ) <EOL> def _test_add_nat_rule_helper ( self , use_ipv6 ) : <EOL> self . iptables = iptables_manager . IptablesManager ( <EOL> use_ipv6 = use_ipv6 ) <EOL> self . execute = mock . patch . object ( self . iptables , "<STR_LIT>" ) . start ( ) <EOL> nat_dump = NAT_TEMPLATE % IPTABLES_ARG <EOL> nat_dump_mod = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % IPTABLES_ARG ) <EOL> raw_dump = RAW_DUMP % IPTABLES_ARG <EOL> expected_calls_and_values = [ <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( FILTER_DUMP + MANGLE_DUMP + <EOL> nat_dump_mod + RAW_DUMP ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( FILTER_DUMP + MANGLE_DUMP + nat_dump + <EOL> RAW_DUMP ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ] <EOL> if use_ipv6 : <EOL> self . _extend_with_ip6tables_filter ( <EOL> expected_calls_and_values , <EOL> FILTER_DUMP + MANGLE_DUMP_V6 + raw_dump ) <EOL> tools . setup_mock_calls ( self . execute , expected_calls_and_values ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_chain ( '<STR_LIT>' ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_rule ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % IPTABLES_ARG ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_rule ( '<STR_LIT>' , <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> self . iptables . apply ( ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_rule ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_rule ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % IPTABLES_ARG ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_chain ( '<STR_LIT>' ) <EOL> self . iptables . apply ( ) <EOL> tools . verify_mock_calls ( self . execute , expected_calls_and_values ) <EOL> def test_add_nat_rule ( self ) : <EOL> self . _test_add_nat_rule_helper ( False ) <EOL> def test_add_nat_rule_with_ipv6 ( self ) : <EOL> self . _test_add_nat_rule_helper ( True ) <EOL> def _test_add_raw_rule_helper ( self , use_ipv6 ) : <EOL> self . iptables = iptables_manager . IptablesManager ( <EOL> use_ipv6 = use_ipv6 ) <EOL> self . execute = mock . patch . object ( self . iptables , "<STR_LIT>" ) . start ( ) <EOL> raw_dump_mod = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % IPTABLES_ARG ) <EOL> expected_calls_and_values = [ <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( FILTER_DUMP + MANGLE_DUMP + NAT_DUMP + <EOL> raw_dump_mod ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ( mock . call ( [ '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> process_input = ( FILTER_DUMP + MANGLE_DUMP + NAT_DUMP + <EOL> RAW_DUMP ) , <EOL> run_as_root = True ) , <EOL> None ) , <EOL> ] <EOL> if use_ipv6 : <EOL> self . _extend_with_ip6tables_filter ( <EOL> expected_calls_and_values , <EOL> FILTER_DUMP + MANGLE_DUMP_V6 + RAW_DUMP ) <EOL> tools . setup_mock_calls ( self . execute , expected_calls_and_values ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_chain ( '<STR_LIT>' ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . add_rule ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . iptables . apply ( ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_rule ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_chain ( '<STR_LIT>' ) <EOL> self . iptables . apply ( ) <EOL> tools . verify_mock_calls ( self . execute , expected_calls_and_values ) <EOL> def test_add_raw_rule ( self ) : <EOL> self . _test_add_raw_rule_helper ( False ) <EOL> def test_add_raw_rule_with_ipv6 ( self ) : <EOL> self . _test_add_raw_rule_helper ( True ) <EOL> def test_add_rule_to_a_nonexistent_chain ( self ) : <EOL> self . assertRaises ( LookupError , self . iptables . ipv4 [ '<STR_LIT>' ] . add_rule , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_remove_nonexistent_chain ( self ) : <EOL> with mock . patch . object ( iptables_manager , "<STR_LIT>" ) as log : <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_chain ( '<STR_LIT>' ) <EOL> log . debug . assert_called_once_with ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_remove_nonexistent_rule ( self ) : <EOL> with mock . patch . object ( iptables_manager , "<STR_LIT>" ) as log : <EOL> self . iptables . ipv4 [ '<STR_LIT>' ] . remove_rule ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> log . warning . assert_called_once_with ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : True , '<STR_LIT>' : False , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_iptables_failure_with_no_failing_line_number ( self ) : <EOL> with mock . patch . object ( iptables_manager , "<STR_LIT>" ) as log : <EOL> def iptables_restore_failer ( * args , ** kwargs ) : <EOL> if '<STR_LIT>' in args [ <NUM_LIT:0> ] : <EOL> self . input_lines = kwargs [ '<STR_LIT>' ] . split ( '<STR_LIT:\n>' ) <EOL> raise RuntimeError ( ) <EOL> return FILTER_DUMP <EOL> self . execute . side_effect = iptables_restore_failer <EOL> self . assertRaises ( RuntimeError , <EOL> self . iptables . _apply_synchronized ) <EOL> logged = [ '<STR_LIT>' % ( n , l ) <EOL> for n , l in enumerate ( self . input_lines , <NUM_LIT:1> ) ] <EOL> log . error . assert_called_once_with ( _ ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> '<STR_LIT:\n>' . join ( logged ) <EOL> ) <EOL> def test_iptables_failure_on_specific_line ( self ) : <EOL> with mock . patch . object ( iptables_manager , "<STR_LIT>" ) as log : <EOL> def iptables_restore_failer ( * args , ** kwargs ) : <EOL> if '<STR_LIT>' in args [ <NUM_LIT:0> ] : <EOL> self . input_lines = kwargs [ '<STR_LIT>' ] . split ( '<STR_LIT:\n>' ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise RuntimeError ( msg ) <EOL> return FILTER_DUMP <EOL> self . execute . side_effect = iptables_restore_failer <EOL> self . assertRaises ( RuntimeError , <EOL> self . iptables . _apply_synchronized ) <EOL> ctx = iptables_manager . IPTABLES_ERROR_LINES_OF_CONTEXT <EOL> log_start = max ( <NUM_LIT:0> , <NUM_LIT:11> - ctx ) <EOL> log_end = <NUM_LIT:11> + ctx <EOL> logged = [ '<STR_LIT>' % ( n , l ) <EOL> for n , l in enumerate ( self . input_lines [ log_start : log_end ] , <EOL> log_start + <NUM_LIT:1> ) ] <EOL> log . error . assert_called_once_with ( _ ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> '<STR_LIT:\n>' . join ( logged ) <EOL> ) <EOL> def test_get_traffic_counters_chain_notexists ( self ) : <EOL> with mock . patch . object ( iptables_manager , "<STR_LIT>" ) as log : <EOL> acc = self . iptables . get_traffic_counters ( '<STR_LIT>' ) <EOL> self . assertIsNone ( acc ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . execute . call_count ) <EOL> log . warning . assert_called_once_with ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _test_get_traffic_counters_helper ( self , use_ipv6 ) : <EOL> self . iptables = iptables_manager . IptablesManager ( <EOL> use_ipv6 = use_ipv6 ) <EOL> self . execute = mock . patch . object ( self . iptables , "<STR_LIT>" ) . start ( ) <EOL> exp_packets = <NUM_LIT> <EOL> exp_bytes = <NUM_LIT> <EOL> expected_calls_and_values = [ <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> TRAFFIC_COUNTERS_DUMP ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> if use_ipv6 : <EOL> expected_calls_and_values . append ( <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , run_as_root = True ) , <EOL> '<STR_LIT>' ) ) <EOL> expected_calls_and_values . append ( <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> TRAFFIC_COUNTERS_DUMP ) ) <EOL> expected_calls_and_values . append ( <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , run_as_root = True ) , <EOL> '<STR_LIT>' ) ) <EOL> exp_packets *= <NUM_LIT:2> <EOL> exp_bytes *= <NUM_LIT:2> <EOL> tools . setup_mock_calls ( self . execute , expected_calls_and_values ) <EOL> acc = self . iptables . get_traffic_counters ( '<STR_LIT>' ) <EOL> self . assertEqual ( acc [ '<STR_LIT>' ] , exp_packets ) <EOL> self . assertEqual ( acc [ '<STR_LIT>' ] , exp_bytes ) <EOL> tools . verify_mock_calls ( self . execute , expected_calls_and_values , <EOL> any_order = True ) <EOL> def test_get_traffic_counters ( self ) : <EOL> self . _test_get_traffic_counters_helper ( False ) <EOL> def test_get_traffic_counters_with_ipv6 ( self ) : <EOL> self . _test_get_traffic_counters_helper ( True ) <EOL> def _test_get_traffic_counters_with_zero_helper ( self , use_ipv6 ) : <EOL> self . iptables = iptables_manager . IptablesManager ( <EOL> use_ipv6 = use_ipv6 ) <EOL> self . execute = mock . patch . object ( self . iptables , "<STR_LIT>" ) . start ( ) <EOL> exp_packets = <NUM_LIT> <EOL> exp_bytes = <NUM_LIT> <EOL> expected_calls_and_values = [ <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> TRAFFIC_COUNTERS_DUMP ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) , <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> '<STR_LIT>' ) <EOL> ] <EOL> if use_ipv6 : <EOL> expected_calls_and_values . append ( <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , run_as_root = True ) , <EOL> '<STR_LIT>' ) ) <EOL> expected_calls_and_values . append ( <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> run_as_root = True ) , <EOL> TRAFFIC_COUNTERS_DUMP ) ) <EOL> expected_calls_and_values . append ( <EOL> ( mock . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , run_as_root = True ) , <EOL> '<STR_LIT>' ) ) <EOL> exp_packets *= <NUM_LIT:2> <EOL> exp_bytes *= <NUM_LIT:2> <EOL> tools . setup_mock_calls ( self . execute , expected_calls_and_values ) <EOL> acc = self . iptables . get_traffic_counters ( '<STR_LIT>' , zero = True ) <EOL> self . assertEqual ( acc [ '<STR_LIT>' ] , exp_packets ) <EOL> self . assertEqual ( acc [ '<STR_LIT>' ] , exp_bytes ) <EOL> tools . verify_mock_calls ( self . execute , expected_calls_and_values , <EOL> any_order = True ) <EOL> def test_get_traffic_counters_with_zero ( self ) : <EOL> self . _test_get_traffic_counters_with_zero_helper ( False ) <EOL> def test_get_traffic_counters_with_zero_with_ipv6 ( self ) : <EOL> self . _test_get_traffic_counters_with_zero_helper ( True ) <EOL> class IptablesManagerStateLessTestCase ( base . BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( IptablesManagerStateLessTestCase , self ) . setUp ( ) <EOL> cfg . CONF . set_override ( '<STR_LIT>' , False , '<STR_LIT>' ) <EOL> self . iptables = ( iptables_manager . IptablesManager ( state_less = True ) ) <EOL> def test_nat_not_found ( self ) : <EOL> self . assertNotIn ( '<STR_LIT>' , self . iptables . ipv4 ) <EOL> def test_mangle_not_found ( self ) : <EOL> self . assertNotIn ( '<STR_LIT>' , self . iptables . ipv4 ) </s>
