Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_cleanup_action_body_and_cleanup_has_exception(self):
invoke_order = [""]
def cleanup_func():
invoke_order[0] += "cleanup"
raise Exception("test")
class CustomException(Exception):
pass
with self.assertRaises(CustomException):
with cb_helpers.cleanup_action(lambda: cleanup_func()):
invoke_order[0] += "body_"
raise CustomException()
self.assertEqual(invoke_order[0], "body_cleanup")
def test_upload_download_bucket_content(self):
name = "cbtestbucketobjs-{0}".format(helpers.get_uuid())
test_bucket = self.provider.storage.buckets.create(name)
with cb_helpers.cleanup_action(lambda: test_bucket.delete()):
obj_name = "hello_upload_download.txt"
obj = test_bucket.objects.create(obj_name)
with cb_helpers.cleanup_action(lambda: obj.delete()):
content = b"Hello World. Here's some content."
# TODO: Upload and download methods accept different parameter
# types. Need to make this consistent - possibly provider
# multiple methods like upload_from_file, from_stream etc.
obj.upload(content)
target_stream = BytesIO()
obj.save_content(target_stream)
self.assertEqual(target_stream.getvalue(), content)
target_stream2 = BytesIO()
for data in obj.iter_content():
target_stream2.write(data)
self.assertEqual(target_stream2.getvalue(), content)
temp_dir = tempfile.gettempdir()
file_name = '6GigTest.tmp'
six_gig_file = os.path.join(temp_dir, file_name)
with open(six_gig_file, "wb") as out:
out.truncate(6 * 1024 * 1024 * 1024) # 6 Gig...
with cb_helpers.cleanup_action(lambda: os.remove(six_gig_file)):
download_file = "{0}/cbtestfile-{1}".format(temp_dir, file_name)
bucket_name = "cbtestbucketlargeobjs-{0}".format(
helpers.get_uuid())
test_bucket = self.provider.storage.buckets.create(bucket_name)
with cb_helpers.cleanup_action(lambda: test_bucket.delete()):
test_obj = test_bucket.objects.create(file_name)
with cb_helpers.cleanup_action(lambda: test_obj.delete()):
file_uploaded = test_obj.upload_from_file(six_gig_file)
self.assertTrue(file_uploaded, "Could not upload object?")
with cb_helpers.cleanup_action(
lambda: os.remove(download_file)):
with open(download_file, 'wb') as f:
test_obj.save_content(f)
self.assertTrue(
filecmp.cmp(six_gig_file, download_file),
"Uploaded file != downloaded")
def test_cleanup_action_body_has_exception(self):
invoke_order = [""]
def cleanup_func():
invoke_order[0] += "cleanup"
class CustomException(Exception):
pass
with self.assertRaises(CustomException):
with cb_helpers.cleanup_action(lambda: cleanup_func()):
invoke_order[0] += "body_"
raise CustomException()
self.assertEqual(invoke_order[0], "body_cleanup")
test_vol = self.provider.storage.volumes.create(
label, 1,
helpers.get_provider_test_data(self.provider,
"placement"))
with cb_helpers.cleanup_action(lambda: test_vol.delete()):
test_vol.wait_till_ready()
test_snap = test_vol.create_snapshot(label=label,
description=label)
def cleanup_snap(snap):
if snap:
snap.delete()
snap.wait_for([SnapshotState.UNKNOWN],
terminal_states=[SnapshotState.ERROR])
with cb_helpers.cleanup_action(lambda: cleanup_snap(test_snap)):
test_snap.wait_till_ready()
lc = self.provider.compute.instances.create_launch_config()
# Add a new blank volume
lc.add_volume_device(size=1, delete_on_terminate=True)
# Attach an existing volume
lc.add_volume_device(size=1, source=test_vol,
delete_on_terminate=True)
# Add a new volume based on a snapshot
lc.add_volume_device(size=1, source=test_snap,
delete_on_terminate=True)
# Override root volume size
def test_floating_ip_properties(self):
# Check floating IP address
gw = helpers.get_test_gateway(
self.provider)
fip = gw.floating_ips.create()
with cb_helpers.cleanup_action(
lambda: helpers.cleanup_gateway(gw)):
with cb_helpers.cleanup_action(lambda: fip.delete()):
fipl = list(gw.floating_ips)
self.assertIn(fip, fipl)
# 2016-08: address filtering not implemented in moto
# empty_ipl = self.provider.network.floating_ips('dummy-net')
# self.assertFalse(
# empty_ipl,
# "Bogus network should not have any floating IPs: {0}"
# .format(empty_ipl))
self.assertFalse(
fip.private_ip,
"Floating IP should not have a private IP value ({0})."
.format(fip.private_ip))
self.assertFalse(
fip.in_use,
def test_crud_vm_firewall_rules(self):
label = 'cb-crudfw-rules-{0}'.format(helpers.get_uuid())
subnet = helpers.get_or_create_default_subnet(self.provider)
net = subnet.network
fw = None
with cb_helpers.cleanup_action(lambda: fw.delete()):
fw = self.provider.security.vm_firewalls.create(
label=label, description=label, network=net.id)
def create_fw_rule(label):
return fw.rules.create(
direction=TrafficDirection.INBOUND, protocol='tcp',
from_port=1111, to_port=1111, cidr='0.0.0.0/0')
def cleanup_fw_rule(rule):
if rule:
rule.delete()
sit.check_crud(self, fw.rules, VMFirewallRule, "cb-crudfwrule",
create_fw_rule, cleanup_fw_rule,
skip_name_check=True)