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    # Example output:
97    # root@witherspoon:~# ls -lh /tmp/flashimg
98    # -rwxr-xr-x    1 root     root       32.0M Jun 29 01:12 /tmp/flashimg
99    BMC Execute Command  rm -f /tmp/flashimg
100    scp.Put File  ${image_file_path}  ${targ_file_path}
101
102    ${file_size}  ${stderr}  ${rc}=  BMC Execute Command
103    ...  ls -lh ${targ_file_path}
104    ${status}=  Run Keyword And Return Status
105    ...  Should Contain  ${file_size}  32.0M  msg=Incomplete file transfer.
106    [return]  ${status}
107
108
109Check If File Exist
110    [Documentation]  Verify that the file exists on this machine.
111    [Arguments]  ${filepath}
112
113    # Description of argument(s):
114    # filepath  The path of the file whose existence is to be checked.
115
116    Log   \n PATH: ${filepath}
117    OperatingSystem.File Should Exist  ${filepath}
118    ...    msg=${filepath} doesn't exist [ ERROR ]
119
120    Set Global Variable   ${FILE_PATH}  ${filepath}
121
122
123System Readiness Test
124    [Documentation]  Verify ping and REST authenticatec for the target
125    ...              system.
126
127    ${l_status}=  Run Keyword and Return Status
128    ...  Verify Ping and REST Authentication
129    Run Keyword If  '${l_status}' == '${False}'
130    ...  Fail  msg=System not in ideal state to use [ERROR]
131
132
133Validate BMC Version
134    [Documentation]  Get BMC version from /etc/os-release and compare.
135    [Arguments]  ${version}
136
137    # Description of argument(s):
138    # version  Software version (e.g. "v1.99.8-41-g86a4abc").
139
140    Open Connection And Log In
141    ${cmd}=  Set Variable  grep ^VERSION_ID= /etc/os-release | cut -f 2 -d '='
142    ${output}  ${stderr}  ${rc}=  BMC Execute Command  ${cmd}
143    Should Be Equal As Strings  ${version}  ${output[1:-1]}
144
145
146Trigger Warm Reset via Reboot
147    [Documentation]    Execute reboot command on the remote BMC and
148    ...                returns immediately. This keyword "Start Command"
149    ...                returns nothing and does not wait for the command
150    ...                execution to be finished.
151
152    Open Connection And Log In
153
154    Start Command   /sbin/reboot
155
156
157Set Policy Setting
158    [Documentation]   Set the given test policy
159    [Arguments]   ${policy}
160
161    # Description of argument(s):
162    # policy  Policy value to set (e.g. ${RESTORE_LAST_STATE},
163    #         ${ALWAYS_POWER_ON}, or ${ALWAYS_POWER_OFF}).
164
165    ${valueDict}=  Create Dictionary  data=${policy}
166    Write Attribute  ${HOST_SETTING}  power_policy  data=${valueDict}
167    ${currentPolicy}=  Read Attribute  ${HOST_SETTING}  power_policy
168    Should Be Equal  ${currentPolicy}  ${policy}
169