Add code to kill restperfclient if hung.
[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__Invoke_With_Timeout
56     [Arguments]    ${timeout}    ${command}
57     [Timeout]    ${timeout}
58     Execute_Command_Passes    ${command} >${RestPerfClient__restperfclientlog} 2>&1
59
60 Invoke_Restperfclient
61     [Arguments]    ${timeout}    ${url}    ${testcase}=${EMPTY}    ${ip}=${ODL_SYSTEM_IP}    ${port}=${RESTCONFPORT}    ${count}=${REQUEST_COUNT}
62     ...    ${async}=false    ${user}=${ODL_RESTCONF_USER}    ${password}=${ODL_RESTCONF_PASSWORD}
63     [Documentation]    Invoke RestPerfClient on the specified URL with the specified timeout.
64     ...    Assemble the RestPerfClient invocation commad, setup the specified
65     ...    timeout for the SSH connection, invoke the assembled command and
66     ...    then check that RestPerfClient finished its run correctly.
67     [Teardown]    BuiltIn.Run_Keyword_If    ${restperfclient_running}    BuiltIn.Run_Keyword_And_Ignore_Error    RestPerfClient__Kill
68     ${restperfclient_running}=    Set_Variable    False
69     ${logname}=    Utils.Get_Log_File_Name    restperfclient    ${testcase}
70     BuiltIn.Set_Suite_Variable    ${RestPerfClient__restperfclientlog}    ${logname}
71     ${options}=    BuiltIn.Set_Variable    --ip ${ip} --port ${port} --edits ${count}
72     ${options}=    BuiltIn.Set_Variable    ${options} --edit-content request1.json --async-requests ${async}
73     ${options}=    BuiltIn.Set_Variable    ${options} --auth ${user} ${password}
74     ${timeout_in_minutes}=    Utils.Convert_To_Minutes    ${timeout}
75     ${options}=    BuiltIn.Set_Variable    ${options} --timeout ${timeout_in_minutes} --destination ${url}
76     ${command}=    BuiltIn.Set_Variable    ${RestPerfClient__restperfclient_invocation_command_prefix} ${options}
77     BuiltIn.Log    Running restperfclient: ${command}
78     SSHLibrary.Switch_Connection    ${RestPerfClient__restperfclient}
79     SSHLibrary.Set_Client_Configuration    timeout=${timeout}
80     ${keyword_timeout}=    DateTime.Add_Time_To_Time    ${timeout}    2m    result_format=compact
81     SetupUtils.Set_Known_Bug_Id    5413
82     ${restperfclient_running}=    Set_Variable    True
83     Restperfclient__Invoke_With_Timeout    ${keyword_timeout}    ${command}
84     ${restperfclient_running}=    Set_Variable    False
85     SetupUtils.Set_Unknown_Bug_Id
86     ${result}=    Grep_Restperfclient_Log    FINISHED. Execution time:
87     BuiltIn.Should_Not_Be_Equal    '${result}'    ''
88
89 Grep_Restperfclient_Log
90     [Arguments]    ${pattern}
91     [Documentation]    Search for the specified string in the log file produced by latest invocation of RestPerfClient
92     BuiltIn.Should_Not_Be_Equal    '${RestPerfClient__restperfclientlog}'    ''
93     ${result}=    SSHLibrary.Execute_Command    grep '${pattern}' ${RestPerfClient__restperfclientlog}
94     [Return]    ${result}
95
96 Collect_From_Restperfclient
97     [Documentation]    Collect useful data produced by restperfclient
98     BuiltIn.Should_Not_Be_Equal    '${RestPerfClient__restperfclientlog}'    ''
99     SSHLibrary.Get_File    ${RestPerfClient__restperfclientlog}
100     BuiltIn.Set_Suite_Variable    ${RestPerfClient__restperfclientlog}    ${EMPTY}
101
102 Teardown_Restperfclient
103     [Documentation]    Free resources allocated during the RestPerfClient setup
104     SSHLibrary.Switch_Connection    ${RestPerfClient__restperfclient}
105     SSHLibrary.Close_Connection