|
|
@@ -1,15 +1,17 @@
|
|
|
import ipaddress
|
|
|
import uuid
|
|
|
|
|
|
-import six
|
|
|
+from test import helpers
|
|
|
+from test.helpers import ProviderTestBase
|
|
|
|
|
|
-from cloudbridge.cloud.interfaces import InvalidConfigurationException
|
|
|
from cloudbridge.cloud.interfaces import InstanceState
|
|
|
-from cloudbridge.cloud.interfaces.resources import InstanceType
|
|
|
+from cloudbridge.cloud.interfaces import InvalidConfigurationException
|
|
|
+from cloudbridge.cloud.interfaces import TestMockHelperMixin
|
|
|
from cloudbridge.cloud.interfaces.exceptions import WaitStateException
|
|
|
+from cloudbridge.cloud.interfaces.resources import InstanceType
|
|
|
+# from cloudbridge.cloud.interfaces.resources import SnapshotState
|
|
|
|
|
|
-from test.helpers import ProviderTestBase
|
|
|
-import test.helpers as helpers
|
|
|
+import six
|
|
|
|
|
|
|
|
|
class CloudComputeServiceTestCase(ProviderTestBase):
|
|
|
@@ -19,11 +21,16 @@ class CloudComputeServiceTestCase(ProviderTestBase):
|
|
|
name = "CBInstCrud-{0}-{1}".format(
|
|
|
self.provider.name,
|
|
|
uuid.uuid4())
|
|
|
- net, subnet = helpers.create_test_network(self.provider, name)
|
|
|
- inst = helpers.get_test_instance(self.provider, name, subnet=subnet)
|
|
|
-
|
|
|
+ # Declare these variables and late binding will allow
|
|
|
+ # the cleanup method access to the most current values
|
|
|
+ inst = None
|
|
|
+ net = None
|
|
|
with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
|
|
|
inst, net)):
|
|
|
+ net, subnet = helpers.create_test_network(self.provider, name)
|
|
|
+ inst = helpers.get_test_instance(self.provider, name,
|
|
|
+ subnet=subnet)
|
|
|
+
|
|
|
all_instances = self.provider.compute.instances.list()
|
|
|
|
|
|
list_instances = [i for i in all_instances if i.name == name]
|
|
|
@@ -93,17 +100,24 @@ class CloudComputeServiceTestCase(ProviderTestBase):
|
|
|
name = "CBInstProps-{0}-{1}".format(
|
|
|
self.provider.name,
|
|
|
uuid.uuid4())
|
|
|
- net, subnet = helpers.create_test_network(self.provider, name)
|
|
|
- kp = self.provider.security.key_pairs.create(name=name)
|
|
|
- sg = self.provider.security.security_groups.create(
|
|
|
- name=name, description=name, network_id=net.id)
|
|
|
- test_instance = helpers.get_test_instance(self.provider,
|
|
|
- name, key_pair=kp,
|
|
|
- security_groups=[sg],
|
|
|
- subnet=subnet)
|
|
|
|
|
|
+ # Declare these variables and late binding will allow
|
|
|
+ # the cleanup method access to the most current values
|
|
|
+ test_instance = None
|
|
|
+ net = None
|
|
|
+ sg = None
|
|
|
+ kp = None
|
|
|
with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
|
|
|
test_instance, net, sg, kp)):
|
|
|
+ net, subnet = helpers.create_test_network(self.provider, name)
|
|
|
+ kp = self.provider.security.key_pairs.create(name=name)
|
|
|
+ sg = self.provider.security.security_groups.create(
|
|
|
+ name=name, description=name, network_id=net.id)
|
|
|
+ test_instance = helpers.get_test_instance(self.provider,
|
|
|
+ name, key_pair=kp,
|
|
|
+ security_groups=[sg],
|
|
|
+ subnet=subnet)
|
|
|
+
|
|
|
self.assertTrue(
|
|
|
test_instance.id in repr(test_instance),
|
|
|
"repr(obj) should contain the object id so that the object"
|
|
|
@@ -196,12 +210,12 @@ class CloudComputeServiceTestCase(ProviderTestBase):
|
|
|
# Override root volume size
|
|
|
image_id = helpers.get_provider_test_data(self.provider, "image")
|
|
|
img = self.provider.compute.images.get(image_id)
|
|
|
+ # The size should be greater then the ami size
|
|
|
+ # and therefore, img.min_disk is used.
|
|
|
lc.add_volume_device(
|
|
|
is_root=True,
|
|
|
source=img,
|
|
|
- # TODO: This should be greater than the ami size or tests will fail
|
|
|
- # on actual infrastructure. Needs an image.size method
|
|
|
- size=2,
|
|
|
+ size=img.min_disk if img and img.min_disk else 2,
|
|
|
delete_on_terminate=True)
|
|
|
|
|
|
# Attempting to add more than one root volume should raise an
|
|
|
@@ -237,81 +251,156 @@ class CloudComputeServiceTestCase(ProviderTestBase):
|
|
|
self.provider.name,
|
|
|
uuid.uuid4())
|
|
|
|
|
|
-# test_vol = self.provider.block_store.volumes.create(
|
|
|
-# name,
|
|
|
-# 1,
|
|
|
-# helpers.get_provider_test_data(self.provider, "placement"))
|
|
|
-# with helpers.cleanup_action(lambda: test_vol.delete()):
|
|
|
-# test_vol.wait_till_ready()
|
|
|
-# test_snap = test_vol.create_snapshot(name=name,
|
|
|
-# description=name)
|
|
|
-#
|
|
|
-# def cleanup_snap(snap):
|
|
|
-# snap.delete()
|
|
|
-# snap.wait_for(
|
|
|
-# [SnapshotState.UNKNOWN],
|
|
|
-# terminal_states=[SnapshotState.ERROR])
|
|
|
+ # Comment out BDM tests because OpenStack is not stable enough yet
|
|
|
+ if True:
|
|
|
+ if True:
|
|
|
+
|
|
|
+ # test_vol = self.provider.block_store.volumes.create(
|
|
|
+ # name,
|
|
|
+ # 1,
|
|
|
+ # helpers.get_provider_test_data(self.provider,
|
|
|
+ # "placement"))
|
|
|
+ # with helpers.cleanup_action(lambda: test_vol.delete()):
|
|
|
+ # test_vol.wait_till_ready()
|
|
|
+ # test_snap = test_vol.create_snapshot(name=name,
|
|
|
+ # description=name)
|
|
|
+ #
|
|
|
+ # def cleanup_snap(snap):
|
|
|
+ # snap.delete()
|
|
|
+ # snap.wait_for(
|
|
|
+ # [SnapshotState.UNKNOWN],
|
|
|
+ # terminal_states=[SnapshotState.ERROR])
|
|
|
+ #
|
|
|
+ # with 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)
|
|
|
#
|
|
|
-# with 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
|
|
|
+# # 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
|
|
|
+#
|
|
|
+# # 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
|
|
|
- image_id = helpers.get_provider_test_data(
|
|
|
- self.provider,
|
|
|
- "image")
|
|
|
- img = self.provider.compute.images.get(image_id)
|
|
|
- lc.add_volume_device(
|
|
|
- is_root=True,
|
|
|
- source=img,
|
|
|
- # TODO: This should be greater than the ami size or tests
|
|
|
- # will fail on actual infrastructure. Needs an image.size
|
|
|
- # method
|
|
|
- size=8,
|
|
|
- delete_on_terminate=True)
|
|
|
+ # Override root volume size
|
|
|
+ image_id = helpers.get_provider_test_data(
|
|
|
+ self.provider,
|
|
|
+ "image")
|
|
|
+ img = self.provider.compute.images.get(image_id)
|
|
|
+ # The size should be greater then the ami size
|
|
|
+ # and therefore, img.min_disk is used.
|
|
|
+ lc.add_volume_device(
|
|
|
+ is_root=True,
|
|
|
+ source=img,
|
|
|
+ size=img.min_disk if img and img.min_disk else 2,
|
|
|
+ delete_on_terminate=True)
|
|
|
+
|
|
|
+ # Add all available ephemeral devices
|
|
|
+ instance_type_name = helpers.get_provider_test_data(
|
|
|
+ self.provider,
|
|
|
+ "instance_type")
|
|
|
+ inst_type = self.provider.compute.instance_types.find(
|
|
|
+ name=instance_type_name)[0]
|
|
|
+ for _ in range(inst_type.num_ephemeral_disks):
|
|
|
+ lc.add_ephemeral_device()
|
|
|
+
|
|
|
+ net, subnet = helpers.create_test_network(self.provider, name)
|
|
|
+
|
|
|
+ with helpers.cleanup_action(lambda:
|
|
|
+ helpers.delete_test_network(net)):
|
|
|
+
|
|
|
+ inst = helpers.create_test_instance(
|
|
|
+ self.provider,
|
|
|
+ name,
|
|
|
+ subnet=subnet,
|
|
|
+ launch_config=lc)
|
|
|
+
|
|
|
+ with helpers.cleanup_action(lambda:
|
|
|
+ helpers.delete_test_instance(
|
|
|
+ inst)):
|
|
|
+ try:
|
|
|
+ inst.wait_till_ready()
|
|
|
+ except WaitStateException as e:
|
|
|
+ self.fail("The block device mapped launch did not "
|
|
|
+ " complete successfully: %s" % e)
|
|
|
+ # TODO: Check instance attachments and make sure they
|
|
|
+ # correspond to requested mappings
|
|
|
|
|
|
- # Add all available ephemeral devices
|
|
|
- instance_type_name = helpers.get_provider_test_data(
|
|
|
- self.provider,
|
|
|
- "instance_type")
|
|
|
- inst_type = self.provider.compute.instance_types.find(
|
|
|
- name=instance_type_name)[0]
|
|
|
- for _ in range(inst_type.num_ephemeral_disks):
|
|
|
- lc.add_ephemeral_device()
|
|
|
+ @helpers.skipIfNoService(['compute.instances', 'network',
|
|
|
+ 'security.security_groups'])
|
|
|
+ def test_instance_methods(self):
|
|
|
+ name = "CBInstProps-{0}-{1}".format(
|
|
|
+ self.provider.name,
|
|
|
+ uuid.uuid4())
|
|
|
|
|
|
- net, subnet = helpers.create_test_network(self.provider, name)
|
|
|
+ # Declare these variables and late binding will allow
|
|
|
+ # the cleanup method access to the most current values
|
|
|
+ test_inst = None
|
|
|
+ net = None
|
|
|
+ sg = None
|
|
|
+ with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
|
|
|
+ test_inst, net, sg)):
|
|
|
+ net, subnet = helpers.create_test_network(self.provider, name)
|
|
|
+ test_inst = helpers.get_test_instance(self.provider, name,
|
|
|
+ subnet=subnet)
|
|
|
+ sg = self.provider.security.security_groups.create(
|
|
|
+ name=name, description=name, network_id=net.id)
|
|
|
|
|
|
- inst = helpers.create_test_instance(
|
|
|
- self.provider,
|
|
|
- name,
|
|
|
- subnet=subnet,
|
|
|
- zone=helpers.get_provider_test_data(self.provider, 'placement'),
|
|
|
- launch_config=lc)
|
|
|
-
|
|
|
- def cleanup(instance, net):
|
|
|
- instance.terminate()
|
|
|
- instance.wait_for(
|
|
|
- [InstanceState.TERMINATED, InstanceState.UNKNOWN],
|
|
|
- terminal_states=[InstanceState.ERROR])
|
|
|
- helpers.delete_test_network(net)
|
|
|
-
|
|
|
- with helpers.cleanup_action(lambda: cleanup(inst, net)):
|
|
|
- try:
|
|
|
- inst.wait_till_ready()
|
|
|
- except WaitStateException as e:
|
|
|
- self.fail("The block device mapped launch did not "
|
|
|
- " complete successfully: %s" % e)
|
|
|
- # TODO: Check instance attachments and make sure they
|
|
|
- # correspond to requested mappings
|
|
|
+ # Check adding a security group to a running instance
|
|
|
+ test_inst.add_security_group(sg)
|
|
|
+ test_inst.refresh()
|
|
|
+ self.assertTrue(
|
|
|
+ sg in test_inst.security_groups, "Expected security group '%s'"
|
|
|
+ " to be among instance security_groups: [%s]" %
|
|
|
+ (sg, test_inst.security_groups))
|
|
|
+
|
|
|
+ # Check removing a security group from a running instance
|
|
|
+ test_inst.remove_security_group(sg)
|
|
|
+ test_inst.refresh()
|
|
|
+ self.assertTrue(
|
|
|
+ sg not in test_inst.security_groups, "Expected security group"
|
|
|
+ " '%s' to be removed from instance security_groups: [%s]" %
|
|
|
+ (sg, test_inst.security_groups))
|
|
|
+
|
|
|
+ # check floating ips
|
|
|
+ router = self.provider.network.create_router(name=name)
|
|
|
+
|
|
|
+ with helpers.cleanup_action(lambda: router.delete()):
|
|
|
+
|
|
|
+ # TODO: Cloud specific code, needs fixing
|
|
|
+ if self.provider.PROVIDER_ID == 'openstack':
|
|
|
+ for n in self.provider.network.list():
|
|
|
+ if n.external:
|
|
|
+ external_net = n
|
|
|
+ break
|
|
|
+ else:
|
|
|
+ external_net = net
|
|
|
+ router.attach_network(external_net.id)
|
|
|
+ router.add_route(subnet.id)
|
|
|
+
|
|
|
+ def cleanup_router():
|
|
|
+ router.remove_route(subnet.id)
|
|
|
+ router.detach_network()
|
|
|
+
|
|
|
+ with helpers.cleanup_action(lambda: cleanup_router()):
|
|
|
+ # check whether adding an elastic ip works
|
|
|
+ fip = self.provider.network.create_floating_ip()
|
|
|
+ with helpers.cleanup_action(lambda: fip.delete()):
|
|
|
+ test_inst.add_floating_ip(fip.public_ip)
|
|
|
+ test_inst.refresh()
|
|
|
+ self.assertIn(fip.public_ip, test_inst.public_ips)
|
|
|
+
|
|
|
+ if isinstance(self.provider, TestMockHelperMixin):
|
|
|
+ # TODO: Moto bug does not refresh removed public ip
|
|
|
+ return
|
|
|
+
|
|
|
+ # check whether removing an elastic ip works
|
|
|
+ test_inst.remove_floating_ip(fip.public_ip)
|
|
|
+ test_inst.refresh()
|
|
|
+ self.assertNotIn(fip.public_ip, test_inst.public_ips)
|