diff --git a/instances/tests/test_new_disk_name.py b/instances/tests/test_new_disk_name.py index 8cc6d786..76435381 100644 --- a/instances/tests/test_new_disk_name.py +++ b/instances/tests/test_new_disk_name.py @@ -6,9 +6,10 @@ from unittest.mock import patch from computes.models import Compute from django.contrib.auth import get_user_model -from django.test import TestCase +from django.test import SimpleTestCase, TestCase from django.urls import reverse from instances.models import Instance +from instances.utils import get_new_disk_dev def dev(name, bus): @@ -84,3 +85,12 @@ class NewDiskNameTestCase(TestCase): def test_a_cdrom_without_media_is_empty(self): call = self.attach("add_cdrom", {"bus": "scsi", "media": ""}, status=1) self.assertEqual((call.kwargs["target_bus"], call.args[1]), ("scsi", "")) + + +class BusPrefixTestCase(SimpleTestCase): + def test_each_bus_names_its_disks_with_its_prefix(self): + for bus, name in (("virtio", "vdb"), ("ide", "hdb"), ("fdc", "fdb"), ("sata", "sdb"), ("scsi", "sdb"), + ("usb", "sdb"), ("xen", "sdb")): + with self.subTest(bus=bus): + taken = [{"dev": name[:2] + "a"}] + self.assertEqual(get_new_disk_dev(taken, [], bus), name) # a CD-ROM's name is taken too diff --git a/instances/utils.py b/instances/utils.py index 281c684d..bad97e42 100644 --- a/instances/utils.py +++ b/instances/utils.py @@ -18,7 +18,7 @@ from logs.audit import reason, write from operations.models import Operation from vrtManager.connection import other_hosts, wvmConnect from vrtManager.instance import wvmInstance, wvmInstances -from vrtManager.util import ConnectionFailed, message +from vrtManager.util import ConnectionFailed, message, vol_dev_type from .models import Instance @@ -206,14 +206,7 @@ def check_user_quota(user, instance, cpu, memory, disk_size): def get_new_disk_dev(media, disks, bus): existing_disk_devs = [] existing_media_devs = [] - if bus == "virtio": - dev_base = "vd" - elif bus == "ide": - dev_base = "hd" - elif bus == "fdc": - dev_base = "fd" - else: - dev_base = "sd" + dev_base = vol_dev_type(bus) or "sd" if disks: existing_disk_devs = [disk["dev"] for disk in disks]