本文整理汇总了Python中testlunr.unit.patch函数的典型用法代码示例。如果您正苦于以下问题:Python patch函数的具体用法?Python patch怎么用?Python patch使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了patch函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_create_from_image_no_scratch_space
def test_create_from_image_no_scratch_space(self):
def mock_lvcreate_v1(cmd, vg, **kwargs):
raise ProcessError('%s %s' % (cmd, vg), '',
'Insufficient free extents', 5)
def glance_conn(conf, tenant_id, glance_urls=None):
data = 'A' * 4096
image = MockImage(image_id, len(data), data)
glance = MockImageGlance(image)
return glance
volume.get_glance_conn = glance_conn
h = volume.VolumeHelper(self.conf)
volume_id = uuid4()
image_id = uuid4()
with patch(volume, 'execute', mock_lvcreate_v1):
self.assertRaises(ServiceUnavailable, h.create, volume_id,
image_id=image_id, lock=self.lock)
def mock_lvcreate_v2(cmd, vg, **kwargs):
raise ProcessError('%s %s' % (cmd, vg), '',
'insufficient free space', 5)
with patch(volume, 'execute', mock_lvcreate_v2):
self.assertRaises(ServiceUnavailable, h.create, volume_id,
image_id=image_id, lock=self.lock)
示例2: test_chain_vhd_ovf
def test_chain_vhd_ovf(self):
image_id = uuid4()
data = 'A' * 4096
image = MockImage(image_id, len(data), data, disk_format='vhd',
container_format='ovf')
glance = MockImageGlance(image)
def mock_getsize(*args, **kwargs):
return 1234567
def mock_vhd_chain(path):
return ['0.vhd', '1.vhd', '2.vhd', '3.vhd']
def scrub_cb():
scrub_cb.called = True
scrub_cb.called = False
with patch(self.helper, 'get_vhd_chain', mock_vhd_chain):
with patch(os.path, 'getsize', mock_getsize):
self.helper.copy_image(self.volume, image.head, glance,
self.tmp_vol, scrub_cb)
with open(self.volume['path'], 'r') as f:
# Fake qemu-img just writes to the volume
stuff = f.read()
self.assertIn('qemu-img', stuff)
self.assertIn(self.volume['path'], stuff)
self.assertEquals(scrub_cb.called, True)
示例3: test_make_api_request_volume_id
def test_make_api_request_volume_id(self):
volume_id = 'v1'
volume_name = 'volume_name'
cinder_host = 'node_cinder_host'
def mock_urlopen(req, data=None):
expected = 'http://localhost:8080/v1.0/admin/volumes/v1'
self.assertEquals(req.get_full_url(), expected)
mock_urlopen.called = True
mock_urlopen.called = False
def mock_lookup_id(id, api_server, cinder_host):
self.assertEquals(id, volume_name)
mock_lookup_id.called = True
return volume_id
mock_lookup_id.called = False
data = {'cinder_host': cinder_host, 'foo': 'bar'}
with patch(utils, 'urlopen', mock_urlopen):
with patch(utils, 'lookup_id', mock_lookup_id):
utils.make_api_request('volumes', volume_name, data=data)
self.assertTrue(mock_urlopen.called)
self.assertTrue(mock_lookup_id.called)
示例4: test_create_fail_ioerror
def test_create_fail_ioerror(self):
h = backup.BackupHelper(self.conf)
def callback():
callback.ran = True
callback.ran = False
def error_callback():
error_callback.ran = True
error_callback.ran = False
snapshot = {
'id': 'bak1',
'timestamp': 1.0,
}
snapshot['path'] = os.path.join(self.scratch, 'bak1')
snapshot['origin'] = 'vol1'
snapshot['size'] = 4 * 1024 * 1024
with open(snapshot['path'], 'w') as f:
f.write('\x00' * snapshot['size'])
backup_id = 'backup1'
def fake_hydrate(junk):
raise BlockReadFailed("cant read!")
with patch(Block, "_hydrate", fake_hydrate):
h.create(snapshot, backup_id,
callback=callback, error_callback=error_callback,
lock=MockResourceLock())
self.assertFalse(callback.ran)
self.assertTrue(error_callback.ran)
stats_path = h._stats_file('vol1')
self.assertFalse(os.path.exists(stats_path))
示例5: test_create_storage_node_failover
def test_create_storage_node_failover(self):
self.node2 = db.Session.add(
db.models.Node('somenode2', 100000000000, volume_type=self.vtype,
port=8081, hostname='127.0.0.1'))
self.node3 = db.Session.add(
db.models.Node('somenode3', 100000000000, volume_type=self.vtype,
port=8082, hostname='127.0.0.1'))
db.Session.commit()
def fail_response(*args, **kwargs):
raise socket.timeout("too slow!")
def success_response(*args, **kwargs):
data = {'status': 'ACTIVE'}
return MockResponse(200, json.dumps(data))
self.responses = [fail_response, fail_response, success_response]
def mock_urlopen(*args, **kwargs):
func = self.responses.pop(0)
return func(*args, **kwargs)
with patch(base, 'urlopen', mock_urlopen):
resp = self.request("/v1.0/account/volumes/test", 'PUT',
{'size': 1, 'volume_type_name': 'vtype'})
self.assertEquals(resp.code, 200)
self.assertEquals(resp.body['status'], 'ACTIVE')
self.assertEqual(self.responses, [])
示例6: test_check_reg_api_server_error_retry_success
def test_check_reg_api_server_error_retry_success(self):
h = base.Helper(self.conf)
name = 'volume-%s' % uuid4()
out = h.volumes.create(name)
def error_validator(req):
error_validator.called = True
body = StringIO(dumps({'reason': 'Internal Error'}))
raise HTTPError(req.get_full_url(), 500, 'Server Error', {}, body)
error_validator.called = False
def success_validator(req):
success_validator.called = True
return 200, {}
success_validator.called = False
validators = [
# listing
lambda *args: (200, []),
error_validator,
success_validator,
]
self.validator_gen = iter(validators)
with patch(base, 'sleep', lambda t: None):
h.check_registration()
self.assert_(error_validator.called)
self.assert_(success_validator.called)
示例7: test_update
def test_update(self):
ip = '123.456.123.456'
initiator = 'something.long.and.ugly.with.dots'
def node_request(*args, **kwargs):
return {
'sessions': [{
'ip': ip,
'initiator': initiator
}]
}
url = "/v1.0/%s/volumes/%s/export" % (self.account.id, self.volume.id)
instance_id = 'someinstanceid'
status = 'attaching'
params = {'instance_id': instance_id, 'status': status}
with patch(ExportController, 'node_request', node_request):
resp = self.request(url, 'POST', params)
self.assertEquals(resp.code // 100, 2)
self.assertEquals(resp.body['instance_id'], instance_id)
self.assertEquals(resp.body['status'], status)
self.assertEquals(resp.body['session_ip'], ip)
self.assertEquals(resp.body['session_initiator'], initiator)
self.db.refresh(self.export)
self.assertEquals(self.export.session_ip, ip)
self.assertEquals(self.export.session_initiator, initiator)
示例8: test_used
def test_used(self):
backup = Backup(self.volume, id='1', status='SAVING', size=1,
last_modified=datetime(2000, 01, 01, 1, 1, 1))
def urlopen(request, **kwargs):
return MockResponse(dumps({
'in-use': True,
'uri': 'DELETE /volumes/ed209cdd-1317-41e8-8474-b0c0f6c3369c/'
'backups/a30a6e5b-2a96-489c-bde1-56f9c615ea1f',
}), 200)
prune = PruneSuspects(self.conf, self.sess)
with patch(suspects, 'urlopen', urlopen):
with patch(suspects, 'log', MockLog()):
prune.locked(backup)
self.assertEquals(suspects.log.count, 1)
示例9: test_no_free_space
def test_no_free_space(self):
def mock_statvfs(path):
stat = os.statvfs(path)
kwargs = {}
for key in [k for k in dir(stat) if k.startswith("f_")]:
if key == "f_bfree":
kwargs[key] = 0
else:
kwargs[key] = getattr(stat, key)
return Struct(**kwargs)
class MockOS(object):
def __getattribute__(self, attr):
if attr == "statvfs":
return mock_statvfs
else:
return getattr(os, attr)
with patch(disk, "os", MockOS()):
with temp_client() as conn:
self.assertRaises(disk.ClientException, conn.head_account)
try:
conn.head_account()
except disk.ClientException, e:
self.assert_("no free space" in str(e).lower())
示例10: test_check_reg_unable_to_contact_api
def test_check_reg_unable_to_contact_api(self):
h = base.Helper(self.conf)
def exploding_validator(req):
raise URLError('connection refused')
self.validator_gen = itertools.cycle([exploding_validator])
with patch(base, 'sleep', lambda t: None):
self.assertRaises(APIError, h.check_registration)
示例11: test_db_goes_away_recovery
def test_db_goes_away_recovery(self):
attempts = [0]
original = getattr(db.Session, 'get_or_create_account')
def raise_exc(account):
if attempts[0] == 0:
attempts[0] += 1
raise OperationalError('', '', Struct(args=[2006]))
return original(account)
with patch(base, 'urlopen', MockUrlopen):
with patch(server, 'sleep', lambda i: True):
with patch(db.Session, 'get_or_create_account', raise_exc):
resp = self.request("/v1.0/account/volumes/thrawn", 'PUT',
{'size': 1,
'volume_type_name': 'vtype'})
self.assertEquals(resp.code, 200)
示例12: test_make_api_request_defaults
def test_make_api_request_defaults(self):
def mock_urlopen(req, data=None):
expected = 'http://localhost:8080/v1.0/admin/nodes'
self.assertEquals(req.get_full_url(), expected)
mock_urlopen.called = True
with patch(utils, 'urlopen', mock_urlopen):
utils.make_api_request('nodes')
self.assert_(mock_urlopen.called)
示例13: test_check_reg_api_server_error
def test_check_reg_api_server_error(self):
h = base.Helper(self.conf)
def error_validator(req):
body = StringIO(dumps({'reason': 'Internal Error'}))
raise HTTPError(req.get_full_url(), 500, 'Server Error', {}, body)
self.validator_gen = itertools.cycle([error_validator])
with patch(base, 'sleep', lambda t: None):
self.assertRaises(APIError, h.check_registration)
示例14: test_status_client_exception
def test_status_client_exception(self):
h = backup.BackupHelper(self.conf)
conn = get_conn(self.conf)
def mock_head_account(*args, **kwargs):
raise conn.ClientException('unable to connect')
conn.head_account = mock_head_account
with patch(backup, 'get_conn', lambda *args: conn):
self.assertRaises(ServiceUnavailable, h.status)
示例15: test_delete_node_error
def test_delete_node_error(self):
def raise_exc(*args, **kwargs):
e = base.NodeError(MockRequest(), URLError("somthing bad"))
e.code = 400
e.status = '400 something bad'
raise e
url = "/v1.0/%s/volumes/%s/export" % (self.account.id, self.volume.id)
with patch(ExportController, 'node_request', raise_exc):
resp = self.request(url, 'DELETE')
self.assertEquals(resp.code, 400)