Refactor suite to use RemoteBash, clean Utils
[integration/test.git] / csit / libraries / BGPcliKeywords.robot
1 *** Settings ***
2 Documentation     Robot keyword library (Resource) for handling the BGP speaker CLI tools
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 ...               This library contains keywords to handle command line tools in BGP Application
11 ...               for handling shell connections.
12 ...
13 ...               TODO: RemoteBash.robot contains logic which could be reused here.
14 Library           SSHLibrary    timeout=10s
15 Library           RequestsLibrary
16 Variables         ${CURDIR}/../variables/Variables.py
17 Resource          ${CURDIR}/RemoteBash.robot
18
19 *** Keywords ***
20 Start_Console_Tool
21     [Arguments]    ${command}    ${tool_opt}
22     [Documentation]    Start the tool ${command} ${tool_opt}
23     BuiltIn.Log    ${command}
24     ${output}=    SSHLibrary.Write    ${command} ${tool_opt}
25     BuiltIn.Log    ${output}
26
27 Wait_Until_Console_Tool_Finish
28     [Arguments]    ${timeout}
29     [Documentation]    Wait ${timeout} for the tool exit.
30     BuiltIn.Wait Until Keyword Succeeds    ${timeout}    1s    SSHLibrary.Read Until Prompt
31
32 Stop_Console_Tool
33     [Documentation]    Stop the tool if still running.
34     RemoteBash.Write_Bare_Ctrl_C
35     ${output}=    SSHLibrary.Read    delay=1s
36     BuiltIn.Log    ${output}
37
38 Read_And_Fail_If_Prompt_Is_Seen
39     [Documentation]    Try to read SSH to see prompt, but expect to see no prompt within SSHLibrary's timeout.
40     ${passed}=    BuiltIn.Run_Keyword_And_Return_Status    BuiltIn.Run_Keyword_And_Expect_Error    No match found for '${ODL_SYSTEM_PROMPT}' in *.    Read_Text_Before_Prompt
41     BuiltIn.Return_From_Keyword_If    ${passed}
42     BGPSpeaker.Dump_BGP_Speaker_Logs
43     Builtin.Fail    The prompt was seen but it was not expected yet
44
45 Read_Text_Before_Prompt
46     [Documentation]    Log text gathered by SSHLibrary.Read_Until_Prompt.
47     ...    This needs to be a separate keyword just because how Read_And_Fail_If_Prompt_Is_Seen is implemented.
48     ${text}=    SSHLibrary.Read_Until_Prompt
49     BuiltIn.Log    ${text}
50
51 Store_File_To_Workspace
52     [Arguments]    ${source_file_name}    ${target_file_name}
53     [Documentation]    Store the ${source_file_name} to the workspace as ${target_file_name}.
54     ${output_log}=    SSHLibrary.Execute_Command    cat ${source_file_name}
55     BuiltIn.Log    ${output_log}
56     Create File    ${target_file_name}    ${output_log}
57
58 Check_File_For_Word_Count
59     [Arguments]    ${file_name}    ${word}    ${expected_count}
60     [Documentation]    Count ${word} in ${file_name}. Expect ${expected_count} occurence(s)
61     ${output_log}=    SSHLibrary.Execute_Command    grep -o '${word}' ${file_name} | wc -l
62     BuiltIn.Log    ${output_log}
63     BuiltIn.Should_Be_Equal_As_Strings    ${output_log}    ${expected_count}
64
65 Count_Key_Value_Pairs
66     [Arguments]    ${file_name}    ${keyword}    ${value}=''
67     [Documentation]    Check file for ${keyword} or ${keyword} ${value} pair and returns number of occurences
68     ${output_log}=    SSHLibrary.Execute_Command    grep '${keyword}' ${file_name} | grep -c ${value}
69     ${count}=    Convert To Integer    ${output_log}
70     [Return]    ${count}
71
72 Check_File_For_Occurence
73     [Arguments]    ${file_name}    ${keyword}    ${value}=''
74     [Documentation]    Check file for ${keyword} or ${keyword} ${value} pair and returns number of occurences
75     ${output_log}=    SSHLibrary.Execute_Command    grep '${keyword}' ${file_name} | grep -c ${value}
76     ${count}=    Convert To Integer    ${output_log}
77     [Return]    ${count}