1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-28 11:18:11 +00:00

Support %h in VPCS config file

This commit is contained in:
Julien Duponchelle 2015-02-03 21:48:20 +01:00
parent a12f753136
commit aeb83a7945
2 changed files with 13 additions and 22 deletions

View File

@ -189,10 +189,11 @@ class VPCSVM(BaseVM):
if self._script_file is None: if self._script_file is None:
self._script_file = os.path.join(self.working_dir, 'startup.vpcs') self._script_file = os.path.join(self.working_dir, 'startup.vpcs')
try: try:
with open(self._script_file, '+w') as f: with open(self._script_file, 'w+') as f:
if startup_script is None: if startup_script is None:
f.write('') f.write('')
else: else:
startup_script = startup_script.replace("%h", self._name)
f.write(startup_script) f.write(startup_script)
except OSError as e: except OSError as e:
raise VPCSError("Can't write VPCS startup file '{}'".format(self._script_file)) raise VPCSError("Can't write VPCS startup file '{}'".format(self._script_file))
@ -426,16 +427,3 @@ class VPCSVM(BaseVM):
""" """
return self._script_file return self._script_file
@script_file.setter
def script_file(self, script_file):
"""
Sets the script-file for this VPCS instance.
:param script_file: path to base-script-file
"""
self._script_file = script_file
log.info("VPCS {name} [{uuid}]: script_file set to {config}".format(name=self._name,
uuid=self.uuid,
config=self._script_file))

View File

@ -149,6 +149,15 @@ def test_update_startup_script(vm):
assert f.read() == content assert f.read() == content
def test_update_startup_script_h(vm):
content = "setname %h\n"
vm.name = "pc1"
vm.startup_script = content
assert os.path.exists(vm.script_file)
with open(vm.script_file) as f:
assert f.read() == "setname pc1\n"
def test_get_startup_script(vm): def test_get_startup_script(vm):
content = "echo GNS3 VPCS\nip 192.168.1.2\n" content = "echo GNS3 VPCS\nip 192.168.1.2\n"
vm.startup_script = content vm.startup_script = content
@ -159,7 +168,7 @@ def test_get_startup_script_using_default_script(vm):
content = "echo GNS3 VPCS\nip 192.168.1.2\n" content = "echo GNS3 VPCS\nip 192.168.1.2\n"
# Reset script file location # Reset script file location
vm.script_file = None vm._script_file = None
filepath = os.path.join(vm.working_dir, 'startup.vpc') filepath = os.path.join(vm.working_dir, 'startup.vpc')
with open(filepath, 'w+') as f: with open(filepath, 'w+') as f:
@ -187,19 +196,13 @@ def test_change_name(vm, tmpdir):
vm.name = "world" vm.name = "world"
with open(path, 'w+') as f: with open(path, 'w+') as f:
f.write("name world") f.write("name world")
vm.script_file = path vm._script_file = path
vm.name = "hello" vm.name = "hello"
assert vm.name == "hello" assert vm.name == "hello"
with open(path) as f: with open(path) as f:
assert f.read() == "name hello" assert f.read() == "name hello"
def test_change_script_file(vm, tmpdir):
path = os.path.join(str(tmpdir), 'startup2.vpcs')
vm.script_file = path
assert vm.script_file == path
def test_close(vm, port_manager): def test_close(vm, port_manager):
with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True): with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()): with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):