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