diff --git a/tests/test_cloner.py b/tests/test_cloner.py index 6f0843003..01df2fbf6 100644 --- a/tests/test_cloner.py +++ b/tests/test_cloner.py @@ -42,3 +42,14 @@ def test_clone_unmanaged(): assert open(tmp1.name).read() == open(inp1).read() assert open(tmp2.name).read() == open(inp2).read() + + +def test_generate_name(): + conn = utils.URIs.open_testdriver_cached() + def _g(n): + return Cloner.generate_clone_name(conn, n) + + assert _g("test") == "test-clone1" + assert _g("test-clone-simple") == "test-clone-simple-clone" + assert _g("test-clone-simple-clone") == "test-clone-simple-clone1" + assert _g("test-clone-simple-clone5") == "test-clone-simple-clone6" diff --git a/virtinst/cloner.py b/virtinst/cloner.py index 3435db7f6..6d160e619 100644 --- a/virtinst/cloner.py +++ b/virtinst/cloner.py @@ -53,15 +53,16 @@ def _generate_clone_name(conn, basename): If the orig name is "foo-clone", we don't want the clone to be "foo-clone-clone", we want "foo-clone1" """ - match = re.search("-clone[1-9]*$", basename) + regex = r"-clone[1-9]*$" + match = re.search(regex, basename) start_num = 1 force_num = False if match: num_match = re.search("[1-9]+$", match.group()) + force_num = True if num_match: start_num = int(str(num_match.group())) + 1 - force_num = True - basename = basename.replace(match.group(), "") + basename = basename[:match.start()] def cb(n): return generatename.check_libvirt_collision(