9703bb3d573dec789d7aae70b418b3405e681ebd
[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 Library           SSHLibrary
25 Resource          ${CURDIR}/SSHKeywords.robot
26
27 *** Keywords ***
28 Write_Bare_Ctrl_C
29     [Documentation]    Construct ctrl+c character and SSH-write it (without endline) to the current SSH connection.
30     ...    Do not read anything yet.
31     ${ctrl_c} =    BuiltIn.Evaluate    chr(int(3))
32     SSHLibrary.Write_Bare    ${ctrl_c}
33
34 Write_Bare_Ctrl_D
35     [Documentation]    Construct ctrl+d character and SSH-write it (without endline) to the current SSH connection.
36     ...    Do not read anything yet.
37     ${ctrl_d} =    BuiltIn.Evaluate    chr(int(4))
38     SSHLibrary.Write_Bare    ${ctrl_d}
39
40 Flush_Read
41     [Documentation]    Attempt to read excess data (probably just multiple prompts), ignoring failure.
42     ...    Log the data or error message. Return None.
43     ${status}    ${message} =    BuiltIn.Run_Keyword_And_Ignore_Error    SSHLibrary.Read
44     BuiltIn.Log    ${message}
45
46 Abort_Execution
47     [Documentation]    Send ctrl+c, read until prompt, Log and return the read text.
48     # TODO: Maybe timeout can be specified, but the tools usually return quickly.
49     Write_Bare_Ctrl_C
50     ${text} =    SSHLibrary.Read_Until_Prompt
51     BuiltIn.Log    ${text}
52     Flush_Read
53     [Return]    ${text}
54
55 RemoteBash__Wait_Iteration
56     [Documentation]    Send a newline and wait for prompt. On success, return the text before the prompt.
57     ...    The newline is there to avoid disconnection due to idling.
58     SSHLibrary.Write    ${EMPTY}
59     ${text} =    SSHLibrary.Read_Until_Prompt
60     [Return]    ${text}
61
62 Wait_Without_Idle
63     [Arguments]    ${timeout}    ${refresh}=1s
64     [Documentation]    Wait until prompt, while sending newlines to avoid idling.
65     ...    Flush read and return the text before prompt.
66     ${text} =    Wait_Until_Keyword_Succeeds    ${timeout}    ${refresh}    RemoteBash__Wait_Iteration
67     Flush_Read
68     [Return]    ${text}
69
70 Invoke_With_Timeout
71     [Arguments]    ${command}    ${timeout}
72     [Documentation]    Enter ${command}, wait until it finishes and return the output.
73     ...    If it does not finish within timeout, abort it and fail.
74     ...    In either case, flush read.
75     # TODO: Total duration is WUKS timeout plus RUP timeout. Should we do some computation?
76     SSHLibrary.Write    ${command}
77     ${text} =    Wait_Without_Idle    ${timeout}
78     [Teardown]    BuiltIn.Run_Keyword_If    "${KEYWORD_STATUS}" != "PASS"    Abort_Execution
79     [Return]    ${text}
80
81 RemoteBash__Log_Text_Before_Prompt
82     [Documentation]    Log text gathered by SSHLibrary.Read_Until_Prompt.
83     ...    This needs to be a separate keyword just because how Verify_Tool_Has_Not_Finished_Yet is implemented.
84     ${text} =    SSHLibrary.Read_Until_Prompt
85     BuiltIn.Log    ${text}
86
87 Verify_Tool_Has_Not_Finished_Yet
88     [Documentation]    Try to read SSH to see prompt, but expect to see no prompt within SSHLibrary's timeout.
89     ...    Log any text seen, to help debug what happened when the tool exited early.
90     ${status}    ${message} =    BuiltIn.Run_Keyword_And_Ignore_Error    RemoteBash__Log_Text_Before_Prompt
91     BuiltIn.Return_From_Keyword_If    "${status}" == "FAIL"
92     Builtin.Fail    The prompt was seen but it was not expected yet.
93
94 Check_Return_Code
95     [Arguments]    ${expected_rc}=0
96     [Documentation]    Get return code of the previous command, fail if it does not match the expectation..
97     SSHLibrary.Write    echo \$?
98     ${rc_and_prompt} =    SSHLibrary.Read_Until_Prompt
99     ${rc} =    String.Fetch_From_Left    ${rc_and_prompt}    ${\n}
100     BuiltIn.Should_Be_Equal_As_Integers    0    ${rc}