mirror of
https://github.com/tnb-labs/panel.git
synced 2026-10-16 15:42:54 +08:00
- 依赖注入换到 libtnb/wire v0.3.0,各层包导出 Module 并逐类型 Export, wire.Struct 改用 Struct[T](),cleanup 签名改为 func() error - 测试断言换到 libtnb/assert 的 must/check,20 个 testify suite 拆成标准测试函数 - mock 改用 matryer 模板输出到 internal/mocks,wire 走 go.mod tool 指令, mockery 用 go run 调用,避免它的依赖树污染 go.mod - 引入 .golangci.yml 并修完全部告警,darwin 与 linux 双平台 0 issues - 工作流改用 go-version-file、govulncheck 官方 action, wire 校验移入 test.yml,mockery 由自动提交改为 PR 校验 顺带修复:pkg/db 四处漏查 rows.Err()(DatabaseExists 查询出错会被当成库不存在)、 websitestat 负值转 uint64 污染流量统计、pty 终端尺寸超 uint16 截断、 文件权限位超 32 位静默截断、面板与 ACME challenge 服务器缺 ReadHeaderTimeout、 GetDefault 丢失操作人、三处对结构体值恒成立的假断言、两处带空格而失效的 nolint Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
63 lines
1.9 KiB
Go
63 lines
1.9 KiB
Go
package network
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/libtnb/assert/check"
|
|
"github.com/libtnb/assert/must"
|
|
)
|
|
|
|
func TestValidate(t *testing.T) {
|
|
valid := Config{
|
|
Name: "eth0",
|
|
MTU: 1500,
|
|
IPv4: FamilyConfig{Mode: ModeManual, Addresses: []string{"192.0.2.10/24"}, Gateway: "198.51.100.1", DNS: []string{"1.1.1.1"}},
|
|
IPv6: FamilyConfig{Mode: ModeDisabled},
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
change func(*Config)
|
|
}{
|
|
{name: "invalid interface", change: func(config *Config) { config.Name = "../eth0" }},
|
|
{name: "invalid CIDR", change: func(config *Config) { config.IPv4.Addresses = []string{"192.0.2.10"} }},
|
|
{name: "wrong family", change: func(config *Config) { config.IPv4.Gateway = "2001:db8::1" }},
|
|
{name: "duplicate DNS", change: func(config *Config) { config.IPv4.DNS = []string{"1.1.1.1", "1.1.1.1"} }},
|
|
{name: "manual without address", change: func(config *Config) { config.IPv4.Addresses = nil }},
|
|
{name: "both disabled", change: func(config *Config) { config.IPv4 = FamilyConfig{Mode: ModeDisabled} }},
|
|
{name: "invalid MTU", change: func(config *Config) { config.MTU = 67 }},
|
|
}
|
|
must.NoError(t, Validate(valid))
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
config := valid
|
|
test.change(&config)
|
|
check.ErrorIs(t, Validate(config), ErrValidation)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIfupdownParse(t *testing.T) {
|
|
backend := &ifupdownBackend{}
|
|
file := backend.parse("/etc/network/interfaces", `auto eth0
|
|
iface eth0 inet dhcp
|
|
dns-nameservers 1.1.1.1
|
|
mtu 1500
|
|
|
|
iface eth0 inet6 static
|
|
address 2001:db8::10/64
|
|
`)
|
|
must.Len(t, file.stanzas, 2)
|
|
check.DeepEqual(t, backend.family(file.stanzas, "inet"), FamilyConfig{
|
|
Mode: ModeAuto,
|
|
Addresses: []string{},
|
|
AutoDNS: true,
|
|
DNS: []string{"1.1.1.1"},
|
|
})
|
|
check.DeepEqual(t, backend.family(file.stanzas, "inet6"), FamilyConfig{
|
|
Mode: ModeManual,
|
|
Addresses: []string{"2001:db8::10/64"},
|
|
AutoDNS: true,
|
|
DNS: []string{},
|
|
})
|
|
}
|