Skip to content

Commit

Permalink
use random port in testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Hex committed Oct 28, 2024
1 parent 775b28d commit 5251e81
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PUIPUI_LINUX_VERSION := 1.0.1
PUIPUI_LINUX_VERSION := 1.0.2
ARCH := $(shell uname -m)
KERNEL_ARCH := $(shell echo $(ARCH) | sed -e s/arm64/aarch64/)
KERNEL_TAR := puipui_linux_v$(PUIPUI_LINUX_VERSION)_$(KERNEL_ARCH).tar.gz
Expand Down Expand Up @@ -36,4 +36,4 @@ install/stringer:

.PHONY: clean
clean:
@rm testdata/{Image,initramfs.cpio.gz,*.tar.gz}
@rm testdata/{Image,initramfs.cpio.gz,*.tar.gz}
20 changes: 18 additions & 2 deletions virtualization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"math"
"net"
"os"
"runtime"
"syscall"
Expand Down Expand Up @@ -112,15 +113,30 @@ func (c *Container) NewSession(t *testing.T) *ssh.Session {
return sshSession
}

func getFreePort() (int, error) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}

func newVirtualizationMachine(
t *testing.T,
configs ...func(*vz.VirtualMachineConfiguration) error,
) *Container {
port, err := getFreePort()
if err != nil {
t.Fatalf("failed to resolve free tcp addr: %v", err)
}

vmlinuz := "./testdata/Image"
initramfs := "./testdata/initramfs.cpio.gz"
cmdline := fmt.Sprintf("console=hvc0 vsock_port=%d", port)
bootLoader, err := vz.NewLinuxBootLoader(
vmlinuz,
vz.WithCommandLine("console=hvc0"),
vz.WithCommandLine(cmdline),
vz.WithInitrd(initramfs),
)
if err != nil {
Expand Down Expand Up @@ -176,7 +192,7 @@ func newVirtualizationMachine(
max := 8
RETRY:
for i := 1; ; i++ {
conn, err := socketDevice.Connect(2222)
conn, err := socketDevice.Connect(uint32(port))
if err != nil {
var nserr *vz.NSError
if !errors.As(err, &nserr) || i > max {
Expand Down

0 comments on commit 5251e81

Please sign in to comment.