Skip to content

Commit

Permalink
prelim: add qemu integration test for Addbr Delbr
Browse files Browse the repository at this point in the history
Signed-off-by: leongross <leon.gross@9elements.com>
  • Loading branch information
leongross committed Apr 25, 2024
1 parent d0fc8c1 commit 4646757
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
40 changes: 40 additions & 0 deletions pkg/brctl/brctl_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !race
// +build !race

package brctl

import (
"fmt"
"testing"
"time"

"github.com/hugelgupf/vmtest"
"github.com/hugelgupf/vmtest/qemu"
)

var (
BRCTL_TEST_IFACE_0 = "eth0"
BRCTL_TEST_IFACE_1 = "eth1"
BRCTL_TEST_IFACES = []string{BRCTL_TEST_IFACE_0, BRCTL_TEST_IFACE_1}

BRCTL_TEST_BR_0 = "br0"
BRCTL_TEST_BR_1 = "br1"
BRCTL_TEST_BRIDGES = []string{BRCTL_TEST_BR_0, BRCTL_TEST_BR_1}
)

// TODO: Since ioctl needs root privileges, we need to run the tests in a VM with root privileges.
func TestIntegration(t *testing.T) {
vmtest.SkipIfNotArch(t, qemu.ArchAMD64)
vmtest.RunGoTestsInVM(t, []string{"github.com/u-root/u-root/pkg/brctl"},
vmtest.WithVMOpt(vmtest.WithQEMUFn(
qemu.WithVMTimeout(time.Minute),
qemu.ArbitraryArgs("-device", "nvme,drive=NVME1,serial=nvme-1,use-intel-id"),
qemu.ArbitraryArgs("-nic", fmt.Sprintf("user,id=%s", BRCTL_TEST_IFACE_0)),
qemu.ArbitraryArgs("-nic", fmt.Sprintf("user,id=%s", BRCTL_TEST_IFACE_1)),
)),
)
}
65 changes: 65 additions & 0 deletions pkg/brctl/brctl_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package brctl

import (
"net"
"testing"

"golang.org/x/sys/unix"
)

// Check if the list of interfaces exist in the system
func interfacesExist(ifs []string) error {
for _, iface := range ifs {
if _, err := net.InterfaceByName(iface); err != nil {
return err
}
}
return nil
}

var test_str_to_tv = []struct {
name string
input string
Expand Down Expand Up @@ -90,3 +101,57 @@ func TestFromJiffies(t *testing.T) {
})
}
}

// All the following tests require virtual hardware to work properly, hence they need to be run in a VM.
// This is done by the integration test in brctl_integration_test.go.
func TestAddbrDelbr(t *testing.T) {
// Check if interfaces exist
if err := interfacesExist(BRCTL_TEST_IFACES); err != nil {
t.Fatalf("interfacesExist(%v) = %v, want nil", BRCTL_TEST_IFACES, err)
}

// Add bridges
for _, bridge := range BRCTL_TEST_BRIDGES {
err := Addbr(bridge)
if err != nil {
t.Fatalf("AddBr(%q) = %v, want nil", bridge, err)
}
}

// Check if bridges were created successfully
if err := interfacesExist(BRCTL_TEST_BRIDGES); err != nil {
t.Fatalf("interfacesExist(%v) = %v, want nil", BRCTL_TEST_BRIDGES, err)
}

// Cleanup the VM
for _, bridge := range BRCTL_TEST_BRIDGES {
err := Delbr(bridge)
if err != nil {
t.Fatalf("DelBr(%q) = %v, want nil", bridge, err)
}
}

// Check if bridges were deleted successfully
for _, iface := range BRCTL_TEST_BRIDGES {
if _, err := net.InterfaceByName(iface); err == nil {
t.Fatalf("net.InterfaceByName(%q) = nil, want an error", iface)
}
}
}

func TestIfDelIf(t *testing.T) {
// Check if interfaces exist
if err := interfacesExist(BRCTL_TEST_IFACES); err != nil {
t.Fatalf("interfacesExist(%v) = %v, want nil", BRCTL_TEST_IFACES, err)
}

// Add interface to bridge
// brrctl addbr br0 eht0
// brrctl addbr br1 eht1
for _, bridge := range BRCTL_TEST_BRIDGES {
err := Addbr(bridge)
if err != nil {
t.Fatalf("AddBr(%q) = %v, want nil", bridge, err)
}
}
}

0 comments on commit 4646757

Please sign in to comment.