Ejemplo n.º 1
0
def setUpPack(test):
    first_oid = 1234567
    prefixes = 'foo/bar/', 'foo/baz/', 'my/blobs/'
    blobs = {}
    rdata = ""
    for day in range(1, 10):
        ts = ZODB.TimeStamp.TimeStamp(2013, 10, day, 10, 35, 21)
        if day in (2, 4):
            rdata += ZODB.utils.p64(first_oid+day).encode('hex')+"\n"
            for prefix in prefixes:
                blobs[prefix+".removed%s.blob" % int(ts.timeTime())] = rdata
            rdata = ""

        for oid in range(first_oid, first_oid+10):
            oid = ZODB.utils.p64(oid).encode('hex')
            key = oid + repr(ts).encode('hex') + '.blob'
            if day < 4:
                rdata += key[:-5]+"\n"
            for prefix in prefixes:
                blobs[prefix + key] = key+' data'

    conn = S3Connection(dict(blobs=blobs))
    setupstack.context_manager(
        test, mock.patch("boto.s3.connection.S3Connection",
                         side_effect=lambda : conn))

    ts = ZODB.TimeStamp.TimeStamp(2013, 10, 10, 15, 1, 2).timeTime()
    setupstack.context_manager(test, mock.patch('time.time', return_value=ts))
Ejemplo n.º 2
0
Archivo: testing.py Proyecto: zc/sqs
def setUp(test):
    setupstack.setUpDirectory(test)
    test.globs['sqs_queues'] = queues = Queues()
    setupstack.context_manager(
        test,
        mock.patch("boto.sqs.connect_to_region",
                   side_effect=queues.connect_to_region))
Ejemplo n.º 3
0
def setUpFile(test):
    setupstack.setUpDirectory(test)
    zc.zk.testing.setUp(test)
    test.globs['now'] = 1380541206.52
    def time():
        return test.globs['now']
    setupstack.context_manager(test, mock.patch("time.time", side_effect=time))
Ejemplo n.º 4
0
def setUp(test):
    setupstack.context_manager(
        test, mock.patch('time.time', return_value=1349378051.73))
    setupstack.context_manager(test, mock.patch('time.ctime',
                                                side_effect=ctime))
    zc.zk.testing.setUp(test, '', 'zookeeper:2181')
    zim.zkdeploymentclustermonitor.basemonitor.testing.setUp(test)
Ejemplo n.º 5
0
def setUpPack(test):
    first_oid = 1234567
    prefixes = 'foo/bar/', 'foo/baz/', 'my/blobs/'
    blobs = {}
    rdata = ""
    for day in range(1, 10):
        ts = ZODB.TimeStamp.TimeStamp(2013, 10, day, 10, 35, 21)
        if day in (2, 4):
            rdata += ZODB.utils.p64(first_oid + day).encode('hex') + "\n"
            for prefix in prefixes:
                blobs[prefix + ".removed%s.blob" % int(ts.timeTime())] = rdata
            rdata = ""

        for oid in range(first_oid, first_oid + 10):
            oid = ZODB.utils.p64(oid).encode('hex')
            key = oid + repr(ts).encode('hex') + '.blob'
            if day < 4:
                rdata += key[:-5] + "\n"
            for prefix in prefixes:
                blobs[prefix + key] = key + ' data'

    conn = S3Connection(dict(blobs=blobs))
    setupstack.context_manager(
        test,
        mock.patch("boto.s3.connection.S3Connection",
                   side_effect=lambda: conn))

    ts = ZODB.TimeStamp.TimeStamp(2013, 10, 10, 15, 1, 2).timeTime()
    setupstack.context_manager(test, mock.patch('time.time', return_value=ts))
Ejemplo n.º 6
0
def setUp(test):
    setupstack.context_manager(
        test, mock.patch('time.time', return_value=1349378051.73))
    setupstack.context_manager(
        test, mock.patch('time.ctime', side_effect=ctime))
    zc.zk.testing.setUp(test, '', 'zookeeper:2181')
    zim.zkdeploymentclustermonitor.basemonitor.testing.setUp(test)
Ejemplo n.º 7
0
def setUp(test):
    setupstack.setUpDirectory(test)
    zc.zk.testing.setUp(test, tree='/databases\n  /demo\n')

    # The original server loop spews thread exceptions during shutdowm.
    # This version doesn't.
    from ZEO.zrpc.connection import server_loop as _server_loop
    def server_loop(map):
        try:
            _server_loop(map)
        except Exception:
            if len(map) > 1:
                raise

    setupstack.context_manager(
        test, mock.patch('ZEO.zrpc.connection.server_loop')
        ).side_effect = server_loop

    setupstack.context_manager(
        test, mock.patch('netifaces.interfaces')).return_value = ['iface']

    globs = test.globs
    globs['ifaddress'] = '1.2.3.4'
    setupstack.context_manager(
        test, mock.patch('netifaces.ifaddresses',
                         lambda i: {2: [dict(addr=globs['ifaddress'])]}
                         ))

    del zc.zk.monitor._servers[:]
Ejemplo n.º 8
0
def setUp(test):
    setupstack.setUpDirectory(test)
    setupstack.context_manager(
        test, mock.patch.dict(os.environ, HOME=os.getcwd()))
    setupstack.context_manager(
        test,
        mock.patch('keyring.get_password',
                   side_effect=lambda name, login: name+login+'pw')
        )
Ejemplo n.º 9
0
def setUp(test):
    setupstack.setUpDirectory(test)
    getpwname = setupstack.context_manager(test, mock.patch("pwd.getpwnam"))
    getpwname.return_value = O(pw_gid=5, pw_uid=99, pw_name="zope")
    setupstack.context_manager(test, mock.patch("os.geteuid")).return_value = 0
    setupstack.context_manager(test, mock.patch("grp.getgrall"))
    setupstack.context_manager(test, mock.patch("os.setgroups"))
    setupstack.context_manager(test, mock.patch("os.setuid"))
    setupstack.context_manager(test, mock.patch("os.setgid"))
Ejemplo n.º 10
0
def setUpFile(test):
    setupstack.setUpDirectory(test)
    zc.zk.testing.setUp(test)
    test.globs['now'] = 1380541206.52

    def time():
        return test.globs['now']

    setupstack.context_manager(test, mock.patch("time.time", side_effect=time))
Ejemplo n.º 11
0
def setUp(test):
    setupstack.setUpDirectory(test)
    getpwname = setupstack.context_manager(test, mock.patch('pwd.getpwnam'))
    getpwname.return_value = O(pw_gid=5, pw_uid=99, pw_name='zope')
    setupstack.context_manager(test, mock.patch('os.geteuid')).return_value = 0
    setupstack.context_manager(test, mock.patch('grp.getgrall'))
    setupstack.context_manager(test, mock.patch('os.setgroups'))
    setupstack.context_manager(test, mock.patch('os.setuid'))
    setupstack.context_manager(test, mock.patch('os.setgid'))
Ejemplo n.º 12
0
def setUp(test):
    setupstack.setUpDirectory(test)
    dt = setupstack.context_manager(test, mock.patch("datetime.datetime"))
    dt.now = lambda: Metric.end
    setupstack.context_manager(
        test,
        mock.patch("boto.ec2.cloudwatch.connect_to_region",
                   side_effect=Connection),
    )
Ejemplo n.º 13
0
def setUp(test):
    setupstack.setUpDirectory(test)
    getpwname = setupstack.context_manager(test, mock.patch('pwd.getpwnam'))
    getpwname.return_value = O(pw_gid=5, pw_uid=99, pw_name='zope')
    setupstack.context_manager(test, mock.patch('os.geteuid')).return_value = 0
    setupstack.context_manager(test, mock.patch('grp.getgrall'))
    setupstack.context_manager(test, mock.patch('os.setgroups'))
    setupstack.context_manager(test, mock.patch('os.setuid'))
    setupstack.context_manager(test, mock.patch('os.setgid'))
Ejemplo n.º 14
0
def setUpClient(test):
    setupstack.setUpDirectory(test)
    def print_bytes(bytes):
        print '_check_blob_size(%s)' % bytes
    setupstack.context_manager(
        test,
        mock.patch("ZEO.ClientStorage.ClientStorage._check_blob_size",
                   side_effect = print_bytes),
        )
    zc.zk.testing.setUp(test)
Ejemplo n.º 15
0
def setUpPP(test):
    # Setting up a function makes it easier to inject pdb.
    def faux_json_dumps(o, sort_keys=True, **kw):
        return original_dumps(o, sort_keys=sort_keys, **kw)

    setupstack.context_manager(test, mock.patch('json.dumps', faux_json_dumps))
    test.globs.update(
        pdb=pdb,
        pprint=pprint.pprint,
        pp=pprint.pprint,
    )
Ejemplo n.º 16
0
def setUpClient(test):
    setupstack.setUpDirectory(test)

    def print_bytes(bytes):
        print '_check_blob_size(%s)' % bytes

    setupstack.context_manager(
        test,
        mock.patch("ZEO.ClientStorage.ClientStorage._check_blob_size",
                   side_effect=print_bytes),
    )
    zc.zk.testing.setUp(test)
Ejemplo n.º 17
0
def setUp(test):
    setUpPP(test)
    setupstack.setUpDirectory(test)
    with open(os.path.join(os.path.dirname(__file__), 'filecheck_py')) as src:
        with open('filecheck.py', 'w') as dest:
            dest.write(src.read())

    setupstack.context_manager(
        test, mock.patch('socket.getfqdn', return_value='test.example.com'))

    global meta_db
    meta_db = zc.cimaa.stub.MemoryDB({})
    setupstack.context_manager(test,
                               mock.patch('getpass.getuser', lambda: 'tester'))
Ejemplo n.º 18
0
def setUpLogging(test):
    setUp(test)
    setupstack.context_manager(test, mock.patch('logging.basicConfig'))
    setupstack.context_manager(test, mock.patch('logging.getLogger'))
    setupstack.context_manager(
        test, mock.patch('raven.handlers.logging.SentryHandler'))
    setupstack.context_manager(test, mock.patch('ZConfig.configureLoggers'))
Ejemplo n.º 19
0
def setup(test):

    os.environ['PATH'] = '/bin'

    setupstack.context_manager(
        test, mock.patch(
            'pwd.getpwuid',
            side_effect=lambda uid: Ob(**dict(
                pw_name='testy',
                pw_gecos='Testy Tester',
                ))))

    setupstack.context_manager(test, mock.patch('time.sleep'))

    volumes = FauxVolumes(test)
Ejemplo n.º 20
0
def setUpREADME(test):
    zc.zk.testing.setUp(test)

    @side_effect(setupstack.context_manager(test,
                                            mock.patch('socket.getfqdn')))
    def getfqdn():
        return 'socket.getfqdn'
Ejemplo n.º 21
0
def setUpREADME(test):
    zc.zk.testing.setUp(test)

    @side_effect(
        setupstack.context_manager(test, mock.patch('socket.getfqdn')))
    def getfqdn():
        return 'socket.getfqdn'
Ejemplo n.º 22
0
def setup(test):
    connections = dict(test_region=Connection())

    @side_effect(
        setupstack.context_manager(
            test, mock.patch('boto.ec2.connection.EC2Connection')))
    def connect(region):
        return connections[region]
Ejemplo n.º 23
0
def setUp(test):
    zc.zk.testing.setUp(test, '', 'zookeeper:2181')
    client = FauxDockerClient()
    setupstack.context_manager(
        test, mock.patch("docker.Client", side_effect=lambda: client))
    setupstack.context_manager(
        test, mock.patch("time.sleep", side_effect=client.sleep))
    setupstack.context_manager(
        test,
        mock.patch("socket.getfqdn", side_effect=lambda: 'app42.example.com'))
Ejemplo n.º 24
0
    def __init__(self, test):
        self.init()
        test.globs['volumes'] = self
        setupstack.context_manager(
            test, mock.patch('os.path.exists', side_effect=self.exists))
        setupstack.context_manager(
            test, mock.patch('os.rename', side_effect=self.rename))
        setupstack.context_manager(
            test, mock.patch('zc.awsrecipes.open', create=True,
                             side_effect=self.open))

        def Popen(command, stdout=None, stderr=None, shell=False):
            assert_(shell)
            meth = command.split()[0].rsplit('/', 1)[-1].replace('.', '_')
            meth = getattr(self, meth)
            return FauxPopen(meth, command, stdout, stderr)
        setupstack.context_manager(
            test, mock.patch('subprocess.Popen', side_effect=Popen))
Ejemplo n.º 25
0
def setUp(test):
    setupstack.setUpDirectory(test)
    connect_to_region = setupstack.context_manager(
        test, mock.patch('boto.cloudformation.connect_to_region'))

    regions = ()
    connections = {}
    for name in 'us-f12g', 'us-manassas':
        regions += (Region(name), )
        connections[name] = CloudFormationConnection(regions[-1])

    test.globs['connections'] = connections

    @side_effect(
        setupstack.context_manager(
            test, mock.patch('boto.cloudformation.connect_to_region')))
    def _(region_name):
        print 'connecting to', region_name
        return connections[region_name]

    setupstack.context_manager(
        test,
        mock.patch('boto.cloudformation.regions', side_effect=lambda: regions))

    @side_effect(
        setupstack.context_manager(test,
                                   mock.patch('boto.ec2.connect_to_region')))
    def _(region_name):
        if region_name != 'us-f12g':
            raise AssertionError
        return EC2Connection()

    test.globs['writefile'] = writefile

    setupstack.context_manager(test, mock.patch('logging.basicConfig'))
    setupstack.context_manager(test, mock.patch('time.sleep'))
Ejemplo n.º 26
0
def setUp(test, tree=None, connection_string='zookeeper.example.com:2181'):
    """Set up zookeeper emulation.

    Standard (mock) testing
    -----------------------

    The first argument is a test case object (either doctest or unittest).

    You can optionally pass:

    tree
       An initial ZooKeeper tree expressed as an import string.
       If not passed, an initial tree will be created with examples
       used in the zc.zk doctests.

    connection_string
       The connection string to use for the emulation server. This
       defaults to 'zookeeper.example.com:2181'.

    Testing with a real ZooKeeper Server
    ------------------------------------

    You can test against a real ZooKeeper server, instead of a mock by
    setting the environment variable TEST_ZOOKEEPER_CONNECTION to the
    connection string of a test server.

    The tests will create a top-level node with a random name that
    starts with 'zc.zk.testing.test-roo', and use that as the virtual
    root for your tests.  Although this is the virtual root, of the
    zookeeper tree in your tests, the presense of the node may be
    shown in your tests. In particularm ``zookeeper.create`` returns
    the path created and the string returned is real, not virtual.
    This node is cleaned up by the ``tearDown``.

    A doctest can determine if it's running with a stub ZooKeeper by
    checking whether the value of the ZooKeeper gloval variable is None.
    A regular unit test can check the ZooKeeper test attribute.
    """

    globs = setupstack.globs(test)
    faux_zookeeper = None
    real_zk = testing_with_real_zookeeper()
    if real_zk:
        test_root = '/zc.zk.testing.test-root%s' % random.randint(0, sys.maxsize)
        globs['/zc.zk.testing.test-root'] = test_root
        setup_tree(tree, real_zk, test_root, True)

        orig_init = zookeeper.init

        @side_effect(
            setupstack.context_manager(test, mock.patch('zookeeper.init')))
        def init(addr, watch=None, session_timeout=1000):
            if addr != connection_string:
                return orig_init(addr, watch, session_timeout)
            else:
                return orig_init(real_zk+test_root, watch, session_timeout)

        setupstack.register(
            test, lambda : setattr(zc.zk.ZooKeeper, 'test_sleep', 0))
        zc.zk.ZooKeeper.test_sleep = .01
        time.sleep(float(os.environ.get('TEST_ZOOKEEPER_SLEEP', 0)))

    else:
        if tree:
            faux_zookeeper = ZooKeeper(connection_string, Node())
        else:
            faux_zookeeper = ZooKeeper(
                connection_string,
                Node(
                    fooservice = Node(
                        json.dumps(dict(
                            database = "/databases/foomain",
                            threads = 1,
                            favorite_color= "red",
                            )),
                        providers = Node()
                        ),
                    zookeeper = Node('', quota=Node()),
                    ),
                )
        for name in ZooKeeper.__dict__:
            if name[0] == '_':
                continue
            m = setupstack.context_manager(test, mock.patch('zookeeper.'+name))
            m.side_effect = getattr(faux_zookeeper, name)

        if tree:
            zk = zc.zk.ZooKeeper(connection_string)
            zk.import_tree(tree)
            zk.close()

    globs['wait_until'] = wait_until # BBB
    globs['ZooKeeper'] = faux_zookeeper
    globs.setdefault('assert_', assert_)
Ejemplo n.º 27
0
def setUpWithoutLogging(test):
    setUp(test)
    setupstack.context_manager(test, mock.patch('time.sleep'))
Ejemplo n.º 28
0
def setUp(test, tree=None, connection_string='zookeeper.example.com:2181'):
    """Set up zookeeper emulation.

    Standard (mock) testing
    -----------------------

    The first argument is a test case object (either doctest or unittest).

    You can optionally pass:

    tree
       An initial ZooKeeper tree expressed as an import string.
       If not passed, an initial tree will be created with examples
       used in the zc.zk doctests.

    connection_string
       The connection string to use for the emulation server. This
       defaults to 'zookeeper.example.com:2181'.

    Testing with a real ZooKeeper Server
    ------------------------------------

    You can test against a real ZooKeeper server, instead of a mock by
    setting the environment variable TEST_ZOOKEEPER_CONNECTION to the
    connection string of a test server.

    The tests will create a top-level node with a random name that
    starts with 'zc.zk.testing.test-root', and use that as the virtual
    root for your tests.  Although this is the virtual root, of the
    zookeeper tree in your tests, the presense of the node may be
    shown in your tests. In particular, ``zookeeper.create`` returns
    the path created and the string returned is real, not virtual.
    This node is cleaned up by the ``tearDown``.

    A doctest can determine if it's running with a stub ZooKeeper by
    checking whether the value of the ZooKeeper global variable is None.
    A regular unit test can check the ZooKeeper test attribute.
    """

    globs = setupstack.globs(test)
    faux_zookeeper = None
    real_zk = testing_with_real_zookeeper()
    if real_zk:
        test_root = '/zc.zk.testing.test-root%s' % random.randint(0, sys.maxint)
        globs['/zc.zk.testing.test-root'] = test_root

        @side_effect(
            setupstack.context_manager(
                test, mock.patch('kazoo.client.KazooClient')))
        def client(addr, *a, **k):
            if addr != connection_string:
                return SlowClient(addr, *a, **k)
            else:
                return SlowClient(real_zk+test_root, *a, **k)

    else:
        faux_zookeeper = ZooKeeper(connection_string, Node())
        test_root = '/'
        real_zk = connection_string

        @side_effect(setupstack.context_manager(
            test, mock.patch('kazoo.client.KazooClient')))
        def client(*a, **k):
            return Client(faux_zookeeper, *a, **k)

    setup_tree(tree, real_zk, test_root, True)

    globs['wait_until'] = wait_until # BBB
    globs['ZooKeeper'] = faux_zookeeper
    globs.setdefault('assert_', assert_)
Ejemplo n.º 29
0
def setUpTime(test):
    setUpLogging(test)
    globs = test.globs
    globs['now'] = 1418487287.82
    setupstack.context_manager(
        test, mock.patch('time.time', side_effect=lambda: globs['now']))
Ejemplo n.º 30
0
def setUp(test):
    zc.zk.testing.setUp(test, '', 'zookeeper:2181')
    setupstack.context_manager(test, mock.patch('subprocess.call', faux_call))