def test_get_dataplanes_fail(self):
     with vplaned.Controller() as ctrl:
         ctrl._cfg_socket.send_string.side_effect = \
             lambda string: self.assertTrue(string == "GETVPCONFIG")
         ctrl._cfg_socket.recv_string.return_value = "FAILURE"
         with self.assertRaises(vplaned.ControllerException):
             dps = list(ctrl.get_dataplanes())
 def test_store_success(self):
     out = {"path": {"to": {"object": {"__SET__": "command 1",
                                       "__INTERFACE__": "ALL"}}}}
     with vplaned.Controller() as ctrl:
         ctrl._store_socket.send_json.side_effect = \
             lambda json: self.assertTrue(json == out)
         ctrl._store_socket.recv_string.return_value = "OK"
         ctrl.store("path to object", "command 1", action="SET")
 def test_get_dataplanes_empty_list(self):
     data = {"dataplanes": []}
     with vplaned.Controller() as ctrl:
         ctrl._cfg_socket.send_string.side_effect = \
             lambda string: self.assertTrue(string == "GETVPCONFIG")
         ctrl._cfg_socket.recv_string.return_value = "OK"
         ctrl._cfg_socket.recv_json.return_value = data
         dps = list(ctrl.get_dataplanes())
         self.assertFalse(dps)
 def test_store_fail(self):
     out = {"path": {"to": {"object": {"__DELETE__": "command 2",
                                       "__INTERFACE__": "ALL"}}}}
     with vplaned.Controller() as ctrl:
         ctrl._store_socket.send_json.side_effect = \
             lambda json: self.assertTrue(json == out)
         ctrl._store_socket.recv_string.return_value = "FAIL"
         with self.assertRaises(vplaned.ControllerException):
             ctrl.store("path to object", "command 2", action="DELETE")
    def test_no_interfaces(self):
        data = {"dataplanes": [{"interfaces": []}]}
        with vplaned.Controller() as ctrl:
            ctrl._cfg_socket.recv_string.return_value = "OK"
            ctrl._cfg_socket.recv_json.return_value = data
            dps = list(ctrl.get_dataplanes())

            self.assertEqual(len(dps[0].interfaces.keys()), 0)
            with self.assertRaises(KeyError):
                dps[0].interfaces["dp0s1"]
 def test_get_dataplanes_success(self):
     data = {"dataplanes": [{"id": 0, "control": "ipc:///dev/null"},
                            {"id": 1, "control": "ipc:///dev/nuller"}]}
     with vplaned.Controller() as ctrl:
         ctrl._cfg_socket.send_string.side_effect = \
             lambda string: self.assertTrue(string == "GETVPCONFIG")
         ctrl._cfg_socket.recv_string.return_value = "OK"
         ctrl._cfg_socket.recv_json.return_value = data
         dps = list(ctrl.get_dataplanes())
         self.assertEqual(dps[0].id, 0)
         self.assertEqual(dps[1].id, 1)
         self.assertEqual(dps[0].control, "ipc:///dev/null")
         self.assertEqual(dps[1].control, "ipc:///dev/nuller")
 def test_string_command_fail(self):
     data = {"dataplanes": [{"id": 0, "control": "ipc:///dev/null",
                             "local": True}]}
     with vplaned.Controller() as ctrl:
         ctrl._cfg_socket.recv_string.return_value = "OK"
         ctrl._cfg_socket.recv_json.return_value = data
         dps = list(ctrl.get_dataplanes())
         self.assertEqual(dps[0].id, 0)
         self.assertTrue(dps[0].local)
         self.assertEqual(dps[0].control, "ipc:///dev/null")
         with dps[0]:
             dps[0]._socket.send_string.side_effect = \
                 lambda string: self.assertEqual(string, "command")
             dps[0]._socket.recv_string.return_value = "FAIL"
             with self.assertRaises(vplaned.DataplaneException):
                 dps[0].string_command("command")
 def test_string_command_success(self):
     data = {"dataplanes": [{"id": 0, "control": "ipc:///dev/null",
                             "local": True}]}
     with vplaned.Controller() as ctrl:
         ctrl._cfg_socket.recv_string.return_value = "OK"
         ctrl._cfg_socket.recv_json.return_value = data
         dps = list(ctrl.get_dataplanes())
         self.assertEqual(dps[0].id, 0)
         self.assertTrue(dps[0].local)
         self.assertEqual(dps[0].control, "ipc:///dev/null")
         with dps[0]:
             dps[0]._socket.send_string.side_effect = \
                 lambda string: self.assertTrue(string == "command")
             dps[0]._socket.recv_string.side_effect = ["OK", "some result"]
             self.assertEqual(dps[0].string_command("command"),
                              "some result")
 def test_json_command_success(self):
     data = {"dataplanes": [{"id": 0, "control": "ipc:///dev/null",
                             "local": False, "stuff": "good stuff"}]}
     with vplaned.Controller() as ctrl:
         ctrl._cfg_socket.recv_string.return_value = "OK"
         ctrl._cfg_socket.recv_json.return_value = data
         dps = list(ctrl.get_dataplanes())
         self.assertEqual(dps[0].id, 0)
         self.assertFalse(dps[0].local)
         self.assertEqual(dps[0].stuff, "good stuff")
         self.assertEqual(dps[0].control, "ipc:///dev/null")
         with dps[0]:
             dps[0]._socket.send_string.side_effect = \
                 lambda string: self.assertEqual(string, "command")
             dps[0]._socket.recv_string.return_value = "OK"
             dps[0]._socket.recv_json.return_value = {"best":
                                                      {"json": "ever"}}
             self.assertEqual(dps[0].json_command("command"),
                              {"best": {"json": "ever"}})
def npf_show_address_group(name=None, af=None, option=None,
                           hc1="Address-group",
                           hc0w=0, hc1w=0, hc2w=0,
                           ac0w=2, ac1w=14, ac2w=0):
    """For fetching and displaying one or more address-groups"""

    #
    # Prepare the dataplane command
    #
    cmd = "npf-op fw show address-group"

    if name:
        cmd += " name=%s" % (name)

    if af:
        cmd += " af=%s" % (af)

    if option:
        cmd += " option=%s" % (option)

    # List of address-groups.  May be IPv4, IPv6 or a mix.
    ag_list = []

    with vplaned.Controller() as controller:
        for dp in controller.get_dataplanes():
            with dp:
                rv = dp.json_command(cmd)
                if "address-groups" in rv:
                    ag_list.extend(rv["address-groups"])

    # Exit if no address-groups returned
    if not ag_list:
        return

    for ag in ag_list:
        npf_show_address_group_one(ag, hc1, hc0w, hc1w, hc2w,
                                   ac0w, ac1w, ac2w)
    def test_multiple_interfaces(self):
        data = {"dataplanes": [{"interfaces": [{"index": 6, "name": "dp0s3",
                                                "state": "up"},
                                               {"index": 7, "name": "dp0s7",
                                                "state": "up"},
                                               {"index": 8, "name": "dp0s9",
                                                "state": "down"}]}]}
        with vplaned.Controller() as ctrl:
            ctrl._cfg_socket.recv_string.return_value = "OK"
            ctrl._cfg_socket.recv_json.return_value = data
            dps = list(ctrl.get_dataplanes())

            self.assertEqual(len(dps[0].interfaces.keys()), 3)
            self.assertEqual(dps[0].interfaces["dp0s3"].state, "up")
            self.assertEqual(dps[0].interfaces["dp0s7"].state, "up")
            self.assertEqual(dps[0].interfaces["dp0s9"].state, "down")
            self.assertEqual(dps[0].interfaces["dp0s3"].index, 6)
            self.assertEqual(dps[0].interfaces["dp0s7"].index, 7)
            self.assertEqual(dps[0].interfaces["dp0s9"].index, 8)
            self.assertEqual(dps[0].interfaces["dp0s3"].dp_id, 0)
            self.assertEqual(dps[0].interfaces["dp0s7"].dp_id, 0)
            self.assertEqual(dps[0].interfaces["dp0s9"].dp_id, 0)
            with self.assertRaises(KeyError):
                dps[0].interfaces["dp0s1"]
Example #12
0
def npf_show_address_group(name,
                           af=None,
                           hc1="Address-group",
                           hc0w=0,
                           hc1w=0,
                           hc2w=0,
                           ac0w=4,
                           ac1w=14,
                           ac2w=0):
    """For displaying an npf address-group"""

    # Dataplane does not currently support fetching more than one
    # address-group at a time
    if not name:
        return

    # Prepare the dataplane command
    cmd = "npf-op fw show address-group"

    # For when 'name' is optional
    if name:
        cmd += " name=%s" % (name)

    if af:
        cmd += " af=%s" % (af)

    # List of dictionaries returned from dataplane (one per address-group)
    ag_list = []

    with vplaned.Controller() as controller:
        for dp in controller.get_dataplanes():
            with dp:
                ag_dict = dp.json_command(cmd)
                if "address-group" in ag_dict:
                    ag_list.append(ag_dict)

    # Headline format specifier
    hfmt = "%*s%-*s %*s"

    # Entry format specifier
    afmt = "%*s%-*s %*s"

    # If no address-groups not returned ...
    if not ag_list:
        name = "%s (-)" % (name)
        print(hfmt % (hc0w, "", hc1w, hc1, hc2w, name))
        return

    for ag in ag_list:
        # Display name and table ID
        name = "%s (%u)" % (ag["address-group"]["name"],
                            ag["address-group"]["id"])

        # Print headline and address-group name
        print(hfmt % (hc0w, "", hc1w, hc1, hc2w, name))

        addr_list = []

        # Get IPv4 entries
        if "ipv4" in ag["address-group"]:
            tmp_list = ag["address-group"]["ipv4"]["list-entries"]
            if tmp_list:
                addr_list.extend(tmp_list)

        # Get IPv6 entries
        if "ipv6" in ag["address-group"]:
            tmp_list = ag["address-group"]["ipv6"]["list-entries"]
            if tmp_list:
                addr_list.extend(tmp_list)

        for addr in addr_list:
            # Type 0 contain prefix and mask, or an address
            if addr["type"] == 0:
                if "mask" in addr:
                    addr_type = "Prefix"
                    val = "%s/%u" % (addr["prefix"], addr["mask"])
                else:
                    addr_type = "Address"
                    val = "%s" % (addr["prefix"])

            # Type 1 is an address range
            elif addr["type"] == 1:
                addr_type = "Address-range"
                val = "%s to %s" % (addr["start"], addr["end"])

            print(afmt % (ac0w, "", ac1w, addr_type, ac2w, val))
 def test_store_action_exception(self):
     with self.assertRaises(vplaned.ControllerException):
         with vplaned.Controller() as ctrl:
             ctrl._store_socket.recv_string.return_value = "OK"
             ctrl.store("path to object", "command 2")