xref: /openbmc/openbmc-test-automation/lib/bmc_date_and_time_utils.robot (revision 286e2f07a1261ad043902a6db05d39429a31a1d4)
1*** Settings ***
2Documentation  This module provides general keywords for date time and ntp.
3
4Resource                     ../../lib/resource.robot
5Resource                     ../../lib/bmc_redfish_resource.robot
6Resource                     ../../lib/common_utils.robot
7Resource                     ../../lib/openbmc_ffdc.robot
8Resource                     ../../lib/utils.robot
9Resource                     ../../lib/rest_client.robot
10Library                      ../../lib/gen_robot_valid.py
11
12*** Variables ***
13
14${year_without_ntp}          1970
15
16*** Keywords ***
17
18
19Redfish Get DateTime
20    [Documentation]  Returns BMC Datetime value from Redfish.
21
22    ${date_time}=  Redfish.Get Attribute  ${REDFISH_BASE_URI}Managers/${MANAGER_ID}  DateTime
23    RETURN  ${date_time}
24
25
26Redfish Set DateTime
27    [Documentation]  Set DateTime using Redfish.
28    [Arguments]  ${date_time}=${EMPTY}  ${request_type}=valid
29    # Description of argument(s):
30    # date_time                     New time to set for BMC (eg.
31    #                               "2019-06-30 09:21:28"). If this value is
32    #                               empty, it will be set to the UTC current
33    #                               date time of the local system.
34    # request_type                  By default user request is valid.
35    #                               User can pass invalid to identify the user
36    #                               date time input will result in failure as
37    #                               expected.
38
39    # Assign default value of UTC current date time if date_time is empty.
40    ${current_date_time}=  Get Current Date  time_zone=UTC
41    ${date_time}=  Set Variable If  '${date_time}' == '${EMPTY}'
42    ...  ${current_date_time}  ${date_time}
43
44    # Patch date_time based on type of ${request_type}.
45    IF  '${request_type}' == 'valid'
46        ${date_time}=  Convert Date  ${date_time}  result_format=%Y-%m-%dT%H:%M:%S+00:00
47        Wait Until Keyword Succeeds  1min  5sec
48        ...  Redfish.Patch  ${REDFISH_BASE_URI}Managers/${MANAGER_ID}
49        ...  body={'DateTime': '${date_time}'}
50        ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
51    ELSE
52        Wait Until Keyword Succeeds  1min  5sec
53        ...  Redfish.Patch  ${REDFISH_BASE_URI}Managers/${MANAGER_ID}
54        ...  body={'DateTime': '${date_time}'}
55        ...  valid_status_codes=[${HTTP_BAD_REQUEST}]
56    END
57
58
59Set Time To Manual Mode
60    [Documentation]  Set date time to manual mode via Redfish.
61
62    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={'NTP':{'ProtocolEnabled': ${False}}}
63    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
64
65
66Set BMC Date And Verify
67    [Documentation]  Set BMC Date Time at a given host state and verify.
68    [Arguments]  ${host_state}
69    # Description of argument(s):
70    # host_state  Host state at which date time will be updated for verification
71    #             (eg. on, off).
72
73    IF  '${host_state}' == 'on'
74        Redfish Power On  stack_mode=skip
75    ELSE
76        Redfish Power off  stack_mode=skip
77    END
78
79    ${current_date}=  Get Current Date  time_zone=UTC
80    ${new_value}=  Subtract Time From Date  ${current_date}  1 day
81    Redfish Set DateTime  ${new_value}
82    ${current_value}=  Redfish Get DateTime
83    ${time_diff}=  Subtract Date From Date  ${current_value}  ${new_value}
84    Should Be True  '${time_diff}'<='3'
85
86
87Set NTP state
88    [Documentation]  Set NTP service inactive.
89    [Arguments]  ${state}
90
91    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={'NTP':{'ProtocolEnabled': ${state}}}
92    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
93
94
95Get NTP Initial Status
96    [Documentation]  Get NTP service Status.
97
98    ${original_ntp}=  Redfish.Get Attribute  ${REDFISH_NW_PROTOCOL_URI}  NTP
99    Set Suite Variable  ${original_ntp}
100
101
102Restore NTP Status
103    [Documentation]  Restore NTP Status.
104
105    IF  '${original_ntp["ProtocolEnabled"]}' == 'True'
106        Set NTP state  ${TRUE}
107    ELSE
108        Set NTP state  ${FALSE}
109    END
110
111
112Verify NTP Servers Are Populated
113    [Documentation]  Redfish GET request /redfish/v1/Managers/${MANAGER_ID}/NetworkProtocol response
114    ...              and verify if NTP servers are populated.
115
116    ${network_protocol}=  Redfish.Get Properties  ${REDFISH_NW_PROTOCOL_URI}
117    Should Contain  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_1}
118    ...  msg=NTP server value ${ntp_server_1} not stored.
119    Should Contain  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_2}
120    ...  msg=NTP server value ${ntp_server_2} not stored.
121
122
123Verify System Time Sync Status
124    [Documentation]  Verify the status of service systemd-timesyncd matches the NTP protocol enabled state.
125    [Arguments]  ${expected_sync_status}=${True}
126
127    # Description of argument(s):
128    # expected_sync_status  expected status at which NTP protocol enabled will be updated for verification
129    #                       (eg. True, False).
130
131    ${resp}=  BMC Execute Command
132    ...  systemctl status systemd-timesyncd
133    ...  ignore_err=${1}
134    ${sync_status}=  Get Lines Matching Regexp  ${resp[0]}  .*Active.*
135    IF  ${expected_sync_status}==${True}
136        Should Contain  ${sync_status}  active (running)
137    END
138    IF  ${expected_sync_status}==${False}
139        Should Contain  ${sync_status}  inactive (dead)
140    END
141
142
143Enable NTP And Add NTP Address
144    [Documentation]  Enable NTP Protocol and Add NTP Address.
145
146    Set NTP state  ${TRUE}
147
148    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={'NTP':{'NTPServers': ${NTP_SERVER_ADDRESSES}}}
149    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
150
151    Wait Until Keyword Succeeds  1 min  10 sec  Check Date And Time Was Changed
152
153
154Check Date And Time Was Changed
155    [Documentation]  Verify date was current date and time.
156
157    ${new_date_time}=  CLI Get BMC DateTime
158    Should Not Contain  ${new_date_time}  ${year_without_ntp}
159
160
161Restore NTP Mode
162    [Documentation]  Restore the original NTP mode.
163
164    Return From Keyword If  &{original_ntp} == &{EMPTY}
165    Print Timen  Restore NTP Mode.
166    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}
167    ...  body={'NTP':{'ProtocolEnabled': ${original_ntp["ProtocolEnabled"]}}}
168    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
169