예제 #1
0
def test_utils_get_config_values_exceptions():
    """Test utils get config values function invalid path."""
    with pytest.raises(IpaUtilsException) as error:
        ipa_utils.get_config_values('tests/data/fake.config', 'ec2',
                                    'img_proof')

    assert str(error.value) == \
        'Config file not found: tests/data/fake.config'
예제 #2
0
    def post_init(self):
        """Initialize EC2 cloud framework class."""

        # Get command line values that are not None
        cmd_line_values = self._get_non_null_values(self.custom_args)

        self.zone = None
        self.account_name = self.custom_args.get('account_name')

        if not self.account_name:
            self.logger.debug(
                'No account provided. To use the EC2 config file an '
                'account name is required.')

        if not self.region:
            raise EC2CloudException('Region is required to connect to EC2.')
        elif self.region[-1].isalpha():
            self.zone = self.region
            self.region = self.region[:-1]

        config_file = self.cloud_config or EC2_CONFIG_FILE

        ec2_config = {}
        try:
            ec2_config = ipa_utils.get_config_values(
                config_file, ''.join(['region-', self.region]),
                ''.join(['account-', self.account_name]))
            self.logger.debug('Using EC2 config file: %s' % config_file)
        except Exception:
            self.logger.debug('EC2 config file not found: %s' % config_file)

        self.ec2_config = defaultdict(
            lambda: None, ChainMap(cmd_line_values, ec2_config,
                                   self.ipa_config))

        self.access_key_id = self.ec2_config['access_key_id']
        self.secret_access_key = self.ec2_config['secret_access_key']
        self.security_group_id = self.ec2_config['security_group_id']
        self.ssh_key_name = self.ec2_config['ssh_key_name']
        self.additional_info = self.ec2_config['additional_info']
        self.subnet_id = self.ec2_config['subnet_id']

        self.ssh_user = (cmd_line_values.get('ssh_user')
                         or ec2_config.get('user')
                         or self.ipa_config.get('ssh_user')
                         or EC2_DEFAULT_USER)

        self.ssh_private_key_file = (
            cmd_line_values.get('ssh_private_key_file')
            or ec2_config.get('ssh_private_key')
            or self.ipa_config.get('ssh_private_key_file'))

        if not self.ssh_private_key_file:
            raise EC2CloudException(
                'SSH private key file is required to connect to instance.')
        else:
            self.ssh_private_key_file = os.path.expanduser(
                self.ssh_private_key_file)
예제 #3
0
    def __init__(self,
                 cleanup=None,
                 config=None,
                 description=None,
                 distro_name=None,
                 early_exit=None,
                 history_log=None,
                 image_id=None,
                 inject=None,
                 instance_type=None,
                 log_level=None,
                 no_default_test_dirs=None,
                 cloud_config=None,
                 region=None,
                 results_dir=None,
                 running_instance_id=None,
                 test_dirs=None,
                 test_files=None,
                 timeout=None,
                 collect_vm_info=None,
                 ssh_private_key_file=None,
                 ssh_user=None,
                 subnet_id=None,
                 enable_secure_boot=None,
                 enable_uefi=None,
                 log_callback=None,
                 prefix_name=None,
                 retry_count=None,
                 custom_args=None):
        """Initialize base cloud framework class."""
        super(IpaCloud, self).__init__()
        # Get command line values that are not None
        cmd_line_values = self._get_non_null_values(locals())

        ipa_utils.clear_cache()

        self.custom_args = custom_args if custom_args else {}
        self.host_key_fingerprint = None
        self.instance_ip = None

        self.config = config or default_values['config']
        log_level = log_level or default_values['log_level']

        if log_callback:
            self.logger = log_callback
        else:
            self.logger = logging.getLogger('img_proof')
            self.logger.setLevel(log_level)
            self.logger.propagate = False

        try:
            self.ipa_config = ipa_utils.get_config_values(
                self.config, self.cloud, 'img_proof')
            self.logger.debug('Using img-proof config file: %s' % self.config)
        except IpaException:
            self.ipa_config = {}
            self.logger.debug('img-proof config file not found: %s' %
                              self.config)

        # Chain map options in order: cmdline -> config -> defaults
        self.ipa_config = defaultdict(
            lambda: None,
            ChainMap(cmd_line_values, self.ipa_config, default_values))

        self.description = self.ipa_config['description']
        self.cleanup = self.ipa_config['cleanup']
        self.distro_name = self.ipa_config['distro_name']
        self.early_exit = self.ipa_config['early_exit']
        self.image_id = self.ipa_config['image_id']
        self.inject = self.ipa_config['inject']
        self.instance_type = self.ipa_config['instance_type']
        self.test_files = list(self.ipa_config['test_files'])
        self.timeout = int(self.ipa_config['timeout'])
        self.history_log = self.ipa_config['history_log']
        self.region = self.ipa_config['region']
        self.collect_vm_info = self.ipa_config['collect_vm_info']
        self.cloud_config = self.ipa_config['cloud_config']
        self.running_instance_id = self.ipa_config['running_instance_id']
        self.results_dir = os.path.expanduser(self.ipa_config['results_dir'])
        self.ssh_private_key_file = self.ipa_config['ssh_private_key_file']
        self.ssh_user = self.ipa_config['ssh_user']
        self.subnet_id = self.ipa_config['subnet_id']
        self.enable_secure_boot = self.ipa_config['enable_secure_boot']
        self.enable_uefi = self.ipa_config['enable_uefi']
        self.no_default_test_dirs = bool(
            strtobool(str(self.ipa_config['no_default_test_dirs'])))
        self.prefix_name = self.ipa_config['prefix_name']
        self.retry_count = int(self.ipa_config['retry_count'])

        if self.enable_secure_boot and not self.enable_uefi:
            self.enable_uefi = True

        if self.cloud_config:
            self.cloud_config = os.path.expanduser(self.cloud_config)

        if self.ssh_private_key_file:
            self.ssh_private_key_file = os.path.expanduser(
                self.ssh_private_key_file)

        if not self.distro_name:
            raise IpaCloudException('Distro name is required.')
        else:
            self.distro_name = self.distro_name.lower()

        if self.cloud != 'ssh':
            if not self.image_id and not self.running_instance_id:
                raise IpaCloudException(
                    'Image ID or running instance is required.')

        self.results = {
            "tests": [],
            "summary":
            defaultdict(int, {
                "duration": 0,
                "passed": 0,
                "num_tests": 0
            })
        }

        self._parse_test_files(test_dirs, self.no_default_test_dirs)
        self.post_init()
예제 #4
0
    def __init__(self,
                 access_key_id=None,
                 account_name=None,
                 cleanup=None,
                 config=None,
                 description=None,
                 distro_name=None,
                 early_exit=None,
                 history_log=None,
                 image_id=None,
                 inject=None,
                 instance_type=None,
                 log_level=None,
                 no_default_test_dirs=False,
                 cloud_config=None,
                 region=None,
                 results_dir=None,
                 running_instance_id=None,
                 secret_access_key=None,
                 security_group_id=None,
                 ssh_key_name=None,
                 ssh_private_key_file=None,
                 ssh_user=None,
                 subnet_id=None,
                 test_dirs=None,
                 test_files=None,
                 timeout=None,
                 collect_vm_info=None,
                 enable_secure_boot=None,
                 enable_uefi=None):
        """Initialize EC2 cloud framework class."""
        super(EC2Cloud,
              self).__init__('ec2', cleanup, config, description, distro_name,
                             early_exit, history_log, image_id, inject,
                             instance_type, log_level, no_default_test_dirs,
                             cloud_config, region, results_dir,
                             running_instance_id, test_dirs, test_files,
                             timeout, collect_vm_info, ssh_private_key_file,
                             ssh_user, subnet_id, enable_secure_boot,
                             enable_uefi)
        # Get command line values that are not None
        cmd_line_values = self._get_non_null_values(locals())

        self.zone = None
        self.account_name = account_name

        if not self.account_name:
            self.logger.debug(
                'No account provided. To use the EC2 config file an '
                'account name is required.')

        if not self.region:
            raise EC2CloudException('Region is required to connect to EC2.')
        elif self.region[-1].isalpha():
            self.zone = self.region
            self.region = self.region[:-1]

        config_file = self.cloud_config or EC2_CONFIG_FILE

        ec2_config = {}
        try:
            ec2_config = ipa_utils.get_config_values(
                config_file, ''.join(['region-', self.region]),
                ''.join(['account-', self.account_name]))
            self.logger.debug('Using EC2 config file: %s' % config_file)
        except Exception:
            self.logger.debug('EC2 config file not found: %s' % config_file)

        self.ec2_config = defaultdict(
            lambda: None, ChainMap(cmd_line_values, ec2_config,
                                   self.ipa_config))

        self.access_key_id = self.ec2_config['access_key_id']
        self.secret_access_key = self.ec2_config['secret_access_key']
        self.security_group_id = self.ec2_config['security_group_id']
        self.ssh_key_name = self.ec2_config['ssh_key_name']
        self.subnet_id = self.ec2_config['subnet_id']

        self.ssh_user = (cmd_line_values.get('ssh_user')
                         or ec2_config.get('user')
                         or self.ipa_config.get('ssh_user')
                         or EC2_DEFAULT_USER)

        self.ssh_private_key_file = (
            cmd_line_values.get('ssh_private_key_file')
            or ec2_config.get('ssh_private_key')
            or self.ipa_config.get('ssh_private_key_file'))

        if not self.ssh_private_key_file:
            raise EC2CloudException(
                'SSH private key file is required to connect to instance.')
        else:
            self.ssh_private_key_file = os.path.expanduser(
                self.ssh_private_key_file)
예제 #5
0
def test_get_config_values():
    """Test utils get config values function."""
    data = ipa_utils.get_config_values('tests/data/config', 'ec2', 'img_proof')

    assert data['region'] == 'us-west-1'
    assert data['test_dirs'] == 'tests/data/tests'