xref: /openbmc/openbmc-test-automation/lib/bmc_network_utils.robot (revision bfa16ee4f68964bd5dd20618cb3b293584b78c69)
1*** Settings ***
2Resource                ../lib/utils.robot
3Resource                ../lib/connection_client.robot
4Resource                ../lib/boot_utils.robot
5Library                 ../lib/gen_misc.py
6Library                 ../lib/utils.py
7
8*** Variables ***
9# MAC input from user.
10${MAC_ADDRESS}          ${EMPTY}
11
12
13*** Keywords ***
14
15Check And Reset MAC
16    [Documentation]  Update BMC with user input MAC address.
17    [Arguments]  ${mac_address}=${MAC_ADDRESS}
18
19    # Description of argument(s):
20    # mac_address  The mac address (e.g. 00:01:6c:80:02:28).
21
22    Should Not Be Empty  ${mac_address}
23    Open Connection And Log In
24    ${bmc_mac_addr}=  Execute Command On BMC  cat /sys/class/net/eth0/address
25    Run Keyword If  '${mac_address.lower()}' != '${bmc_mac_addr.lower()}'
26    ...  Set MAC Address
27
28
29Set MAC Address
30    [Documentation]  Update eth0 with input MAC address.
31    [Arguments]  ${mac_address}=${MAC_ADDRESS}
32
33    # Description of argument(s):
34    # mac_address  The mac address (e.g. 00:01:6c:80:02:28).
35
36    Write  fw_setenv ethaddr ${mac_address}
37    OBMC Reboot (off)
38
39    # Take SSH session post BMC reboot.
40    Open Connection And Log In
41    ${bmc_mac_addr}=  Execute Command On BMC  cat /sys/class/net/eth0/address
42    Should Be Equal  ${bmc_mac_addr}  ${mac_address}  ignore_case=True
43
44
45Get BMC IP Info
46    [Documentation]  Get system IP address and prefix length.
47
48    Open Connection And Login
49
50    # Get system IP address and prefix length details using "ip addr"
51    # Sample Output of "ip addr":
52    # 1: eth0: <BROADCAST,MULTIAST> mtu 1500 qdisc mq state UP qlen 1000
53    #     link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
54    #     inet xx.xx.xx.xx/24 brd xx.xx.xx.xx scope global eth0
55
56    ${cmd_output}=  Execute Command On BMC  /sbin/ip addr | grep eth0
57
58    # Get line having IP address details.
59    ${lines}=  Get Lines Containing String  ${cmd_output}  inet
60
61    # List IP address details.
62    @{ip_components}=  Split To Lines  ${lines}
63
64    @{ip_data}=  Create List
65
66    # Get all IP addresses and prefix lengths on system.
67    :FOR  ${ip_component}  IN  @{ip_components}
68    \  @{if_info}=  Split String  ${ip_component}
69    \  ${ip_n_prefix}=  Get From List  ${if_info}  1
70    \  Append To List  ${ip_data}  ${ip_n_prefix}
71
72    [Return]  ${ip_data}
73
74Get BMC Route Info
75    [Documentation]  Get system route info.
76
77    Open Connection And Login
78
79    # Sample output of "ip route":
80    # default via xx.xx.xx.x dev eth0
81    # xx.xx.xx.0/23 dev eth0  src xx.xx.xx.xx
82    # xx.xx.xx.0/24 dev eth0  src xx.xx.xx.xx
83
84    ${cmd_output}=  Execute Command On BMC  /sbin/ip route
85
86    [Return]  ${cmd_output}
87
88Get BMC MAC Address
89    [Documentation]  Get system MAC address.
90
91    Open Connection And Login
92
93    # Sample output of "ip addr | grep ether":
94    # link/ether xx.xx.xx.xx.xx.xx brd ff:ff:ff:ff:ff:ff
95
96    ${cmd_output}=  Execute Command On BMC  /sbin/ip addr | grep ether
97
98    # Split the line and return MAC address.
99    # Split list data:
100    # link/ether | xx:xx:xx:xx:xx:xx | brd | ff:ff:ff:ff:ff:ff
101
102    @{words}=  Split String  ${cmd_output}
103
104    [Return]  ${words[1]}
105
106Get BMC Hostname
107    [Documentation]  Get BMC hostname.
108
109    # Sample output of  "hostnamectl":
110    #   Static hostname: xxyyxxyyxx
111    #         Icon name: computer
112    #        Machine ID: 6939927dc0db409ea09289d5b56eef08
113    #           Boot ID: bb806955fd904d47b6aa4bc7c34df482
114    #  Operating System: Phosphor OpenBMC (xxx xx xx) v1.xx.x-xx
115    #            Kernel: Linux 4.10.17-d6ae40dc4c4dff3265cc254d404ed6b03fcc2206
116    #      Architecture: arm
117
118    ${output}=  Execute Command on BMC  hostnamectl | grep hostname
119
120    [Return]  ${output}
121
122Get List Of IP Address Via REST
123    [Documentation]  Get list of IP address via REST.
124    [Arguments]  @{ip_uri_list}
125
126    # Description of argument(s):
127    # ip_uri_list  List of IP objects.
128    # Example:
129    #   "data": [
130    #     "/xyz/openbmc_project/network/eth0/ipv4/e9767624",
131    #     "/xyz/openbmc_project/network/eth0/ipv4/31f4ce8b"
132    #   ],
133
134    ${ip_list}=  Create List
135
136    : FOR  ${ip_uri}  IN  @{ip_uri_list}
137    \  ${ip_addr}=  Read Attribute  ${ip_uri}  Address
138    \  Append To List  ${ip_list}  ${ip_addr}
139
140    [Return]  @{ip_list}
141
142Delete IP And Object
143    [Documentation]  Delete IP and object.
144    [Arguments]  ${ip_addr}  @{ip_uri_list}
145
146    # Description of argument(s):
147    # ip_addr      IP address to be deleted.
148    # ip_uri_list  List of IP object URIs.
149
150    # Find IP object having this IP address.
151
152    : FOR  ${ip_uri}  IN  @{ip_uri_list}
153    \  ${ip_addr1}=  Read Attribute  ${ip_uri}  Address
154    \  Run Keyword If  '${ip_addr}' == '${ip_addr1}'  Exit For Loop
155
156    # If the given IP address is not configured, return.
157    # Otherwise, delete the IP and object.
158
159    Run Keyword And Return If  '${ip_addr}' != '${ip_addr1}'
160    ...  Pass Execution  IP address to be deleted is not configured.
161
162    Run Keyword And Ignore Error  OpenBMC Delete Request  ${ip_uri}
163
164    # After any modification on network interface, BMC restarts network
165    # module, wait until it is reachable.
166
167    Wait For Host To Ping  ${OPENBMC_HOST}  ${NETWORK_RETRY_TIME}
168    ...  ${NETWORK_TIMEOUT}
169
170    # Verify whether deleted IP address is removed from BMC system.
171
172    ${ip_data}=  Get BMC IP Info
173    Should Not Contain Match  ${ip_data}  ${ip_addr}*
174    ...  msg=IP address not deleted.
175
176Get First Non Pingable IP From Subnet
177    [Documentation]  Find first non-pingable IP from the subnet and return it.
178    [Arguments]  ${host}=${OPENBMC_HOST}
179
180    # Description of argument(s):
181    # host  Any valid host name or IP address
182    #       (e.g. "machine1" or "9.xx.xx.31").
183
184    # Non-pingable IP is unused IP address in the subnet.
185    ${host_name}  ${ip_addr}=  Get Host Name IP
186
187    # Split IP address into network part and host part.
188    # IP address will have 4 octets xx.xx.xx.xx.
189    # Sample output after split:
190    # split_ip  [xx.xx.xx, xx]
191
192    ${split_ip}=  Split String From Right  ${ip_addr}  .  1
193    # First element in list is Network part.
194    ${network_part}=  Get From List  ${split_ip}  0
195
196    : FOR  ${octet4}  IN RANGE  1  255
197    \  ${new_ip}=  Catenate  ${network_part}.${octet4}
198    \  ${status}=  Run Keyword And Return Status  Ping Host  ${new_ip}
199    # If IP is non-pingable, return it.
200    \  Return From Keyword If  '${status}' == 'False'  ${new_ip}
201
202    Fail  msg=No non-pingable IP could be found in subnet ${network_part}.
203
204
205Validate MAC On BMC
206    [Documentation]  Validate MAC on BMC.
207    [Arguments]  ${mac_addr}
208
209    # Description of argument(s):
210    # mac_addr  MAC address of the BMC.
211
212    ${system_mac}=  Get BMC MAC Address
213
214    ${status}=  Compare MAC Address  ${system_mac}  ${mac_addr}
215    Should Be True  ${status}
216    ...  msg=MAC address ${system_mac} does not match ${mac_addr}.
217
218