mirror of
https://github.com/tnb-labs/panel.git
synced 2026-09-01 14:55:12 +08:00
9dac05e25c
三个网络管理器改为 backend 接口实现,各自的辅助方法收进对应结构体, 不再由包级函数经 switch 分发;探测结果缓存,更新时只加载目标网卡。 新增两阶段提交:应用后校验配置是否真正生效,随后进入待确认状态, 30 秒内未确认则自动回滚,避免配错网卡导致远程失联无法恢复。 修复 NetworkManager 读写 MTU 未带设置名导致网卡全部不可编辑, 以及 netplan set 键含点号时 VLAN 网卡配置被写入错误层级。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
53 lines
1.7 KiB
Go
53 lines
1.7 KiB
Go
package network
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
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 }},
|
|
}
|
|
require.NoError(t, Validate(valid))
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
config := valid
|
|
test.change(&config)
|
|
assert.Error(t, Validate(config))
|
|
})
|
|
}
|
|
}
|
|
|
|
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
|
|
`)
|
|
require.Len(t, file.stanzas, 2)
|
|
assert.Equal(t, ModeAuto, backend.family(file.stanzas, "inet").Mode)
|
|
assert.Equal(t, []string{"2001:db8::10/64"}, backend.family(file.stanzas, "inet6").Addresses)
|
|
}
|