1*** Settings ***
2Documentation    Test BMC manager time functionality.
3Resource                     ../../lib/resource.robot
4Resource                     ../../lib/bmc_redfish_resource.robot
5Resource                     ../../lib/common_utils.robot
6Resource                     ../../lib/openbmc_ffdc.robot
7Resource                     ../../lib/utils.robot
8Resource                     ../../lib/rest_client.robot
9Library                      ../../lib/gen_robot_valid.py
10
11Test Teardown                Test Teardown Execution
12Suite Setup                  Suite Setup Execution
13Suite Teardown               Suite Teardown Execution
14
15*** Variables ***
16${max_time_diff_in_seconds}  6
17# The "offset" consists of the value "26" specified for hours.  Redfish will
18# convert that to the next day + 2 hours.
19${date_time_with_offset}     2019-04-25T26:24:46+00:00
20${expected_date_time}        2019-04-26T02:24:46+00:00
21${invalid_datetime}          "2019-04-251T12:24:46+00:00"
22${ntp_server_1}              "9.9.9.9"
23${ntp_server_2}              "2.2.3.3"
24&{original_ntp}              &{EMPTY}
25
26*** Test Cases ***
27
28Verify Redfish BMC Time
29    [Documentation]  Verify that date/time obtained via redfish matches
30    ...  date/time obtained via BMC command line.
31    [Tags]  Verify_Redfish_BMC_Time
32
33    ${redfish_date_time}=  Redfish Get DateTime
34    ${cli_date_time}=  CLI Get BMC DateTime
35    ${time_diff}=  Subtract Date From Date  ${cli_date_time}
36    ...  ${redfish_date_time}
37    ${time_diff}=  Evaluate  abs(${time_diff})
38    Rprint Vars  redfish_date_time  cli_date_time  time_diff
39    Should Be True  ${time_diff} < ${max_time_diff_in_seconds}
40    ...  The difference between Redfish time and CLI time exceeds the allowed time difference.
41
42
43Verify Set Time Using Redfish
44    [Documentation]  Verify set time using redfish API.
45    [Tags]  Verify_Set_Time_Using_Redfish
46
47    Rest Set Time Owner
48
49    ${old_bmc_time}=  CLI Get BMC DateTime
50    # Add 3 days to current date.
51    ${new_bmc_time}=  Add Time to Date  ${old_bmc_time}  3 Days
52    Redfish Set DateTime  ${new_bmc_time}
53    ${cli_bmc_time}=  CLI Get BMC DateTime
54    ${time_diff}=  Subtract Date From Date  ${cli_bmc_time}
55    ...  ${new_bmc_time}
56    ${time_diff}=  Evaluate  abs(${time_diff})
57    Rprint Vars   old_bmc_time  new_bmc_time  cli_bmc_time  time_diff  max_time_diff_in_seconds
58    Should Be True  ${time_diff} < ${max_time_diff_in_seconds}
59    ...  The difference between Redfish time and CLI time exceeds the allowed time difference.
60    # Setting back to old bmc time.
61    Redfish Set DateTime  ${old_bmc_time}
62
63
64Verify Set DateTime With Offset Using Redfish
65    [Documentation]  Verify set DateTime with offset using redfish API.
66    [Tags]  Verify_Set_DateTime_With_Offset_Using_Redfish
67    [Teardown]  Run Keywords  Redfish Set DateTime  AND  FFDC On Test Case Fail
68
69    Redfish Set DateTime  ${date_time_with_offset}
70    ${cli_bmc_time}=  CLI Get BMC DateTime
71
72    ${date_time_diff}=  Subtract Date From Date  ${cli_bmc_time}
73    ...  ${expected_date_time}  exclude_millis=yes
74    ${date_time_diff}=  Convert to Integer  ${date_time_diff}
75    Rpvars  date_time_with_offset  expected_date_time  cli_bmc_time
76    ...  date_time_diff  max_time_diff_in_seconds
77    Rvalid Range  date_time_diff  0..${max_time_diff_in_seconds}
78
79
80Verify Set DateTime With Invalid Data Using Redfish
81    [Documentation]  Verify error while setting invalid DateTime using Redfish.
82    [Tags]  Verify_Set_DateTime_With_Invalid_Data_Using_Redfish
83
84    Redfish Set DateTime  ${invalid_datetime}  valid_status_codes=[${HTTP_BAD_REQUEST}]
85
86
87Verify DateTime Persists After Reboot
88    [Documentation]  Verify date persists after BMC reboot.
89    [Tags]  Verify_DateTime_Persists_After_Reboot
90
91    # Synchronize BMC date/time to local system date/time.
92    ${local_system_time}=  Get Current Date
93    Redfish Set DateTime  ${local_system_time}
94    Redfish OBMC Reboot (off)
95    Redfish.Login
96    ${bmc_time}=  CLI Get BMC DateTime
97    ${local_system_time}=  Get Current Date
98    ${time_diff}=  Subtract Date From Date  ${bmc_time}
99    ...  ${local_system_time}
100    ${time_diff}=  Evaluate  abs(${time_diff})
101    Rprint Vars   local_system_time  bmc_time  time_diff  max_time_diff_in_seconds
102    Should Be True  ${time_diff} < ${max_time_diff_in_seconds}
103    ...  The difference between Redfish time and CLI time exceeds the allowed time difference.
104
105
106Verify NTP Server Set
107    [Documentation]  Verify NTP server set.
108    [Tags]  Verify_NTP_Server_Set
109
110    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={'NTPServers': ['${ntp_server_1}', '${ntp_server_2}']}
111    ${network_protocol}=  Redfish.Get Properties  ${REDFISH_NW_PROTOCOL_URI}
112    Should Contain  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_1}
113    ...  msg=NTP server value ${ntp_server_1} not stored.
114    Should Contain  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_2}
115    ...  msg=NTP server value ${ntp_server_2} not stored.
116
117
118Verify NTP Server Value Not Duplicated
119    [Documentation]  Verify NTP servers value not same for both primary and secondary server.
120    [Tags]  Verify_NTP_Server_Value_Not_Duplicated
121
122    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={'NTPServers': ['${ntp_server_1}', '${ntp_server_1}']}
123    ${network_protocol}=  Redfish.Get Properties  ${REDFISH_NW_PROTOCOL_URI}
124    Should Contain X Times  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_1}  1
125    ...  msg=NTP primary and secondary server values should not be same.
126
127
128Verify NTP Server Setting Persist After BMC Reboot
129    [Documentation]  Verify NTP server setting persist after BMC reboot.
130    [Tags]  Verify_NTP_Server_Setting_Persist_After_BMC_Reboot
131
132    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={'NTPServers': ['${ntp_server_1}', '${ntp_server_2}']}
133    Redfish OBMC Reboot (off)
134    Redfish.Login
135    ${network_protocol}=  Redfish.Get Properties  ${REDFISH_NW_PROTOCOL_URI}
136    Should Contain  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_1}
137    ...  msg=NTP server value ${ntp_server_1} not stored.
138    Should Contain  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_2}
139    ...  msg=NTP server value ${ntp_server_2} not stored.
140
141
142Verify Enable NTP
143    [Documentation]  Verify NTP protocol mode can be enabled.
144    [Teardown]  Restore NTP Mode
145    [Tags]  Verify_Enable_NTP
146
147    ${original_ntp}=  Redfish.Get Attribute  ${REDFISH_NW_PROTOCOL_URI}  NTP
148    Set Suite Variable  ${original_ntp}
149    Rprint Vars  original_ntp
150    # The following patch command should set the ["NTP"]["ProtocolEnabled"] property to "True".
151    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={u'NTPEnabled': ${True}}
152    ${ntp}=  Redfish.Get Attribute  ${REDFISH_NW_PROTOCOL_URI}  NTP
153    Rprint Vars  ntp
154    Rvalid Value  ntp["ProtocolEnabled"]  valid_values=[True]
155
156
157*** Keywords ***
158
159
160Test Teardown Execution
161    [Documentation]  Do the post test teardown.
162
163    FFDC On Test Case Fail
164
165
166Redfish Get DateTime
167    [Documentation]  Returns BMC Datetime value from Redfish.
168
169    ${date_time}=  Redfish.Get Attribute  ${REDFISH_BASE_URI}Managers/bmc  DateTime
170    [Return]  ${date_time}
171
172
173Redfish Set DateTime
174    [Documentation]  Set DateTime using Redfish.
175    [Arguments]  ${date_time}=${EMPTY}  &{kwargs}
176    # Description of argument(s):
177    # date_time                     New time to set for BMC (eg.
178    #                               "2019-06-30 09:21:28"). If this value is
179    #                               empty, it will be set to the UTC current
180    #                               date time of the local system.
181    # kwargs                        Additional parms to be passed directly to
182    #                               th Redfish.Patch function.  A good use for
183    #                               this is when testing a bad date-time, the
184    #                               caller can specify
185    #                               valid_status_codes=[${HTTP_BAD_REQUEST}].
186
187    # Assign default value of UTC current date time if date_time is empty.
188    ${date_time}=  Run Keyword If
189    ...  '${date_time}' == '${EMPTY}'  Get Current Date  time_zone=UTC
190    ...  ELSE
191    ...  Set Variable  ${date_time}
192    Redfish.Patch  ${REDFISH_BASE_URI}Managers/bmc  body={'DateTime': '${date_time}'}
193    ...  &{kwargs}
194
195
196Rest Set Time Owner
197    [Documentation]  Set time owner of the system via REST.
198
199    # BMC_OWNER is defined in variable.py.
200    ${data}=  Create Dictionary  data=${BMC_OWNER}
201    Write Attribute  ${TIME_MANAGER_URI}owner  TimeOwner  data=${data}  verify=${TRUE}
202
203
204Restore NTP Mode
205    [Documentation]  Restore the original NTP mode.
206
207
208    Return From Keyword If  &{original_ntp} == &{EMPTY}
209    Print Timen  Restore NTP Mode.
210    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}
211    ...  body={u'NTPEnabled': ${original_ntp["ProtocolEnabled"]}}
212
213
214Suite Setup Execution
215    [Documentation]  Do the suite level setup.
216
217    Printn
218    Redfish.Login
219    Rest Set Time Owner
220
221Suite Teardown Execution
222    [Documentation]  Do the suite level teardown.
223
224    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}
225    ...  body={'NTPServers': ['${EMPTY}', '${EMPTY}']}
226    Rest Set Time Owner
227    Redfish.Logout
228