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    [Arguments]         ${filepath}
58    Open Connection for SCP
59    scp.Put File      ${filepath}   /tmp/flashimg
60
61
62Check If File Exist
63    [Arguments]  ${filepath}
64    Log   \n PATH: ${filepath}
65    OperatingSystem.File Should Exist  ${filepath}
66    ...    msg=${filepath} doesn't exist [ ERROR ]
67
68    Set Global Variable   ${FILE_PATH}  ${filepath}
69
70
71System Readiness Test
72    ${l_status}=   Run Keyword and Return Status
73    ...   Verify Ping and REST Authentication
74    Run Keyword If  '${l_status}' == '${False}'
75    ...   Fail  msg=System not in ideal state to use [ERROR]
76
77
78Validate BMC Version
79    [Arguments]   ${args}=post
80    # Check BMC installed version
81    Open Connection And Log In
82    ${version}   ${stderr}=    Execute Command   cat /etc/version
83    ...    return_stderr=True
84    Should Be Empty     ${stderr}
85    # The File name contains the version installed
86    Run Keyword If   '${args}' == 'before'
87    ...    Should not Contain  ${FILE_PATH}   ${version}
88    ...    msg=Same version already installed
89    ...    ELSE
90    ...    Should Contain      ${FILE_PATH}   ${version}
91    ...    msg=Code update Failed
92
93
94Trigger Warm Reset via Reboot
95    [Documentation]    Execute reboot command on the remote BMC and
96    ...                returns immediately. This keyword "Start Command"
97    ...                returns nothing and does not wait for the command
98    ...                execution to be finished.
99    Open Connection And Log In
100
101    Start Command   /sbin/reboot
102
103Set Policy Setting
104    [Documentation]   Set the given test policy
105    [Arguments]   ${policy}
106
107    ${valueDict}=     create dictionary  data=${policy}
108    Write Attribute    ${HOST_SETTING}    power_policy   data=${valueDict}
109    ${currentPolicy}=  Read Attribute     ${HOST_SETTING}   power_policy
110    Should Be Equal    ${currentPolicy}   ${policy}
111