1*** Settings ***
2Documentation    Code update utility
3
4Resource                 ../../lib/rest_client.robot
5Resource                 ../../lib/connection_client.robot
6Resource                 ../../lib/utils.robot
7Library                  OperatingSystem
8
9*** Variables ***
10
11# Fix old org path locally for non-witherspoon system.
12${ORG_OPENBMC_BASE_URI}  /org/openbmc/
13${ORG_CONTROL_FLASH}     ${ORG_OPENBMC_BASE_URI}control/flash/
14${BMC_UPD_METHOD}        ${ORG_CONTROL_FLASH}bmc/action/update
15${BMC_PREP_METHOD}       ${ORG_CONTROL_FLASH}bmc/action/PrepareForUpdate
16${BMC_UPD_ATTR}          ${ORG_CONTROL_FLASH}bmc
17${HOST_SETTING}          ${ORG_OPENBMC_BASE_URI}settings/host0
18
19*** Keywords ***
20
21Preserve BMC Network Setting
22    [Documentation]   Preserve Network setting
23
24    ${policy}=  Set Variable  ${1}
25    ${value}=  Create Dictionary  data=${policy}
26    Write Attribute  ${BMC_UPD_ATTR}  preserve_network_settings  data=${value}
27    ${data}=  Read Properties  ${BMC_UPD_ATTR}
28    Should Be Equal As Strings
29    ...  ${data['preserve_network_settings']}   ${True}
30    ...  msg=False indicates network is not preserved.
31
32
33Activate BMC Flash Image
34    [Documentation]   Activate and verify the update status.
35    ...               The status could be either one of these:
36    ...               'Deferred for mounted filesystem. reboot BMC to apply.'
37    ...               'Image ready to apply.'
38
39    @{img_path}=  Create List  /tmp/flashimg
40    ${data}=  Create Dictionary  data=@{img_path}
41    ${resp}=  OpenBMC Post Request  ${BMC_UPD_METHOD}  data=${data}
42    ...  timeout=${30}
43    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
44
45    ${data}=  Read Properties  ${BMC_UPD_ATTR}
46    Should Be Equal As Strings  ${data["filename"]}  /tmp/flashimg
47    Should Contain  ${data['status']}  to apply
48
49
50Prepare For Update
51    [Documentation]   Switch to update mode in progress. This method calls
52    ...               the Abort method to remove the pending update if there
53    ...               is any before code activation.
54
55    ${data}=  Create Dictionary  data=@{EMPTY}
56    ${resp}=  Openbmc Post Request  ${BMC_PREP_METHOD}  data=${data}
57
58    # Update method will reset the BMC, adding delay for reboot to
59    # come into force.
60    Sleep  10s
61
62
63SCP Tar Image File to BMC
64    [Documentation]  Copy BMC tar image to BMC.
65    [Arguments]  ${image_file_path}
66
67    # Description of argument(s):
68    # image_file_path  Downloaded BMC tar file image path.
69
70    Open Connection for SCP
71    Open Connection And Log In
72    Loop SCP Retry  ${image_file_path}
73
74
75Loop SCP Retry
76    [Documentation]  Try transferring the file 4 times.
77    [Arguments]  ${image_file_path}
78
79    # Description of argument(s):
80    # image_file_path  Downloaded BMC tar file image path.
81
82    : FOR  ${index}  IN RANGE  0  4
83    \  ${status}=  Retry SCP  ${image_file_path}
84    \  Exit For Loop If  '${status}' == '${True}'
85
86
87Retry SCP
88    [Documentation]  Delete the incomplete file and scp file.
89    [Arguments]  ${image_file_path}
90
91    # Description of argument(s):
92    # image_file_path  Downloaded BMC tar file image path.
93
94    ${targ_file_path}=  Set Variable  /tmp/flashimg
95
96    # TODO: Need to remove this when new code update in place.
97    # Example output:
98    # root@witherspoon:~# ls -lh /tmp/flashimg
99    # -rwxr-xr-x    1 root     root       32.0M Jun 29 01:12 /tmp/flashimg
100    BMC Execute Command  rm -f /tmp/flashimg
101    scp.Put File  ${image_file_path}  ${targ_file_path}
102
103    ${file_size}  ${stderr}  ${rc}=  BMC Execute Command
104    ...  ls -lh ${targ_file_path}
105    ${status}=  Run Keyword And Return Status
106    ...  Should Contain  ${file_size}  32.0M  msg=Incomplete file transfer.
107    [return]  ${status}
108
109
110Check If File Exist
111    [Documentation]  Verify that the file exists on this machine.
112    [Arguments]  ${filepath}
113
114    # Description of argument(s):
115    # filepath  The path of the file whose existence is to be checked.
116
117    Log   \n PATH: ${filepath}
118    OperatingSystem.File Should Exist  ${filepath}
119    ...    msg=${filepath} doesn't exist [ ERROR ]
120
121    Set Global Variable   ${FILE_PATH}  ${filepath}
122
123
124System Readiness Test
125    [Documentation]  Verify ping and REST authenticatec for the target
126    ...              system.
127
128    ${l_status}=  Run Keyword and Return Status
129    ...  Verify Ping and REST Authentication
130    Run Keyword If  '${l_status}' == '${False}'
131    ...  Fail  msg=System not in ideal state to use [ERROR]
132
133
134Validate BMC Version
135    [Documentation]  Get BMC version from /etc/os-release and compare.
136    [Arguments]  ${version}
137
138    # Description of argument(s):
139    # version  Software version (e.g. "v1.99.8-41-g86a4abc").
140
141    Open Connection And Log In
142    ${cmd}=  Set Variable  grep ^VERSION_ID= /etc/os-release | cut -f 2 -d '='
143    ${output}  ${stderr}  ${rc}=  BMC Execute Command  ${cmd}
144    Should Be Equal As Strings  ${version}  ${output[1:-1]}
145
146
147Trigger Warm Reset via Reboot
148    [Documentation]    Execute reboot command on the remote BMC and
149    ...                returns immediately. This keyword "Start Command"
150    ...                returns nothing and does not wait for the command
151    ...                execution to be finished.
152
153    Open Connection And Log In
154
155    Start Command   /sbin/reboot
156
157
158Set Policy Setting
159    [Documentation]   Set the given test policy
160    [Arguments]   ${policy}
161
162    # Description of argument(s):
163    # policy  Policy value to set (e.g. ${RESTORE_LAST_STATE},
164    #         ${ALWAYS_POWER_ON}, or ${ALWAYS_POWER_OFF}).
165
166    ${valueDict}=  Create Dictionary  data=${policy}
167    Write Attribute  ${HOST_SETTING}  power_policy  data=${valueDict}
168    ${currentPolicy}=  Read Attribute  ${HOST_SETTING}  power_policy
169    Should Be Equal  ${currentPolicy}  ${policy}
170