Merge branch 'master' into dev
SerialConn: check if port actually exists

If a given port does not exist, MONK did not respond with any kind of error
message. Now it says that the port is not correct.

For this purpose SerialConn got a new method _is_serialport which checks
whether the given port is a usable tty, and uses this method before keeping the
value of the constructor parameter 'port'. If it is not even possible to open
the given file name, the OSError is caught and False is returned. It seems more
reasonable to respond with return False in this case, because the task of the
method is to check the file name. People might actually call this method
specifically to not run into OSErrors.

See GitHub Issue #100.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
SerialConn: take emptiness check out of _cmd()

Having a _cmd() method check for an empty response and raise a corresponding
Exception was a bad design decision, because it adds on the burden of a
developer that might create their own connection type.

Therefore this check and corresponding param was taken out of _cmd() and needs
to be handled elsewhere. The only place where this feature was used until now
was the _prompt() method. Therefore this method gots a corresponding check
added.

See GitHub Issue #100

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
EchoConnection: explicit "empty" response

It happens that the EchoConnection gets a _cmd() request with an empty command
message, e.g., in the case of being called by _prompt(). However it is unlikely
that there might ever be a case where EchoConnection actually should respond
with an empty string, because such a string is often treated the same way as
`None`. Therefore the decision was made to explicitely return "<empty>" in
these cases.

See GitHub Issue #100

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
Disconnected: don't connect without I/O

While using MONK it feels strange to see a SerialConnection move to the state
Connected even when the target device is still deactivated. Therefore the state
Disconnected now asks for a new prompt after a connection was "established" and
if nothing is received (=_prompt raises EmptyResponseException) the state stays
at Disconnected and raises a CantConnectException.

See GitHub Issue #100.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
SerialConn: remove spelling error

In one place the unparsed variable `out` was used instead of `out_repl` which
already removed the \r

See GitHub Issue #98

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
AConnection.can_login(): Only Check Login Prompt

Until now the can_login Method checked for both password and login prompt. This
seems unreasonable. Either the content or the method's name had to be changed.
For now a change of content was assumed the better solution, because the method
was used in several places already.

Also for usability the method was transformed into a property.

See GitHub Issue #104.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
AConnection._prompt(): Exception depends on prompt

At first _prompt() raised an EmptyResponseException always when an empty string
was the result of its _cmd() call. But that is actually bad in the most common
scenario in which _prompt() basically asks for a current prompt string.
Updating the last_prompt string does not result in any output. Therefore
_prompt() raised an exception in the case where everything worked as expected.

The problem was resolved by also checking the last_prompt attribute. It gets
deleted before the _cmd() call and checked for existance after the call.
Therefore it must have been set in the middle. Only if that didn't happen and
no output was generated then an exception is raised.

FYI: The EmptyResponseException is important for the Disconnected state to
verify if a switch to the Connected state is possible. If you ask for a new
prompt and recieve absolutely nothing back than it is likely that the device is
not running or not connected to the test host properly. Therefore catching an
EmptyResponseException results in not switching to the Connected state.

See GitHub Issue #104.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
EchoConn: set last_prompt in _cmd()

In the previous commit to the same GitHub Issue #104 a check for last_prompt
was added to AConnection's _prompt() method. Therefore the test connections
also need to provide one if one is requested.

See GitHub Issue #104.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
SerialConnection._cmd(): debug-log formatted msg

The message that is actually sent to the device might be transformed in many
ways from what is actually entered into the public cmd() method by the user. To
better see what actually gets transmitted between test host and target device a
logging message was added.

See GitHub Issue #104.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
SerialConn: be more verbose about auth. process

Authenticating a user requires actually two steps, sending the username and
then sending the password. While debugging it was not clear whether one of
these steps or something in the middle might result in a buggy behaviour. It
was not the case, but to clear up the code helped evaluating the situation
better.

See GitHub Issue #104.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
AConnection: Also check for existing attribute

In commit a827b146648 _prompt() got the additional check for the last_prompt
attribute. The goal was to make sure an EmptyResponseException is not just
raised when the _cmd() call does not return any output because that
also happens when the prompt is updated as expected. The new if should only
result in the exception path if last_prompt and output where empty.

I completely forgot, though, that I delete last_prompt. So if it is not set it
simply does not exist and the Exception shown to the user will be an
AttributeError.

Here I correct this by checking for the existance of the last_prompt attribute
first. Only then the exception is raised.

The SilentConnection loses the line where it creates an empty last_prompt
attribute to reflect the change of understanding in the test suite.

This is a late additional commit to GitHub Issue #104.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
AConnection: raise Exception when not able to auth

If a login() method just returns False when a login didn't succeed the rest of
the code does not know that a problem occured and then just continues to try to
interact with the target device. Therefore a CantAuthException was introduced
in this commit.

This is a first step to resolve GitHub Issue #99.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
.gitignore: dist/ directory added

contains the data from the setuptools packaging&publishing process.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
AConnection: check first if already authed

One scenario in which an authentication process might fail is when the user is
already authenticated. Therefore before anything else such a test should be
conducted. Here it is implemented in a seperate property which returns True or
False depending on what the shell command "whoami" returns. Only when the
response to "whoami" is the same as the given username in the credentials
attribute then the return value is True, otherwise False.

See GitHub Issue #99.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
AConnection: login reacts to pw prompt

It is possible that as the result of what happened before the login process
actually starts with a password prompt by the connection. This happens for
example, when a is_authenticated request or a _prompt() request is interpreted
as a username by the target device. This is actually what happens in the most
likely scenario of connecting to the device with MONK.

This scenario is treated by checking for it with the previously called
can_auth() and then request a _prompt() when it is likely that the target
device expects a password not a username. This was probably also the reason why
can_auth() tested for user prompt and pw prompt.

Now that I understand the initial idea behind the code I think renaming the
property can_auth to has_user_prompt is better and adding a second one called
has_pw_prompt for the password prompt. Thereby the if-try-if in the login()
method gets easier to read and is less error prone. Now it says that it only
asks for a new prompt when the old one is the password prompt and then only
attempts a login when the given prompt actually is the user prompt.

See GitHub Issue #99.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
conn.py: Debug logging call

Forgot an actual method call.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
conn.py: restructure auth checks

This commit restructures two important methods. One is the login() method from
the Connected state and the other is the _login() method from SerialConn.

The idea before the change was to let Connected do all the checks so that
specific login's connection can focus on what needs to be done to authenticate
the user. Now I think that most of the checks in Connected are actually very
serial specific. For example in an SSH connection there should be a simpler way
to do the authentication via API instead of direct I/O with the device.
Therefore most of the checking that is currently done was moved to SerialConn.

Should support with resolving GitHub Issue #99.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>

For reference. Old commits that influenced SerialConn's _login():
a67f9cb7
ccf81a1a
a67f9cb7
ccf81a1a
1424c353
a67f9cb7
b41467bc

Old commits taht influenced Connected's login():
35ec9832
49c8e41d
9a61f337
49c8e41d
b33936ba
49c8e41d
9a61f337
35ec9832
d4206119
05896498
2d1f363a
cb202fe3
92eb49d7
35ec9832
92eb49d7
6b519e4d
1743aaa7
35ec9832
05896498
35ec9832
fdc27a94
test_conn: Get suite back to working

Through recent debugging endeavours the test suite got some errors. This commit
corrects them. They are mostly small issues where a variable is used in another
way or the credentials attribute of connections become more important. Nothing
serious, though.

See GitHub Issue #99.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>
Update Version strings

See GitHub Issue #106.

Signed-off-by: Erik Bernoth <erik.bernoth@gmail.com>