Bug 2327 fix to verify ipaddress in sol003_02 testsuite
[osm/tests.git] / robot-systest / lib / ssh_lib.resource
1 *** Comments ***
2 #   Licensed under the Apache License, Version 2.0 (the "License");
3 #   you may not use this file except in compliance with the License.
4 #   You may obtain a copy of the License at
5 #
6 #       http://www.apache.org/licenses/LICENSE-2.0
7 #
8 #   Unless required by applicable law or agreed to in writing, software
9 #   distributed under the License is distributed on an "AS IS" BASIS,
10 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 #   See the License for the specific language governing permissions and
12 #   limitations under the License.
13
14
15 *** Settings ***
16 Documentation   Library providing keywords to interact with NF via SSH.
17 Library   SSHLibrary
18
19
20 *** Keywords ***
21 Test SSH Connection
22     [Documentation]   Test the SSH connection to the host with the user and credentials passed as arguments.
23     [Arguments]   ${host}   ${username}   ${password}   ${privatekey}
24     Open Connection   ${host}
25     IF   "${password}" != "${EMPTY}"
26         Login   ${username}   ${password}
27     ELSE
28         Login With Public Key   ${username}   ${privatekey}
29     END
30     Execute Command   hostname
31     Close All Connections
32
33 Check If Remote File Exists
34     [Documentation]   Check via SSH if a file exists in in the remote host, using the user and credentials passed as arguments.
35     [Arguments]   ${host}   ${username}   ${password}   ${privatekey}   ${file}
36     Open Connection   ${host}
37     IF   "${password}" != "${EMPTY}"
38         Login   ${username}   ${password}
39     ELSE
40         Login With Public Key   ${username}   ${privatekey}
41     END
42     ${rc}=   Execute Command   ls ${file} >& /dev/null   return_stdout=False   return_rc=True
43     Close All Connections
44     Should Be Equal As Integers   ${rc}   0
45
46 Check If Remote Folder Exists
47     [Documentation]   Check via SSH if a folder exists in in the remote host, using the user and credentials passed as arguments.
48     [Arguments]   ${host}   ${username}   ${password}   ${privatekey}   ${folder}
49     Open Connection   ${host}
50     IF   "${password}" != "${EMPTY}"
51         Login   ${username}   ${password}
52     ELSE
53         Login With Public Key   ${username}   ${privatekey}
54     END
55     ${output}=   Execute Command   ls -d ${folder}
56     Close All Connections
57     Should Be Equal As Strings   ${output}   ${folder}
58
59 Get Remote File Content
60     [Documentation]   Get via SSH the content of a remote file and return it.
61     [Arguments]   ${host}   ${username}   ${password}   ${privatekey}   ${file}
62     Open Connection   ${host}
63     IF   "${password}" != "${EMPTY}"
64         Login   ${username}   ${password}
65     ELSE
66         Login With Public Key   ${username}   ${privatekey}
67     END
68     ${output}=   Execute Command   cat ${file}
69     Close All Connections
70     RETURN   ${output}
71
72 Ping Many
73     [Documentation]   Connect via SSH to a remote host and test ping to a list of IP addresses, passed as argument.
74     [Arguments]   ${host}   ${username}   ${password}   ${privatekey}   @{ip_list}
75     Open Connection   ${host}
76     IF   "${password}" != "${EMPTY}"
77         Login   ${username}   ${password}
78     ELSE
79         Login With Public Key   ${username}   ${privatekey}
80     END
81     FOR   ${ip}   IN   @{ip_list}
82         ${result}=   Execute Command   ping -c 5 -W 1 ${ip} > /dev/null && echo OK   shell=True
83         Log   ${result}
84         Should Contain   ${result}   OK
85     END
86     Close All Connections
87
88 Execute Remote Command Check Rc Return Output
89     [Documentation]   Execute via SSH a command in a remote host and, if the command succeeds, return the output.
90     [Arguments]   ${host}   ${username}   ${password}   ${privatekey}   ${command}
91     Open Connection   ${host}
92     IF   "${password}" != "${EMPTY}"
93         Login   ${username}   ${password}
94     ELSE
95         Login With Public Key   ${username}   ${privatekey}
96     END
97     ${stdout}   ${rc}=   Execute Command   ${command}   return_rc=True   return_stdout=True
98     Log   ${rc}
99     Log   ${stdout}
100     Close All Connections
101     Should Be Equal As Integers   ${rc}   0
102     RETURN   ${stdout}