Update Robot Framework format - step 1
[integration/test.git] / csit / libraries / RemoteBash.robot
1 *** Settings ***
2 Documentation       Resource for managing bash execution when SSHLibrary.Execute_Command is not enough.
3 ...
4 ...                 Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
5 ...
6 ...                 This program and the accompanying materials are made available under the
7 ...                 terms of the Eclipse Public License v1.0 which accompanies this distribution,
8 ...                 and is available at http://www.eclipse.org/legal/epl-v10.html
9 ...
10 ...
11 ...                 There are many test tools that need to be started from bash,
12 ...                 and then either waited upon or aborted.
13 ...                 Typical hurdles are usage of virtualenv or preventing idle SSH sessions from closing down.
14 ...                 This Resource contains keywords specifically for these situations,
15 ...                 in order to avoid SSHKeywords growing too big.
16 ...
17 ...                 Each keyword assumes There is a SSH session already esteablished and active,
18 ...                 with properties such as timeout already configured.
19 ...                 No keyword should close or switch away from that session,
20 ...                 but the configured timeout may be changed if requested.
21 ...
22 ...                 TODO: Backport improvements from project-specific Resources,
23 ...                 for example logging to generated filename from NetconfKeywords and NexusKeywords.
24
25 Library             SSHLibrary
26 Resource            ${CURDIR}/SSHKeywords.robot
27
28
29 *** Keywords ***
30 Write_Bare_Ctrl_C
31     [Documentation]    Construct ctrl+c character and SSH-write it (without endline) to the current SSH connection.
32     ...    Do not read anything yet.
33     ${ctrl_c} =    BuiltIn.Evaluate    chr(int(3))
34     SSHLibrary.Write_Bare    ${ctrl_c}
35
36 Write_Bare_Ctrl_D
37     [Documentation]    Construct ctrl+d character and SSH-write it (without endline) to the current SSH connection.
38     ...    Do not read anything yet.
39     ${ctrl_d} =    BuiltIn.Evaluate    chr(int(4))
40     SSHLibrary.Write_Bare    ${ctrl_d}
41
42 Flush_Read
43     [Documentation]    Attempt to read excess data (probably just multiple prompts), ignoring failure.
44     ...    Log the data or error message. Return None.
45     ...    \${delay} parameter tunes how long a period of inactivity has to be to consider all excess data to be read.
46     [Arguments]    ${delay}=1
47     ${status}    ${message} =    BuiltIn.Run_Keyword_And_Ignore_Error    SSHLibrary.Read    delay=${delay}
48     BuiltIn.Log    ${message}
49
50 Abort_Execution
51     [Documentation]    Send ctrl+c, read until prompt, Log and return the read text.
52     # TODO: Maybe timeout can be specified, but the tools usually return quickly.
53     Write_Bare_Ctrl_C
54     ${text} =    SSHLibrary.Read_Until_Prompt
55     BuiltIn.Log    ${text}
56     Flush_Read
57     RETURN    ${text}
58
59 RemoteBash__Wait_Iteration
60     [Documentation]    Send a newline and wait for prompt. On success, return the text before the prompt.
61     ...    The newline is there to avoid disconnection due to idling.
62     SSHLibrary.Write    ${EMPTY}
63     ${text} =    SSHLibrary.Read_Until_Prompt
64     RETURN    ${text}
65
66 Wait_Without_Idle
67     [Documentation]    Wait until prompt, while sending newlines to avoid idling.
68     ...    Flush read and return the text before prompt.
69     [Arguments]    ${timeout}    ${refresh}=1s
70     ${text} =    Wait_Until_Keyword_Succeeds    ${timeout}    ${refresh}    RemoteBash__Wait_Iteration
71     Flush_Read
72     RETURN    ${text}
73
74 Invoke_With_Timeout
75     [Documentation]    Enter ${command}, wait until it finishes and return the output.
76     ...    If it does not finish within timeout, abort it and fail.
77     ...    In either case, flush read.
78     [Arguments]    ${command}    ${timeout}
79     # TODO: Total duration is WUKS timeout plus RUP timeout. Should we do some computation?
80     SSHLibrary.Write    ${command}
81     ${text} =    Wait_Without_Idle    ${timeout}
82     RETURN    ${text}
83     [Teardown]    BuiltIn.Run_Keyword_If    "${KEYWORD_STATUS}" != "PASS"    Abort_Execution
84
85 RemoteBash__Log_Text_Before_Prompt
86     [Documentation]    Log text gathered by SSHLibrary.Read_Until_Prompt.
87     ...    This needs to be a separate keyword just because how Verify_Tool_Has_Not_Finished_Yet is implemented.
88     ${text} =    SSHLibrary.Read_Until_Prompt
89     BuiltIn.Log    ${text}
90
91 Verify_Tool_Has_Not_Finished_Yet
92     [Documentation]    Try to read SSH to see prompt, but expect to see no prompt within SSHLibrary's timeout.
93     ...    Log any text seen, to help debug what happened when the tool exited early.
94     ${status}    ${message} =    BuiltIn.Run_Keyword_And_Ignore_Error    RemoteBash__Log_Text_Before_Prompt
95     IF    "${status}" == "FAIL"    RETURN
96     Builtin.Fail    The prompt was seen but it was not expected yet.
97
98 Check_Return_Code
99     [Documentation]    Get return code of the previous command, fail if it does not match the expectation..
100     [Arguments]    ${expected_rc}=0
101     SSHLibrary.Write    echo \$?
102     ${rc_and_prompt} =    SSHLibrary.Read_Until_Prompt
103     ${rc} =    String.Fetch_From_Left    ${rc_and_prompt}    ${\n}
104     BuiltIn.Should_Be_Equal_As_Integers    0    ${rc}