Fix robot syntax with robot.tidy tool
[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 Library           SSHLibrary
27 Library           RequestsLibrary
28 Resource          RemoteBash.robot
29 Resource          ../variables/Variables.robot
30
31 *** Variables ***
32 ${BGPSpeaker__OUTPUT_LOG}    play.py.out
33 ${PEER_URL}       restconf/operational/bgp-rib:bgp-rib/rib/example-bgp-rib/peer/bgp:%2F%2F
34
35 *** Keywords ***
36 Start_BGP_Speaker
37     [Arguments]    ${arguments}
38     [Documentation]    Start the BGP speaker python utility. Redirect its error output to a log file
39     ...    so it can be dumped into the logs later, when stopping it. This also avoids polluting the
40     ...    output seen by "Read Until Prompt" with false propmpts so it won't stop prematurely
41     ...    leading to a spurious test failure, messy log content or other misbehavior.
42     ${command} =    BuiltIn.Set_Variable    python play.py ${arguments} &> ${BGPSpeaker__OUTPUT_LOG}
43     BuiltIn.Log    ${command}
44     ${output} =    SSHLibrary.Write    ${command}
45
46 Start_BGP_Speaker_And_Verify_Connected
47     [Arguments]    ${arguments}    ${session}    ${speaker_ip}=${TOOLS_SYSTEM_IP}    ${connected}=${True}
48     [Documentation]    Start the BGP speaker python utility, and verifies it's connection.
49     ...    We can change connected variable to false to verify Speaker did not connect.
50     Start_BGP_Speaker    ${arguments}
51     ${message}    BuiltIn.Wait_Until_Keyword_Succeeds    5x    2s    Verify_BGP_Speaker_Connection    ${session}    ${speaker_ip}
52     ...    ${connected}
53     [Return]    ${message}
54
55 Verify_BGP_Speaker_Connection
56     [Arguments]    ${session}    ${ip}    ${connected}=${True}
57     [Documentation]    Verifies peer's presence in bgp rib.
58     ${exp_status_code}    BuiltIn.Set_Variable_If    ${connected}    ${200}    ${404}
59     ${url}    BuiltIn.Set_Variable    ${PEER_URL}${ip}
60     ${response}    RequestsLibrary.Get_Request    ${session}    ${url}
61     BuiltIn.Should_Be_Equal_As_Numbers    ${exp_status_code}    ${response.status_code}
62     [Return]    ${response.content}
63
64 Start_BGP_Manager
65     [Arguments]    ${arguments}
66     [Documentation]    Start the BGP manager python utility. Redirect its error output to a log file.
67     ${command}=    BuiltIn.Set_Variable    python play.py ${arguments} &> ${BGPSpeaker__OUTPUT_LOG}
68     BuiltIn.Log    ${command}
69     ${output}=    SSHLibrary.Write    ${command}
70
71 Dump_BGP_Speaker_Logs
72     [Documentation]    Send all output produced by the play.py utility to Robot logs.
73     ...    This needs to be called if your suite detects play.py crashing and bypasses
74     ...    Kill_BGP_Speaker in that case otherwise the output of play.py (which most
75     ...    likely contains clues about why it crashed) will be lost.
76     ${output_log} =    SSHLibrary.Execute_Command    cat ${BGPSpeaker__OUTPUT_LOG}
77     BuiltIn.Log    ${output_log}
78
79 Kill_BGP_Speaker
80     [Documentation]    Interrupt play.py, fail if no prompt is seen within SSHLibrary timeout.
81     ...    Also dump the logs with the output the program produced.
82     ...    This keyword is also suitable for stopping BGP manager.
83     RemoteBash.Write_Bare_Ctrl_C
84     ${status}    ${message} =    BuiltIn.Run_Keyword_And_Ignore_Error    SSHLibrary.Read_Until_Prompt
85     Dump_BGP_Speaker_Logs
86     # TODO: When Propagate_Failure is moved to better Resource, use it instead of the following.
87     BuiltIn.Return_From_Keyword_If    '${status}' == 'PASS'
88     BuiltIn.Log    ${message}
89     BuiltIn.Fail    The prompt was not seen within timeout period.
90
91 Kill_All_BGP_Speakers
92     [Documentation]    Kill all play.py processes.
93     ${command}=    BuiltIn.Set_Variable    ps axf | grep play.py | grep -v grep | awk '{print \"kill -9 \" $1}' | sh
94     SSHLibrary.Write    ${command}
95     ${output}=    SSHLibrary.Read_Until_Prompt
96     BuiltIn.Log    ${output}