Example #1
0
 def test_check_char(self):
     shell = ShellCommand("%s\n" % 'ls', Timeout('fake', 30), logger=logging.getLogger())
     if shell.exitstatus:
         raise JobError("%s command exited %d: %s" % ('ls', shell.exitstatus, shell.readlines()))
     connection = ShellSession(self.job, shell)
     self.assertFalse(hasattr(shell, 'check_char'))
     self.assertTrue(hasattr(connection, 'check_char'))
     self.assertIsNotNone(connection.check_char)
Example #2
0
    def run(self, connection, max_end_time):
        # substitutions
        substitutions = {
            "{emptyimage}":
            self.get_namespace_data(action="prepare-empty-image",
                                    label="prepare-empty-image",
                                    key="output")
        }
        sub_command = self.get_namespace_data(
            action="prepare-qemu-commands",
            label="prepare-qemu-commands",
            key="sub_command",
        )
        sub_command = substitute(sub_command, substitutions)
        command_line = " ".join(sub_command)

        commands = []
        # get the download args in run()
        image_arg = self.get_namespace_data(action="download-action",
                                            label="iso",
                                            key="image_arg")
        action_arg = self.get_namespace_data(action="download-action",
                                             label="iso",
                                             key="file")
        substitutions["{%s}" % "iso"] = action_arg
        commands.append(image_arg)
        command_line += " ".join(substitute(commands, substitutions))

        preseed_file = self.get_namespace_data(action="download-action",
                                               label="file",
                                               key="preseed")
        if not preseed_file:
            raise JobError("Unable to identify downloaded preseed filename.")
        substitutions = {"{preseed}": preseed_file}
        append_args = self.get_namespace_data(action="prepare-qemu-commands",
                                              label="prepare-qemu-commands",
                                              key="append")
        append_args = substitute([append_args], substitutions)
        command_line += " ".join(append_args)

        self.logger.info(command_line)
        shell = ShellCommand(command_line, self.timeout, logger=self.logger)
        if shell.exitstatus:
            raise JobError(
                "%s command exited %d: %s" %
                (sub_command[0], shell.exitstatus, shell.readlines()))
        self.logger.debug("started a shell command")

        shell_connection = ShellSession(self.job, shell)
        shell_connection.prompt_str = self.get_namespace_data(
            action="prepare-qemu-commands",
            label="prepare-qemu-commands",
            key="prompts")
        shell_connection = super().run(shell_connection, max_end_time)
        return shell_connection
Example #3
0
    def run(self, connection, max_end_time):
        if connection:
            self.logger.debug("Already connected")
            return connection
        # ShellCommand executes the connection command

        self._check_params()
        command = self.command[:]  # local copy for idempotency
        overrides = None
        if self.key:
            overrides = self.get_namespace_data(action='prepare-scp-overlay',
                                                label="prepare-scp-overlay",
                                                key=self.key)
        host_address = None
        if overrides:
            host_address = str(
                self.get_namespace_data(action='prepare-scp-overlay',
                                        label="prepare-scp-overlay",
                                        key=overrides[0]))
        if host_address:
            self.logger.info(
                "Using common data to retrieve host_address for secondary connection."
            )
            command_str = " ".join(str(item) for item in command)
            self.logger.info("%s Connecting to device %s using '%s'",
                             self.name, host_address, command_str)
            command.append("%s@%s" % (self.ssh_user, host_address))
        elif self.host and self.primary:
            self.logger.info(
                "Using device data host_address for primary connection.")
            command_str = " ".join(str(item) for item in command)
            self.logger.info("%s Connecting to device %s using '%s'",
                             self.name, self.host, command_str)
            command.append("%s@%s" % (self.ssh_user, self.host))
        else:
            raise JobError("Unable to identify host address. Primary? %s" %
                           self.primary)
        command_str = " ".join(str(item) for item in command)
        shell = ShellCommand("%s\n" % command_str,
                             self.timeout,
                             logger=self.logger)
        if shell.exitstatus:
            raise JobError("%s command exited %d: %s" %
                           (self.command, shell.exitstatus, shell.readlines()))
        # SshSession monitors the pexpect
        connection = SShSession(self.job, shell)
        connection = super().run(connection, max_end_time)
        connection.prompt_str = list(self.parameters.get('prompts', []))
        connection.connected = True
        self.wait(connection)
        self.set_namespace_data(action='shared',
                                label='shared',
                                key='connection',
                                value=connection)
        return connection
Example #4
0
    def run(self, connection, max_end_time, args=None):
        # substitutions
        substitutions = {
            '{emptyimage}':
            self.get_namespace_data(action='prepare-empty-image',
                                    label='prepare-empty-image',
                                    key='output')
        }
        sub_command = self.get_namespace_data(action='prepare-qemu-commands',
                                              label='prepare-qemu-commands',
                                              key='sub_command')
        sub_command = substitute(sub_command, substitutions)
        command_line = ' '.join(sub_command)

        commands = []
        # get the download args in run()
        image_arg = self.get_namespace_data(action='download-action',
                                            label='iso',
                                            key='image_arg')
        action_arg = self.get_namespace_data(action='download-action',
                                             label='iso',
                                             key='file')
        substitutions["{%s}" % 'iso'] = action_arg
        commands.append(image_arg)
        command_line += ' '.join(substitute(commands, substitutions))

        preseed_file = self.get_namespace_data(action='download-action',
                                               label='file',
                                               key='preseed')
        if not preseed_file:
            raise JobError("Unable to identify downloaded preseed filename.")
        substitutions = {'{preseed}': preseed_file}
        append_args = self.get_namespace_data(action='prepare-qemu-commands',
                                              label='prepare-qemu-commands',
                                              key='append')
        append_args = substitute([append_args], substitutions)
        command_line += ' '.join(append_args)

        self.logger.info(command_line)
        shell = ShellCommand(command_line, self.timeout, logger=self.logger)
        if shell.exitstatus:
            raise JobError(
                "%s command exited %d: %s" %
                (sub_command[0], shell.exitstatus, shell.readlines()))
        self.logger.debug("started a shell command")

        shell_connection = ShellSession(self.job, shell)
        shell_connection.prompt_str = self.get_namespace_data(
            action='prepare-qemu-commands',
            label='prepare-qemu-commands',
            key='prompts')
        shell_connection = super(IsoCommandLine,
                                 self).run(shell_connection, max_end_time,
                                           args)
        return shell_connection
Example #5
0
 def test_check_char(self):
     shell = ShellCommand("%s\n" % "ls",
                          Timeout("fake", 30),
                          logger=logging.getLogger())
     if shell.exitstatus:
         raise JobError("%s command exited %d: %s" %
                        ("ls", shell.exitstatus, shell.readlines()))
     connection = ShellSession(self.job, shell)
     self.assertFalse(hasattr(shell, "check_char"))
     self.assertTrue(hasattr(connection, "check_char"))
     self.assertIsNotNone(connection.check_char)
Example #6
0
File: iso.py Project: slawr/lava
    def run(self, connection, max_end_time):
        """
        qemu needs help to reboot after running the debian installer
        and typically the boot is quiet, so there is almost nothing to log.
        """
        base_image = self.get_namespace_data(
            action="prepare-empty-image", label="prepare-empty-image", key="output"
        )
        self.sub_command.append("-drive format=raw,file=%s" % base_image)
        guest = self.get_namespace_data(
            action="apply-overlay-guest", label="guest", key="filename"
        )
        if guest:
            self.logger.info("Extending command line for qcow2 test overlay")
            self.sub_command.append(
                "-drive format=qcow2,file=%s,media=disk" % (os.path.realpath(guest))
            )
            # push the mount operation to the test shell pre-command to be run
            # before the test shell tries to execute.
            shell_precommand_list = []
            mountpoint = self.get_namespace_data(
                action="test", label="results", key="lava_test_results_dir"
            )
            shell_precommand_list.append("mkdir %s" % mountpoint)
            shell_precommand_list.append("mount -L LAVA %s" % mountpoint)
            self.set_namespace_data(
                action="test",
                label="lava-test-shell",
                key="pre-command-list",
                value=shell_precommand_list,
            )

        self.logger.info("Boot command: %s", " ".join(self.sub_command))
        shell = ShellCommand(
            " ".join(self.sub_command), self.timeout, logger=self.logger
        )
        if shell.exitstatus:
            raise JobError(
                "%s command exited %d: %s"
                % (self.sub_command, shell.exitstatus, shell.readlines())
            )
        self.logger.debug("started a shell command")

        shell_connection = ShellSession(self.job, shell)
        shell_connection = super().run(shell_connection, max_end_time)
        shell_connection.prompt_str = [INSTALLER_QUIET_MSG]
        self.wait(shell_connection)
        self.set_namespace_data(
            action="shared", label="shared", key="connection", value=shell_connection
        )
        return shell_connection
Example #7
0
    def run(self, connection, max_end_time, args=None):
        if connection:
            self.logger.debug("Already connected")
            return connection
        # ShellCommand executes the connection command

        self._check_params()
        command = self.command[:]  # local copy for idempotency
        overrides = None
        if self.key:
            overrides = self.get_namespace_data(action='prepare-scp-overlay', label="prepare-scp-overlay", key=self.key)
        host_address = None
        if overrides:
            host_address = str(self.get_namespace_data(
                action='prepare-scp-overlay', label="prepare-scp-overlay", key=overrides[0]))
        if host_address:
            self.logger.info("Using common data to retrieve host_address for secondary connection.")
            command_str = " ".join(str(item) for item in command)
            self.logger.info("%s Connecting to device %s using '%s'", self.name, host_address, command_str)
            command.append("%s@%s" % (self.ssh_user, host_address))
        elif self.host and self.primary:
            self.logger.info("Using device data host_address for primary connection.")
            command_str = " ".join(str(item) for item in command)
            self.logger.info("%s Connecting to device %s using '%s'", self.name, self.host, command_str)
            command.append("%s@%s" % (self.ssh_user, self.host))
        else:
            raise JobError("Unable to identify host address. Primary? %s" % self.primary)
        command_str = " ".join(str(item) for item in command)
        shell = ShellCommand("%s\n" % command_str, self.timeout, logger=self.logger)
        if shell.exitstatus:
            raise JobError("%s command exited %d: %s" % (
                self.command, shell.exitstatus, shell.readlines()))
        # SshSession monitors the pexpect
        connection = SShSession(self.job, shell)
        connection = super(ConnectSsh, self).run(connection, max_end_time, args)
        connection.sendline('export PS1="%s"' % self.job.device.get_constant(
            'default-shell-prompt'))
        connection.prompt_str = [self.job.device.get_constant(
            'default-shell-prompt')]
        connection.connected = True
        self.wait(connection)
        self.set_namespace_data(action='shared', label='shared', key='connection', value=connection)
        return connection
Example #8
0
    def run(self, connection, max_end_time, args=None):
        # substitutions
        substitutions = {'{emptyimage}': self.get_namespace_data(action='prepare-empty-image', label='prepare-empty-image', key='output')}
        sub_command = self.get_namespace_data(action='prepare-qemu-commands', label='prepare-qemu-commands', key='sub_command')
        sub_command = substitute(sub_command, substitutions)
        command_line = ' '.join(sub_command)

        commands = []
        # get the download args in run()
        image_arg = self.get_namespace_data(action='download-action', label='iso', key='image_arg')
        action_arg = self.get_namespace_data(action='download-action', label='iso', key='file')
        substitutions["{%s}" % 'iso'] = action_arg
        commands.append(image_arg)
        command_line += ' '.join(substitute(commands, substitutions))

        preseed_file = self.get_namespace_data(action='download-action', label='file', key='preseed')
        if not preseed_file:
            raise JobError("Unable to identify downloaded preseed filename.")
        substitutions = {'{preseed}': preseed_file}
        append_args = self.get_namespace_data(action='prepare-qemu-commands', label='prepare-qemu-commands', key='append')
        append_args = substitute([append_args], substitutions)
        command_line += ' '.join(append_args)

        self.logger.info(command_line)
        shell = ShellCommand(command_line, self.timeout, logger=self.logger)
        if shell.exitstatus:
            raise JobError("%s command exited %d: %s" % (sub_command[0], shell.exitstatus, shell.readlines()))
        self.logger.debug("started a shell command")

        shell_connection = ShellSession(self.job, shell)
        shell_connection.prompt_str = self.get_namespace_data(
            action='prepare-qemu-commands', label='prepare-qemu-commands', key='prompts')
        shell_connection = super(IsoCommandLine, self).run(shell_connection, max_end_time, args)
        return shell_connection
Example #9
0
    def run(self, connection, max_end_time, args=None):
        """
        qemu needs help to reboot after running the debian installer
        and typically the boot is quiet, so there is almost nothing to log.
        """
        base_image = self.get_namespace_data(action='prepare-empty-image', label='prepare-empty-image', key='output')
        self.sub_command.append('-drive format=raw,file=%s' % base_image)
        guest = self.get_namespace_data(action='apply-overlay-guest', label='guest', key='filename')
        if guest:
            self.logger.info("Extending command line for qcow2 test overlay")
            self.sub_command.append('-drive format=qcow2,file=%s,media=disk' % (os.path.realpath(guest)))
            # push the mount operation to the test shell pre-command to be run
            # before the test shell tries to execute.
            shell_precommand_list = []
            mountpoint = self.get_namespace_data(action='test', label='results', key='lava_test_results_dir')
            shell_precommand_list.append('mkdir %s' % mountpoint)
            shell_precommand_list.append('mount -L LAVA %s' % mountpoint)
            self.set_namespace_data(action='test', label='lava-test-shell', key='pre-command-list', value=shell_precommand_list)

        self.logger.info("Boot command: %s", ' '.join(self.sub_command))
        shell = ShellCommand(' '.join(self.sub_command), self.timeout, logger=self.logger)
        if shell.exitstatus:
            raise JobError("%s command exited %d: %s" % (self.sub_command, shell.exitstatus, shell.readlines()))
        self.logger.debug("started a shell command")

        shell_connection = ShellSession(self.job, shell)
        shell_connection = super(IsoRebootAction, self).run(shell_connection, max_end_time, args)
        shell_connection.prompt_str = [INSTALLER_QUIET_MSG]
        self.wait(shell_connection)
        self.set_namespace_data(action='shared', label='shared', key='connection', value=shell_connection)
        return shell_connection
Example #10
0
    def run(self, connection, max_end_time):
        """
        CommandRunner expects a pexpect.spawn connection which is the return value
        of target.device.power_on executed by boot in the old dispatcher.

        In the new pipeline, the pexpect.spawn is a ShellCommand and the
        connection is a ShellSession. CommandRunner inside the ShellSession
        turns the ShellCommand into a runner which the ShellSession uses via ShellSession.run()
        to run commands issued *after* the device has booted.
        pexpect.spawn is one of the raw_connection objects for a Connection class.
        """
        # initialise the first Connection object, a command line shell into the running QEMU.
        self.results = self.qemu_data
        guest = self.get_namespace_data(action='apply-overlay-guest',
                                        label='guest',
                                        key='filename')
        # check for NFS
        if 'qemu-nfs' in self.methods and self.parameters.get(
                'media') == 'nfs':
            self.logger.debug("Adding NFS arguments to kernel command line.")
            root_dir = self.get_namespace_data(action='extract-rootfs',
                                               label='file',
                                               key='nfsroot')
            self.substitutions["{NFSROOTFS}"] = root_dir
            params = self.methods['qemu-nfs']['parameters']['append']
            # console=ttyAMA0 root=/dev/nfs nfsroot=10.3.2.1:/var/lib/lava/dispatcher/tmp/dirname,tcp,hard,intr ip=dhcp
            append = [
                'console=%s' % params['console'], 'root=/dev/nfs',
                '%s rw' %
                substitute([params['nfsrootargs']], self.substitutions)[0],
                "%s" % params['ipargs']
            ]
            self.sub_command.append('--append')
            self.sub_command.append('"%s"' % ' '.join(append))
        elif guest:
            self.logger.info("Extending command line for qcow2 test overlay")
            # interface is ide by default in qemu
            interface = self.job.device['actions']['deploy']['methods'][
                'image']['parameters']['guest'].get('interface', 'ide')
            self.sub_command.append(
                '-drive format=qcow2,file=%s,media=disk,if=%s' %
                (os.path.realpath(guest), interface))
            # push the mount operation to the test shell pre-command to be run
            # before the test shell tries to execute.
            shell_precommand_list = []
            mountpoint = self.get_namespace_data(action='test',
                                                 label='results',
                                                 key='lava_test_results_dir')
            uuid = '/dev/disk/by-uuid/%s' % self.get_namespace_data(
                action='apply-overlay-guest', label='guest', key='UUID')
            shell_precommand_list.append('mkdir %s' % mountpoint)
            # prepare_guestfs always uses ext2
            shell_precommand_list.append('mount %s -t ext2 %s' %
                                         (uuid, mountpoint))
            # debug line to show the effect of the mount operation
            # also allows time for kernel messages from the mount operation to be processed.
            shell_precommand_list.append('ls -la %s/bin/lava-test-runner' %
                                         mountpoint)
            self.set_namespace_data(action='test',
                                    label='lava-test-shell',
                                    key='pre-command-list',
                                    value=shell_precommand_list)

        self.logger.info("Boot command: %s", ' '.join(self.sub_command))
        shell = ShellCommand(' '.join(self.sub_command),
                             self.timeout,
                             logger=self.logger)
        if shell.exitstatus:
            raise JobError(
                "%s command exited %d: %s" %
                (self.sub_command, shell.exitstatus, shell.readlines()))
        self.logger.debug("started a shell command")

        shell_connection = ShellSession(self.job, shell)
        shell_connection = super().run(shell_connection, max_end_time)

        self.set_namespace_data(action='shared',
                                label='shared',
                                key='connection',
                                value=shell_connection)
        return shell_connection
Example #11
0
    def run(self, connection, max_end_time, args=None):
        """
        CommandRunner expects a pexpect.spawn connection which is the return value
        of target.device.power_on executed by boot in the old dispatcher.

        In the new pipeline, the pexpect.spawn is a ShellCommand and the
        connection is a ShellSession. CommandRunner inside the ShellSession
        turns the ShellCommand into a runner which the ShellSession uses via ShellSession.run()
        to run commands issued *after* the device has booted.
        pexpect.spawn is one of the raw_connection objects for a Connection class.
        """
        # initialise the first Connection object, a command line shell into the running QEMU.
        guest = self.get_namespace_data(action='apply-overlay-guest', label='guest', key='filename')
        # check for NFS
        if 'qemu-nfs' in self.methods and self.parameters.get('media', None) == 'nfs':
            self.logger.debug("Adding NFS arguments to kernel command line.")
            root_dir = self.get_namespace_data(action='extract-rootfs', label='file', key='nfsroot')
            self.substitutions["{NFSROOTFS}"] = root_dir
            params = self.methods['qemu-nfs']['parameters']['append']
            # console=ttyAMA0 root=/dev/nfs nfsroot=10.3.2.1:/var/lib/lava/dispatcher/tmp/dirname,tcp,hard,intr ip=dhcp
            append = [
                'console=%s' % params['console'],
                'root=/dev/nfs',
                '%s rw' % substitute([params['nfsrootargs']], self.substitutions)[0],
                "%s" % params['ipargs']
            ]
            self.sub_command.append('--append')
            self.sub_command.append('"%s"' % ' '.join(append))
        elif guest:
            self.logger.info("Extending command line for qcow2 test overlay")
            # interface is ide by default in qemu
            interface = self.job.device['actions']['deploy']['methods']['image']['parameters']['guest'].get('interface', 'ide')
            self.sub_command.append('-drive format=qcow2,file=%s,media=disk,if=%s' %
                                    (os.path.realpath(guest), interface))
            # push the mount operation to the test shell pre-command to be run
            # before the test shell tries to execute.
            shell_precommand_list = []
            mountpoint = self.get_namespace_data(action='test', label='results', key='lava_test_results_dir')
            uuid = '/dev/disk/by-uuid/%s' % self.get_namespace_data(action='apply-overlay-guest', label='guest', key='UUID')
            shell_precommand_list.append('mkdir %s' % mountpoint)
            # prepare_guestfs always uses ext2
            shell_precommand_list.append('mount %s -t ext2 %s' % (uuid, mountpoint))
            # debug line to show the effect of the mount operation
            # also allows time for kernel messages from the mount operation to be processed.
            shell_precommand_list.append('ls -la %s/bin/lava-test-runner' % mountpoint)
            self.set_namespace_data(action='test', label='lava-test-shell', key='pre-command-list', value=shell_precommand_list)

        self.logger.info("Boot command: %s", ' '.join(self.sub_command))
        shell = ShellCommand(' '.join(self.sub_command), self.timeout, logger=self.logger)
        if shell.exitstatus:
            raise JobError("%s command exited %d: %s" % (self.sub_command, shell.exitstatus, shell.readlines()))
        self.logger.debug("started a shell command")

        shell_connection = ShellSession(self.job, shell)
        shell_connection = super(CallQemuAction, self).run(shell_connection, max_end_time, args)

        self.set_namespace_data(action='shared', label='shared', key='connection', value=shell_connection)
        return shell_connection