Add DHCP Client libs and VPP sends DHCPv4 Discover test 72/1172/8
authorMatej Klotton <mklotton@cisco.com>
Tue, 17 May 2016 12:21:18 +0000 (14:21 +0200)
committerJan Gelety <jgelety@cisco.com>
Mon, 30 May 2016 21:10:17 +0000 (21:10 +0000)
 -JIRA: CSIT-100

Change-Id: I4b6fc5d974ebe500d6c6bc74c14e1db7b3d10c3a
Signed-off-by: Matej Klotton <mklotton@cisco.com>
resources/libraries/python/Dhcp.py [new file with mode: 0644]
resources/libraries/robot/dhcp_client.robot [new file with mode: 0644]
resources/templates/vat/dhcp_client.vat [new file with mode: 0644]
resources/traffic_scripts/dhcp/check_dhcp_discover.py [new file with mode: 0755]
tests/suites/dhcp/dhcp_client.robot [new file with mode: 0644]

diff --git a/resources/libraries/python/Dhcp.py b/resources/libraries/python/Dhcp.py
new file mode 100644 (file)
index 0000000..8b78f45
--- /dev/null
@@ -0,0 +1,48 @@
+# Copyright (c) 2016 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""DHCP utilities for VPP."""
+
+
+from resources.libraries.python.VatExecutor import VatExecutor
+from resources.libraries.python.topology import Topology
+
+
+class DhcpClient(object):
+    """DHCP Client utilities."""
+
+    @staticmethod
+    def set_dhcp_client_on_interface(vpp_node, interface, hostname=None):
+        """Set DHCP client on interface.
+
+        :param vpp_node: VPP node to set DHCP client on.
+        :param interface: Interface name to set DHCP client on.
+        :param hostname: Hostname used in DHCP DISCOVER.
+        :type vpp_node: dict
+        :type interface: str
+        :type hostname: str
+        :raises RuntimeError: If unable to set DHCP client on interface.
+        """
+        sw_if_index = Topology.get_interface_sw_index(vpp_node, interface)
+        interface = 'sw_if_index {}'.format(sw_if_index)
+        hostname = 'hostname {}'.format(hostname) if hostname else ''
+        output = VatExecutor.cmd_from_template(vpp_node,
+                                               "dhcp_client.vat",
+                                               interface=interface,
+                                               hostname=hostname)
+        output = output[0]
+
+        if output["retval"] != 0:
+            raise RuntimeError('Unable to set DHCP client on node {} and'
+                               ' interface {}.'
+                               .format(vpp_node, interface))
diff --git a/resources/libraries/robot/dhcp_client.robot b/resources/libraries/robot/dhcp_client.robot
new file mode 100644 (file)
index 0000000..115646d
--- /dev/null
@@ -0,0 +1,49 @@
+# Copyright (c) 2016 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+*** Settings ***
+| Library  | Collections
+| Resource | resources/libraries/robot/default.robot
+| Library  | resources.libraries.python.Dhcp.DhcpClient
+| Library  | resources.libraries.python.TrafficScriptExecutor
+| Documentation | DHCP Client specific keywords.
+
+*** Keywords ***
+| Check DHCP DISCOVER header
+| | [Documentation] | Check if DHCP message contains all required fields.
+| | ...
+| | ... | *Arguments:*
+| | ... | - tg_node - TG node. Type: dictionary
+| | ... | - interface - TGs interface where listen for DHCP DISCOVER message.
+| | ... |   Type: string
+| | ... | - src_mac - DHCP clients MAC address. Type: string
+| | ... | - hostname - DHCP clients hostname (Optional, Default="", if not
+| | ... |   specified, the hostneme is not configured). Type: string
+| | ...
+| | ... | *Return:*
+| | ... | - No value returned
+| | ...
+| | ... | *Example:*
+| | ...
+| | ... | \| Check DHCP DISCOVER header \| ${nodes['TG']} \
+| | ... | \| eth2 \| 08:00:27:66:b8:57 \|
+| | ... | \| Check DHCP DISCOVER header \| ${nodes['TG']} \
+| | ... | \| eth2 \| 08:00:27:66:b8:57 \| client-hostname \|
+| | ...
+| | [Arguments] | ${tg_node} | ${interface} | ${src_mac} | ${hostname}=${EMPTY}
+| | ${args}= | Run Keyword If | "${hostname}" == "" | Catenate
+| |          | ...  | --rx_if | ${interface} | --rx_src_mac | ${src_mac}
+| | ...      | ELSE | Catenate | --rx_if | ${interface} | --rx_src_mac
+| |          | ...  | ${src_mac} | --hostname | ${hostname}
+| | Run Traffic Script On Node | dhcp/check_dhcp_discover.py
+| | ... | ${tg_node} | ${args}
diff --git a/resources/templates/vat/dhcp_client.vat b/resources/templates/vat/dhcp_client.vat
new file mode 100644 (file)
index 0000000..2fd99e3
--- /dev/null
@@ -0,0 +1 @@
+dhcp_client_config {interface} {hostname}
diff --git a/resources/traffic_scripts/dhcp/check_dhcp_discover.py b/resources/traffic_scripts/dhcp/check_dhcp_discover.py
new file mode 100755 (executable)
index 0000000..2fdc5b7
--- /dev/null
@@ -0,0 +1,150 @@
+#!/usr/bin/env python
+# Copyright (c) 2016 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Traffic script that receives an DHCP packet on given interface and check if
+is correct DHCP DISCOVER message.
+"""
+
+import sys
+
+from scapy.layers.inet import UDP_SERVICES
+
+from resources.libraries.python.PacketVerifier import RxQueue
+from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
+
+
+def main():
+    args = TrafficScriptArg(['rx_src_mac'], ['hostname'])
+
+    rx_if = args.get_arg('rx_if')
+    rx_src_mac = args.get_arg('rx_src_mac')
+    hostname = args.get_arg('hostname')
+
+    rx_dst_mac = 'ff:ff:ff:ff:ff:ff'
+    rx_src_ip = '0.0.0.0'
+    rx_dst_ip = '255.255.255.255'
+    boot_request = 1
+    dhcp_magic = 'c\x82Sc'
+
+    rxq = RxQueue(rx_if)
+
+    ether = rxq.recv(10)
+
+    if ether is None:
+        raise RuntimeError("DHCP DISCOVER Rx timeout.")
+
+    if ether.dst != rx_dst_mac:
+        raise RuntimeError("Destination MAC address error.")
+    print "Destination MAC address: OK."
+
+    if ether.src != rx_src_mac:
+        raise RuntimeError("Source MAC address error.")
+    print "Source MAC address: OK."
+
+    if ether['IP'].dst != rx_dst_ip:
+        raise RuntimeError("Destination IP address error.")
+    print "Destination IP address: OK."
+
+    if ether['IP'].src != rx_src_ip:
+        raise RuntimeError("Source IP address error.")
+    print "Source IP address: OK."
+
+    if ether['IP']['UDP'].dport != UDP_SERVICES.bootps:
+        raise RuntimeError("UDP destination port error.")
+    print "UDP destination port: OK."
+
+    if ether['IP']['UDP'].sport != UDP_SERVICES.bootpc:
+        raise RuntimeError("UDP source port error.")
+    print "UDP source port: OK."
+
+    bootp = ether['BOOTP']
+
+    if bootp.op != boot_request:
+        raise RuntimeError("BOOTP message type error.")
+    print "BOOTP message type: OK"
+
+    if bootp.ciaddr != '0.0.0.0':
+        raise RuntimeError("BOOTP client IP address error.")
+    print "BOOTP client IP address: OK"
+
+    if bootp.yiaddr != '0.0.0.0':
+        raise RuntimeError("BOOTP 'your' (client) IP address error.")
+    print "BOOTP 'your' (client) IP address: OK"
+
+    if bootp.siaddr != '0.0.0.0':
+        raise RuntimeError("BOOTP next server IP address error.")
+    print "BOOTP next server IP address: OK"
+
+    if bootp.giaddr != '0.0.0.0':
+        raise RuntimeError("BOOTP relay agent IP address error.")
+    print "BOOTP relay agent IP address: OK"
+
+    chaddr = bootp.chaddr[:bootp.hlen].encode('hex')
+    if chaddr != rx_src_mac.replace(':', ''):
+        raise RuntimeError("BOOTP client hardware address error.")
+    print "BOOTP client hardware address: OK"
+
+    # Check hostname
+    if bootp.sname != 64*'\x00':
+        raise RuntimeError("BOOTP server name error.")
+    print "BOOTP server name: OK"
+
+    # Check boot file
+    if bootp.file != 128*'\x00':
+        raise RuntimeError("BOOTP boot file name error.")
+    print "BOOTP boot file name: OK"
+
+    # Check bootp magic
+    if bootp.options != dhcp_magic:
+        raise RuntimeError("DHCP magic error.")
+    print "DHCP magic: OK"
+
+    # Check options
+    dhcp_options = ether['DHCP options'].options
+
+    # Option 12
+    hn = filter(lambda x: x[0] == 'hostname', dhcp_options)
+    if hostname:
+        try:
+            if hn[0][1] != hostname:
+                raise RuntimeError("Client's hostname doesn't match.")
+        except IndexError:
+            raise RuntimeError("Option list doesn't contain hostname option.")
+    else:
+        if len(hn) != 0:
+            raise RuntimeError("Option list contains hostname option.")
+    print "Option 12 hostname: OK"
+
+    # Option 53
+    mt = filter(lambda x: x[0] == 'message-type', dhcp_options)[0][1]
+    if mt != 1:
+        raise RuntimeError("Option 53 message-type error.")
+    print "Option 53 message-type: OK"
+
+    # Option 55
+    prl = filter(lambda x: x[0] == 'param_req_list', dhcp_options)[0][1]
+    if prl != '\x01\x1c\x02\x03\x0f\x06w\x0c,/\x1ay*':
+        raise RuntimeError("Option 55 param_req_list error.")
+    print "Option 55 param_req_list: OK"
+
+    # Option 255
+    if 'end' not in dhcp_options:
+        raise RuntimeError("end option error.")
+    print "end option: OK"
+
+    sys.exit(0)
+
+
+if __name__ == "__main__":
+    main()
diff --git a/tests/suites/dhcp/dhcp_client.robot b/tests/suites/dhcp/dhcp_client.robot
new file mode 100644 (file)
index 0000000..693be26
--- /dev/null
@@ -0,0 +1,51 @@
+# Copyright (c) 2016 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+*** Settings ***
+| Resource | resources/libraries/robot/default.robot
+| Resource | resources/libraries/robot/testing_path.robot
+| Resource | resources/libraries/robot/dhcp_client.robot
+| Library | resources.libraries.python.Trace
+| Force Tags | HW_ENV | VM_ENV | 3_NODE_DOUBLE_LINK_TOPO
+| Test Setup | Run Keywords | Setup all DUTs before test
+| ...        | AND          | Setup all TGs before traffic script
+| Documentation | *DHCP Client related test cases*
+
+*** Variables ***
+| ${client_hostname}= | dhcp-client
+
+*** Test Cases ***
+| VPP sends a DHCP DISCOVER
+| | [Documentation] | Configure DHCP client on interface to TG without hostname
+| | ...             | and check if DHCP DISCOVER message contains all required
+| | ...             | fields with expected values.
+| | ...
+| | Given Path for 2-node testing is set
+| |       ... | ${nodes['TG']} | ${nodes['DUT1']} | ${nodes['TG']}
+| | And   Interfaces in 2-node path are up
+| | When  Set DHCP client on Interface | ${dut_node} | ${dut_to_tg_if1}
+| | Then  Check DHCP DISCOVER header | ${tg_node}
+| |       ... | ${tg_to_dut_if1} | ${dut_to_tg_if1_mac}
+
+| VPP sends a DHCP DISCOVER with hostname
+| | [Documentation] | Configure DHCP client on interface to TG with hostname
+| | ...             | and check if DHCP DISCOVER message contains all required
+| | ...             | fields with expected values.
+| | ...
+| | Given Path for 2-node testing is set
+| |       ... | ${nodes['TG']} | ${nodes['DUT1']} | ${nodes['TG']}
+| | And   Interfaces in 2-node path are up
+| | When  Set DHCP client on Interface | ${dut_node} | ${dut_to_tg_if1}
+| |       ... | ${client_hostname}
+| | Then  Check DHCP DISCOVER header | ${tg_node}
+| |       ... | ${tg_to_dut_if1} | ${dut_to_tg_if1_mac} | ${client_hostname}