From 1e73962e33665a4d8c38516e77c169ad8d221b36 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Thu, 3 Nov 2016 13:35:44 +0100 Subject: [PATCH] Do not dump iourc_content to .gns3 file Fix #759 --- gns3server/controller/node.py | 25 ++++++++++++++++++------- tests/controller/test_node.py | 20 +++++++++++++++++--- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/gns3server/controller/node.py b/gns3server/controller/node.py index 378dc04d..0c4e4ad5 100644 --- a/gns3server/controller/node.py +++ b/gns3server/controller/node.py @@ -501,7 +501,7 @@ class Node: if self._node_type == "atm_switch": atm_port = set() # Mapping is like {"1:0:100": "10:0:200"} - for source, dest in self.properties["mappings"].items(): + for source, dest in self._properties["mappings"].items(): atm_port.add(int(source.split(":")[0])) atm_port.add(int(dest.split(":")[0])) atm_port = sorted(atm_port) @@ -512,7 +512,7 @@ class Node: elif self._node_type == "frame_relay_switch": frame_relay_port = set() # Mapping is like {"1:101": "10:202"} - for source, dest in self.properties["mappings"].items(): + for source, dest in self._properties["mappings"].items(): frame_relay_port.add(int(source.split(":")[0])) frame_relay_port.add(int(dest.split(":")[0])) frame_relay_port = sorted(frame_relay_port) @@ -520,15 +520,15 @@ class Node: self._ports.append(PortFactory("{}".format(port), 0, 0, port, "frame_relay")) return elif self._node_type == "dynamips": - self._ports = DynamipsPortFactory(self.properties) + self._ports = DynamipsPortFactory(self._properties) return elif self._node_type in ("cloud", "nat", "ethernet_switch", "ethernet_hub"): port_number = 0 - for port in self.properties["ports_mapping"]: + for port in self._properties["ports_mapping"]: self._ports.append(PortFactory(port["name"], 0, 0, port_number, "ethernet")) port_number += 1 else: - self._ports = StandardPortFactory(self.properties, self._port_by_adapter, self._first_port_name, self._port_name_format, self._port_segment_size) + self._ports = StandardPortFactory(self._properties, self._port_by_adapter, self._first_port_name, self._port_name_format, self._port_segment_size) return def __repr__(self): @@ -539,6 +539,17 @@ class Node: return False return self.id == other.id and other.project.id == self.project.id + def _filter_properties(self): + """ + Some properties are private and should not be exposed + """ + PRIVATE_PROPERTIES = ("iourc_content", ) + prop = copy.copy(self._properties) + for k in list(prop.keys()): + if k in PRIVATE_PROPERTIES: + del prop[k] + return prop + def __json__(self, topology_dump=False): """ :param topology_dump: Filter to keep only properties require for saving on disk @@ -551,7 +562,7 @@ class Node: "name": self._name, "console": self._console, "console_type": self._console_type, - "properties": self._properties, + "properties": self._filter_properties(), "label": self._label, "x": self._x, "y": self._y, @@ -574,7 +585,7 @@ class Node: "console_host": str(self._compute.console_host), "console_type": self._console_type, "command_line": self._command_line, - "properties": self._properties, + "properties": self._filter_properties(), "status": self._status, "label": self._label, "x": self._x, diff --git a/tests/controller/test_node.py b/tests/controller/test_node.py index 7ec283de..2715b366 100644 --- a/tests/controller/test_node.py +++ b/tests/controller/test_node.py @@ -20,6 +20,7 @@ import aiohttp import pytest import uuid import asyncio +import copy import os from unittest.mock import MagicMock, ANY @@ -83,6 +84,19 @@ def test_eq(compute, project, node, controller): assert node != Node(Project(str(uuid.uuid4()), controller=controller), compute, "demo3", node_id=node.id, node_type="qemu") +def test_properties_filter(project, compute): + """ + Some properties are private and should not be exposed + """ + node = Node(project, compute, "demo", + node_id=str(uuid.uuid4()), + node_type="vpcs", + console_type="vnc", + properties={"startup_script": "echo test", "iourc_content": "test"}) + assert node._properties == {"startup_script": "echo test", "iourc_content": "test"} + assert node._filter_properties() == {"startup_script": "echo test"} + + def test_json(node, compute): assert node.__json__() == { "compute_id": str(compute.id), @@ -283,8 +297,8 @@ def test_update_properties(node, compute, project, async_run, controller): # The notif should contain the old properties because it's the compute that will emit # the correct info - node_notif = node.__json__() - node_notif["properties"]["startup_config"] = "echo test" + node_notif = copy.deepcopy(node.__json__()) + node_notif["properties"]["startup_script"] = "echo test" controller._notification.emit.assert_called_with("node.updated", node_notif) @@ -436,7 +450,7 @@ def test_update_label(node): def test_get_port(node): - node.properties["adapters"] = 2 + node._properties["adapters"] = 2 node._list_ports() port = node.get_port(0, 0) assert port.adapter_number == 0