cloner: Fix some corner cases with name generation

Signed-off-by: Cole Robinson <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2020-09-05 08:51:31 -04:00
parent 5c1a1076c0
commit b9de8ad919
2 changed files with 15 additions and 3 deletions

View File

@ -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"

View File

@ -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(