From: Archana Singh <archus(a)linux.vnet.ibm.com>
If template name is change to update and the new template name to be
updated already exists then exception is raised stating name already exists.
Added unit test case for same.
Signed-off-by: Archana Singh <archus(a)linux.vnet.ibm.com>
---
model/templates.py | 8 ++++++++
tests/test_template.py | 16 ++++++++++++++++
2 files changed, 24 insertions(+)
diff --git a/model/templates.py b/model/templates.py
index c3e24e1..566a20c 100644
--- a/model/templates.py
+++ b/model/templates.py
@@ -231,6 +231,14 @@ class TemplateModel(object):
def update(self, name, params):
edit_template = self.lookup(name)
+ # If new name is not same as existing name
+ # and new name already exists: raise exception
+ with self.objstore as session:
+ if 'name' in params and name != params['name'] \
+ and params['name'] in session.get_list('template'):
+ raise InvalidOperation("KCHTMPL0001E",
+ {'name': params['name']})
+
# Valid interfaces
interfaces = params.get('interfaces', [])
validate_interfaces(interfaces)
diff --git a/tests/test_template.py b/tests/test_template.py
index 6845565..f1dbf2b 100644
--- a/tests/test_template.py
+++ b/tests/test_template.py
@@ -192,6 +192,22 @@ class TemplateTests(unittest.TestCase):
self.request('/plugins/kimchi/templates/test').read()
)
+ # Create another template to test update template name with one of
+ # existing template name
+ req = json.dumps({'name': 'test_new',
+ 'source_media': {'type': 'disk',
'path': MOCK_ISO}})
+ resp = self.request('/plugins/kimchi/templates', req, 'POST')
+ self.assertEquals(201, resp.status)
+ # Update name with one of existing name should fail with 400
+ req = json.dumps({'name': 'test_new'})
+ resp = self.request('/plugins/kimchi/templates/test', req,
'PUT')
+ self.assertEquals(400, resp.status)
+
+ # Delete the test1 template
+ resp = self.request('/plugins/kimchi/templates/test_new', '{}',
+ 'DELETE')
+ self.assertEquals(204, resp.status)
+
# Update name
new_name = u'kīмсhīTmpl'
new_tmpl_uri = '/plugins/kimchi/templates/%s' \
--
2.7.4