fix(api): 服务器显示名称限 64 个可见字符,拒绝换行、控制符和不可见格式符
This commit is contained in:
19 files changed
+197
-20
No files matched your search
+5
-1
@@ -160,6 +160,10 @@ func buildMinecraftServerFromApplyRequest(req applyRequest, namespace string) (*
|
||||
if err := naming.ValidateServerName(req.Subdomain); err != nil {
|
||||
return nil, fmt.Errorf("invalid subdomain: %w", err)
|
||||
}
|
||||
displayName, err := naming.CleanDisplayName(req.DisplayName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid displayName: %w", err)
|
||||
}
|
||||
if strings.TrimSpace(req.Image) == "" {
|
||||
return nil, fmt.Errorf("image is required")
|
||||
}
|
||||
@@ -229,7 +233,7 @@ func buildMinecraftServerFromApplyRequest(req applyRequest, namespace string) (*
|
||||
},
|
||||
Spec: v1alpha1.MinecraftServerSpec{
|
||||
Subdomain: req.Subdomain,
|
||||
DisplayName: req.DisplayName,
|
||||
DisplayName: displayName,
|
||||
Image: req.Image,
|
||||
JavaMemory: deriveApplyJavaHeap(memLim),
|
||||
DesiredState: v1alpha1.DesiredStopped,
|
||||
|
||||
+14
-5
@@ -140,11 +140,12 @@ func TestDeriveApplyJavaHeap(t *testing.T) {
|
||||
|
||||
func TestBuildMinecraftServerFromApplyRequest_Valid(t *testing.T) {
|
||||
req := applyRequest{
|
||||
Name: "test-server",
|
||||
Subdomain: "test-server",
|
||||
Image: "registry.felis.svc/paper:1.21",
|
||||
Memory: "4Gi",
|
||||
Storage: "20Gi",
|
||||
Name: "test-server",
|
||||
Subdomain: "test-server",
|
||||
DisplayName: " Test Server ",
|
||||
Image: "registry.felis.svc/paper:1.21",
|
||||
Memory: "4Gi",
|
||||
Storage: "20Gi",
|
||||
}
|
||||
ms, err := buildMinecraftServerFromApplyRequest(req, "minecraft")
|
||||
if err != nil {
|
||||
@@ -159,6 +160,9 @@ func TestBuildMinecraftServerFromApplyRequest_Valid(t *testing.T) {
|
||||
if ms.Spec.Subdomain != "test-server" {
|
||||
t.Errorf("Subdomain = %q", ms.Spec.Subdomain)
|
||||
}
|
||||
if ms.Spec.DisplayName != "Test Server" {
|
||||
t.Errorf("DisplayName = %q, want it trimmed to Test Server", ms.Spec.DisplayName)
|
||||
}
|
||||
if ms.Spec.Image != "registry.felis.svc/paper:1.21" {
|
||||
t.Errorf("Image = %q", ms.Spec.Image)
|
||||
}
|
||||
@@ -283,6 +287,11 @@ func TestBuildMinecraftServerFromApplyRequest_Errors(t *testing.T) {
|
||||
applyRequest{Name: ok, Subdomain: "", Image: "x", Memory: "1Gi", Storage: "1Gi"},
|
||||
"invalid subdomain",
|
||||
},
|
||||
{
|
||||
"display name with a tab",
|
||||
applyRequest{Name: ok, Subdomain: ok, DisplayName: "a" + string(rune(0x09)) + "b", Image: "x", Memory: "1Gi", Storage: "1Gi"},
|
||||
"invalid displayName",
|
||||
},
|
||||
{
|
||||
"empty image",
|
||||
applyRequest{Name: ok, Subdomain: ok, Image: "", Memory: "1Gi", Storage: "1Gi"},
|
||||
|
||||
Reference in new issue
Block a user