Esempio n. 1
0
def step(request):
    """
    Fixture to log a step in a test.
    """
    return get_logger(OrderedDict([('test_suite', request.module.__name__),
                                   ('test_case', request.function.__name__)]),
                      category='step')
Esempio n. 2
0
    def connect(self, connection=None):
        """
        See :meth:`BaseShell.connect` for more information.
        """
        connection = connection or self._default_connection or '0'

        if connection in self._connections and self.is_connected(connection):
            raise AlreadyConnectedError(connection)

        # Inject framework logger to the spawn object
        spawn_args = {
            'logfile': get_logger(
                OrderedDict([
                    ('node_identifier', self._node_identifier),
                    ('shell_name', self._shell_name),
                    ('connection', connection)
                ]),
                category='pexpect'
            ),
        }

        # Create a child process
        spawn_args.update(self._spawn_args)

        spawn = Spawn(
            self._get_connect_command().strip(),
            **spawn_args
        )

        # Add a connection logger
        # Note: self._node and self._name were added to this shell in the
        #       node's call to its _register_shell method.
        spawn._connection_logger = get_logger(
            OrderedDict([
                ('node_identifier', self._node_identifier),
                ('shell_name', self._shell_name),
                ('connection', connection)
            ]),
            category='connection'
        )

        self._connections[connection] = spawn

        try:
            # If connection is via user
            if self._user is not None:
                spawn.expect(
                    [self._user_match], timeout=self._timeout
                )
                spawn.sendline(self._user)

            # If connection is via password
            if self._password is not None:
                spawn.expect(
                    [self._password_match], timeout=self._timeout
                )
                spawn.sendline(self._password)

            # Setup shell before using it
            self._setup_shell(connection)

            # Execute initial command if required
            if self._initial_command is not None:
                spawn.expect(
                    self._prompt, timeout=self._timeout
                )
                spawn.sendline(self._initial_command)

            # Wait for command response to match the prompt
            spawn.expect(
                self._prompt, timeout=self._timeout
            )

        except:
            # Always remove bad connections if it failed
            del self._connections[connection]
            raise

        # Set connection as default connection if required
        if self.default_connection is None:
            self.default_connection = connection
Esempio n. 3
0
    def connect(self, *args, connection=None, **kwargs):
        """
        See :meth:`BaseShell.connect` for more information.
        """
        connection = connection or self._default_connection or '0'

        connection_is_present = connection in self._connections

        if connection_is_present and self.is_connected(connection=connection):
            raise AlreadyConnectedError(connection)

        spawn = Spawn(
            self._get_connect_command().strip(),
            **self._spawn_args
        )

        # If the disconnect is called on a connection and then connect is
        # called again on the same connection, this will be called twice,
        # making each message from that connection to be logged twice.
        if connection_is_present:
            present_connection = self._connections[connection]

            spawn.logfile_read = present_connection.logfile_read
            spawn.logfile_send = present_connection.logfile_send
            spawn._connection_logger = present_connection._connection_logger

        else:
            spawn.logfile_read = get_logger(
                OrderedDict([
                    ('node_identifier', self._node_identifier),
                    ('shell_name', self._shell_name),
                    ('connection', connection)
                ]),
                category='pexpect_read',
            )

            spawn.logfile_send = get_logger(
                OrderedDict([
                    ('node_identifier', self._node_identifier),
                    ('shell_name', self._shell_name),
                    ('connection', connection)
                ]),
                category='pexpect_send',
            )

            # Add a connection logger
            # Note: self._node and self._name were added to this shell in the
            #       node's call to its _register_shell method.
            spawn._connection_logger = get_logger(
                OrderedDict([
                    ('node_identifier', self._node_identifier),
                    ('shell_name', self._shell_name),
                    ('connection', connection)
                ]),
                category='connection'
            )
        # Set larger PTTY so there are less unnecessary line
        # changes on the stream
        spawn.setwinsize(30, 150)

        self._connections[connection] = spawn

        try:
            def expect_sendline(prompt, command):
                if command is not None:
                    spawn.expect(
                        prompt, timeout=self._timeout
                    )
                    spawn.sendline(command)

            # If connection is via user
            expect_sendline(self._user_match, self._user)

            # If connection is via password
            expect_sendline(self._password_match, self._password)

            # If connection is via initial command
            expect_sendline(self._initial_prompt, self._initial_command)

            # Setup shell before using it
            self._setup_shell(*args, connection=connection, **kwargs)

            # Wait for command response to match the prompt
            spawn.expect(
                self._prompt, timeout=self._timeout
            )

        except Exception:
            # Always remove a bad connection if it failed
            del self._connections[connection]
            raise

        # Set connection as default connection if required
        if self.default_connection is None:
            self.default_connection = connection
Esempio n. 4
0
    def connect(self, connection=None):
        """
        See :meth:`BaseShell.connect` for more information.
        """
        connection = connection or self._default_connection or '0'

        if connection in self._connections and self.is_connected(connection):
            raise AlreadyConnectedError(connection)

        # Inject framework logger to the spawn object
        spawn_args = {
            'logfile':
            get_logger(OrderedDict([('node_identifier', self._node_identifier),
                                    ('shell_name', self._shell_name),
                                    ('connection', connection)]),
                       category='pexpect'),
        }

        # Create a child process
        spawn_args.update(self._spawn_args)

        spawn = Spawn(self._get_connect_command().strip(), **spawn_args)

        # Add a connection logger
        # Note: self._node and self._name were added to this shell in the
        #       node's call to its _register_shell method.
        spawn._connection_logger = get_logger(OrderedDict([
            ('node_identifier', self._node_identifier),
            ('shell_name', self._shell_name), ('connection', connection)
        ]),
                                              category='connection')

        self._connections[connection] = spawn

        try:

            def expect_sendline(prompt, command):
                if command is not None:
                    spawn.expect(prompt, timeout=self._timeout)
                    spawn.sendline(command)

            # If connection is via user
            expect_sendline(self._user_match, self._user)

            # If connection is via password
            expect_sendline(self._password_match, self._password)

            # If connection is via initial command
            expect_sendline(self._initial_prompt, self._initial_command)

            # Setup shell before using it
            self._setup_shell(connection)

            # Wait for command response to match the prompt
            spawn.expect(self._prompt, timeout=self._timeout)

        except:
            # Always remove a bad connection if it failed
            del self._connections[connection]
            raise

        # Set connection as default connection if required
        if self.default_connection is None:
            self.default_connection = connection