Esempio n. 1
0
def test_utils_redirect_output():
    """Test redirect output context manager."""
    temp_file = NamedTemporaryFile(mode='w+', delete=False)

    with ipa_utils.redirect_output(temp_file):
        print('Test message to file!')

    with open(temp_file.name) as temp_file:
        assert temp_file.readline() == 'Test message to file!\n'

    with ipa_utils.ignored(OSError):
        os.remove(temp_file.name)
Esempio n. 2
0
def test_utils_history_log():
    """Test utils history log function."""
    history_file = NamedTemporaryFile(delete=False)
    history_file.close()

    copyfile('tests/data/.history', history_file.name)

    ipa_utils.update_history_log(history_file.name,
                                 test_log='tests/data/history.log')

    with open(history_file.name, 'r+') as f:
        lines = f.readlines()

    assert lines[-1].strip() == 'tests/data/history.log'

    with ipa_utils.ignored(OSError):
        os.remove(history_file.name)
Esempio n. 3
0
    def test_image(self):
        """
        The entry point for testing an image.

        Creates new or initiates existing instance. Runs
        test suite on instance. Collects and returns
        results in json format.

        Returns:
            A tuple with the exit code and results json.
        """
        self._set_distro()

        if self.cloud == 'ssh':
            # SSH cloud framework: instance must be running
            pass
        elif self.running_instance_id:
            # Use existing instance
            self._start_instance_if_stopped()
            self._set_image_id()

            # With a running instance default to no cleanup
            # if a value has not been provided.
            if self.cleanup is None:
                self.cleanup = False
        else:
            # Launch new instance
            self.logger.info('Launching new instance')
            try:
                self._launch_instance()
            except Exception as error:
                with ipa_utils.ignored(Exception):
                    self._cleanup_instance(1)

                self.logger.error(error)
                raise

        if not self.instance_ip:
            self._set_instance_ip()
            self.logger.debug('IP of instance: %s' % self.instance_ip)

        self._set_results_dir()
        self._update_history()
        self._log_info()

        try:
            # Ensure instance running and SSH connection
            # can be established prior to testing instance.
            client = self._get_ssh_client()
            self.host_key_fingerprint = ipa_utils.get_host_key_fingerprint(
                client)
        except IpaSSHException as error:
            console_log = self.get_console_log()
            self._cleanup_instance(1)

            msg = 'Unable to connect to instance: %s' % error
            self._write_to_log(msg)
            self._write_to_log(console_log)

            raise IpaCloudException(msg)
        except Exception as error:
            console_log = self.get_console_log()
            self._cleanup_instance(1)

            msg = 'Unable to connect to instance: %s' % error
            self._write_to_log(msg)
            self._write_to_log(console_log)

            raise IpaCloudException(msg)

        if self.inject:
            self.process_injection_file(self._get_ssh_client())

        status = 0
        with ipa_utils.ssh_config(self.ssh_user, self.ssh_private_key_file)\
                as ssh_config:
            for item in self.test_files:
                if item == 'test_hard_reboot' and self.cloud != 'ssh':
                    self.logger.info('Testing hard reboot')
                    start = time.time()
                    result = 1

                    try:
                        self.hard_reboot_instance()
                        client = self._get_ssh_client()

                        if self.host_key_fingerprint != \
                                ipa_utils.get_host_key_fingerprint(client):
                            raise Exception('Host key has changed.')

                        result = 0
                    except IpaSSHException as error:
                        self.logger.error(
                            'Unable to connect to instance after '
                            'hard reboot: %s' % error)
                        break
                    except Exception as error:
                        self.logger.error('Instance failed hard reboot: %s' %
                                          error)
                        break
                    finally:
                        duration = time.time() - start
                        self._process_test_results(duration,
                                                   'test_hard_reboot', result)
                        status = status or result

                elif item == 'test_soft_reboot':
                    self.logger.info('Testing soft reboot')
                    start = time.time()
                    result = 1

                    try:
                        self.distro.reboot(self._get_ssh_client())
                        client = self._get_ssh_client()

                        if self.host_key_fingerprint != \
                                ipa_utils.get_host_key_fingerprint(client):
                            raise Exception('Host key has changed.')

                        result = 0
                    except IpaSSHException as error:
                        self.logger.error(
                            'Unable to connect to instance after '
                            'soft reboot: %s' % error)
                        break
                    except Exception as error:
                        self.logger.error('Instance failed soft reboot: %s' %
                                          error)
                        break
                    finally:
                        duration = time.time() - start
                        self._process_test_results(duration,
                                                   'test_soft_reboot', result)
                        status = status or result

                elif item == 'test_update':
                    self.logger.info('Testing update')
                    start = time.time()
                    result = 1

                    try:
                        out = self.distro.update(self._get_ssh_client())
                        result = 0
                    except Exception as error:
                        self.logger.error('Instance failed to update')
                        self.logger.debug(error)
                    else:
                        self._write_to_log(out)
                    finally:
                        duration = time.time() - start
                        self._process_test_results(duration, 'test_update',
                                                   result)
                        status = status or result

                elif item == 'test_refresh':
                    self.logger.info('Testing refresh')
                    start = time.time()
                    result = 1

                    try:
                        out = self.distro.repo_refresh(self._get_ssh_client())
                        result = 0
                    except Exception as error:
                        self.logger.error('Instance failed to refresh')
                        self.logger.debug(error)
                    else:
                        self._write_to_log(out)
                    finally:
                        duration = time.time() - start
                        self._process_test_results(duration, 'test_refresh',
                                                   result)
                        status = status or result

                elif isinstance(item, str):
                    # Run tests
                    result = self._run_test(item, ssh_config)
                    status = status or result
                else:
                    self.logger.error('Invalid test item in list: %s' % item)

                if status and self.early_exit:
                    break

        # flag set to collect VM info
        if self.collect_vm_info:
            self._collect_vm_info()

        self._cleanup_instance(status)
        self._save_results()

        # Return status and results json
        return status, self.results