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
8
9Test Setup                   Run Keywords  Printn  AND  redfish.Login
10Test Teardown                Test Teardown Execution
11
12*** Variables ***
13${max_time_diff_in_seconds}  6
14${invalid_datetime}          "2019-04-251T12:24:46+00:00"
15${ntp_server_1}              "9.9.9.9"
16${ntp_server_2}              "2.2.3.3"
17
18*** Test Cases ***
19
20Verify Redfish BMC Time
21    [Documentation]  Verify that date/time obtained via redfish matches
22    ...  date/time obtained via BMC command line.
23    [Tags]  Verify_Redfish_BMC_Time
24
25    ${redfish_date_time}=  Redfish Get DateTime
26    ${cli_date_time}=  CLI Get BMC DateTime
27    ${time_diff}=  Subtract Date From Date  ${cli_date_time}
28    ...  ${redfish_date_time}
29    ${time_diff}=  Evaluate  abs(${time_diff})
30    Rprint Vars  redfish_date_time  cli_date_time  time_diff
31    Should Be True  ${time_diff} < ${max_time_diff_in_seconds}
32    ...  The difference between Redfish time and CLI time exceeds the allowed time difference.
33
34
35Verify Set Time Using Redfish
36    [Documentation]  Verify set time using redfish API.
37    [Tags]  Verify_Set_Time_Using_Redfish
38
39    ${old_bmc_time}=  CLI Get BMC DateTime
40    # Add 3 days to current date.
41    ${new_bmc_time}=  Add Time to Date  ${old_bmc_time}  3 Days
42    Redfish Set DateTime  ${new_bmc_time}
43    ${cli_bmc_time}=  CLI Get BMC DateTime
44    ${time_diff}=  Subtract Date From Date  ${cli_bmc_time}
45    ...  ${new_bmc_time}
46    ${time_diff}=  Evaluate  abs(${time_diff})
47    Rprint Vars   old_bmc_time  new_bmc_time  cli_bmc_time  time_diff  max_time_diff_in_seconds
48    Should Be True  ${time_diff} < ${max_time_diff_in_seconds}
49    ...  The difference between Redfish time and CLI time exceeds the allowed time difference.
50    # Setting back to old bmc time.
51    Redfish Set DateTime  ${old_bmc_time}
52
53
54Verify Set DateTime With Invalid Data Using Redfish
55    [Documentation]  Verify error while setting invalid DateTime using Redfish.
56    [Tags]  Verify_Set_DateTime_With_Invalid_Data_Using_Redfish
57
58    Redfish Set DateTime  ${invalid_datetime}  valid_status_codes=[${HTTP_BAD_REQUEST}]
59
60
61Verify NTP Server Set
62    [Documentation]  Verify NTP server set.
63    [Tags]  Verify_NTP_Server_Set
64
65    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={'NTPServers': ['${ntp_server_1}', '${ntp_server_2}']}
66    ${network_protocol}=  Redfish.Get Properties  ${REDFISH_NW_PROTOCOL_URI}
67    Should Contain  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_1}
68    ...  msg=NTP server value ${ntp_server_1} not stored.
69    Should Contain  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_2}
70    ...  msg=NTP server value ${ntp_server_2} not stored.
71
72
73Verify NTP Server Value Not Duplicated
74    [Documentation]  Verify NTP servers value not same for both primary and secondary server.
75    [Tags]  Verify_NTP_Server_Value_Not_Duplicated
76
77    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={'NTPServers': ['${ntp_server_1}', '${ntp_server_1}']}
78    ${network_protocol}=  Redfish.Get Properties  ${REDFISH_NW_PROTOCOL_URI}
79    Should Contain X Times  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_1}  1
80    ...  msg=NTP primary and secondary server values should not be same.
81
82
83Verify NTP Server Setting Persist After BMC Reboot
84    [Documentation]  Verify NTP server setting persist after BMC reboot.
85    [Tags]  Verify_NTP_Server_Setting_Persist_After_BMC_Reboot
86
87    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={'NTPServers': ['${ntp_server_1}', '${ntp_server_2}']}
88    Redfish OBMC Reboot (off)
89    Redfish.Login
90    ${network_protocol}=  Redfish.Get Properties  ${REDFISH_NW_PROTOCOL_URI}
91    Should Contain  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_1}
92    ...  msg=NTP server value ${ntp_server_1} not stored.
93    Should Contain  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_2}
94    ...  msg=NTP server value ${ntp_server_2} not stored.
95    Redfish.Logout
96
97
98*** Keywords ***
99
100Test Teardown Execution
101    [Documentation]  Do the post test teardown.
102
103    FFDC On Test Case Fail
104    redfish.Logout
105
106
107Redfish Get DateTime
108    [Documentation]  Returns BMC Datetime value from Redfish.
109
110    ${date_time}=  Redfish.Get Attribute  ${REDFISH_BASE_URI}Managers/bmc  DateTime
111    [Return]  ${date_time}
112
113
114Redfish Set DateTime
115    [Documentation]  Set DateTime using Redfish.
116    [Arguments]  ${date_time}  &{kwargs}
117    # Description of argument(s):
118    # date_time                     New time to set for BMC (eg.
119    #                               "2019-06-30 09:21:28").
120    # kwargs                        Additional parms to be passed directly to
121    #                               th Redfish.Patch function.  A good use for
122    #                               this is when testing a bad date-time, the
123    #                               caller can specify
124    #                               valid_status_codes=[${HTTP_BAD_REQUEST}].
125
126    Redfish.Patch  ${REDFISH_BASE_URI}Managers/bmc  body={'DateTime': '${date_time}'}
127    ...  &{kwargs}
128