def test_csphost_new_init(): """Tests Host.__new__ and Host.__init__""" from apyfal.configuration import Configuration from apyfal.host import Host from apyfal.host._csp import CSPHost from apyfal.host.aws import AWSHost from apyfal.exceptions import HostConfigurationException # Mock arguments and configuration # Note that host_type is not specified here config = Configuration() try: for section in list(config._sections): if section.startswith('host'): del config._sections[section] except KeyError: pass kwargs = { 'region': 'region', 'project_id': 'project_id', 'client_id': 'client_id', 'auth_url': 'auth_url', 'interface': 'interface', 'config': config } # Test: Existing CSP class and module assert isinstance(Host(host_type="AWS", **kwargs), AWSHost) # Test: Not existing CSP module with pytest.raises(HostConfigurationException): Host(host_type="no_existing_csp") # Test: Existing CSP module, with no valid class with pytest.raises(HostConfigurationException): Host(host_type="_csp") # Test: direct instantiation of subclass without specify host_type host = AWSHost(**kwargs) # Test: repr assert repr(host) == str(host) # Test: Instantiation with missing mandatory arguments with pytest.raises(HostConfigurationException): Host(host_type="AWS", config=config) kwargs_no_client_id = kwargs.copy() del kwargs_no_client_id['client_id'] with pytest.raises(HostConfigurationException): Host(host_type="AWS", **kwargs_no_client_id) # Test: Abstract class class UncompletedCSP(CSPHost): """Uncompleted CSP class""" with pytest.raises(TypeError): UncompletedCSP()
def _set_accelerator_requirements(self, image_id=None, instance_type=None, *args, **kwargs): """ Configures instance with accelerator client parameters. Needs "accel_client" or "accel_parameters". Args: accelerator (str): Name of the accelerator accel_parameters (dict): Can override parameters from accelerator client. image_id (str): Force the use of specified image ID. instance_type (str): Force the use of specified instance type. Raises: apyfal.exceptions.HostConfigurationException: Parameters are not valid. """ _Host._set_accelerator_requirements( self, request_to_server=not (instance_type and instance_type), *args, **kwargs) # For CSP, config env are in a region sub category try: self._config_env = self._config_env[self._region] except KeyError: if not image_id or not instance_type: raise _exc.HostConfigurationException( ("Region '%s' is not supported. " "Available regions are: %s") % (self._region, ', '.join(region for region in self._config_env if region != 'accelerator'))) # Gets some CSP configuration values from environment self._image_id = image_id or self._config_env.pop('image', None) self._instance_type = instance_type or self._config_env.pop( 'instancetype', None)
def import_from_generic_test(host_type, **kwargs): """ Test to import a class from generic. Args: host_type( str): CSP host_type kwargs: Other args required """ from apyfal.host import Host Host(host_type=host_type, region='dummy_region', client_id='dummy_client_id', secret_id='dummy_secret_id', **kwargs)
def _get_host_iter(host_type, config, host_name_prefix): """ Get hosts generator for the specified host_type Args: host_type (str): host type config (apyfal.configuration.Configuration): Configuration. host_name_prefix (bool or str): see iter_accelerators host_name_prefix Returns: generator: Hosts generator """ try: # Gets generator generator = Host(host_type=host_type, config=config).iter_hosts( host_name_prefix) # Initializes generator and returns it return chain((next(generator),), generator) except (_exc.HostException, StopIteration): return iter(())
def test_host_init(): """Tests Host.__new__ and Host.__init__""" from apyfal.configuration import Configuration from apyfal.host import Host from apyfal.exceptions import HostConfigurationException # Mock arguments and configuration # Note that host_type is not specified here config = Configuration() try: del config._sections['host'] except KeyError: pass # Test: Not existing host module with pytest.raises(HostConfigurationException): Host(host_type="no_existing_csp") # Test: Instantiation of without specify host_type host = Host(config=config) assert host.__class__ is Host # Test: repr assert repr(host) == str(host) # Test: Passing host_ip url = 'http://127.0.0.1' host = Host(config=config, host_ip=url) host.start() assert host.url == url # Test: Passing nothing host = Host(config=config) with pytest.raises(HostConfigurationException): host.start() # Test: iter_hosts empty default assert list(host._iter_hosts()) == [] # Test: iter_hosts with proper _iter_host def _iter_hosts(): """dummy iter_host""" for index in range(2): yield dict(public_ip='127.0.0.1', host_name='prefix_accelize_pytest%d_000000000000' % index) host._iter_hosts = _iter_hosts assert list(host.iter_hosts()) == [{ 'public_ip': '127.0.0.1', 'host_name': 'prefix_accelize_pytest%d_000000000000' % index, 'accelerator': 'pytest%d' % index, 'url': 'http://127.0.0.1', 'host_type': host.host_type, '_repr': ("<apyfal.host.Host name='prefix_accelize_" "pytest%d_000000000000'>") % index } for index in range(2)] # Test: iter_hosts without public ip def _iter_hosts(): """dummy iter_host""" for index in range(2): yield dict(host_name='prefix_accelize_pytest%d_000000000000' % index) host._iter_hosts = _iter_hosts assert list(host.iter_hosts()) == [{ 'host_name': 'prefix_accelize_pytest%d_000000000000' % index, 'accelerator': 'pytest%d' % index, 'host_type': host.host_type, '_repr': ("<apyfal.host.Host name='prefix_accelize_" "pytest%d_000000000000'>") % index } for index in range(2)] # Test: iter_hosts host_name_prefix prefix = 'prefix' host = Host(config=config, host_name_prefix=prefix) assert host._host_name_prefix == prefix assert host._host_name_match is None list(host.iter_hosts(host_name_prefix=True)) assert host._host_name_match is not None assert not host._is_accelerator_host('accelize_pytest_000000000000') assert not host._is_accelerator_host('other_accelize_pytest_000000000000') assert host._is_accelerator_host('prefix_accelize_pytest_000000000000') list(host.iter_hosts(host_name_prefix=False)) assert host._is_accelerator_host('accelize_pytest_000000000000') assert host._is_accelerator_host('other_accelize_pytest_000000000000') assert host._is_accelerator_host('prefix_accelize_pytest_000000000000') list(host.iter_hosts(host_name_prefix='other')) assert not host._is_accelerator_host('accelize_pytest_000000000000') assert host._is_accelerator_host('other_accelize_pytest_000000000000') assert not host._is_accelerator_host('prefix_accelize_pytest_000000000000') # Test: not instance names list(host.iter_hosts(host_name_prefix=False)) assert not host._is_accelerator_host('pytest_000000000000') assert not host._is_accelerator_host('accelize_000000000000') assert not host._is_accelerator_host('accelize_prefix') assert not host._is_accelerator_host('accelize_prefix_000')
def __init__(self, client_id=None, secret_id=None, region=None, instance_type=None, key_pair=None, security_group=None, instance_id=None, init_config=None, init_script=None, ssl_cert_crt=None, ssl_cert_key=None, ssl_cert_generate=None, use_private_ip=None, **kwargs): _Host.__init__(self, **kwargs) # Default some attributes self._instance = None self._image_id = None self._image_name = None self._instance_type = None self._instance_type_name = None self._warn_keep_once = False section = self._config[self._config_section] # CSP self._client_id = client_id or section['client_id'] self._secret_id = secret_id or section['secret_id'] self._region = region or section['region'] # Instance data self._instance_type = instance_type or section['instance_type'] self._instance_id = instance_id or section['instance_id'] self._use_private_ip = (use_private_ip or section.get_literal('use_private_ip') or False) # Security self._key_pair = (key_pair or section['key_pair'] or self._default_parameter_value('KeyPair', include_host=True)) self._security_group = (security_group or section['security_group'] or self._default_parameter_value('SecurityGroup')) # Instance stop on "stop", "with" exit or garbage collection self.stop_mode = ( kwargs.get('stop_mode') or section['stop_mode'] or ('keep' if instance_id or kwargs.get('host_ip') else 'term')) # User data self._init_config = init_config or section['init_config'] self._init_script = init_script or section['init_script'] # Gets SSL certificate self._ssl_cert_key, self._ssl_cert_crt, self._ssl_cert_generate = \ self._get_certificates_arguments( ssl_cert_key, ssl_cert_crt, ssl_cert_generate) self._init_certificates() # Checks mandatory configuration values self._check_arguments('region') self._check_host_id_arguments()
def run_full_real_test_sequence(host_type, environment): """Run common real tests for all CSP. Args: host_type (str): CSP host_type. environment (dict): Environment to use """ from apyfal.configuration import Configuration from apyfal.exceptions import AcceleratorException from apyfal import iter_accelerators # Skip if no correct configuration with this host_type config = Configuration() if config['host']['host_type'] != host_type: config['host']['host_type'] = host_type try: del config['host']['client_id'] except KeyError: pass try: del config['host']['secret_id'] except KeyError: pass section = config['host.%s' % host_type] if not section['client_id'] or not section['secret_id']: pytest.skip('No configuration for %s.' % host_type) elif section['region'] not in environment: pytest.skip("No configuration for '%s' region on %s." % (section['region'], host_type)) # Enable logger from apyfal import get_logger get_logger(stdout=True) # Defines testing prefix. Used to easily found CSP object linked to a # particular test. testing_prefix = datetime.now().strftime('ApyfalTesting%H%M%S') # Add accelerator to environment environment['accelerator'] = 'apyfal_testing' config['host']['host_name_prefix'] = testing_prefix # Changes Host parameter prefix for default names # TODO: replace 'apyfal_testing' by testing_prefix variable once full # clean up of CSP objects is implemented. from apyfal.host import Host host_parameter_prefix = Host._PARAMETER_PREFIX Host._PARAMETER_PREFIX = 'ApyfalTesting' # Tests: instance_id_term = None instance_id_stop = None instance_id_keep = None try: # Start and terminate print('Test: Start and terminate') with Host(config=config, stop_mode='term') as csp_term: csp_term.start(accel_parameters=environment) instance_id_term = csp_term.instance_id # Start and stop, then terminate # Also check getting instance handle with ID print('Test: Start and stop') with Host(config=config, stop_mode='stop') as csp_stop: csp_stop.start(accel_parameters=environment) instance_id_stop = csp_stop.instance_id print('Test: Start from stopped and terminate') with Host(config=config, instance_id=instance_id_stop, stop_mode='term') as csp: csp.start(accel_parameters=environment) assert csp.instance_id == instance_id_stop # Start and keep, then # Also check getting instance handle with URL print('Test: Start and keep') with Host(config=config, stop_mode='keep') as csp_keep: csp_keep.start(accel_parameters=environment) instance_id_keep = csp_keep.instance_id instance_url_keep = csp_keep.url print('Test: Reuse with instance IP/URL') with Host(config=config, host_ip=instance_url_keep) as csp: csp.start(accel_parameters=environment) assert csp.url == instance_url_keep print('Test: Reuse with instance ID and terminate') with Host(config=config, instance_id=instance_id_keep, stop_mode='term') as csp: csp.start(accel_parameters=environment) assert csp.instance_id == instance_id_keep # Clean up testing environment finally: # Restore prefix value Host._PARAMETER_PREFIX = host_parameter_prefix # Stops all instances time.sleep(5) for accelerator in iter_accelerators(config=config): instance_id = None try: instance_id = accelerator.host.instance_id accelerator.stop('term') except AcceleratorException: pass assert instance_id not in (instance_id_term, instance_id_stop, instance_id_keep)