Update redfish rsp code for cert update/install

Changes:
    - Updated redfish response code to expect either 200 or 204
      while updating or installing certificate via redfish.
    - Both client / server certificate response code updated.

Tested:
     - Ran All The Scripts Successfully

Change-Id: Iae3c9c68b433e78c7c51a8891c0df1ac084b8595
Signed-off-by: ganesanb <ganesanb@ami.com>
diff --git a/lib/certificate_utils.robot b/lib/certificate_utils.robot
index 60dcb27..e8faf5d 100755
--- a/lib/certificate_utils.robot
+++ b/lib/certificate_utils.robot
@@ -235,7 +235,7 @@
     ${payload}=  Create Dictionary  CertificateString=${file_data}
     ...  CertificateType=PEM  CertificateUri=${certificate_dict}
 
-    ${expected_resp}=  Set Variable If  '${expected_status}' == 'ok'  ${HTTP_OK}
+    ${expected_resp}=  Set Variable If  '${expected_status}' == 'ok'  ${HTTP_OK}, ${HTTP_NO_CONTENT}
     ...  '${expected_status}' == 'error'  ${HTTP_NOT_FOUND}, ${HTTP_INTERNAL_SERVER_ERROR}
     ${resp}=  redfish.Post  /redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate
     ...  body=${payload}  valid_status_codes=[${expected_resp}]
diff --git a/lib/dmtf_redfishtool_utils.robot b/lib/dmtf_redfishtool_utils.robot
index 936e3d1..b8df802 100644
--- a/lib/dmtf_redfishtool_utils.robot
+++ b/lib/dmtf_redfishtool_utils.robot
@@ -22,11 +22,10 @@
     # expected_error  Expected error optionally provided in testcase (e.g. 401 /
     #                 authentication error, etc. ).
 
-    ${rc}  ${cmd_output}=  Run and Return RC and Output  ${cmd_args} GET ${uri}
-    Run Keyword If  ${rc} == 0
-    ...    Should Be True  ${expected_error} == 200
-    ...  ELSE
-    ...    Is HTTP error Expected  ${cmd_output}  ${expected_error}
+    ${cmd}=  Catenate  ${cmd_args} GET ${uri}
+    Log  ${cmd}
+    ${rc}  ${cmd_output}=  Run and Return RC and Output  ${cmd}
+    Run Keyword If  ${rc} != 0  Is HTTP error Expected  ${cmd_output}  ${expected_error}
 
     [Return]  ${cmd_output}
 
@@ -42,11 +41,10 @@
     # expected_error  Expected error optionally provided in testcase (e.g. 401 /
     #                 authentication error, etc. ).
 
-    ${rc}  ${cmd_output}=  Run and Return RC and Output  ${cmd_args} PATCH ${uri} --data=${payload}
-    Run Keyword If  ${rc} == 0
-    ...    Should Be True  ${expected_error} == 200
-    ...  ELSE
-    ...    Is HTTP error Expected  ${cmd_output}  ${expected_error}
+    ${cmd}=  Catenate  ${cmd_args} PATCH ${uri} --data=${payload}
+    Log  ${cmd}
+    ${rc}  ${cmd_output}=  Run and Return RC and Output  ${cmd}
+    Run Keyword If  ${rc} != 0  Is HTTP error Expected  ${cmd_output}  ${expected_error}
 
     [Return]  ${cmd_output}
 
@@ -62,12 +60,11 @@
     # cmd_args        Commandline arguments.
     # expected_error  Expected error optionally provided in testcase (e.g. 401 /
     #                 authentication error, etc. ).
-
-    ${rc}  ${cmd_output}=  Run and Return RC and Output  ${cmd_args} POST ${uri} --data=${payload}
-    Run Keyword If  ${rc} == 0
-    ...    Should Be True  ${expected_error} == 200
-    ...  ELSE
-    ...    Is HTTP error Expected  ${cmd_output}  ${expected_error}
+    
+    ${cmd}=  Catenate  ${cmd_args} POST ${uri} --data=${payload}
+    Log  ${cmd}
+    ${rc}  ${cmd_output}=  Run and Return RC and Output  ${cmd}
+    Run Keyword If  ${rc} != 0  Is HTTP error Expected  ${cmd_output}  ${expected_error}
 
     [Return]  ${cmd_output}
 
@@ -82,11 +79,10 @@
     # expected_error  Expected error optionally provided in testcase (e.g. 401 /
     #                 authentication error, etc. ).
 
-    ${rc}  ${cmd_output}=  Run and Return RC and Output  ${cmd_args} DELETE ${uri}
-    Run Keyword If  ${rc} == 0
-    ...    Should Be True  ${expected_error} == 200
-    ...  ELSE
-    ...    Is HTTP error Expected  ${cmd_output}  ${expected_error}
+    ${cmd}=  Catenate  ${cmd_args} DELETE ${uri}
+    Log  ${cmd}
+    ${rc}  ${cmd_output}=  Run and Return RC and Output  ${cmd}
+    Run Keyword If  ${rc} != 0  Is HTTP error Expected  ${cmd_output}  ${expected_error}
 
     [Return]  ${cmd_output}
 
@@ -99,7 +95,23 @@
     # cmd_output      Output of an HTTP operation.
     # error_expected  Expected error.
 
-    Should Be True  ${error_expected} != 200
+    ${cmd_rsp}=  Get Regexp Matches  ${cmd_output}  200|204
+    ${cmd_rsp_status}=  Run Keyword And Return Status  Should Not Be Empty  ${cmd_rsp}
+    Return From Keyword IF  ${cmd_rsp_status} == True
+    ${matches}=  Get Regexp Matches  ${error_expected}  200|204
+    ${rsp_status}=  Run Keyword And Return Status  Should Be Empty  ${matches}
+    Run Keyword If  ${rsp_status} == False
+    ...  Fail  msg=${cmd_output}
     @{words} =  Split String  ${error_expected}  ,
     @{errorString}=  Split String  ${cmd_output}  ${SPACE}
-    Should Contain Any  ${errorString}  @{words}
+    FOR  ${error}  IN  @{words}
+      ${status}=  Run Keyword And Return Status  Should Contain Any  ${errorString}  ${error}
+      Return From Keyword If  ${status} == True
+    END
+    ${rsp_code}=  Run Keyword If  ${status} == False  Get Regexp Matches  ${cmd_output}  [0-9][0-9][0-9]
+    ${rsp_code_status}=  Run Keyword And Return Status  Should Not Be Empty  ${rsp_code}
+    Run Keyword If  ${rsp_code_status} == True
+    ...    Fail  msg=Getting status code as ${rsp_code[0]} instead of ${error_expected}, status code mismatch.
+    ...  ELSE
+    ...    Fail  msg=${cmd_output}
+
diff --git a/redfish/dmtf_tools/test_redfishtool_certificate.robot b/redfish/dmtf_tools/test_redfishtool_certificate.robot
index 9c3a68b..f6bc373 100644
--- a/redfish/dmtf_tools/test_redfishtool_certificate.robot
+++ b/redfish/dmtf_tools/test_redfishtool_certificate.robot
@@ -148,6 +148,7 @@
 
     ${response}=  Redfishtool Post
     ...  ${payload}  /redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate
+    ...  valid_status_codes=${HTTP_OK}, ${HTTP_NO_CONTENT}
 
     Wait Until Keyword Succeeds  2 mins  15 secs  Verify Certificate Visible Via OpenSSL  ${cert_file_path}
 
@@ -223,7 +224,8 @@
     Run Keyword If  '${key_pair_algorithm}' == 'EC'  Remove From Dictionary  ${csr_dict}  KeyBitLength
     ...  ELSE IF  '${key_pair_algorithm}' == 'RSA'  Remove From Dictionary  ${csr_dict}  KeyCurveId
 
-    ${expected_resp}=  Set Variable If  '${expected_status}' == 'ok'  ${HTTP_OK}
+    ${expected_resp}=  Set Variable If
+    ...  '${expected_status}' == 'ok'     ${HTTP_OK}, ${HTTP_NO_CONTENT}
     ...  '${expected_status}' == 'error'  ${HTTP_BAD_REQUEST}
 
     ${string}=  Convert To String  ${csr_dict}
@@ -243,6 +245,7 @@
 Verify Redfishtool Install Certificate
     [Documentation]  Install and verify certificate using Redfishtool.
     [Arguments]  ${cert_type}  ${cert_format}  ${expected_status}  ${delete_cert}=${True}
+    ...  ${install_type}=install
 
     # Description of argument(s):
     # cert_type           Certificate type (e.g. "Client" or "CA").
@@ -251,10 +254,12 @@
     #                     request (i.e. "ok" or "error").
     # delete_cert         Certificate will be deleted before installing if this True.
 
-    Run Keyword If  '${cert_type}' == 'CA' and '${delete_cert}' == '${True}'
-    ...  Delete All CA Certificate Via Redfisthtool
-    ...  ELSE IF  '${cert_type}' == 'Client' and '${delete_cert}' == '${True}'
-    ...  Redfishtool Delete Certificate Via BMC CLI  ${cert_type}
+    Run Keyword If  '${cert_type}' == 'CA'
+    ...  Delete All CA Certificate Via Redfishtool  ${delete_cert}
+    ...  ELSE IF  '${cert_type}' == 'Client'
+    ...  Redfishtool Delete Certificate Via BMC CLI  ${cert_type}  ${delete_cert}
+
+    Return From Keyword If  "${install_type}" != "install" and "${file_status}" != "Not Found"
 
     ${cert_file_path}=  Generate Certificate File Via Openssl  ${cert_format}
     ${bytes}=  OperatingSystem.Get Binary File  ${cert_file_path}
@@ -278,15 +283,24 @@
 
     Run Keyword If  '${expected_status}' == 'ok'  Should Contain  ${cert_file_content}  ${bmc_cert_content}
 
-    [Return]  ${cert_id}
 
-
-Delete All CA Certificate Via Redfisthtool
+Delete All CA Certificate Via Redfishtool
     [Documentation]  Delete all CA certificate via Redfish.
+    [Arguments]  ${delete_cert}=${True}
 
     ${cmd_output}=  Redfishtool Get  /redfish/v1/Managers/${MANAGER_ID}/Truststore/Certificates
     ${cmd_output}=  Convert String to JSON  ${cmd_output}
     ${cert_list}=  Set Variable  ${cmd_output["Members"]}
+    ${uri_length}=  Get Length  ${cert_list}
+    ${file_status}=  Set Variable If
+    ...  "${uri_length}" == "0"  Not Found
+    ...  "${uri_length}" != "0"  Found
+    ${cert_id}=  Set Variable If
+    ...  "${uri_length}" != "0"  ${cert_list[-1]["@odata.id"].split("/")[-1].strip()}
+    ...  "${uri_length}" == "0"  None
+    Set Test Variable  ${cert_id}
+    Set Test Variable  ${file_status}
+    Return From Keyword If  "${file_status}" != "Found" or "${delete_cert}" != "${True}"
     FOR  ${cert}  IN  @{cert_list}
       Redfishtool Delete  ${cert["@odata.id"]}  ${root_cmd_args}
     END
@@ -294,7 +308,7 @@
 
 Redfishtool Delete Certificate Via BMC CLI
     [Documentation]  Delete certificate via BMC CLI.
-    [Arguments]  ${cert_type}
+    [Arguments]  ${cert_type}  ${delete_cert}=${True}
 
     # Description of argument(s):
     # cert_type           Certificate type (e.g. "Client" or "CA").
@@ -310,7 +324,8 @@
     ${file_status}  ${stderr}  ${rc}=  BMC Execute Command
     ...  [ -f ${certificate_file_path} ] && echo "Found" || echo "Not Found"
 
-    Return From Keyword If  "${file_status}" != "Found"
+    Set Test Variable  ${file_status}
+    Return From Keyword If  "${file_status}" != "Found" or '${delete_cert}' != "${True}"
     BMC Execute Command  rm ${certificate_file_path}
     BMC Execute Command  systemctl restart ${certificate_service}
     BMC Execute Command  systemctl daemon-reload
@@ -335,10 +350,12 @@
     Set To Dictionary  ${kwargs}  headers  ${headers}
 
     ${resp}=  POST On Session  openbmc  ${uri}  &{kwargs}  expected_status=any
-    ${cert_id}=  Set Variable If  '${resp.status_code}' == '${HTTP_OK}'  ${resp.json()["Id"]}  -1
+    ${cert_id}=  Set Variable If
+    ...  '${resp.status_code}' == '${HTTP_OK}'  ${resp.json()["Id"]}
+    ...  '${resp.status_code}' == '${HTTP_NO_CONTENT}'  ${resp.json()["Id"]}  -1
 
     Run Keyword If  '${status}' == 'ok'
-    ...  Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
+    ...  Should Contain Any  "${resp.status_code}"  ${HTTP_OK}  ${HTTP_NO_CONTENT}
     ...  ELSE IF  '${status}' == 'error'
     ...  Should Be Equal As Strings  ${resp.status_code}  ${HTTP_INTERNAL_SERVER_ERROR}
 
@@ -359,10 +376,12 @@
     #                  request (i.e. "ok" or "error").
 
     # Install certificate before replacing client or CA certificate.
-    ${cert_id}=  Run Keyword If  '${cert_type}' == 'Client'
-    ...    Verify Redfishtool Install Certificate  ${cert_type}  Valid Certificate Valid Privatekey  ok
+    Run Keyword If  '${cert_type}' == 'Client'
+    ...    Verify Redfishtool Install Certificate  ${cert_type}  ${cert_format}  ${expected_status}
+    ...    ${False}  replace
     ...  ELSE IF  '${cert_type}' == 'CA'
-    ...    Verify Redfishtool Install Certificate  ${cert_type}  Valid Certificate  ok
+    ...    Verify Redfishtool Install Certificate  ${cert_type}  ${cert_format}  ${expected_status}
+    ...    ${False}  replace
 
     ${cert_file_path}=  Generate Certificate File Via Openssl  ${cert_format}
     ${bytes}=  OperatingSystem.Get Binary File  ${cert_file_path}
@@ -380,8 +399,9 @@
     ${string}=  Replace String  ${string}  '  "
     ${payload}=  Set Variable  '${string}'
 
-    ${expected_resp}=  Set Variable If  '${expected_status}' == 'ok'  ${HTTP_OK}
-    ...  '${expected_status}' == 'error'  ${HTTP_NOT_FOUND},${HTTP_INTERNAL_SERVER_ERROR}
+    ${expected_resp}=  Set Variable If
+    ...  '${expected_status}' == 'ok'     ${HTTP_OK}, ${HTTP_NO_CONTENT}
+    ...  '${expected_status}' == 'error'  ${HTTP_BAD_REQUEST}
 
     ${response}=  Redfishtool Post
     ...  ${payload}  /redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate
diff --git a/redfish/managers/test_certificate.robot b/redfish/managers/test_certificate.robot
index 4f62eb4..e9f7fd8 100644
--- a/redfish/managers/test_certificate.robot
+++ b/redfish/managers/test_certificate.robot
@@ -130,7 +130,7 @@
     ...  CertificateType=PEM  CertificateUri=${certificate_dict}
 
     ${resp}=  redfish.Post  /redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate
-    ...  body=${payload}
+    ...  body=${payload}  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
 
     Wait Until Keyword Succeeds  2 mins  15 secs  Verify Certificate Visible Via OpenSSL  ${cert_file_path}