Fix bgpcep testtool deploy when running with snapshots.
[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     [Arguments]    ${delay}=1
42     [Documentation]    Attempt to read excess data (probably just multiple prompts), ignoring failure.
43     ...    Log the data or error message. Return None.
44     ...    \${delay} parameter tunes how long a period of inactivity has to be to consider all excess data to be read.
45     ${status}    ${message} =    BuiltIn.Run_Keyword_And_Ignore_Error    SSHLibrary.Read    delay=${delay}
46     BuiltIn.Log    ${message}
47
48 Abort_Execution
49     [Documentation]    Send ctrl+c, read until prompt, Log and return the read text.
50     # TODO: Maybe timeout can be specified, but the tools usually return quickly.
51     Write_Bare_Ctrl_C
52     ${text} =    SSHLibrary.Read_Until_Prompt
53     BuiltIn.Log    ${text}
54     Flush_Read
55     [Return]    ${text}
56
57 RemoteBash__Wait_Iteration
58     [Documentation]    Send a newline and wait for prompt. On success, return the text before the prompt.
59     ...    The newline is there to avoid disconnection due to idling.
60     SSHLibrary.Write    ${EMPTY}
61     ${text} =    SSHLibrary.Read_Until_Prompt
62     [Return]    ${text}
63
64 Wait_Without_Idle
65     [Arguments]    ${timeout}    ${refresh}=1s
66     [Documentation]    Wait until prompt, while sending newlines to avoid idling.
67     ...    Flush read and return the text before prompt.
68     ${text} =    Wait_Until_Keyword_Succeeds    ${timeout}    ${refresh}    RemoteBash__Wait_Iteration
69     Flush_Read
70     [Return]    ${text}
71
72 Invoke_With_Timeout
73     [Arguments]    ${command}    ${timeout}
74     [Documentation]    Enter ${command}, wait until it finishes and return the output.
75     ...    If it does not finish within timeout, abort it and fail.
76     ...    In either case, flush read.
77     # TODO: Total duration is WUKS timeout plus RUP timeout. Should we do some computation?
78     SSHLibrary.Write    ${command}
79     ${text} =    Wait_Without_Idle    ${timeout}
80     [Teardown]    BuiltIn.Run_Keyword_If    "${KEYWORD_STATUS}" != "PASS"    Abort_Execution
81     [Return]    ${text}
82
83 RemoteBash__Log_Text_Before_Prompt
84     [Documentation]    Log text gathered by SSHLibrary.Read_Until_Prompt.
85     ...    This needs to be a separate keyword just because how Verify_Tool_Has_Not_Finished_Yet is implemented.
86     ${text} =    SSHLibrary.Read_Until_Prompt
87     BuiltIn.Log    ${text}
88
89 Verify_Tool_Has_Not_Finished_Yet
90     [Documentation]    Try to read SSH to see prompt, but expect to see no prompt within SSHLibrary's timeout.
91     ...    Log any text seen, to help debug what happened when the tool exited early.
92     ${status}    ${message} =    BuiltIn.Run_Keyword_And_Ignore_Error    RemoteBash__Log_Text_Before_Prompt
93     BuiltIn.Return_From_Keyword_If    "${status}" == "FAIL"
94     Builtin.Fail    The prompt was seen but it was not expected yet.
95
96 Check_Return_Code
97     [Arguments]    ${expected_rc}=0
98     [Documentation]    Get return code of the previous command, fail if it does not match the expectation..
99     SSHLibrary.Write    echo \$?
100     ${rc_and_prompt} =    SSHLibrary.Read_Until_Prompt
101     ${rc} =    String.Fetch_From_Left    ${rc_and_prompt}    ${\n}
102     BuiltIn.Should_Be_Equal_As_Integers    0    ${rc}