Skip to content

Commit 81321cd

Browse files
committed
Check for fs.ErrExist and fs.ErrNotExist in netlink Adapter
...instead of os.IsNotExist and os.IsNotExist as advised in the docs. Also add unit tests for Adapter. Signed-off-by: Tom Pantelis <[email protected]>
1 parent 34132dc commit 81321cd

File tree

2 files changed

+159
-5
lines changed

2 files changed

+159
-5
lines changed

pkg/netlink/adapter.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ limitations under the License.
1919
package netlink
2020

2121
import (
22-
"os"
23-
"syscall"
22+
"io/fs"
2423

2524
"github.com/pkg/errors"
2625
"github.com/submariner-io/admiral/pkg/resource"
@@ -34,7 +33,7 @@ type Adapter struct {
3433

3534
func (a *Adapter) RuleAddIfNotPresent(rule *netlink.Rule) error {
3635
err := a.RuleAdd(rule)
37-
if err != nil && !os.IsExist(err) {
36+
if err != nil && !errors.Is(err, fs.ErrExist) {
3837
return errors.Wrapf(err, "failed to add rule %s", rule)
3938
}
4039

@@ -43,7 +42,7 @@ func (a *Adapter) RuleAddIfNotPresent(rule *netlink.Rule) error {
4342

4443
func (a *Adapter) RuleDelIfPresent(rule *netlink.Rule) error {
4544
err := a.RuleDel(rule)
46-
if err != nil && !os.IsNotExist(err) {
45+
if err != nil && !errors.Is(err, fs.ErrNotExist) {
4746
return errors.Wrapf(err, "failed to delete rule %s", rule)
4847
}
4948

@@ -53,7 +52,7 @@ func (a *Adapter) RuleDelIfPresent(rule *netlink.Rule) error {
5352
func (a *Adapter) RouteAddOrReplace(route *netlink.Route) error {
5453
err := a.RouteAdd(route)
5554

56-
if errors.Is(err, syscall.EEXIST) {
55+
if errors.Is(err, fs.ErrExist) {
5756
err = a.RouteReplace(route)
5857
}
5958

pkg/netlink/adapter_test.go

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
SPDX-License-Identifier: Apache-2.0
3+
4+
Copyright Contributors to the Submariner project.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package netlink_test
20+
21+
import (
22+
"net"
23+
24+
. "github.com/onsi/ginkgo/v2"
25+
. "github.com/onsi/gomega"
26+
netlinkAPI "github.com/submariner-io/submariner/pkg/netlink"
27+
"github.com/submariner-io/submariner/pkg/netlink/fake"
28+
"github.com/vishvananda/netlink"
29+
k8snet "k8s.io/utils/net"
30+
)
31+
32+
var _ = Describe("Adapter", func() {
33+
var (
34+
adapter *netlinkAPI.Adapter
35+
fakeNL *fake.NetLink
36+
rule *netlink.Rule
37+
)
38+
39+
BeforeEach(func() {
40+
fakeNL = fake.New()
41+
adapter = &netlinkAPI.Adapter{Basic: fakeNL.Basic}
42+
43+
rule = netlinkAPI.NewTableRule(100, k8snet.IPv4)
44+
})
45+
46+
Describe("RuleAddIfNotPresent", func() {
47+
Context("when the rule does not exist", func() {
48+
It("should add it successfully", func() {
49+
Expect(adapter.RuleAddIfNotPresent(rule)).To(Succeed())
50+
fakeNL.AwaitRule(rule.Table, "", "")
51+
})
52+
})
53+
54+
Context("when the rule already exists", func() {
55+
BeforeEach(func() {
56+
Expect(fakeNL.RuleAdd(rule)).To(Succeed())
57+
})
58+
59+
It("should not return an error", func() {
60+
Expect(adapter.RuleAddIfNotPresent(rule)).To(Succeed())
61+
})
62+
})
63+
64+
Context("when RuleAdd fails", func() {
65+
BeforeEach(func() {
66+
_, rule.Src, _ = net.ParseCIDR("10.253.0.0/16")
67+
_, rule.Dst, _ = net.ParseCIDR("2001:0:0:1234::/64")
68+
})
69+
70+
It("should return the error", func() {
71+
Expect(adapter.RuleAddIfNotPresent(rule)).NotTo(Succeed())
72+
})
73+
})
74+
})
75+
76+
Describe("RuleDelIfPresent", func() {
77+
Context("when the rule exists", func() {
78+
BeforeEach(func() {
79+
Expect(fakeNL.RuleAdd(rule)).To(Succeed())
80+
})
81+
82+
It("should delete it successfully", func() {
83+
Expect(adapter.RuleDelIfPresent(rule)).To(Succeed())
84+
fakeNL.AwaitNoRule(rule.Table, "", "")
85+
})
86+
})
87+
88+
Context("when the rule does not exist", func() {
89+
It("should not return an error", func() {
90+
Expect(adapter.RuleDelIfPresent(rule)).To(Succeed())
91+
})
92+
})
93+
})
94+
95+
Describe("RouteAddOrReplace", func() {
96+
var route *netlink.Route
97+
98+
BeforeEach(func() {
99+
_, destNet, _ := net.ParseCIDR("192.168.1.0/24")
100+
route = &netlink.Route{
101+
LinkIndex: 1,
102+
Dst: destNet,
103+
Gw: net.ParseIP("192.168.1.1"),
104+
Table: 100,
105+
}
106+
})
107+
108+
Context("when the route does not exist", func() {
109+
It("should add it successfully", func() {
110+
Expect(adapter.RouteAddOrReplace(route)).To(Succeed())
111+
fakeNL.AwaitDstRoutes(route.LinkIndex, route.Table, route.Dst.String())
112+
})
113+
})
114+
115+
Context("when the route already exists", func() {
116+
BeforeEach(func() {
117+
Expect(fakeNL.RouteAdd(route)).To(Succeed())
118+
})
119+
120+
It("should replace it successfully", func() {
121+
route.Priority = 200
122+
123+
Expect(adapter.RouteAddOrReplace(route)).To(Succeed())
124+
125+
routes, _ := fakeNL.RouteList(&netlink.GenericLink{
126+
LinkAttrs: netlink.LinkAttrs{Index: route.LinkIndex},
127+
}, k8snet.IPv4)
128+
Expect(routes).To(ContainElement(HaveField("Priority", 200)))
129+
})
130+
})
131+
})
132+
133+
Describe("GetDefaultGatewayInterface", func() {
134+
intf := net.Interface{Name: "eth0", Index: 99}
135+
136+
Context("when the default gateway route exists", func() {
137+
BeforeEach(func() {
138+
fakeNL.SetupDefaultGateway(k8snet.IPv4, intf)
139+
})
140+
141+
It("should return the default gateway interface", func() {
142+
iface, err := adapter.GetDefaultGatewayInterface(k8snet.IPv4)
143+
Expect(err).NotTo(HaveOccurred())
144+
Expect(iface.Index()).To(Equal(intf.Index))
145+
})
146+
})
147+
148+
Context("when no default gateway route exists", func() {
149+
It("should return an error", func() {
150+
_, err := adapter.GetDefaultGatewayInterface(k8snet.IPv4)
151+
Expect(err).To(HaveOccurred())
152+
})
153+
})
154+
})
155+
})

0 commit comments

Comments
 (0)