Skip to content

Commit bc8307a

Browse files
committed
tests: add util/net coverage
Signed-off-by: Francesco Giudici <[email protected]>
1 parent a278cc6 commit bc8307a

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

pkg/util/net_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
Copyright © 2022 - 2024 SUSE LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"net"
23+
24+
. "github.com/onsi/ginkgo/v2"
25+
. "github.com/onsi/gomega"
26+
27+
utilmocks "github.com/rancher/elemental-operator/pkg/util/mocks"
28+
gomock "go.uber.org/mock/gomock"
29+
)
30+
31+
var _ = Describe("GetIPByIfName", func() {
32+
var ctrl *gomock.Controller
33+
var netctrl *utilmocks.MockNetController
34+
35+
BeforeEach(func() {
36+
ctrl = gomock.NewController(GinkgoT())
37+
netctrl = utilmocks.NewMockNetController(ctrl)
38+
})
39+
It("should return IPv4 address if available", func() {
40+
netctrl.EXPECT().GetInterfaceAddresses("eth0").Return(
41+
[]net.IPNet{
42+
{
43+
IP: []byte{0x20, 0x1, 0x11, 0x11, 0x22, 0x22, 0x33, 0x33,
44+
0x44, 0x44, 0x55, 0x55, 0x66, 0x66, 0x77, 0x77},
45+
},
46+
{
47+
IP: []byte{192, 168, 1, 10},
48+
},
49+
},
50+
)
51+
52+
ret := getIPByIfName("eth0", netctrl)
53+
Expect(ret).To(Equal("192.168.1.10"))
54+
})
55+
It("should return IPv6 address if IPv4 is not available", func() {
56+
netctrl.EXPECT().GetInterfaceAddresses("eth0").Return(
57+
[]net.IPNet{
58+
{
59+
IP: []byte{0x20, 0x1, 0x11, 0x11, 0x22, 0x22, 0x33, 0x33,
60+
0x44, 0x44, 0x55, 0x55, 0x66, 0x66, 0x77, 0x77},
61+
},
62+
},
63+
)
64+
65+
ret := getIPByIfName("eth0", netctrl)
66+
Expect(ret).To(Equal("2001:1111:2222:3333:4444:5555:6666:7777"))
67+
})
68+
It("should return the empty string when no IP addresses are available", func() {
69+
netctrl.EXPECT().GetInterfaceAddresses("eth0").Return([]net.IPNet{})
70+
ret := getIPByIfName("eth0", netctrl)
71+
Expect(ret).To(BeEmpty())
72+
})
73+
It("should return the empty string if returned addresses are invalid", func() {
74+
netctrl.EXPECT().GetInterfaceAddresses("eth0").Return(
75+
[]net.IPNet{
76+
{
77+
IP: []byte{0x20, 0x1, 0x11, 0x11, 0x22, 0x22, 0x33, 0x33,
78+
0x44, 0x44},
79+
},
80+
},
81+
)
82+
ret := getIPByIfName("eth0", netctrl)
83+
Expect(ret).To(BeEmpty())
84+
})
85+
})
86+
87+
var _ = Describe("GetNMActivatedDevices", func() {
88+
var ctrl *gomock.Controller
89+
var netctrl *utilmocks.MockNetController
90+
91+
BeforeEach(func() {
92+
ctrl = gomock.NewController(GinkgoT())
93+
netctrl = utilmocks.NewMockNetController(ctrl)
94+
})
95+
It("should return only devices activated by NetworkManager", func() {
96+
buf := &bytes.Buffer{}
97+
buf.Write([]byte("eth0:connected\n"))
98+
buf.Write([]byte("lo:connected (externally)\n"))
99+
buf.Write([]byte("tun0:connected (externally)\n"))
100+
buf.Write([]byte("garbage\n"))
101+
buf.Write([]byte("mynic:connected\n"))
102+
buf.Write([]byte("eth1:unmanaged\n"))
103+
netctrl.EXPECT().GetNMDeviceState().Return(buf, nil)
104+
ret := getNMActivatedDevices(netctrl)
105+
Expect(ret).To(Equal([]string{"eth0", "mynic"}))
106+
})
107+
It("should return no devices if NetworkManager returns error", func() {
108+
buf := &bytes.Buffer{}
109+
buf.Write([]byte("eth0:connected\n"))
110+
netctrl.EXPECT().GetNMDeviceState().Return(buf, fmt.Errorf("nmcli failed"))
111+
ret := getNMActivatedDevices(netctrl)
112+
Expect(ret).To(BeEmpty())
113+
})
114+
})

0 commit comments

Comments
 (0)