Enable test cases for host's network settings

What test cases are added
    - Added test cases to set static and dhcp setting using REST
    - Added test cases to set static and dhcp setting using IPMI
    - Added test case to clear settings using IPMI
    - Added test cases to set invalid settings using both REST and IPMI

Resolves openbmc/openbmc-test-automation#48

Change-Id: Ia9ac56ad884318de703406d070aa7985474fab05
Signed-off-by: Rahul Maheshwari <rahulmaheshwari@in.ibm.com>
diff --git a/extended/test_host_network.robot b/extended/test_host_network.robot
new file mode 100644
index 0000000..d3b9bf9
--- /dev/null
+++ b/extended/test_host_network.robot
@@ -0,0 +1,193 @@
+*** Settings ***
+
+Documentation   This testsuite is for testing network setting
+...             of host OS.
+
+Resource            ../lib/rest_client.robot
+Resource            ../lib/ipmi_client.robot
+Resource            ../lib/utils.robot
+Resource            ../lib/openbmc_ffdc.robot
+Library             ../lib/utilities.py
+
+Suite Setup         Open Connection And Log In
+Suite Teardown      Close All Connections
+
+Test Teardown       Post Test Execution
+
+*** Variables ***
+${SET_ADDR_PREFIX}  0x00 0x08 0x61 0x80 0x21 0x70 0x62 0x21 0x00 0x01 0x06 0x04
+${STATIC}           0x00 0x01                       #equivalent address type 1
+${DHCP}             0x00 0x00                       #equivalent address type 0
+${CLEAR_ADDR}       0x00 0x08 0x61 0x80 0x00 0x00 0x00 0x00
+${INVALID_MAC}      f4:52:14
+
+*** Test Cases ***
+
+Set Static Address With REST
+    [Documentation]   This testcase is to set static address for host's network
+    ...               setting using REST. Later verify using REST
+    ...               that it is set correctly.
+    [Tags]  Set_Static_Address_With_REST
+
+    ${ip_address}=  utilities.random_ip
+    ${gateway}=  utilities.random_ip
+    ${mac_address}=  utilities.random_mac
+    ${subnet}=  Evaluate    random.randint(0, 32)    modules=random
+
+    ${settings}=  Catenate   SEPARATOR=
+    ...    ipaddress=${ip_address},prefix=${subnet},
+    ...    gateway=${gateway},mac=${mac_address},addr_type=1
+
+    Set Network Override Setting   ${settings}
+
+    ${resp}=  Read Attribute  /org/openbmc/settings/host0    network_config
+    Should Be Equal    ${resp}   ${settings}
+
+
+Set DHCP Address With REST
+    [Documentation]   This testcase is to set dhcp address for host's network
+    ...               setting using REST. Later verify using REST that it
+    ...               is set correctly.
+    [Tags]  Set_DHCP_Address_With_REST
+
+    ${mac_address}=  utilities.random_mac
+
+    ${settings}=  Catenate   SEPARATOR=
+    ...    ipaddress=,prefix=,gateway=,mac=${mac_address},addr_type=0
+
+    Set Network Override Setting   ${settings}
+
+    ${resp}=  Read Attribute  /org/openbmc/settings/host0    network_config
+    Should Be Equal    ${resp}    ${settings}
+
+
+Set Static Address With IPMI
+    [Documentation]   This testcase is to set static address for host's network
+    ...               setting using IPMI. Later verify using REST that it is
+    ...               set correctly.
+    [Tags]  Set_Static_Address_With_IPMI
+
+    ${ip_address}=  utilities.random_ip
+    ${gateway}=  utilities.random_ip
+    ${mac_address}=  utilities.random_mac
+    ${subnet}=  Evaluate    random.randint(0, 32)    modules=random
+
+    ${mac_address_hex}=  Mac Address To Hex String    ${mac_address}
+    ${ip_address_hex}=  IP Address To Hex String      ${ip_address}
+    ${gateway_hex}=  IP Address To Hex String      ${gateway}
+    ${subnet_hex}=  Convert To Hex    ${subnet}    prefix=0x
+    ...    lowercase=yes
+
+    ${ipmi_raw_cmd}=  Catenate  SEPARATOR=
+    ...    ${SET_ADDR_PREFIX}${SPACE}${mac_address_hex}${SPACE}${STATIC}${SPACE}
+    ...    ${ip_address_hex}${SPACE}${subnet_hex}${SPACE}${gateway_hex}
+
+    Run IPMI command   ${ipmi_raw_cmd}
+
+    ${resp}=  Read Attribute  /org/openbmc/settings/host0    network_config
+
+    ${settings}=  Catenate   SEPARATOR=
+    ...    ipaddress=${ip_address},prefix=${subnet},gateway=${gateway},
+    ...    mac=${mac_address},addr_type=1
+
+    Should Be Equal    ${resp}    ${settings}
+
+
+Set DHCP Address With IPMI
+    [Documentation]   This testcase is to set dhcp address for host's network
+    ...               setting using IPMI. Later verify using REST that it is
+    ...               set correctly.
+    [Tags]  Set_DHCP_Address_With_IPMI
+
+    ${mac_address}=  utilities.random_mac
+    ${mac_address_hex}=  Mac Address To Hex String    ${mac_address}
+
+    ${ipmi_raw_cmd}=  Catenate  SEPARATOR=
+    ...    ${SET_ADDR_PREFIX}${SPACE}${mac_address_hex}${SPACE}${DHCP}
+    Run IPMI command   ${ipmi_raw_cmd}
+
+    ${resp}=  Read Attribute  /org/openbmc/settings/host0    network_config
+    Should Contain    ${resp}    addr_type=0
+
+
+Clear Address With IPMI
+    [Documentation]   This testcase is to clear host's network setting
+    ...               using IPMI. Later verify using REST that it is
+    ...               cleared.
+    [Tags]  Clear_Address_With_IPMI
+
+    Run IPMI command   ${CLEAR_ADDR}
+
+    ${resp}=  Read Attribute  /org/openbmc/settings/host0    network_config
+    Should Be Equal    ${resp}    ipaddress=,prefix=,gateway=,mac=,addr_type=
+
+
+Set Invalid Address With REST
+    [Documentation]   This testcase is to verify that proper error message is
+    ...               prompted by REST with invalid mac address for
+    ...               host's network setting.
+    [Tags]  Set_Invalid_Address_With_REST
+
+    ${ip_address}=  utilities.random_ip
+    ${gateway}=  utilities.random_ip
+    ${subnet}=  Evaluate    random.randint(0, 32)    modules=random
+
+    ${invalid_settings}=  Catenate   SEPARATOR=
+    ...    ipaddress=${ip_address},prefix=${subnet},gateway=${gateway},
+    ...    mac=${INVALID_MAC},addr_type=1
+
+    ${resp}=  Set Network Override Setting   ${invalid_settings}
+    Should Be Equal    ${resp}    error
+
+
+Set Invalid Address With IPMI
+    [Documentation]   This testcase is to verify that invalid mac address for
+    ...               host's network setting can not be set by IPMI.
+    [Tags]  Set_Invalid_Address_With_IPMI
+
+    ${ip_address}=  utilities.random_ip
+    ${gateway}=  utilities.random_ip
+    ${subnet}=  Evaluate    random.randint(0, 32)    modules=random
+
+    ${ip_address_hex}=  IP Address To Hex String      ${ip_address}
+    ${gateway_hex}=  IP Address To Hex String      ${gateway}
+    ${invalid_mac_hex}=  Mac Address To Hex String    ${INVALID_MAC}
+    ${subnet_hex}=  Convert To Hex    ${subnet}    prefix=0x
+    ...    lowercase=yes
+
+    ${ipmi_raw_cmd}=  Catenate  SEPARATOR=
+    ...    ${SET_ADDR_PREFIX}${SPACE}${invalid_mac_hex}${SPACE}${STATIC}${SPACE}
+    ...    ${ip_address_hex}${SPACE}${subnet_hex}${SPACE}${gateway_hex}
+    Run IPMI command   ${ipmi_raw_cmd}
+
+    ${invalid_settings}=  Catenate   SEPARATOR=
+    ...    ipaddress=${ip_address},prefix=${subnet},gateway=${gateway},
+    ...    mac=${INVALID_MAC},addr_type=1
+
+    ${resp}=  Read Attribute  /org/openbmc/settings/host0    network_config
+    Should Not Be Equal    ${resp}    ${invalid_settings}
+
+
+*** Keywords ***
+
+Set Network Override Setting
+    [Documentation]  Set host's network settings with passed arguments and
+    ...              return its status.
+    ...              Description of arguments:
+    ...              settings  Network settings which need to be set
+    [Arguments]    ${settings}
+    ${host_network}=  Set Variable   ${settings}
+    ${valueDict}=  create dictionary   data=${host_network}
+    ${resp}=   OpenBMC Put Request
+    ...    /org/openbmc/settings/host0/attr/network_config    data=${valueDict}
+    ${jsondata}=  to json    ${resp.content}
+    [return]    ${jsondata['status']}
+
+Post Test Execution
+    [Documentation]  Perform operations after test execution. Captures FFDC
+    ...              in case of test case failure and sets defaults values
+    ...              for host's network settings.
+
+    FFDC On Test Case Fail
+
+    Run IPMI command   ${CLEAR_ADDR}
diff --git a/lib/utilities.py b/lib/utilities.py
index 980d06b..f316b3b 100755
--- a/lib/utilities.py
+++ b/lib/utilities.py
@@ -3,7 +3,23 @@
 from robot.libraries.BuiltIn import BuiltIn
 import imp
 import string
+import random
 
+def random_mac():
+    r"""
+    Return random mac address in the following format.
+    Example: 00:01:6C:80:02:78
+    """
+    return ":".join(map(lambda x: "%02x" % x, (random.randint(0x00, 0xff)
+        for _ in range(6))))
+
+def random_ip():
+    r"""
+    Return random ip address in the following format.
+    Example: 9.3.128.100
+    """
+    return ".".join(map(str, (random.randint(0, 255)
+        for _ in range(4))))
 
 def get_sensor(module_name, value):
     m = imp.load_source('module.name', module_name)
diff --git a/lib/utils.robot b/lib/utils.robot
index 82fcdbd..76195c3 100644
--- a/lib/utils.robot
+++ b/lib/utils.robot
@@ -408,3 +408,34 @@
     Execute Command    rm ${file_path}-${LOG_TIME}
 
     [Return]    ${journal_log}
+
+Mac Address To Hex String
+    [Documentation]   Converts MAC address into hex format.
+    ...               Example
+    ...               Given the following MAC: 00:01:6C:80:02:78
+    ...               This keyword will return: 0x00 0x01 0x6C 0x80 0x02 0x78
+    ...               Description of arguments:
+    ...               i_macaddress  MAC address in the following format 00:01:6C:80:02:78
+    [Arguments]    ${i_macaddress}
+
+    ${mac_hex}=  Catenate  0x${i_macaddress.replace(':', ' 0x')}
+    [return]    ${mac_hex}
+
+IP Address To Hex String
+    [Documentation]   Converts IP address into hex format.
+    ...               Example:
+    ...               Given the following IP: 10.3.164.100
+    ...               This keyword will return: 0xa 0x3 0xa4 0xa0
+    ...               Description of arguments:
+    ...               i_ipaddress  IP address in the following format 10.10.10.10
+    [Arguments]    ${i_ipaddress}
+
+    @{ip}=  Split String  ${i_ipaddress}    .
+    ${index}=  Set Variable  ${0}
+
+    :FOR    ${item}     IN      @{ip}
+    \   ${hex}=  Convert To Hex    ${item}    prefix=0x    lowercase=yes
+    \   Set List Value    ${ip}    ${index}    ${hex}
+    \   ${index}=  Set Variable    ${index + 1}
+    ${ip_hex}=  Catenate    @{ip}
+    [return]    ${ip_hex}