Address review feedback

This commit is contained in:
Brian Tiemann
2026-03-06 20:13:29 -05:00
parent 229a154211
commit 7eb108244c
2 changed files with 52 additions and 2 deletions

View File

@@ -239,7 +239,8 @@ class ModuleSerializer(PrimaryModelSerializer):
if template.name.count(MODULE_TOKEN) != len(module_bays):
raise serializers.ValidationError(
_(
"Cannot install module with {tokens} placeholder(s) in a module bay at depth {level}."
"Cannot install module with placeholder values in a module bay tree {level} in tree "
"but {tokens} placeholders given."
).format(
level=len(module_bays), tokens=template.name.count(MODULE_TOKEN)
)
@@ -257,7 +258,7 @@ class ModuleSerializer(PrimaryModelSerializer):
)
)
if not adopt_components and replicate_components and resolved_name in installed_components:
if not adopt_components and resolved_name in installed_components:
raise serializers.ValidationError(
_("A {model} named {name} already exists").format(
model=template.component_model.__name__,

View File

@@ -1882,6 +1882,55 @@ class ModuleTest(APIViewTestCases.APIViewTestCase):
# eth1 should have been created
self.assertTrue(device.interfaces.filter(name='eth1').exists())
def test_module_token_no_position(self):
"""
Installing a module whose type has a template with a MODULE_TOKEN placeholder into a
module bay with no position defined should return a validation error.
"""
self.add_permissions('dcim.add_module')
manufacturer = Manufacturer.objects.get(name='Generic')
device = create_test_device('Device for Token No-Position Test')
module_type = ModuleType.objects.create(manufacturer=manufacturer, model='Token No-Position Module Type')
# Template name contains the MODULE_TOKEN placeholder
InterfaceTemplate.objects.create(
module_type=module_type, name=f'{MODULE_TOKEN}-eth0', type='1000base-t'
)
# Module bay has no position
module_bay = ModuleBay.objects.create(device=device, name='No-Position Bay')
url = reverse('dcim-api:module-list')
data = {
'device': device.pk,
'module_bay': module_bay.pk,
'module_type': module_type.pk,
}
response = self.client.post(url, data, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
def test_module_token_depth_mismatch(self):
"""
Installing a module whose template name has more MODULE_TOKEN placeholders than the
depth of the module bay tree should return a validation error.
"""
self.add_permissions('dcim.add_module')
manufacturer = Manufacturer.objects.get(name='Generic')
device = create_test_device('Device for Token Depth Mismatch Test')
module_type = ModuleType.objects.create(manufacturer=manufacturer, model='Token Depth Mismatch Module Type')
# Template name has two placeholders but the bay is at depth 1
InterfaceTemplate.objects.create(
module_type=module_type, name=f'{MODULE_TOKEN}-{MODULE_TOKEN}-eth0', type='1000base-t'
)
module_bay = ModuleBay.objects.create(device=device, name='Depth 1 Bay', position='1')
url = reverse('dcim-api:module-list')
data = {
'device': device.pk,
'module_bay': module_bay.pk,
'module_type': module_type.pk,
}
response = self.client.post(url, data, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
class ConsolePortTest(Mixins.ComponentTraceMixin, APIViewTestCases.APIViewTestCase):
model = ConsolePort