Do not crash if attempt to open ODL Netconf connection fails
[integration/test.git] / csit / suites / netconf / MDSAL / northbound.robot
1 *** Settings ***
2 Documentation     Metconf MDSAL Northbound test suite.
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 ...               The request produced by test cases "Get Config Running", "Get Config Running
12 ...               To Confirm No_Edit Before Commit", "Get Config Running To Confirm Delete
13 ...               After Commit" and "Get Config Candidate To Confirm Discard" all use the same
14 ...               message id ("empty") for their requests. This is possible because the
15 ...               requests produced by this suite are strictly serialized. The RFC 6241 does
16 ...               not state that these IDs are unique, it only requires that each ID used is
17 ...               "XML attribute normalized" if the client wants it to be returned unmodified.
18 ...               The RFC specifically says that "the content of this attribute is not
19 ...               interpreted in any way, it only is stored to be returned with the reply to
20 ...               the request. The reuse of the "empty" string for the 4 test cases was chosen
21 ...               for simplicity.
22 ...
23 ...               TODO: Change the 4 testcases to use unique message IDs.
24 Suite Setup       Setup_Everything
25 Suite Teardown    Teardown_Everything
26 Test Setup        SetupUtils.Setup_Test_With_Logging_And_Fast_Failing
27 Test Teardown     FailFast.Start_Failing_Fast_If_This_Failed
28 Library           RequestsLibrary
29 Library           SSHLibrary
30 Resource          ${CURDIR}/../../../libraries/FailFast.robot
31 Resource          ${CURDIR}/../../../libraries/SetupUtils.robot
32 Resource          ${CURDIR}/../../../libraries/SSHKeywords.robot
33 Resource          ${CURDIR}/../../../libraries/Utils.robot
34 Variables         ${CURDIR}/../../../variables/Variables.py
35
36 *** Variables ***
37 ${datadir}        ${CURDIR}/../../../variables/netconf/MDSAL
38 ${dataext}        msg
39 ${ssh_netconf_pid}    -1
40
41 *** Test Cases ***
42 Connect_To_ODL_Netconf
43     [Documentation]    Connect to ODL Netconf and fail if that is not possible.
44     Open_ODL_Netconf_Connection
45     ${hello_message}=    Get_Data    hello
46     Transmit_Message    ${hello_message}
47
48 Get_Config_Running
49     [Documentation]    Make sure the configuration has only the default elements in it.
50     ${reply}=    Load_And_Send_Message    get-config
51     BuiltIn.Should_Not_Contain    ${reply}    <name>name0</name>
52     BuiltIn.Should_Not_Contain    ${reply}    <name>name1</name>
53     BuiltIn.Should_Not_Contain    ${reply}    <name>name2</name>
54     BuiltIn.Should_Not_Contain    ${reply}    <name>name3</name>
55     BuiltIn.Should_Not_Contain    ${reply}    <name>name4</name>
56     BuiltIn.Set_Suite_Variable    ${empty_config}    ${reply}
57
58 Missing_Message_ID_Attribute
59     [Documentation]    Check that messages with missing "message-ID" attribute are rejected with the correct error (RFC 6241, section 4.1).
60     Perform_Test    missing-attr
61
62 Additional_Attributes_In_Message
63     [Documentation]    Check that additional attributes in messages are returned properly (RFC 6241, sections 4.1 and 4.2).
64     ${reply}=    Load_And_Send_Message    additional-attr
65     BuiltIn.Should_Contain    ${reply}    xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"
66     BuiltIn.Should_Contain    ${reply}    message-id="1"
67     BuiltIn.Should_Contain    ${reply}    attribute="something"
68     BuiltIn.Should_Contain    ${reply}    additional="otherthing"
69     BuiltIn.Should_Contain    ${reply}    xmlns:prefix="http://www.example.com/my-schema-example.html"
70
71 Edit_Config_Modules_Merge
72     [Documentation]    Request a "merge" operation adding an element in candidate configuration and check the reply.
73     Perform_Test    config-modules-merge-1
74
75 Get_Config_Running_To_Confirm_No_Edit_Before_Commit
76     [Documentation]    Make sure the running configuration is still unchanged as the change was not commited yet.
77     Send_And_Check    get-config-no-edit-before-commit    ${empty_config}
78
79 Commit_Edit
80     [Documentation]    Commit the change and check the reply.
81     Perform_Test    commit-edit
82
83 name0_In_Config_Running_To_Confirm_Edit_After_Commit
84     [Documentation]    Check that the change is now in the configuration.
85     ${reply}=    Load_And_Send_Message    get-config-edit-after-commit
86     BuiltIn.Should_Contain    ${reply}    <name>name0</name>
87
88 name0_In_Config_Modules_Via_Restconf
89     [Documentation]    Check that the change is also visible through Restconf.
90     ${data}=    Utils.Get_Data_From_URI    config    config:modules    headers=${ACCEPT_XML}
91     BuiltIn.Should_Contain    ${data}    <name>name0</name>
92
93 Edit_Config_Modules_Create_Shall_Fail_Now
94     [Documentation]    Request a "create" operation of an element that already exists and check that it fails with the correct error (RFC 6241, section 7.2, operation "create").
95     Perform_Test    config-modules-create
96
97 Delete_Modules
98     [Documentation]    Delete the created element from the candidate configuration and check the reply.
99     Perform_Test    config-modules-delete
100
101 Get_Config_Running_To_Confirm_No_Delete_Before_Commit
102     [Documentation]    Make sure the element is still present in the running configuration as the delete command was not committed yet.
103     ${reply}=    Load_And_Send_Message    get-config-no-delete-before-commit
104     BuiltIn.Should_Contain    ${reply}    <name>name0</name>
105
106 Commit_Delete
107     [Documentation]    Commit the deletion of the element and check the reply.
108     Perform_Test    commit-delete
109
110 Get_Config_Running_To_Confirm_Delete_After_Commit
111     [Documentation]    Check that the element is gone.
112     Send_And_Check    get-config-delete-after-commit    ${empty_config}
113
114 Restconf_Get_Modules_Shall_Return_404
115     [Documentation]    Check that "Not Found" is returned when Restconf is asked for the deleted element.
116     ${response}=    RequestsLibrary.Get    config    config:modules    ${ACCEPT_XML}
117     BuiltIn.Should_Be_Equal_As_Strings    ${response.status_code}    404
118
119 Commit_No_Transaction
120     [Documentation]    Attempt to perform "commit" when there are no changes in the candidate configuration and check that it fails with the correct error.
121     Test_Commit_With_No_Transactions
122
123 Edit_Config_Another_Modules_Merge
124     [Documentation]    Create the element in the candidate configuration again and check the reply.
125     Perform_Test    config-modules-merge-2
126
127 Get_Config_Candidate
128     [Documentation]    Check that the element is present in the candidate configuration.
129     ${reply}=    Load_And_Send_Message    get-config-candidate
130     BuiltIn.Should_Contain    ${reply}    <name>name1</name>
131     BuiltIn.Should_Not_Contain    ${reply}    name0
132
133 Discard_Changes
134     [Documentation]    Ask the server to discard the candidate and check the reply.
135     Perform_Test    commit-discard
136
137 Get_Config_Candidate_To_Confirm_Discard
138     [Documentation]    Check that the element was really discarded.
139     Send_And_Check    get-config-candidate-discard    ${empty_config}
140
141 Edit_Config_Modules_Multiple_Modules_Merge_1
142     [Documentation]    Create the element with "name2" subelement again and check the reply.
143     Perform_Test    merge-multiple-1
144
145 Edit_Config_Modules_Multiple_Modules_Merge_2
146     [Documentation]    Add a "name3" subelement to the element and check the reply.
147     Perform_Test    merge-multiple-2
148
149 Edit_Config_Modules_Multiple_Modules_Merge_3
150     [Documentation]    Add a "name4" subelement to the element and check the reply.
151     Perform_Test    merge-multiple-3
152
153 Commit_Multiple_Modules_Merge
154     [Documentation]    Commit the changes and check the reply.
155     Perform_Test    merge-multiple-commit
156
157 name2_name3_name4_In_Running_Config
158     [Documentation]    Check that the 3 subelements are now present in the running configuration.
159     ${reply}=    Load_And_Send_Message    merge-multiple-check
160     BuiltIn.Should_Contain    ${reply}    <name>name2</name>
161     BuiltIn.Should_Contain    ${reply}    <name>name3</name>
162     BuiltIn.Should_Contain    ${reply}    <name>name4</name>
163
164 name2_name3_name4_In_Config_Modules_Via_Restconf
165     [Documentation]    Check that the 3 subelements are visible via Restconf.
166     ${data}=    Utils.Get_Data_From_URI    config    config:modules    headers=${ACCEPT_XML}
167     BuiltIn.Should_Contain    ${data}    <name>name2</name>
168     BuiltIn.Should_Contain    ${data}    <name>name3</name>
169     BuiltIn.Should_Contain    ${data}    <name>name4</name>
170
171 Edit_Multiple_Modules_Merge
172     [Documentation]    Add another subelement named "test" to the element and check the reply.
173     Perform_Test    merge-multiple-edit
174
175 Commit_Multiple_Modules_Merge_Edit
176     [Documentation]    Commit the addition of the "test" subelement and check the reply.
177     Perform_Test    merge-multiple-edit-commit
178
179 Check_Multiple_Modules_Merge_Edit
180     [Documentation]    Check that the "test" subelement exists and has correct value for "port" subelement.
181     ${reply}=    Load_And_Send_Message    merge-multiple-edit-check
182     BuiltIn.Should_Contain    ${reply}    <port xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">2022</port>
183
184 Update_Multiple_Modules_Merge
185     [Documentation]    Update the value of the "port" subelement of the "test" subelement and check the reply.
186     Perform_Test    merge-multiple-update
187
188 Commit_Multiple_Modules_Merge_Update
189     [Documentation]    Commit the update and check the reply.
190     Perform_Test    merge-multiple-update-commit
191
192 Check_Multiple_Modules_Merge_Update
193     [Documentation]    Check that the value of the "port" was really updated.
194     ${reply}=    Load_And_Send_Message    merge-multiple-update-check
195     BuiltIn.Should_Contain    ${reply}    <port xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">3000</port>
196
197 Replace_Multiple_Modules_Merge
198     [Documentation]    Replace the content of the "test" with another completely different and check the reply.
199     Perform_Test    merge-multiple-replace
200
201 Commit_Multiple_Modules_Merge_Replace
202     [Documentation]    Commit the replace and check the reply.
203     Perform_Test    merge-multiple-replace-commit
204
205 Check_Multiple_Modules_Merge_Replace
206     [Documentation]    Check that the new content is there and the old content is gone.
207     ${reply}=    Load_And_Send_Message    merge-multiple-replace-check
208     BuiltIn.Should_Contain    ${reply}    <name>test</name>
209     BuiltIn.Should_Contain    ${reply}    <address xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">10.194.132.42</address>
210     BuiltIn.Should_Contain    ${reply}    <tcp-only xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">false</tcp-only>
211     BuiltIn.Should_Contain    ${reply}    <port xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">4000</port>
212     BuiltIn.Should_Contain    ${reply}    <password xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">admin</password>
213     BuiltIn.Should_Contain    ${reply}    <username xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">admin</username>
214     BuiltIn.Should_Not_Contain    ${reply}    <name>global-event-executor</name>
215     BuiltIn.Should_Not_Contain    ${reply}    <name>binding-osgi-broker</name>
216     BuiltIn.Should_Not_Contain    ${reply}    <name>dom-broker</name>
217     BuiltIn.Should_Not_Contain    ${reply}    <name>global-netconf-dispatcher</name>
218     BuiltIn.Should_Not_Contain    ${reply}    <name>global-netconf-processing-executor</name>
219
220 Remove_Multiple_Modules
221     [Documentation]    Remove the testing "module" element and all its subelements and check the reply.
222     Perform_Test    merge-multiple-remove
223
224 Commit_Multiple_Modules_Removal
225     [Documentation]    Commit the removal and check the reply.
226     Perform_Test    merge-multiple-remove-commit
227
228 Delete_Not_Existing_Module
229     [Documentation]    Attempt to delete the "module" element again and check that it fails with the correct error.
230     Perform_Test    delete-not-existing
231
232 Commit_Delete_Not_Existing_Module
233     [Documentation]    Attempt to commit and check the reply.
234     Test_Commit_With_No_Transactions
235     [Teardown]    Utils.Report_Failure_Due_To_Bug    4455
236
237 Remove_Not_Existing_Module
238     [Documentation]    Attempt to remove the "module" element again and check that the operation is "silently ignored".
239     Perform_Test    remove-not-existing
240
241 Commit_Remove_Not_Existing_Module
242     [Documentation]    Attempt to commit and check the reply.
243     Perform_Test    remove-not-existing-commit
244
245 Close_Session
246     [Documentation]    Close the session and check that it was closed properly.
247     Perform_Test    close-session
248
249 *** Keywords ***
250 Get_Data
251     [Arguments]    ${name}
252     [Documentation]    Load the specified data from the data directory and return it.
253     ${data}=    OperatingSystem.Get_File    ${datadir}${/}${name}.${dataext}
254     [Return]    ${data}
255
256 Open_ODL_Netconf_Connection
257     [Arguments]    ${host}=${CONTROLLER}    ${port}=${ODL_NETCONF_PORT}    ${user}=${ODL_NETCONF_USER}    ${password}=${ODL_NETCONF_PASSWORD}
258     [Documentation]    Open a netconf connecion to the given machine.
259     # The "-s netconf" flag (see the "SSHLibrary.Write" line below)    is not
260     # supported by SSHLibrary, therefore we need to use this elaborate and
261     # pretty tricky way to connect to the ODL Netconf port.
262     # TODO: Extract into NetconfKeywords as there are other tests that are
263     #    going to need to use this operation (Netconf Performance and Scaling
264     #    comes to my mind now as one of the things tested is the performance
265     #    of the direct netconf connection.
266     ${control}=    SSHLibrary.Open_Connection    ${host}    prompt=${ODL_SYSTEM_PROMPT}    timeout=10s
267     Utils.Flexible_Controller_Login
268     BuiltIn.Set_Suite_Variable    ${ssh_control}    ${control}
269     ${netconf}=    SSHLibrary.Open_Connection    ${host}    prompt=${ODL_SYSTEM_PROMPT}    timeout=10s
270     Utils.Flexible_Controller_Login
271     BuiltIn.Set_Suite_Variable    ${ssh_netconf}    ${netconf}
272     SSHLibrary.Write    sshpass -p ${password} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ${user}\@127.0.0.1 -p ${port} -s netconf
273     ${hello}=    SSHLibrary.Read_Until    ${ODL_NETCONF_PROMPT}
274     SSHLibrary.Switch_Connection    ${ssh_control}
275     SSHLibrary.Write    ps -A | grep sshpass | cut -b 1-6
276     ${pid}=    SSHKeywords.Read_Command_Response
277     BuiltIn.Set_Suite_Variable    ${ssh_netconf_pid}    ${pid}
278     SSHLibrary.Switch_Connection    ${ssh_netconf}
279     [Return]    ${hello}
280
281 Transmit_Message
282     [Arguments]    ${message}
283     [Documentation]    Transmit message to Netconf connection and discard the echo of the message.
284     SSHLibrary.Write    ${message}
285     SSHLibrary.Write    ${ODL_NETCONF_PROMPT}
286     SSHLibrary.Read_Until    ${ODL_NETCONF_PROMPT}
287
288 Send_Message
289     [Arguments]    ${message}
290     [Documentation]    Send message to Netconf connection and get the reply.
291     Transmit_Message    ${message}
292     ${reply}=    SSHLibrary.Read_Until    ${ODL_NETCONF_PROMPT}
293     [Return]    ${reply}
294
295 Prepare_For_Search
296     [Arguments]    ${searched_string}
297     [Documentation]    Prepare the specified string for searching in Netconf connection replies.
298     ...    The string passed to this keyword is coming from a data
299     ...    file which has different end of line conventions than
300     ...    the actual Netconf reply. This keyword patches the string
301     ...    to match what Netconf actually returns.
302     ${result}=    BuiltIn.Evaluate    "\\r\\n".join("""${searched_string}""".split("\\n"))
303     [Return]    ${result}
304
305 Load_And_Send_Message
306     [Arguments]    ${name}
307     [Documentation]    Load a message from the data file set, send it to Netconf and return the reply.
308     ${request}=    Get_Data    ${name}-request
309     ${reply}=    Send_Message    ${request}
310     [Return]    ${reply}
311
312 Load_Expected_Reply
313     [Arguments]    ${name}
314     [Documentation]    Load the expected reply from the data file set and return it.
315     ${expected_reply}=    Get_Data    ${name}-reply
316     ${expected}=    Prepare_For_Search    ${expected_reply}
317     [Return]    ${expected}
318
319 Close_ODL_Netconf_Connection
320     [Documentation]    Correctly close the Netconf connection and make sure it is really dead.
321     BuiltIn.Return_From_Keyword_If    ${ssh_netconf_pid} == -1
322     ${kill_command}=    BuiltIn.Set_Variable    kill ${ssh_netconf_pid}
323     BuiltIn.Set_Suite_Variable    ${ssh_netconf_pid}    -1
324     SSHLibrary.Switch_Connection    ${ssh_control}
325     SSHLibrary.Write    ${kill_command}
326     SSHLibrary.Read_Until_Prompt
327     SSHLibrary.Switch_Connection    ${ssh_netconf}
328     SSHLibrary.Read_Until_Prompt
329
330 Setup_Everything
331     [Documentation]    Setup resources and create session for Restconf checking.
332     SetupUtils.Setup_Utils_For_Setup_And_Teardown
333     RequestsLibrary.Create_Session    config    http://${CONTROLLER}:${RESTCONFPORT}${CONFIG_API}    auth=${AUTH}
334
335 Teardown_Everything
336     [Documentation]    Close the Netconf connection and destroy all sessions in the requests library.
337     Close_ODL_Netconf_Connection
338     RequestsLibrary.Delete_All_Sessions
339
340 Perform_Test
341     [Arguments]    ${name}
342     [Documentation]    Load and send the request from the dataset and compare the returned reply to the one stored in the dataset.
343     ${actual}=    Load_And_Send_Message    ${name}
344     ${expected}=    Load_Expected_Reply    ${name}
345     ${newline}=    BuiltIn.Evaluate    "\\r\\n"
346     BuiltIn.Should_Be_Equal    ${actual}    ${newline}${expected}${ODL_NETCONF_PROMPT}
347     [Return]    ${actual}
348
349 Send_And_Check
350     [Arguments]    ${name}    ${expected}
351     ${actual}=    Load_And_Send_Message    ${name}
352     BuiltIn.Should_Be_Equal    ${actual}    ${expected}
353
354 Test_Commit_With_No_Transactions
355     [Documentation]    Issue a "commit" RPC request and check that it fails with "No current transactions" error.
356     ${reply}=    Load_And_Send_Message    commit-no-transaction
357     ${expected}=    Load_Expected_Reply    commit-no-transaction
358     BuiltIn.Should_Contain    ${reply}    ${expected}