2026-07-23 12:04:37 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
2026-07-27 15:16:57 +08:00
|
|
|
func intPtr(v int) *int { return &v }
|
|
|
|
|
|
2026-07-23 12:04:37 +08:00
|
|
|
func TestValidateDeliveryInput(t *testing.T) {
|
2026-07-27 15:16:57 +08:00
|
|
|
dataDisks := []string{"/data", "/disk1"}
|
|
|
|
|
valid := MySQLDeliveryInput{BusinessLineID: 1, TargetID: 1, Namespace: "team-a", InstanceName: "mysql-01", CPUMilli: 500, MemoryMi: 2048, StorageGi: 20}
|
|
|
|
|
if err := validateDeliveryInput(valid, dataDisks); err != nil {
|
2026-07-23 12:04:37 +08:00
|
|
|
t.Fatalf("valid input rejected: %v", err)
|
|
|
|
|
}
|
2026-07-27 15:16:57 +08:00
|
|
|
full := valid
|
|
|
|
|
full.MySQLVersion = "8.0"
|
|
|
|
|
full.Topology = "standalone"
|
|
|
|
|
full.MySQLPort = 13306
|
|
|
|
|
full.DataDisk = "/disk1"
|
|
|
|
|
full.MemoryMi = 8192
|
|
|
|
|
full.StorageGi = 2000
|
|
|
|
|
full.Timezone = "+08:00"
|
|
|
|
|
full.LowerCaseTableNames = intPtr(0)
|
|
|
|
|
full.CharacterSet = "utf8mb4"
|
|
|
|
|
full.Collation = "utf8mb4_general_ci"
|
|
|
|
|
full.MaxConnections = "auto"
|
|
|
|
|
full.InnodbRedoLogCapacity = "256M"
|
|
|
|
|
full.InnodbFlushLogAtTrxCommit = intPtr(2)
|
|
|
|
|
full.SyncBinlog = intPtr(0)
|
|
|
|
|
full.InnodbIOCapacity = 2000
|
|
|
|
|
full.LongQueryTime = 0.5
|
|
|
|
|
full.BinlogExpireLogsSeconds = 604800
|
|
|
|
|
full.MaxBinlogSize = "512M"
|
|
|
|
|
if err := validateDeliveryInput(full, dataDisks); err != nil {
|
|
|
|
|
t.Fatalf("valid full input rejected: %v", err)
|
|
|
|
|
}
|
|
|
|
|
namedZone := valid
|
|
|
|
|
namedZone.Timezone = "Asia/Shanghai"
|
|
|
|
|
if err := validateDeliveryInput(namedZone, dataDisks); err != nil {
|
|
|
|
|
t.Fatalf("named timezone rejected: %v", err)
|
|
|
|
|
}
|
2026-07-27 15:18:54 +08:00
|
|
|
lts := valid
|
|
|
|
|
lts.MySQLVersion = "8.4"
|
|
|
|
|
if err := validateDeliveryInput(lts, dataDisks); err != nil {
|
|
|
|
|
t.Fatalf("8.4 LTS rejected: %v", err)
|
|
|
|
|
}
|
2026-07-27 15:16:57 +08:00
|
|
|
for name, mutate := range map[string]func(*MySQLDeliveryInput){
|
|
|
|
|
"uppercase namespace": func(in *MySQLDeliveryInput) { in.Namespace = "Team-A" },
|
|
|
|
|
"bad instance": func(in *MySQLDeliveryInput) { in.InstanceName = "mysql_01" },
|
|
|
|
|
"too little memory": func(in *MySQLDeliveryInput) { in.MemoryMi = 1024 },
|
|
|
|
|
"too much memory": func(in *MySQLDeliveryInput) { in.MemoryMi = 131072 },
|
|
|
|
|
"too little storage": func(in *MySQLDeliveryInput) { in.StorageGi = 10 },
|
|
|
|
|
"too much storage": func(in *MySQLDeliveryInput) { in.StorageGi = 4000 },
|
|
|
|
|
"unsupported version": func(in *MySQLDeliveryInput) { in.MySQLVersion = "5.7" },
|
2026-07-27 15:18:54 +08:00
|
|
|
"eol version": func(in *MySQLDeliveryInput) { in.MySQLVersion = "5.6" },
|
2026-07-27 15:16:57 +08:00
|
|
|
"unsupported topology": func(in *MySQLDeliveryInput) { in.Topology = "mgr_3" },
|
|
|
|
|
"port below pool": func(in *MySQLDeliveryInput) { in.MySQLPort = 3307 },
|
|
|
|
|
"port above pool": func(in *MySQLDeliveryInput) { in.MySQLPort = 14000 },
|
|
|
|
|
"data disk not in list": func(in *MySQLDeliveryInput) { in.DataDisk = "/mnt/other" },
|
|
|
|
|
"bad timezone": func(in *MySQLDeliveryInput) { in.Timezone = "UTC+8" },
|
|
|
|
|
"bad lower case": func(in *MySQLDeliveryInput) { in.LowerCaseTableNames = intPtr(2) },
|
|
|
|
|
"bad charset": func(in *MySQLDeliveryInput) { in.CharacterSet = "big5" },
|
|
|
|
|
"collation mismatch": func(in *MySQLDeliveryInput) { in.CharacterSet = "gbk"; in.Collation = "utf8mb4_general_ci" },
|
|
|
|
|
"collation vs default": func(in *MySQLDeliveryInput) { in.Collation = "gbk_chinese_ci" },
|
|
|
|
|
"bad max connections": func(in *MySQLDeliveryInput) { in.MaxConnections = "300" },
|
|
|
|
|
"bad redo capacity": func(in *MySQLDeliveryInput) { in.InnodbRedoLogCapacity = "2G" },
|
|
|
|
|
"bad flush log": func(in *MySQLDeliveryInput) { in.InnodbFlushLogAtTrxCommit = intPtr(3) },
|
|
|
|
|
"bad sync binlog": func(in *MySQLDeliveryInput) { in.SyncBinlog = intPtr(2) },
|
|
|
|
|
"bad io capacity": func(in *MySQLDeliveryInput) { in.InnodbIOCapacity = 500 },
|
|
|
|
|
"bad long query time": func(in *MySQLDeliveryInput) { in.LongQueryTime = 3 },
|
|
|
|
|
"bad binlog expire": func(in *MySQLDeliveryInput) { in.BinlogExpireLogsSeconds = 3600 },
|
|
|
|
|
"bad max binlog size": func(in *MySQLDeliveryInput) { in.MaxBinlogSize = "64M" },
|
2026-07-23 12:04:37 +08:00
|
|
|
} {
|
2026-07-27 15:16:57 +08:00
|
|
|
input := valid
|
|
|
|
|
mutate(&input)
|
|
|
|
|
if err := validateDeliveryInput(input, dataDisks); err == nil {
|
2026-07-23 12:04:37 +08:00
|
|
|
t.Errorf("%s was accepted", name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-27 15:16:57 +08:00
|
|
|
|
|
|
|
|
func TestAllocatePort(t *testing.T) {
|
|
|
|
|
if port, err := allocatePort(0, nil); err != nil || port != mysqlPortPoolStart {
|
|
|
|
|
t.Fatalf("expected first pool port %d, got %d err=%v", mysqlPortPoolStart, port, err)
|
|
|
|
|
}
|
|
|
|
|
if port, err := allocatePort(0, []int{13306, 13307}); err != nil || port != 13308 {
|
|
|
|
|
t.Fatalf("expected 13308 skipping occupied, got %d err=%v", port, err)
|
|
|
|
|
}
|
|
|
|
|
if port, err := allocatePort(13400, []int{13306}); err != nil || port != 13400 {
|
|
|
|
|
t.Fatalf("expected requested port 13400, got %d err=%v", port, err)
|
|
|
|
|
}
|
|
|
|
|
if _, err := allocatePort(13306, []int{13306}); err == nil {
|
|
|
|
|
t.Fatal("requested occupied port was accepted")
|
|
|
|
|
}
|
|
|
|
|
used := make([]int, 0, mysqlPortPoolEnd-mysqlPortPoolStart+1)
|
|
|
|
|
for p := mysqlPortPoolStart; p <= mysqlPortPoolEnd; p++ {
|
|
|
|
|
used = append(used, p)
|
|
|
|
|
}
|
|
|
|
|
if _, err := allocatePort(0, used); err == nil {
|
|
|
|
|
t.Fatal("exhausted pool still allocated a port")
|
|
|
|
|
}
|
|
|
|
|
}
|