Migrate Get Requests invocations(libraries)
[integration/test.git] / csit / libraries / BGPSpeaker.robot
1 *** Settings ***
2 Documentation       Robot keyword library (Resource) for handling the BGP speaker Python utilities.
3 ...
4 ...                 Copyright (c) 2015,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 ...                 This library assumes that a SSH connection exists (and is switched to)
12 ...                 to a Linux machine (usualy TOOLS_SYSTEM) where the Python BGP speaker should be run.
13 ...                 It also assumes that the current working directory on that connection is the
14 ...                 directory where the speaker tool was deployed as there are no paths to neither
15 ...                 the play.py nor the log files in the commands.
16 ...
17 ...                 Aside BGP Speaker utility, there is also BGP Manager starting utilities in parallel.
18 ...                 For purpose of dumping logs and killing, Manager behaves the same as Speaker.
19 ...
20 ...                 TODO: RemoteBash.robot contains logic which could be reused here.
21 ...
22 ...                 TODO: Update the following TODOs, as SSHKeywords.robot was introduced.
23 ...                 TODO: The Utils.robot library has a "Run Command On Remote System" if we didn't
24 ...                 want to make the assumption that an SSH connection was already open.
25 ...                 alternative TODO: Explain that it is not worth to perform separate SSH logins.
26
27 Library             SSHLibrary
28 Library             RequestsLibrary
29 Resource            RemoteBash.robot
30 Resource            ../variables/Variables.robot
31
32
33 *** Variables ***
34 ${BGPSpeaker__OUTPUT_LOG}       play.py.out
35
36
37 *** Keywords ***
38 Start_BGP_Speaker
39     [Documentation]    Start the BGP speaker python utility. Redirect its error output to a log file
40     ...    so it can be dumped into the logs later, when stopping it. This also avoids polluting the
41     ...    output seen by "Read Until Prompt" with false propmpts so it won't stop prematurely
42     ...    leading to a spurious test failure, messy log content or other misbehavior.
43     [Arguments]    ${arguments}
44     ${command}=    BuiltIn.Set_Variable    python3 play.py ${arguments} &> ${BGPSpeaker__OUTPUT_LOG}
45     BuiltIn.Log    ${command}
46     ${output}=    SSHLibrary.Write    ${command}
47
48 Start_BGP_Speaker_And_Verify_Connected
49     [Documentation]    Start the BGP speaker python utility, and verifies it's connection.
50     ...    We can change connected variable to false to verify Speaker did not connect.
51     [Arguments]    ${arguments}    ${session}    ${speaker_ip}=${TOOLS_SYSTEM_IP}    ${connected}=${True}
52     Start_BGP_Speaker    ${arguments}
53     ${message}=    BuiltIn.Wait_Until_Keyword_Succeeds
54     ...    5x
55     ...    2s
56     ...    Verify_BGP_Speaker_Connection
57     ...    ${session}
58     ...    ${speaker_ip}
59     ...    ${connected}
60     RETURN    ${message}
61
62 Verify_BGP_Speaker_Connection
63     [Documentation]    Verifies peer's presence in bgp rib.
64     [Arguments]    ${session}    ${ip}    ${connected}=${True}
65     ${exp_status_code}=    BuiltIn.Set_Variable_If    ${connected}    200    404
66     ${url}=    BuiltIn.Set_Variable
67     ...    ${REST_API}/bgp-rib:bgp-rib/rib=example-bgp-rib/peer=bgp:%2F%2F${ip}?content=nonconfig
68     ${response}=    RequestsLibrary.GET On Session    ${session}    url=${url}    expected_status=${exp_status_code}
69     RETURN    ${response.content}
70
71 Start_BGP_Manager
72     [Documentation]    Start the BGP manager python utility. Redirect its error output to a log file.
73     [Arguments]    ${arguments}
74     ${command}=    BuiltIn.Set_Variable    python3 play.py ${arguments} &> ${BGPSpeaker__OUTPUT_LOG}
75     BuiltIn.Log    ${command}
76     ${output}=    SSHLibrary.Write    ${command}
77
78 Dump_BGP_Speaker_Logs
79     [Documentation]    Send all output produced by the play.py utility to Robot logs.
80     ...    This needs to be called if your suite detects play.py crashing and bypasses
81     ...    Kill_BGP_Speaker in that case otherwise the output of play.py (which most
82     ...    likely contains clues about why it crashed) will be lost.
83     ${output_log}=    SSHLibrary.Execute_Command    cat ${BGPSpeaker__OUTPUT_LOG}
84     BuiltIn.Log    ${output_log}
85
86 Kill_BGP_Speaker
87     [Documentation]    Interrupt play.py, fail if no prompt is seen within SSHLibrary timeout.
88     ...    Also dump the logs with the output the program produced.
89     ...    This keyword is also suitable for stopping BGP manager.
90     RemoteBash.Write_Bare_Ctrl_C
91     ${status}    ${message}=    BuiltIn.Run_Keyword_And_Ignore_Error    SSHLibrary.Read_Until_Prompt
92     Dump_BGP_Speaker_Logs
93     # TODO: When Propagate_Failure is moved to better Resource, use it instead of the following.
94     IF    '${status}' == 'PASS'    RETURN
95     BuiltIn.Log    ${message}
96     BuiltIn.Fail    The prompt was not seen within timeout period.
97
98 Kill_All_BGP_Speakers
99     [Documentation]    Kill all play.py processes.
100     ${command}=    BuiltIn.Set_Variable    ps axf | grep play.py | grep -v grep | awk '{print \"kill -9 \" $1}' | sh
101     SSHLibrary.Write    ${command}
102     ${output}=    SSHLibrary.Read_Until_Prompt
103     BuiltIn.Log    ${output}