Beispiel #1
0
def test_utils_ssh_exec_command_exception():
    """Test error in ssh exec command raises exception."""
    stdin = MagicMock()

    stderr = MagicMock()
    stderr.read.return_value = b'Exception: ls is not allowed!'

    stdout = MagicMock()
    stdout.read.return_value = b'Other information\n'

    client = MagicMock()
    client.exec_command.return_value = (stdin, stdout, stderr)

    with pytest.raises(IpaSSHException) as error:
        ipa_utils.execute_ssh_command(client, 'ls')

    msg = 'Other information\nException: ls is not allowed!'
    assert str(error.value) == msg
Beispiel #2
0
 def execute_ssh_command(self, client, command):
     """Execute the provided command and log output."""
     try:
         out = ipa_utils.execute_ssh_command(client, command)
     except Exception as error:
         raise IpaCloudException(
             'Command: "{0}", failed execution: {1}.'.format(
                 command, error))
     else:
         self._write_to_log(out)
Beispiel #3
0
 def _set_init_system(self, client):
     """Determine the init system of distribution."""
     if not self.init_system:
         try:
             out = ipa_utils.execute_ssh_command(client, 'ps -p 1 -o comm=')
         except Exception as e:
             raise IpaDistroException('An error occurred while retrieving'
                                      ' the distro init system: %s' % e)
         if out:
             self.init_system = out.strip()
Beispiel #4
0
    def get_vm_info(self, client):
        """Return vm info."""
        out = ''
        self._set_init_system(client)

        if self.init_system == 'systemd':
            try:
                out += 'systemd-analyze:\n\n'
                out += ipa_utils.execute_ssh_command(client, 'systemd-analyze')

                out += 'systemd-analyze blame:\n\n'
                out += ipa_utils.execute_ssh_command(client,
                                                     'systemd-analyze blame')

                out += 'journalctl -b:\n\n'
                out += ipa_utils.execute_ssh_command(client,
                                                     'sudo journalctl -b')
            except Exception as error:
                out = 'Failed to collect VM info: {0}.'.format(error)

        return out
Beispiel #5
0
    def repo_refresh(self, client):
        """Execute repo refresh command on instance."""
        update_cmd = "{sudo} '{refresh}'".format(
            sudo=self.get_sudo_exec_wrapper(),
            refresh=self.get_refresh_repo_cmd())

        out = ''
        try:
            out = ipa_utils.execute_ssh_command(client, update_cmd)
        except Exception as error:
            raise IpaDistroException(
                'An error occurred refreshing repos on instance: %s' % error)
        return out
Beispiel #6
0
    def install_package(self, client, package):
        """Install package on instance."""
        install_cmd = "{sudo} '{install} {package}'".format(
            sudo=self.get_sudo_exec_wrapper(),
            install=self.get_install_cmd(),
            package=package)

        try:
            out = ipa_utils.execute_ssh_command(client, install_cmd)
        except Exception as error:
            raise IpaDistroException(
                'An error occurred installing package {package} '
                'on instance: {error}'.format(package=package, error=error))
        else:
            return out
Beispiel #7
0
def test_utils_ssh_exec_command():
    """Test successful command execution."""
    stdin = MagicMock()
    stdin.read.return_value = ''

    stderr = MagicMock()
    stderr.read.return_value = ''

    stdout = MagicMock()
    stdout.read.return_value = b'test test.sh'

    client = MagicMock()
    client.exec_command.return_value = (stdin, stdout, stderr)

    out = ipa_utils.execute_ssh_command(client, 'ls')
    assert out == 'test test.sh'