Rework of Restperfclient__Invoke_With_Timeout kw to avoid broken ssh session
[integration/test.git] / csit / libraries / RestPerfClient.robot
1 *** Settings ***
2 Documentation     RestPerfClient handling singleton resource.
3 ...
4 ...               Copyright (c) 2016 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 ...               This singleton manages RestPerfClient invocation, tracks the log file
11 ...               produced by the invocation, allows the test suite to easily search this
12 ...               log file and collect it once done.
13 ...
14 ...               TODO: Currently only one RestPerfClient invocation running at a time is
15 ...               supported. Support for multiple concurrently running RestPerfClient
16 ...               invocations might be needed for example when performance testing cluster
17 ...               nodes. However no such suites are planned for now.
18 ...
19 ...               FIXME: There may be suites which want to use this Resource without
20 ...               NetconfKeywords, in which case NexusKeywords will not be initialized
21 ...               and Setup_Restperfclient will fail. Fixing this problem will require
22 ...               updating NexusKeywords initialization (which may break other suites)
23 ...               and currently all suites using this use also NetconfKeywords so this
24 ...               was postponed. Workaround for the problem: Initialize NexusKeywords
25 ...               manually before initializing this resource.
26 Library           DateTime
27 Library           SSHLibrary
28 Resource          ${CURDIR}/NexusKeywords.robot
29 Resource          ${CURDIR}/SetupUtils.robot
30 Resource          ${CURDIR}/SSHKeywords.robot
31 Resource          ${CURDIR}/Utils.robot
32
33 *** Variables ***
34 ${RestPerfClient__restperfclientlog}    ${EMPTY}
35
36 *** Keywords ***
37 Setup_Restperfclient
38     [Documentation]    Deploy RestPerfClient and determine the Java command to use to call it.
39     ...    Open a SSH connection through which the RestPerfClient will be
40     ...    invoked, deploy RestPerfClient and the data files it needs to do
41     ...    its work and initialize the internal state for the remaining
42     ...    keywords.
43     ${connection}=    SSHKeywords.Open_Connection_To_Tools_System
44     BuiltIn.Set_Suite_Variable    ${RestPerfClient__restperfclient}    ${connection}
45     SSHLibrary.Put_File    ${CURDIR}/../variables/netconf/RestPerfClient/request1.json
46     ${filename}=    NexusKeywords.Deploy_Test_Tool    netconf    netconf-testtool    rest-perf-client
47     ${prefix}=    NexusKeywords.Compose_Full_Java_Command    -Xmx1G -XX:MaxPermSize=256M -jar ${filename}
48     BuiltIn.Set_Suite_Variable    ${RestPerfClient__restperfclient_invocation_command_prefix}    ${prefix}
49
50 RestPerfClient__Kill
51     Utils.Write_Bare_Ctrl_C
52     SSHLibrary.Set_Client_Configuration    timeout=5
53     SSHLibrary.Read_Until_Prompt
54
55 Restperfclient__Wait_For_Finish
56     SSHLibrary.Write    ${Empty}
57     ${stdout}=    SSHLibrary.Read_Until_Prompt
58
59 Restperfclient__Invoke_With_Timeout
60     [Arguments]    ${timeout}    ${command}
61     ${stdout}=    SSHLibrary.Read
62     BuiltIn.Log    ${stdout}
63     ${cmd}=    BuiltIn.Set_Variable    ${command} 2>&1 | tee ${RestPerfClient__restperfclientlog}
64     SSHLibrary.Write    ${cmd}
65     SSHLibrary.Set_Client_Configuration    timeout=120
66     BuiltIn.Wait_Until_Keyword_Succeeds    ${timeout}    1s    Restperfclient__Wait_For_Finish
67     Execute_Command_Passes    cat ${RestPerfClient__restperfclientlog}
68
69 Invoke_Restperfclient
70     [Arguments]    ${timeout}    ${url}    ${testcase}=${EMPTY}    ${ip}=${ODL_SYSTEM_IP}    ${port}=${RESTCONFPORT}    ${count}=${REQUEST_COUNT}
71     ...    ${async}=false    ${user}=${ODL_RESTCONF_USER}    ${password}=${ODL_RESTCONF_PASSWORD}
72     [Documentation]    Invoke RestPerfClient on the specified URL with the specified timeout.
73     ...    Assemble the RestPerfClient invocation commad, setup the specified
74     ...    timeout for the SSH connection, invoke the assembled command and
75     ...    then check that RestPerfClient finished its run correctly.
76     ${restperfclient_running}=    Set_Variable    False
77     ${logname}=    Utils.Get_Log_File_Name    restperfclient    ${testcase}
78     BuiltIn.Set_Suite_Variable    ${RestPerfClient__restperfclientlog}    ${logname}
79     ${options}=    BuiltIn.Set_Variable    --ip ${ip} --port ${port} --edits ${count}
80     ${options}=    BuiltIn.Set_Variable    ${options} --edit-content request1.json --async-requests ${async}
81     ${options}=    BuiltIn.Set_Variable    ${options} --auth ${user} ${password}
82     ${timeout_in_minutes}=    Utils.Convert_To_Minutes    ${timeout}
83     ${options}=    BuiltIn.Set_Variable    ${options} --timeout ${timeout_in_minutes} --destination ${url}
84     ${command}=    BuiltIn.Set_Variable    ${RestPerfClient__restperfclient_invocation_command_prefix} ${options}
85     BuiltIn.Log    Running restperfclient: ${command}
86     SSHLibrary.Switch_Connection    ${RestPerfClient__restperfclient}
87     ${keyword_timeout}=    DateTime.Add_Time_To_Time    ${timeout}    2m    result_format=compact
88     SetupUtils.Set_Known_Bug_Id    5413
89     ${restperfclient_running}=    Set_Variable    True
90     Restperfclient__Invoke_With_Timeout    ${keyword_timeout}    ${command}
91     ${restperfclient_running}=    Set_Variable    False
92     SetupUtils.Set_Unknown_Bug_Id
93     ${result}=    Grep_Restperfclient_Log    FINISHED. Execution time:
94     BuiltIn.Should_Not_Be_Equal    '${result}'    ''
95     [Teardown]    BuiltIn.Run_Keyword_If    ${restperfclient_running}    BuiltIn.Run_Keyword_And_Ignore_Error    RestPerfClient__Kill
96
97 Grep_Restperfclient_Log
98     [Arguments]    ${pattern}
99     [Documentation]    Search for the specified string in the log file produced by latest invocation of RestPerfClient
100     BuiltIn.Should_Not_Be_Equal    '${RestPerfClient__restperfclientlog}'    ''
101     ${result}=    SSHLibrary.Execute_Command    grep '${pattern}' ${RestPerfClient__restperfclientlog}
102     [Return]    ${result}
103
104 Collect_From_Restperfclient
105     [Documentation]    Collect useful data produced by restperfclient
106     BuiltIn.Should_Not_Be_Equal    '${RestPerfClient__restperfclientlog}'    ''
107     SSHLibrary.Get_File    ${RestPerfClient__restperfclientlog}
108     BuiltIn.Set_Suite_Variable    ${RestPerfClient__restperfclientlog}    ${EMPTY}
109
110 Teardown_Restperfclient
111     [Documentation]    Free resources allocated during the RestPerfClient setup
112     SSHLibrary.Switch_Connection    ${RestPerfClient__restperfclient}
113     SSHLibrary.Close_Connection