Commit 433b8374 by Őry Máté

vm: fix _rename and its tests

🍻
parent 07e58f59
from __future__ import absolute_import, unicode_literals from __future__ import absolute_import, unicode_literals
from logging import getLogger from logging import getLogger
from re import match from re import search
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.utils import timezone from django.utils import timezone
...@@ -203,13 +203,15 @@ class SaveAsTemplateOperation(InstanceOperation): ...@@ -203,13 +203,15 @@ class SaveAsTemplateOperation(InstanceOperation):
""") """)
icon = 'save' icon = 'save'
def _rename(self, name): @staticmethod
m = match("^(.*)( v(\d+))?$", name) def _rename(name):
if m.group(3) is None: m = search(r" v(\d+)$", name)
v = 2 if m:
v = int(m.group(1)) + 1
name = search(r"^(.*) v(\d+)$", name).group(1)
else: else:
v = int(v) + 1 v = 1
return "%s v%d" % (m.group(1), v) return "%s v%d" % (name, v)
def _operation(self, activity, user, system, timeout=300, def _operation(self, activity, user, system, timeout=300,
with_shutdown=True, **kwargs): with_shutdown=True, **kwargs):
......
...@@ -44,11 +44,12 @@ class SaveAsTemplateOperationTestCase(TestCase): ...@@ -44,11 +44,12 @@ class SaveAsTemplateOperationTestCase(TestCase):
assert SaveAsTemplateOperation.id in getattr(Instance, op_reg_name) assert SaveAsTemplateOperation.id in getattr(Instance, op_reg_name)
def test_rename(self): def test_rename(self):
assert SaveAsTemplateOperation._rename("foo") == "foo v2" self.assertEqual(SaveAsTemplateOperation._rename("foo"), "foo v1")
assert SaveAsTemplateOperation._rename("foo v2") == "foo v3" self.assertEqual(SaveAsTemplateOperation._rename("foo v2"), "foo v3")
assert SaveAsTemplateOperation._rename("foo v") == "foo v v2" self.assertEqual(SaveAsTemplateOperation._rename("foo v"), "foo v v1")
assert SaveAsTemplateOperation._rename("foo v9") == "foo v10" self.assertEqual(SaveAsTemplateOperation._rename("foo v9"), "foo v10")
assert SaveAsTemplateOperation._rename("foo v111") == "foo v112" self.assertEqual(
SaveAsTemplateOperation._rename("foo v111"), "foo v112")
class ShutdownOperationTestCase(TestCase): class ShutdownOperationTestCase(TestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment