mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2026-12-05 14:43:12 +09:00
Squashed commit of the following:
commit 2e2c4c892ed9e82343ee3b77dac50fb1801f9d74
Merge: 92603fbe9 83d484031
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Sep 15 15:38:25 2025 +0300
Merge branch 'master' into AGDNS-3086-aghnet-use-executil
commit 92603fbe97b9b2cd6f37af3c04ff0b388318b92b
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Sep 8 15:56:05 2025 +0300
aghnet: imp tests
commit d7ff6a1fee4f15106d9af9391e3cfe854a2c490a
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Fri Sep 5 09:24:28 2025 +0300
all: imp code, docs
commit 3fed265d8f42c5f308dba369ee274fb0b21b1933
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Sep 3 16:31:55 2025 +0300
all: aghnet use executil
128 lines
3.0 KiB
Go
128 lines
3.0 KiB
Go
//go:build linux
|
|
|
|
package aghnet
|
|
|
|
import (
|
|
"io/fs"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestHasStaticIP(t *testing.T) {
|
|
const ifaceName = "wlan0"
|
|
|
|
const (
|
|
dhcpcd = "etc/dhcpcd.conf"
|
|
netifaces = "etc/network/interfaces"
|
|
)
|
|
|
|
testCases := []struct {
|
|
rootFsys fs.FS
|
|
name string
|
|
wantHas assert.BoolAssertionFunc
|
|
wantErrMsg string
|
|
}{{
|
|
rootFsys: fstest.MapFS{
|
|
dhcpcd: &fstest.MapFile{
|
|
Data: []byte(`#comment` + nl +
|
|
`# comment` + nl +
|
|
`interface eth0` + nl +
|
|
`static ip_address=192.168.0.1/24` + nl +
|
|
`# interface ` + ifaceName + nl +
|
|
`static ip_address=192.168.1.1/24` + nl +
|
|
`# comment` + nl,
|
|
),
|
|
},
|
|
},
|
|
name: "dhcpcd_has_not",
|
|
wantHas: assert.False,
|
|
wantErrMsg: `no information about static ip`,
|
|
}, {
|
|
rootFsys: fstest.MapFS{
|
|
dhcpcd: &fstest.MapFile{
|
|
Data: []byte(`#comment` + nl +
|
|
`# comment` + nl +
|
|
`interface ` + ifaceName + nl +
|
|
`static ip_address=192.168.0.1/24` + nl +
|
|
`# interface ` + ifaceName + nl +
|
|
`static ip_address=192.168.1.1/24` + nl +
|
|
`# comment` + nl,
|
|
),
|
|
},
|
|
},
|
|
name: "dhcpcd_has",
|
|
wantHas: assert.True,
|
|
wantErrMsg: ``,
|
|
}, {
|
|
rootFsys: fstest.MapFS{
|
|
netifaces: &fstest.MapFile{
|
|
Data: []byte(`allow-hotplug ` + ifaceName + nl +
|
|
`#iface enp0s3 inet static` + nl +
|
|
`# address 192.168.0.200` + nl +
|
|
`# netmask 255.255.255.0` + nl +
|
|
`# gateway 192.168.0.1` + nl +
|
|
`iface ` + ifaceName + ` inet dhcp` + nl,
|
|
),
|
|
},
|
|
},
|
|
name: "netifaces_has_not",
|
|
wantHas: assert.False,
|
|
wantErrMsg: `no information about static ip`,
|
|
}, {
|
|
rootFsys: fstest.MapFS{
|
|
netifaces: &fstest.MapFile{
|
|
Data: []byte(`allow-hotplug ` + ifaceName + nl +
|
|
`iface ` + ifaceName + ` inet static` + nl +
|
|
` address 192.168.0.200` + nl +
|
|
` netmask 255.255.255.0` + nl +
|
|
` gateway 192.168.0.1` + nl +
|
|
`#iface ` + ifaceName + ` inet dhcp` + nl,
|
|
),
|
|
},
|
|
},
|
|
name: "netifaces_has",
|
|
wantHas: assert.True,
|
|
wantErrMsg: ``,
|
|
}, {
|
|
rootFsys: fstest.MapFS{
|
|
netifaces: &fstest.MapFile{
|
|
Data: []byte(`source hello` + nl +
|
|
`#iface ` + ifaceName + ` inet static` + nl,
|
|
),
|
|
},
|
|
"hello": &fstest.MapFile{
|
|
Data: []byte(`iface ` + ifaceName + ` inet static` + nl),
|
|
},
|
|
},
|
|
name: "netifaces_another_file",
|
|
wantHas: assert.True,
|
|
wantErrMsg: ``,
|
|
}, {
|
|
rootFsys: fstest.MapFS{
|
|
netifaces: &fstest.MapFile{
|
|
Data: []byte(`source hello` + nl +
|
|
`iface ` + ifaceName + ` inet static` + nl,
|
|
),
|
|
},
|
|
},
|
|
name: "netifaces_ignore_another",
|
|
wantHas: assert.True,
|
|
wantErrMsg: ``,
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
substRootDirFS(t, tc.rootFsys)
|
|
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
|
has, err := IfaceHasStaticIP(ctx, testCmdCons, ifaceName)
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
|
|
tc.wantHas(t, has)
|
|
})
|
|
}
|
|
}
|