2025-11-06 14:53:58 +01:00
|
|
|
from unittest.mock import Mock
|
|
|
|
|
|
2025-11-06 15:06:10 +01:00
|
|
|
import jsonschema
|
2025-11-06 14:53:58 +01:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
|
|
from servala.core.crd import generate_custom_form_class
|
2025-11-06 15:06:10 +01:00
|
|
|
from servala.core.forms import ServiceDefinitionAdminForm
|
2025-11-06 14:53:58 +01:00
|
|
|
from servala.core.models import ControlPlaneCRD
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_custom_model_form_class_is_none_when_no_form_config():
|
|
|
|
|
crd = Mock(spec=ControlPlaneCRD)
|
|
|
|
|
service_def = Mock()
|
|
|
|
|
service_def.form_config = None
|
|
|
|
|
crd.service_definition = service_def
|
|
|
|
|
crd.django_model = Mock()
|
|
|
|
|
|
|
|
|
|
if not (
|
|
|
|
|
crd.django_model
|
|
|
|
|
and crd.service_definition
|
|
|
|
|
and crd.service_definition.form_config
|
|
|
|
|
and crd.service_definition.form_config.get("fieldsets")
|
|
|
|
|
):
|
|
|
|
|
result = None
|
|
|
|
|
else:
|
|
|
|
|
result = generate_custom_form_class(
|
|
|
|
|
crd.service_definition.form_config, crd.django_model
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert result is None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_custom_model_form_class_returns_class_when_form_config_exists():
|
|
|
|
|
|
|
|
|
|
crd = Mock(spec=ControlPlaneCRD)
|
|
|
|
|
service_def = Mock()
|
|
|
|
|
service_def.form_config = {
|
|
|
|
|
"fieldsets": [
|
|
|
|
|
{
|
|
|
|
|
"title": "General",
|
|
|
|
|
"fields": [
|
|
|
|
|
{
|
|
|
|
|
"type": "text",
|
|
|
|
|
"label": "Name",
|
|
|
|
|
"controlplane_field_mapping": "name",
|
|
|
|
|
"required": True,
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
crd.service_definition = service_def
|
|
|
|
|
|
|
|
|
|
class TestModel(models.Model):
|
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
app_label = "test"
|
|
|
|
|
|
|
|
|
|
crd.django_model = TestModel
|
|
|
|
|
|
|
|
|
|
if not (
|
|
|
|
|
crd.django_model
|
|
|
|
|
and crd.service_definition
|
|
|
|
|
and crd.service_definition.form_config
|
|
|
|
|
and crd.service_definition.form_config.get("fieldsets")
|
|
|
|
|
):
|
|
|
|
|
result = None
|
|
|
|
|
else:
|
|
|
|
|
result = generate_custom_form_class(
|
|
|
|
|
crd.service_definition.form_config, crd.django_model
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert result is not None
|
|
|
|
|
assert hasattr(result, "form_config")
|
2025-11-06 15:06:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_form_config_schema_validates_minimal_config():
|
|
|
|
|
form = ServiceDefinitionAdminForm()
|
|
|
|
|
schema = form.form_config_schema
|
|
|
|
|
|
|
|
|
|
minimal_config = {
|
|
|
|
|
"fieldsets": [
|
|
|
|
|
{
|
|
|
|
|
"fields": [
|
|
|
|
|
{
|
|
|
|
|
"type": "text",
|
|
|
|
|
"label": "Service Name",
|
|
|
|
|
"controlplane_field_mapping": "spec.serviceName",
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonschema.validate(instance=minimal_config, schema=schema)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_form_config_schema_validates_config_with_null_integers():
|
|
|
|
|
form = ServiceDefinitionAdminForm()
|
|
|
|
|
schema = form.form_config_schema
|
|
|
|
|
|
|
|
|
|
config_with_nulls = {
|
|
|
|
|
"fieldsets": [
|
|
|
|
|
{
|
|
|
|
|
"fields": [
|
|
|
|
|
{
|
|
|
|
|
"type": "text",
|
|
|
|
|
"label": "Service Name",
|
|
|
|
|
"controlplane_field_mapping": "spec.serviceName",
|
|
|
|
|
"max_length": None,
|
|
|
|
|
"required": False,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "textarea",
|
|
|
|
|
"label": "Description",
|
|
|
|
|
"controlplane_field_mapping": "spec.description",
|
|
|
|
|
"rows": None,
|
|
|
|
|
"max_length": None,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "number",
|
|
|
|
|
"label": "Port",
|
|
|
|
|
"controlplane_field_mapping": "spec.port",
|
|
|
|
|
"min_value": None,
|
|
|
|
|
"max_value": None,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "array",
|
|
|
|
|
"label": "Tags",
|
|
|
|
|
"controlplane_field_mapping": "spec.tags",
|
|
|
|
|
"min_values": None,
|
|
|
|
|
"max_values": None,
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonschema.validate(instance=config_with_nulls, schema=schema)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_form_config_schema_validates_full_config():
|
|
|
|
|
form = ServiceDefinitionAdminForm()
|
|
|
|
|
schema = form.form_config_schema
|
|
|
|
|
|
|
|
|
|
full_config = {
|
|
|
|
|
"fieldsets": [
|
|
|
|
|
{
|
|
|
|
|
"title": "Service Configuration",
|
|
|
|
|
"fields": [
|
|
|
|
|
{
|
|
|
|
|
"type": "text",
|
|
|
|
|
"label": "Service Name",
|
|
|
|
|
"controlplane_field_mapping": "spec.serviceName",
|
|
|
|
|
"help_text": "Enter a unique service name",
|
|
|
|
|
"required": True,
|
|
|
|
|
"max_length": 100,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "email",
|
|
|
|
|
"label": "Admin Email",
|
|
|
|
|
"controlplane_field_mapping": "spec.adminEmail",
|
|
|
|
|
"help_text": "Contact email for service administrator",
|
|
|
|
|
"required": True,
|
|
|
|
|
"max_length": 255,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "textarea",
|
|
|
|
|
"label": "Description",
|
|
|
|
|
"controlplane_field_mapping": "spec.description",
|
|
|
|
|
"help_text": "Describe the service purpose",
|
|
|
|
|
"required": False,
|
|
|
|
|
"rows": 5,
|
|
|
|
|
"max_length": 500,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "number",
|
|
|
|
|
"label": "Port",
|
|
|
|
|
"controlplane_field_mapping": "spec.port",
|
|
|
|
|
"help_text": "Service port number",
|
|
|
|
|
"required": True,
|
|
|
|
|
"min_value": 1,
|
|
|
|
|
"max_value": 65535,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "choice",
|
|
|
|
|
"label": "Environment",
|
|
|
|
|
"controlplane_field_mapping": "spec.environment",
|
|
|
|
|
"help_text": "Deployment environment",
|
|
|
|
|
"required": True,
|
|
|
|
|
"choices": [
|
|
|
|
|
["dev", "Development"],
|
|
|
|
|
["staging", "Staging"],
|
|
|
|
|
["prod", "Production"],
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "checkbox",
|
|
|
|
|
"label": "Enable Monitoring",
|
|
|
|
|
"controlplane_field_mapping": "spec.monitoring.enabled",
|
|
|
|
|
"help_text": "Enable service monitoring",
|
|
|
|
|
"required": False,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "array",
|
|
|
|
|
"label": "Tags",
|
|
|
|
|
"controlplane_field_mapping": "spec.tags",
|
|
|
|
|
"help_text": "Service tags for organization",
|
|
|
|
|
"required": False,
|
|
|
|
|
"min_values": 0,
|
|
|
|
|
"max_values": 10,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
jsonschema.validate(instance=full_config, schema=schema)
|