Skip to content

Commit

Permalink
Add utility function + test for validating whether loopback status is UP
Browse files Browse the repository at this point in the history
Signed-off-by: Sameer <[email protected]>
  • Loading branch information
sameersaeed committed Dec 5, 2024
1 parent 9fbd439 commit 3240b21
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
21 changes: 21 additions & 0 deletions integration/cni_setup_teardown_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"context"
"crypto/rand"
"fmt"
"net"
"os"
"path"
"runtime"
Expand All @@ -46,6 +47,7 @@ import (
"github.com/containerd/continuity/testutil"
"github.com/containerd/go-cni"
"github.com/stretchr/testify/assert"
"github.com/vishvananda/netlink"
)

var (
Expand Down Expand Up @@ -306,6 +308,25 @@ func TestBasicSetupAndRemovePluginWithoutVersion(t *testing.T) {
}
}

// TestLoopbackStatus validates whether the loopback interface state is UP.
func TestLoopbackStatus(t *testing.T) {
t.Log("Checking loopback interface status")
up, err := isLoInterfaceUp()
assert.NoError(t, err, "could not check lo interface status")

t.Logf("loopback interface status is %v", map[bool]string{true: "UP", false: "DOWN"}[up])
assert.True(t, up)
}

// isLoInterfaceUp returns true if the loopback interface status is UP, otherwise false.
func isLoInterfaceUp() (bool, error) {
link, err := netlink.LinkByName("lo")
if err != nil {
return false, fmt.Errorf("could not find interface lo: %w", err)
}
return link.Attrs().Flags&net.FlagUp != 0, nil
}

// createNetNS returns temp netns path.
//
// NOTE: It is based on https://github.com/containernetworking/plugins/blob/v1.0.1/pkg/testutils/netns_linux.go.
Expand Down
2 changes: 2 additions & 0 deletions integration/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/containerd/continuity v0.2.2
github.com/containerd/go-cni v0.0.0-00010101000000-000000000000
github.com/stretchr/testify v1.8.0
github.com/vishvananda/netlink v1.3.0
)

require (
Expand All @@ -14,6 +15,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.7.0 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a // indirect
golang.org/x/sys v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions integration/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c/go.mod h1:hzIxponao9Kjc7aWznkXaL4U4TWaDSs8zcsY4Ka08nM=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/vishvananda/netlink v1.3.0 h1:X7l42GfcV4S6E4vHTsw48qbrV+9PVojNfIhZcwQdrZk=
github.com/vishvananda/netlink v1.3.0/go.mod h1:i6NetklAujEcC6fK0JPjT8qSwWyO0HLn4UKG+hGqeJs=
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
Expand Down Expand Up @@ -153,6 +155,8 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191210023423-ac6580df4449/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down

0 comments on commit 3240b21

Please sign in to comment.