2aef5fa39dd1ad35a727128dcf6c815bb1192d96
[integration/test.git] / csit / suites / bgpcep / bgpuser / cases.robot
1 *** Settings ***
2 Documentation     Basic tests for odl-bgpcep-bgp-all feature.
3 ...
4 ...               Copyright (c) 2015 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 ...               Brief description of what this suite should do:
12 ...               https://wiki.opendaylight.org/view/BGP_LS_PCEP:Lithium_Feature_Tests#How_to_test_2
13 Suite Setup       Setup_Everything
14 Suite Teardown    Teardown_Everything
15 Test Setup        FailFast.Fail_This_Fast_On_Previous_Error
16 Test Teardown     FailFast.Start_Failing_Fast_If_This_Failed
17 Library           OperatingSystem
18 Library           SSHLibrary    prompt=]>    timeout=10s    # FIXME: The prompt should have default value from a common resource, and should be overwritable by pybot -v in scripts.
19 Library           RequestsLibrary
20 Library           ${CURDIR}/../../../libraries/HsfJson/hsf_json.py
21 Variables         ${CURDIR}/../../../variables/Variables.py
22 Variables         ${CURDIR}/../../../variables/bgpuser/variables.py    ${MININET}
23 Resource          ${CURDIR}/../../../libraries/ConfigViaRestconf.robot
24 Resource          ${CURDIR}/../../../libraries/FailFast.robot
25 Resource          ${CURDIR}/../../../libraries/BGPSpeaker.robot
26 Resource          ${CURDIR}/../../../libraries/KillPythonTool.robot
27 Resource          ${CURDIR}/../../../libraries/WaitForFailure.robot
28 Resource          ${CURDIR}/../../../libraries/Utils.robot
29
30 *** Variables ***
31 ${directory_for_actual_responses}    ${TEMPDIR}/actual
32 ${directory_for_expected_responses}    ${TEMPDIR}/expected
33 ${directory_with_template_folders}    ${CURDIR}/../../../variables/bgpuser/
34 ${HOLDTIME}       180
35
36 *** Test Cases ***
37 Check_For_Empty_Topology_Before_Talking
38     [Documentation]    Sanity check example-ipv4-topology is up but empty.
39     [Tags]    critical
40     Wait_For_Topology_To_Change_To    ${empty_json}    010_Empty.json    timeout=120s
41     # TODO: Verify that 120 seconds is not too short if this suite is run immediatelly after ODL is started.
42
43 Reconfigure_ODL_To_Accept_Connection
44     [Documentation]    Configure BGP peer module with initiate-connection set to false.
45     ${template_as_string}=    BuiltIn.Set_Variable    {'IP': '${MININET}', 'HOLDTIME': '${HOLDTIME}', 'PEER_PORT': '${BGP_TOOL_PORT}', 'INITIATE': 'false'}
46     ConfigViaRestconf.Put_Xml_Template_Folder_Config_Via_Restconf    ${directory_with_template_folders}${/}bgp_peer    ${template_as_string}
47
48 Start_Talking_BGP_speaker
49     [Documentation]    Start Python speaker to connect to ODL, verify that the tool does not promptly exit.
50     # Myport value is needed for checking whether connection at precise port was established.
51     BGPSpeaker.Start_BGP_Speaker    --amount 2 --myip=${MININET} --myport=${BGP_TOOL_PORT} --peerip=${CONTROLLER} --peerport=${ODL_BGP_PORT}
52     Read_And_Fail_If_Prompt_Is_Seen
53
54 Check_Talking_Connection_Is_Established
55     [Documentation]    See TCP (BGP) connection in established state.
56     # This case is separate from the previous one, to resemble structure of the second half of this suite more closely.
57     Check_Speaker_Is_Connected
58
59 Check_Talking_Topology_Is_Filled
60     [Documentation]    See new routes in example-ipv4-topology as a proof that synchronization was correct.
61     [Tags]    critical
62     Wait_For_Topology_To_Change_To    ${filled_json}    020_Filled.json
63
64 Kill_Talking_BGP_Speaker
65     [Documentation]    Abort the Python speaker. Also, attempt to stop failing fast.
66     [Setup]    FailFast.Run_Even_When_Failing_Fast
67     BGPSpeaker.Kill_BGP_Speaker
68     FailFast.Do_Not_Fail_Fast_From_Now_On
69     # NOTE: It is still possible to remain failing fast, if both previous and this test have failed.
70     [Teardown]    FailFast.Do_Not_Start_Failing_If_This_Failed
71
72 Check_For_Empty_Topology_After_Talking
73     [Documentation]    See example-ipv4-topology empty again.
74     [Tags]    critical
75     Wait_For_Topology_To_Change_To    ${empty_json}    030_Empty.json
76
77 Start_Listening_BGP_Speaker
78     [Documentation]    Start Python speaker in listening mode, verify that the tool does not exit quickly.
79     BGPSpeaker.Start_BGP_Speaker    --amount 2 --listen --myip=${MININET} --myport=${BGP_TOOL_PORT} --peerip=${CONTROLLER}
80     Read_And_Fail_If_Prompt_Is_Seen
81
82 Check_Listening_Connection_Is_Not_Established_Yet
83     [Documentation]    See no TCP connection, as both ODL and tool are in listening mode.
84     Check_Speaker_Is_Not_Connected
85
86 Check_For_Empty_Topology_Before_Listening
87     [Documentation]    Sanity check example-ipv4-topology is still empty.
88     [Tags]    critical
89     Verify_That_Topology_Does_Not_Change_From    ${empty_json}    040_Empty.json
90
91 Reconfigure_ODL_To_Initiate_Connection
92     [Documentation]    Replace BGP peer config module, now with initiate-connection set to true.
93     ${template_as_string}=    BuiltIn.Set_Variable    {'IP': '${MININET}', 'HOLDTIME': '${HOLDTIME}', 'PEER_PORT': '${BGP_TOOL_PORT}', 'INITIATE': 'true'}
94     ConfigViaRestconf.Put_Xml_Template_Folder_Config_Via_Restconf    ${directory_with_template_folders}${/}bgp_peer    ${template_as_string}
95
96 Check_Listening_Connection_Is_Established
97     [Documentation]    See TCP (BGP) connection in established state.
98     Check_Speaker_Is_Connected
99
100 Check_Listening_Topology_Is_Filled
101     [Documentation]    See new routes in example-ipv4-topology as a proof that synchronization was correct.
102     [Tags]    critical
103     Wait_For_Topology_To_Change_To    ${filled_json}    050_Filled.json
104
105 Kill_Listening_BGP_Speaker
106     [Documentation]    Abort the Python speaker. Also, attempt to stop failing fast.
107     [Setup]    FailFast.Run_Even_When_Failing_Fast
108     BGPSpeaker.Kill_BGP_Speaker
109     FailFast.Do_Not_Fail_Fast_From_Now_On
110     # NOTE: It is still possible to remain failing, if both previous and this test failed.
111     [Teardown]    FailFast.Do_Not_Start_Failing_If_This_Failed
112
113 Check_For_Empty_Topology_After_Listening
114     [Documentation]    Post-condition: Check example-ipv4-topology is empty again.
115     [Tags]    critical
116     Wait_For_Topology_To_Change_To    ${empty_json}    060_Empty.json
117
118 Delete_Bgp_Peer_Configuration
119     [Documentation]    Revert the BGP configuration to the original state: without any configured peers.
120     ConfigViaRestconf.Delete_Xml_Template_Folder_Config_Via_Restconf    ${directory_with_template_folders}${/}bgp_peer
121     # TODO: Do we need to check something else?
122
123 *** Keywords ***
124 Setup_Everything
125     [Documentation]    SSH-login to mininet machine, save prompt to variable, create HTTP session,
126     ...    prepare directories for responses, put Python tool to mininet machine, setup imported resources.
127     SSHLibrary.Open_Connection    ${MININET}
128     Utils.Flexible_SSH_Login    ${MININET_USER}    ${MININET_PASSWORD}
129     ${current_connection}=    Get_Connection
130     ${current_prompt}=    BuiltIn.Set_Variable    ${current_connection.prompt}
131     BuiltIn.Log    ${current_prompt}
132     Builtin.Set_Suite_Variable    ${prompt}    ${current_prompt}
133     RequestsLibrary.Create_Session    ses    http://${CONTROLLER}:${RESTCONFPORT}${OPERATIONAL_TOPO_API}    auth=${AUTH}
134     # TODO: Do not include slash in ${OPERATIONAL_TOPO_API}, having it typed here is more readable.
135     # TODO: Alternatively, create variable in Variables which starts with http.
136     # Both TODOs would probably need to update every suite relying on current Variables.
137     OperatingSystem.Remove_Directory    ${directory_for_expected_responses}    recursive=True
138     OperatingSystem.Remove_Directory    ${directory_for_actual_responses}    recursive=True
139     # The previous suite may have been using the same directories.
140     OperatingSystem.Create_Directory    ${directory_for_expected_responses}
141     OperatingSystem.Create_Directory    ${directory_for_actual_responses}
142     SSHLibrary.Put_File    ${CURDIR}/../../../../tools/fastbgp/play.py
143     ConfigViaRestconf.Setup_Config_Via_Restconf
144     FailFast.Do_Not_Fail_Fast_From_Now_On
145
146 Teardown_Everything
147     [Documentation]    Create and Log the diff between expected and actual responses, make sure Python tool was killed.
148     ...    Tear down imported Resources.
149     ${diff}=    OperatingSystem.Run    diff -dur ${directory_for_expected_responses} ${directory_for_actual_responses}
150     BuiltIn.Log    ${diff}
151     KillPythonTool.Search_And_Kill_Remote_Python    'play\.py'
152     ConfigViaRestconf.Teardown_Config_Via_Restconf
153     RequestsLibrary.Delete_All_Sessions
154     SSHLibrary.Close_All_Connections
155
156 Wait_For_Topology_To_Change_To
157     [Arguments]    ${json_topology}    ${filename}    ${timeout}=10s    ${refresh}=1s
158     [Documentation]    Normalize the expected json topology and save it to ${directory_for_expected_responses}.
159     ...    Wait until Compare_Topology matches. ${directory_for_actual_responses} will hold its last result.
160     ${topology_normalized}=    Normalize_And_Save_Expected_Json    ${json_topology}    ${filename}    ${directory_for_expected_responses}
161     BuiltIn.Wait_Until_Keyword_Succeeds    ${timeout}    ${refresh}    Compare_Topology    ${topology_normalized}    ${filename}
162
163 Verify_That_Topology_Does_Not_Change_From
164     [Arguments]    ${json_topology}    ${filename}    ${timeout}=10s    ${refresh}=1s
165     [Documentation]    Normalize the expected json topology and save it to ${directory_for_expected_responses}.
166     ...    Verify that Compare_Topology keeps passing. ${directory_for_actual_responses} will hold its last result.
167     ${topology_normalized}=    Normalize_And_Save_Expected_Json    ${json_topology}    ${filename}    ${directory_for_expected_responses}
168     WaitForFailure.Verify_Keyword_Does_Not_Fail_Within_Timeout    ${timeout}    ${refresh}    Compare_Topology    ${topology_normalized}    ${filename}
169
170 Compare_Topology
171     [Arguments]    ${expected_normalized}    ${filename}
172     [Documentation]    Get current example-ipv4-topology as json, normalize it, save to ${directory_for_actual_responses}.
173     ...    Check that status code is 200, check that normalized jsons match exactly.
174     ${response}=    RequestsLibrary.Get    ses    topology/example-ipv4-topology
175     BuiltIn.Log    ${response.status_code}
176     BuiltIn.Log    ${response.text}
177     ${actual_normalized}=    Normalize_And_Save_Expected_Json    ${response.text}    ${filename}    ${directory_for_actual_responses}
178     BuiltIn.Should_Be_Equal_As_Strings    ${response.status_code}    200
179     BuiltIn.Should_Be_Equal    ${actual_normalized}    ${expected_normalized}
180
181 Normalize_And_Save_Expected_Json
182     [Arguments]    ${json_text}    ${filename}    ${directory}
183     [Documentation]    Normalize given json using hsf_json library. Log and save the result to given filename under given directory.
184     ${json_normalized}=    hsf_json.Hsf_Json    ${json_text}
185     BuiltIn.Log    ${json_normalized}
186     OperatingSystem.Create_File    ${directory}${/}${filename}    ${json_normalized}
187     # TODO: Should we prepend .json to the filename? When we detect it is not already prepended?
188     [Return]    ${json_normalized}
189
190 Check_Speaker_Is_Not_Connected
191     [Documentation]    Give it a few tries to see zero established connections.
192     BuiltIn.Wait_Until_Keyword_Succeeds    3s    1s    Check_Number_Of_Speaker_Connections    0
193
194 Check_Speaker_Is_Connected
195     [Documentation]    Give it several tries to see exactly one established connection.
196     BuiltIn.Wait_Until_Keyword_Succeeds    5s    1s    Check_Number_Of_Speaker_Connections    1
197
198 Check_Number_Of_Speaker_Connections
199     [Arguments]    ${howmany}
200     [Documentation]    Run netstat in mininet machine and parse it for number of established connections. Check it is ${howmany}.
201     ${output}=    SSHLibrary.Execute_Command    netstat -npt 2> /dev/null | grep -E ":17900 .+ ESTABLISHED .+python" | wc -l
202     BuiltIn.Should_Be_Equal_As_Strings    ${output}    ${howmany}
203
204 Read_And_Fail_If_Prompt_Is_Seen
205     [Documentation]    Try to read SSH to see prompt, but expect to see no prompt within SSHLibrary's timeout.
206     ${passed}=    BuiltIn.Run_Keyword_And_Return_Status    BuiltIn.Run_Keyword_And_Expect_Error    No match found for '${prompt}' in *.    Read_Text_Before_Prompt
207     BuiltIn.Return_From_Keyword_If    ${passed}
208     BGPSpeaker.Dump_BGP_Speaker_Logs
209     Builtin.Fail    The prompt was seen but it was not expected yet
210
211 Read_Text_Before_Prompt
212     [Documentation]    Log text gathered by SSHLibrary.Read_Until_Prompt.
213     ...    This needs to be a separate keyword just because how Read_And_Fail_If_Prompt_Is_Seen is implemented.
214     ${text}=    SSHLibrary.Read_Until_Prompt
215     BuiltIn.Log    ${text}