From 324a4f73d0a675b680935e70fa3fe14e076146f1 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Fri, 24 Apr 2015 15:12:58 +0200 Subject: [PATCH] Do not erase the IOU config --- gns3server/handlers/api/iou_handler.py | 2 ++ gns3server/modules/iou/iou_vm.py | 2 +- tests/handlers/api/test_iou.py | 24 +++++++++++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/gns3server/handlers/api/iou_handler.py b/gns3server/handlers/api/iou_handler.py index 3314e580..477d31d6 100644 --- a/gns3server/handlers/api/iou_handler.py +++ b/gns3server/handlers/api/iou_handler.py @@ -58,6 +58,8 @@ class IOUHandler: for name, value in request.json.items(): if hasattr(vm, name) and getattr(vm, name) != value: + if name == "initial_config_content" and (vm.initial_config_content and len(vm.initial_config_content) > 0): + continue setattr(vm, name, value) response.set_status(201) response.json(vm) diff --git a/gns3server/modules/iou/iou_vm.py b/gns3server/modules/iou/iou_vm.py index 783b1357..b7214bd4 100644 --- a/gns3server/modules/iou/iou_vm.py +++ b/gns3server/modules/iou/iou_vm.py @@ -973,7 +973,7 @@ class IOUVM(BaseVM): if len(initial_config) == 0 and os.path.exists(initial_config_path): return - with open(initial_config_path, "w+", encoding="utf-8") as f: + with open(initial_config_path, 'w+', encoding='utf-8') as f: if len(initial_config) == 0: f.write('') else: diff --git a/tests/handlers/api/test_iou.py b/tests/handlers/api/test_iou.py index 643ebc34..ebc3c5d1 100644 --- a/tests/handlers/api/test_iou.py +++ b/tests/handlers/api/test_iou.py @@ -19,6 +19,7 @@ import pytest import os import stat import sys +import uuid from tests.utils import asyncio_patch from unittest.mock import patch, MagicMock, PropertyMock @@ -94,11 +95,32 @@ def test_iou_create_with_params(server, project, base_params): assert "initial-config.cfg" in response.json["initial_config"] with open(initial_config_file(project, response.json)) as f: - assert f.read() == params["initial_config_content"] + assert f.read() == "hostname test" assert "iourc" in response.json["iourc_path"] +def test_iou_create_initial_config_already_exist(server, project, base_params): + """We don't erase an initial config if already exist at project creation""" + + vm_id = str(uuid.uuid4()) + initial_config_file_path = initial_config_file(project, {'vm_id': vm_id}) + with open(initial_config_file_path, 'w+') as f: + f.write("echo hello") + + params = base_params + params["vm_id"] = vm_id + params["initial_config_content"] = "hostname test" + + response = server.post("/projects/{project_id}/iou/vms".format(project_id=project.id), params, example=True) + assert response.status == 201 + assert response.route == "/projects/{project_id}/iou/vms" + + assert "initial-config.cfg" in response.json["initial_config"] + with open(initial_config_file(project, response.json)) as f: + assert f.read() == "echo hello" + + def test_iou_get(server, project, vm): response = server.get("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True) assert response.status == 200