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