Skip to content

Commit 3240b21

Browse files
committed
Add utility function + test for validating whether loopback status is UP
Signed-off-by: Sameer <[email protected]>
1 parent 9fbd439 commit 3240b21

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

integration/cni_setup_teardown_linux_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"context"
3636
"crypto/rand"
3737
"fmt"
38+
"net"
3839
"os"
3940
"path"
4041
"runtime"
@@ -46,6 +47,7 @@ import (
4647
"github.com/containerd/continuity/testutil"
4748
"github.com/containerd/go-cni"
4849
"github.com/stretchr/testify/assert"
50+
"github.com/vishvananda/netlink"
4951
)
5052

5153
var (
@@ -306,6 +308,25 @@ func TestBasicSetupAndRemovePluginWithoutVersion(t *testing.T) {
306308
}
307309
}
308310

311+
// TestLoopbackStatus validates whether the loopback interface state is UP.
312+
func TestLoopbackStatus(t *testing.T) {
313+
t.Log("Checking loopback interface status")
314+
up, err := isLoInterfaceUp()
315+
assert.NoError(t, err, "could not check lo interface status")
316+
317+
t.Logf("loopback interface status is %v", map[bool]string{true: "UP", false: "DOWN"}[up])
318+
assert.True(t, up)
319+
}
320+
321+
// isLoInterfaceUp returns true if the loopback interface status is UP, otherwise false.
322+
func isLoInterfaceUp() (bool, error) {
323+
link, err := netlink.LinkByName("lo")
324+
if err != nil {
325+
return false, fmt.Errorf("could not find interface lo: %w", err)
326+
}
327+
return link.Attrs().Flags&net.FlagUp != 0, nil
328+
}
329+
309330
// createNetNS returns temp netns path.
310331
//
311332
// NOTE: It is based on https://github.com/containernetworking/plugins/blob/v1.0.1/pkg/testutils/netns_linux.go.

integration/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/containerd/continuity v0.2.2
77
github.com/containerd/go-cni v0.0.0-00010101000000-000000000000
88
github.com/stretchr/testify v1.8.0
9+
github.com/vishvananda/netlink v1.3.0
910
)
1011

1112
require (
@@ -14,6 +15,7 @@ require (
1415
github.com/davecgh/go-spew v1.1.1 // indirect
1516
github.com/pmezard/go-difflib v1.0.0 // indirect
1617
github.com/sirupsen/logrus v1.7.0 // indirect
18+
github.com/vishvananda/netns v0.0.4 // indirect
1719
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a // indirect
1820
golang.org/x/sys v0.20.0 // indirect
1921
gopkg.in/yaml.v3 v3.0.1 // indirect

integration/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
120120
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
121121
github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c/go.mod h1:hzIxponao9Kjc7aWznkXaL4U4TWaDSs8zcsY4Ka08nM=
122122
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
123+
github.com/vishvananda/netlink v1.3.0 h1:X7l42GfcV4S6E4vHTsw48qbrV+9PVojNfIhZcwQdrZk=
124+
github.com/vishvananda/netlink v1.3.0/go.mod h1:i6NetklAujEcC6fK0JPjT8qSwWyO0HLn4UKG+hGqeJs=
123125
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
124126
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
125127
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
@@ -153,6 +155,8 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
153155
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
154156
golang.org/x/sys v0.0.0-20191210023423-ac6580df4449/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
155157
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
158+
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
159+
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
156160
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
157161
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
158162
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

0 commit comments

Comments
 (0)