예제 #1
0
 def hard_reboot_instance(self):
     """Stop then start the instance."""
     self._stop_instance()
     self._start_instance()
     self._set_instance_ip()
     self.logger.debug('IP of instance: %s' % self.instance_ip)
     ipa_utils.clear_cache()
예제 #2
0
def test_utils_get_ssh_connection(mock_connect, mock_exec_cmd):
    """Test successful ssh connection."""
    stdin = stdout = stderr = MagicMock()
    stderr.read.return_value = b''

    mock_connect.return_value = None
    mock_exec_cmd.return_value = (stdin, stdout, stderr)

    ipa_utils.get_ssh_client(LOCALHOST, 'tests/data/ida_test')
    assert mock_connect.call_count == 1
    assert mock_exec_cmd.call_count == 1

    # Clear cache for subsequent tests.
    ipa_utils.clear_cache()
예제 #3
0
    def test_cloud_get_ssh_client(self, mock_get_ssh_client):
        """Test get ssh client method."""
        cloud = IpaCloud(*args, **self.kwargs)

        cloud.instance_ip = '127.0.0.1'
        cloud.ssh_user = '******'
        cloud.ssh_private_key_file = 'tests/data/ida_test'

        client = MagicMock()
        mock_get_ssh_client.return_value = client

        val = cloud._get_ssh_client()
        assert val == client
        assert mock_get_ssh_client.call_count == 1

        ipa_utils.clear_cache()
예제 #4
0
def test_utils_clear_cache(mock_exec_cmd):
    """Test img_proof utils client cache and clear specific ip."""
    client = MagicMock()
    ipa_utils.CLIENT_CACHE[LOCALHOST] = client

    val = ipa_utils.get_ssh_client(LOCALHOST, 'tests/data/ida_test')
    assert client == val

    # Test clear specfic IP
    ipa_utils.clear_cache(LOCALHOST)
    with pytest.raises(KeyError):
        ipa_utils.CLIENT_CACHE[LOCALHOST]

    ipa_utils.CLIENT_CACHE[LOCALHOST] = client

    # Test clear all cache
    ipa_utils.clear_cache()
    with pytest.raises(KeyError):
        ipa_utils.CLIENT_CACHE[LOCALHOST]
예제 #5
0
    def reboot(self, client):
        """Execute reboot command on instance."""
        self._set_init_system(client)

        reboot_cmd = \
            "{sudo} '(sleep 1 && {stop_ssh} && {reboot} &)' && exit".format(
                sudo=self.get_sudo_exec_wrapper(),
                stop_ssh=self.get_stop_ssh_service_cmd(),
                reboot=self.get_reboot_cmd()
            )

        try:
            transport = client.get_transport()
            channel = transport.open_session()
            channel.exec_command(reboot_cmd)
            time.sleep(2)  # Required for delay in reboot
            transport.close()
        except Exception as error:
            raise IpaDistroException(
                'An error occurred rebooting instance: %s' % error)
        ipa_utils.clear_cache()
예제 #6
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()