Skip to content

Commit

Permalink
fixed setup in testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Hex committed Feb 25, 2024
1 parent 5e2e6b7 commit e37b63e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fmt:

.PHONY: test
test:
go test -p 1 -exec "go run $(PWD)/cmd/codesign" ./... -timeout 60s -v
go test -p 1 -exec "go run $(PWD)/cmd/codesign" ./... -timeout 2m -v

.PHONY: test/run
test/run:
Expand Down
16 changes: 10 additions & 6 deletions virtualization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vz_test
import (
"errors"
"fmt"
"math"
"os"
"runtime"
"syscall"
Expand Down Expand Up @@ -172,24 +173,23 @@ func newVirtualizationMachine(
time.Sleep(5 * time.Second)
}

max := 8
RETRY:
for i := 1; ; i++ {
conn, err := socketDevice.Connect(2222)
if err != nil {
var nserr *vz.NSError
if !errors.As(err, &nserr) || i > 5 {
if !errors.As(err, &nserr) || i > max {
t.Fatal(err)
}
if nserr.Code == int(syscall.ECONNRESET) {
t.Logf("retry vsock connect: %d", i)
time.Sleep(time.Second)
time.Sleep(backOffDelay(i))
continue RETRY
}
t.Fatalf("failed to connect vsock: %v", err)
}

t.Log("setup ssh client in container")

initialized := make(chan struct{})
retry := make(chan struct{})
go func() {
Expand All @@ -215,15 +215,19 @@ RETRY:

close(initialized)

t.Logf("container setup done")

return &Container{
VirtualMachine: vm,
Client: sshClient,
}
}
}

func backOffDelay(retryAttempts int) time.Duration {
factor := 0.5
delay := math.Exp2(float64(retryAttempts)) * factor
return time.Duration(math.Min(delay, 10)) * time.Second
}

func waitState(t *testing.T, wait time.Duration, vm *vz.VirtualMachine, want vz.VirtualMachineState) {
t.Helper()
select {
Expand Down

0 comments on commit e37b63e

Please sign in to comment.