Skip to content

Commit 7924011

Browse files
committed
⭐️ add linux tests
Signed-off-by: Salim Afiune Maya <afiune@mondoo.com>
1 parent 76c2551 commit 7924011

4 files changed

Lines changed: 96 additions & 2 deletions

File tree

providers/os/id/networki/darwin_n.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import (
99
"strconv"
1010
"strings"
1111

12+
"go.mondoo.com/cnquery/v11/providers-sdk/v1/util/convert"
13+
1214
"github.com/cockroachdb/errors"
1315
"github.com/rs/zerolog/log"
1416
"github.com/spf13/afero"
15-
"go.mondoo.com/cnquery/v11/providers-sdk/v1/util/convert"
1617
"howett.net/plist"
1718
)
1819

providers/os/id/networki/interfaces_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,50 @@ func TestInterfacesDarwin(t *testing.T) {
6060
}
6161
}
6262
}
63+
64+
func TestInterfacesLinux(t *testing.T) {
65+
conn, err := mock.New(0, "./testdata/linux.toml", &inventory.Asset{})
66+
require.NoError(t, err)
67+
platform, ok := detector.DetectOS(conn)
68+
require.True(t, ok)
69+
70+
interfaces, err := subject.Interfaces(conn, platform)
71+
require.NoError(t, err)
72+
assert.Len(t, interfaces, 2)
73+
74+
index := subject.FindInterface(interfaces, subject.Interface{Name: "enX0"})
75+
if assert.NotEqual(t, -1, index) {
76+
enX0 := interfaces[index]
77+
assert.Equal(t, "enX0", enX0.Name)
78+
assert.Equal(t, "0a:ff:de:6b:e3:19", enX0.MACAddress)
79+
assert.Equal(t, "", enX0.Vendor)
80+
assert.Equal(t, 9001, enX0.MTU)
81+
if assert.NotNil(t, enX0.Active) {
82+
assert.True(t, *enX0.Active)
83+
}
84+
if assert.NotNil(t, enX0.Virtual) {
85+
assert.False(t, *enX0.Virtual)
86+
}
87+
assert.Equal(t, []string{"BROADCAST", "MULTICAST", "UP", "LOWER_UP"}, enX0.Flags)
88+
if assert.NotEmpty(t, enX0.IPAddresses) {
89+
i4 := enX0.FindIP(net.ParseIP("172.31.24.71"))
90+
if assert.NotEqual(t, -1, i4) {
91+
ipv4 := enX0.IPAddresses[i4]
92+
assert.Equal(t, "172.31.24.71", ipv4.IP.String())
93+
assert.Equal(t, "172.31.24.71/20", ipv4.CIDR)
94+
assert.Equal(t, "172.31.16.0/20", ipv4.Subnet)
95+
assert.Equal(t, "172.31.31.255", ipv4.Broadcast)
96+
assert.Equal(t, "172.31.16.1", ipv4.Gateway)
97+
}
98+
i6 := enX0.FindIP(net.ParseIP("fe80::8ff:deff:fe6b:e319"))
99+
if assert.NotEqual(t, -1, i6) {
100+
ipv6 := enX0.IPAddresses[i6]
101+
assert.Equal(t, "fe80::8ff:deff:fe6b:e319", ipv6.IP.String())
102+
assert.Equal(t, "fe80::8ff:deff:fe6b:e319/64", ipv6.CIDR)
103+
assert.Equal(t, "fe80::/64", ipv6.Subnet)
104+
assert.Equal(t, "", ipv6.Broadcast)
105+
assert.Equal(t, "", ipv6.Gateway)
106+
}
107+
}
108+
}
109+
}

providers/os/id/networki/linux_n.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010
"strconv"
1111
"strings"
1212

13+
"go.mondoo.com/cnquery/v11/providers-sdk/v1/util/convert"
14+
1315
"github.com/cockroachdb/errors"
1416
"github.com/rs/zerolog/log"
1517
"github.com/spf13/afero"
16-
"go.mondoo.com/cnquery/v11/providers-sdk/v1/util/convert"
1718
)
1819

1920
// detectLinuxInterfaces detects network interfaces on Linux.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
[commands."uname -s"]
2+
stdout = "Linux"
3+
4+
[commands."uname -m"]
5+
stdout = "x86_64"
6+
7+
[commands."uname -r"]
8+
stdout = "4.9.125-linuxkit"
9+
10+
[files."/sys/class/net/enX0/address"]
11+
content = "0a:ff:de:6b:e3:19"
12+
13+
[files."/sys/class/net/enX0/operstate"]
14+
content = "up"
15+
16+
[commands."ip addr show"]
17+
stdout = """
18+
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
19+
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
20+
inet 127.0.0.1/8 scope host lo
21+
valid_lft forever preferred_lft forever
22+
inet6 ::1/128 scope host noprefixroute
23+
valid_lft forever preferred_lft forever
24+
2: enX0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc fq_codel state UP group default qlen 1000
25+
link/ether 0a:ff:de:6b:e3:19 brd ff:ff:ff:ff:ff:ff
26+
altname eni-087a980ef8e9331bc
27+
altname device-number-0.0
28+
inet 172.31.24.71/20 metric 512 brd 172.31.31.255 scope global dynamic enX0
29+
valid_lft 3284sec preferred_lft 3284sec
30+
inet6 fe80::8ff:deff:fe6b:e319/64 scope link proto kernel_ll
31+
valid_lft forever preferred_lft forever
32+
"""
33+
34+
[commands."ip route show"]
35+
stdout = """
36+
default via 172.31.16.1 dev enX0 proto dhcp src 172.31.24.71 metric 512
37+
172.31.0.2 via 172.31.16.1 dev enX0 proto dhcp src 172.31.24.71 metric 512
38+
172.31.16.0/20 dev enX0 proto kernel scope link src 172.31.24.71 metric 512
39+
172.31.16.1 dev enX0 proto dhcp scope link src 172.31.24.71 metric 512
40+
"""
41+
42+
[commands."ip -6 route show"]
43+
stdout = """
44+
fe80::/64 dev enX0 proto kernel metric 256 pref medium
45+
"""

0 commit comments

Comments
 (0)