File: netavark_suite_test.go

package info (click to toggle)
golang-github-containers-common 0.64.2%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,528 kB
  • sloc: makefile: 130; sh: 102
file content (94 lines) | stat: -rw-r--r-- 2,538 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//go:build linux

package netavark_test

import (
	"fmt"
	"net"
	"os"
	"reflect"
	"testing"

	"github.com/containers/common/internal/attributedstring"
	"github.com/containers/common/libnetwork/netavark"
	"github.com/containers/common/libnetwork/types"
	"github.com/containers/common/libnetwork/util"
	"github.com/containers/common/pkg/config"
	"github.com/containers/storage/pkg/unshare"
	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
	gomegaTypes "github.com/onsi/gomega/types"
)

func TestNetavark(t *testing.T) {
	// commit 26fc823c27 added a check for the userns env var in the network interface code,
	// it does not actually check the uid
	if unshare.IsRootless() {
		t.Setenv(unshare.UsernsEnvName, "done")
	}
	RegisterFailHandler(Fail)
	RunSpecs(t, "Netavark Suite")
}

var netavarkBinary string

func init() {
	netavarkBinary = os.Getenv("NETAVARK_BINARY")
	if netavarkBinary == "" {
		netavarkBinary = "/usr/libexec/podman/netavark"
	}
}

func getNetworkInterface(confDir string) (types.ContainerNetwork, error) {
	return netavark.NewNetworkInterface(&netavark.InitConfig{
		Config:           &config.Config{},
		NetworkConfigDir: confDir,
		NetavarkBinary:   netavarkBinary,
		NetworkRunDir:    confDir,
	})
}

func getNetworkInterfaceWithPlugins(confDir string, pluginDirs []string) (types.ContainerNetwork, error) {
	return netavark.NewNetworkInterface(&netavark.InitConfig{
		NetworkConfigDir: confDir,
		NetavarkBinary:   netavarkBinary,
		NetworkRunDir:    confDir,
		Config: &config.Config{
			Network: config.NetworkConfig{
				NetavarkPluginDirs: attributedstring.NewSlice(pluginDirs),
			},
		},
	})
}

// EqualSubnet is a custom GomegaMatcher to match a subnet
// This makes sure to not use the 16 bytes ip representation.
func EqualSubnet(subnet *net.IPNet) gomegaTypes.GomegaMatcher {
	return &equalSubnetMatcher{
		expected: subnet,
	}
}

type equalSubnetMatcher struct {
	expected *net.IPNet
}

func (m *equalSubnetMatcher) Match(actual any) (bool, error) {
	util.NormalizeIP(&m.expected.IP)

	subnet, ok := actual.(*net.IPNet)
	if !ok {
		return false, fmt.Errorf("EqualSubnet expects a *net.IPNet")
	}
	util.NormalizeIP(&subnet.IP)

	return reflect.DeepEqual(subnet, m.expected), nil
}

func (m *equalSubnetMatcher) FailureMessage(actual any) string {
	return fmt.Sprintf("Expected subnet %#v to equal subnet %#v", actual, m.expected)
}

func (m *equalSubnetMatcher) NegatedFailureMessage(actual any) string {
	return fmt.Sprintf("Expected subnet %#v not to equal subnet %#v", actual, m.expected)
}