Robustness enhancements to test_ipmi_user

- Includes refactoring code from test_ipmi_user to ipmi_client,
  which will allow greater reuse.

- Ipmi_client changes:
  - Enhancements to keyword: Create Random IPMI User
    -- invokes Find Free User ID : to prevent clobbering pre-existing
       IPMI userIDs;
    -- invokes Wait And Confirm New User Entry : so that we have
       confirmation before returning.
  - In support of this:
    - Add necessary Variables;
    - Pull over keywords from test_ipmi_user :
      -- Find Free User Id
      -- Check Enabled User Count
      -- Wait And Confirm New User Entry
      -- Verify IPMI Username Visible.

- Test_ipmi_user changes:
  - Remove unused variable (moved to ipmi_client);
  - Verify IPMI User Summary : remove now redundant
    Wait And Confirm New User Entry, since incorporated into
    Create Random IPMI User;
  - Test IPMI User Privilege : invoke the new and enhanced
    Create Random IPMI User , to prevent clobbering pre-existing
    IPMI userIDs and leverage the built-in confirmation;
  - Remove the keywords that are being pulled over to ipmi_client.

  Additional test_ipmi_user changes for robustness,
  consistent with use already elsewhere in this suite:
  - Verify Administrator And User Privilege For Different Channels :
    Sleep 5s delay after Set Channel Access to allow privileges to set;
  - Verify Operator And User Privilege For Different Channels :
    Sleep 5s delay after Set Channel Access to allow privileges to set.

Tested: on x86 platforms.

Change-Id: I10548d5ad7f85542ab5b6b165dddb01089da0661
Signed-off-by: Ruud A. Haring <ruud@us.ibm.com>
diff --git a/lib/ipmi_client.robot b/lib/ipmi_client.robot
index 984b187..2088df3 100755
--- a/lib/ipmi_client.robot
+++ b/lib/ipmi_client.robot
@@ -22,7 +22,9 @@
 ${IPMI_INBAND_CMD}=    ipmitool -C ${IPMI_CIPHER_LEVEL} -N ${IPMI_TIMEOUT} -p ${IPMI_PORT}
 ${HOST}=               -H
 ${RAW}=                raw
-${IPMITOOL_PATH}  /tmp/ipmitool
+${IPMITOOL_PATH}       /tmp/ipmitool
+${expected_max_ids}    15
+${empty_name_pattern}  ^User Name\\s.*\\s:\\s$
 
 *** Keywords ***
 
@@ -430,11 +432,64 @@
     [Documentation]  Create IPMI user with random username and userid and return those fields.
 
     ${random_username}=  Generate Random String  8  [LETTERS]
-    ${random_userid}=  Evaluate  random.randint(2, 15)  modules=random
+    ${random_userid}=  Find Free User Id
     IPMI Create User  ${random_userid}  ${random_username}
+    Wait And Confirm New User Entry  ${random_username}
     [Return]  ${random_userid}  ${random_username}
 
 
+Find Free User Id
+    [Documentation]  Find a userid that is not being used.
+
+    Check Enabled User Count
+    FOR    ${num}    IN RANGE    300
+        ${random_userid}=  Evaluate  random.randint(1, ${expected_max_ids})  modules=random
+        ${access}=  Run IPMI Standard Command  channel getaccess ${CHANNEL_NUMBER} ${random_userid}
+
+        ${name_line}=  Get Lines Containing String  ${access}  User Name
+        Log To Console  For ID ${random_userid}: ${name_line}
+        ${is_empty}=  Run Keyword And Return Status
+        ...  Should Match Regexp  ${name_line}  ${empty_name_pattern}
+
+        Exit For Loop If  ${is_empty} == ${True}
+    END
+    [Return]  ${random_userid}
+
+
+Check Enabled User Count
+    [Documentation]  Ensure that there are available user IDs.
+
+    # Check for the enabled user count
+    ${resp}=  Run IPMI Standard Command  user summary ${CHANNEL_NUMBER}
+    ${enabled_user_count}=
+    ...  Get Lines Containing String  ${resp}  Enabled User Count
+
+    Should not contain  ${enabled_user_count}  ${expected_max_ids}
+    ...  msg=IPMI has reached maximum user count
+
+
+Wait And Confirm New User Entry
+    [Documentation]  Wait in loop until new user appears with given username.
+    [Arguments]  ${username}
+
+    # Description of argument(s):
+    # username         The user name (e.g. "root", "robert", etc.).
+
+    Wait Until Keyword Succeeds  45 sec  1 sec  Verify IPMI Username Visible
+    ...  ${username}
+
+
+Verify IPMI Username Visible
+    [Documentation]  Confirm that username is present in user list.
+    [Arguments]  ${username}
+
+    # Description of argument(s):
+    # username         The user name (e.g. "root", "robert", etc.).
+
+    ${resp}=  Run IPMI Standard Command  user list
+    Should Contain  ${resp}  ${username}
+
+
 Delete Created User
     [Documentation]  Delete created IPMI user.
     [Arguments]  ${userid}