|
|
@@ -2,14 +2,10 @@ import functools
|
|
|
import operator
|
|
|
import os
|
|
|
import sys
|
|
|
-import traceback
|
|
|
import unittest
|
|
|
import uuid
|
|
|
-from contextlib import contextmanager
|
|
|
|
|
|
-import six
|
|
|
-
|
|
|
-from cloudbridge.cloud.base.helpers import get_env
|
|
|
+from cloudbridge.cloud.base import helpers as cb_helpers
|
|
|
from cloudbridge.cloud.factory import CloudProviderFactory
|
|
|
from cloudbridge.cloud.interfaces import InstanceState
|
|
|
from cloudbridge.cloud.interfaces import TestMockHelperMixin
|
|
|
@@ -25,41 +21,6 @@ def parse_bool(val):
|
|
|
return False
|
|
|
|
|
|
|
|
|
-@contextmanager
|
|
|
-def cleanup_action(cleanup_func):
|
|
|
- """
|
|
|
- Context manager to carry out a given
|
|
|
- cleanup action after carrying out a set
|
|
|
- of tasks, or when an exception occurs.
|
|
|
- If any errors occur during the cleanup
|
|
|
- action, those are ignored, and the original
|
|
|
- traceback is preserved.
|
|
|
-
|
|
|
- :params func: This function is called if
|
|
|
- an exception occurs or at the end of the
|
|
|
- context block. If any exceptions raised
|
|
|
- by func are ignored.
|
|
|
- Usage:
|
|
|
- with cleanup_action(lambda e: print("Oops!")):
|
|
|
- do_something()
|
|
|
- """
|
|
|
- try:
|
|
|
- yield
|
|
|
- except Exception:
|
|
|
- ex_class, ex_val, ex_traceback = sys.exc_info()
|
|
|
- try:
|
|
|
- cleanup_func()
|
|
|
- except Exception as e:
|
|
|
- print("Error during exception cleanup: {0}".format(e))
|
|
|
- traceback.print_exc()
|
|
|
- six.reraise(ex_class, ex_val, ex_traceback)
|
|
|
- try:
|
|
|
- cleanup_func()
|
|
|
- except Exception as e:
|
|
|
- print("Error during cleanup: {0}".format(e))
|
|
|
- traceback.print_exc()
|
|
|
-
|
|
|
-
|
|
|
def skipIfNoService(services):
|
|
|
"""
|
|
|
A decorator for skipping tests if the provider
|
|
|
@@ -118,31 +79,32 @@ def skipIfPython(op, major, minor):
|
|
|
TEST_DATA_CONFIG = {
|
|
|
"AWSCloudProvider": {
|
|
|
# Match the ami value with entry in custom_amis.json for use with moto
|
|
|
- "image": get_env('CB_IMAGE_AWS', 'ami-aa2ea6d0'),
|
|
|
- "vm_type": get_env('CB_VM_TYPE_AWS', 't2.nano'),
|
|
|
- "placement": get_env('CB_PLACEMENT_AWS', 'us-east-1a'),
|
|
|
+ "image": cb_helpers.get_env('CB_IMAGE_AWS', 'ami-aa2ea6d0'),
|
|
|
+ "vm_type": cb_helpers.get_env('CB_VM_TYPE_AWS', 't2.nano'),
|
|
|
+ "placement": cb_helpers.get_env('CB_PLACEMENT_AWS', 'us-east-1a'),
|
|
|
},
|
|
|
'OpenStackCloudProvider': {
|
|
|
- 'image': os.environ.get('CB_IMAGE_OS',
|
|
|
- 'c66bdfa1-62b1-43be-8964-e9ce208ac6a5'),
|
|
|
- "vm_type": os.environ.get('CB_VM_TYPE_OS', 'm1.tiny'),
|
|
|
- "placement": os.environ.get('CB_PLACEMENT_OS', 'nova'),
|
|
|
+ 'image': cb_helpers.get_env('CB_IMAGE_OS',
|
|
|
+ 'c66bdfa1-62b1-43be-8964-e9ce208ac6a5'),
|
|
|
+ "vm_type": cb_helpers.get_env('CB_VM_TYPE_OS', 'm1.tiny'),
|
|
|
+ "placement": cb_helpers.get_env('CB_PLACEMENT_OS', 'nova'),
|
|
|
},
|
|
|
'GCPCloudProvider': {
|
|
|
- 'image': ('https://www.googleapis.com/compute/v1/'
|
|
|
- 'projects/ubuntu-os-cloud/global/images/'
|
|
|
- 'ubuntu-1710-artful-v20180126'),
|
|
|
- 'vm_type': 'f1-micro',
|
|
|
- 'placement': os.environ.get('GCP_DEFAULT_ZONE', 'us-central1-a'),
|
|
|
+ 'image': cb_helpers.get_env(
|
|
|
+ 'CB_IMAGE_GCP',
|
|
|
+ 'https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/'
|
|
|
+ 'global/images/ubuntu-1710-artful-v20180126'),
|
|
|
+ 'vm_type': cb_helpers.get_env('CB_VM_TYPE_GCP', 'f1-micro'),
|
|
|
+ 'placement': cb_helpers.get_env('GCP_DEFAULT_ZONE', 'us-central1-a'),
|
|
|
},
|
|
|
"AzureCloudProvider": {
|
|
|
"placement":
|
|
|
- get_env('CB_PLACEMENT_AZURE', 'eastus'),
|
|
|
+ cb_helpers.get_env('CB_PLACEMENT_AZURE', 'eastus'),
|
|
|
"image":
|
|
|
- get_env('CB_IMAGE_AZURE',
|
|
|
- 'Canonical:UbuntuServer:16.04.0-LTS:latest'),
|
|
|
+ cb_helpers.get_env('CB_IMAGE_AZURE',
|
|
|
+ 'Canonical:UbuntuServer:16.04.0-LTS:latest'),
|
|
|
"vm_type":
|
|
|
- get_env('CB_VM_TYPE_AZURE', 'Basic_A2'),
|
|
|
+ cb_helpers.get_env('CB_VM_TYPE_AZURE', 'Basic_A2'),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -181,7 +143,7 @@ def cleanup_network(network):
|
|
|
if network:
|
|
|
try:
|
|
|
for sn in network.subnets:
|
|
|
- with cleanup_action(lambda: cleanup_subnet(sn)):
|
|
|
+ with cb_helpers.cleanup_action(lambda: cleanup_subnet(sn)):
|
|
|
pass
|
|
|
finally:
|
|
|
network.delete()
|
|
|
@@ -211,7 +173,7 @@ def cleanup_gateway(gateway):
|
|
|
"""
|
|
|
Delete the supplied network and gateway.
|
|
|
"""
|
|
|
- with cleanup_action(lambda: gateway.delete()):
|
|
|
+ with cb_helpers.cleanup_action(lambda: gateway.delete()):
|
|
|
pass
|
|
|
|
|
|
|
|
|
@@ -261,11 +223,12 @@ def delete_instance(instance):
|
|
|
def cleanup_test_resources(instance=None, vm_firewall=None,
|
|
|
key_pair=None, network=None):
|
|
|
"""Clean up any combination of supplied resources."""
|
|
|
- with cleanup_action(lambda: cleanup_network(network)
|
|
|
- if network else None):
|
|
|
- with cleanup_action(lambda: key_pair.delete() if key_pair else None):
|
|
|
- with cleanup_action(lambda: vm_firewall.delete()
|
|
|
- if vm_firewall else None):
|
|
|
+ with cb_helpers.cleanup_action(
|
|
|
+ lambda: cleanup_network(network) if network else None):
|
|
|
+ with cb_helpers.cleanup_action(
|
|
|
+ lambda: key_pair.delete() if key_pair else None):
|
|
|
+ with cb_helpers.cleanup_action(
|
|
|
+ lambda: vm_firewall.delete() if vm_firewall else None):
|
|
|
delete_instance(instance)
|
|
|
|
|
|
|
|
|
@@ -293,7 +256,7 @@ class ProviderTestBase(unittest.TestCase):
|
|
|
return 1
|
|
|
|
|
|
def create_provider_instance(self):
|
|
|
- provider_name = get_env("CB_TEST_PROVIDER", "aws")
|
|
|
+ provider_name = cb_helpers.get_env("CB_TEST_PROVIDER", "aws")
|
|
|
factory = CloudProviderFactory()
|
|
|
provider_class = factory.get_provider_class(provider_name)
|
|
|
config = {'default_wait_interval':
|