diff --git a/test/functional/hvsock_test.go b/test/functional/hvsock_test.go index a762692268..fb57943573 100644 --- a/test/functional/hvsock_test.go +++ b/test/functional/hvsock_test.go @@ -22,6 +22,7 @@ import ( "golang.org/x/sys/windows" hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2" + "github.com/Microsoft/hcsshim/internal/uvm" "github.com/Microsoft/hcsshim/osversion" testcmd "github.com/Microsoft/hcsshim/test/internal/cmd" @@ -134,8 +135,7 @@ func TestHVSock_UVM_HostBind(t *testing.T) { return err }) - guestPath := filepath.Join(`C:\`, filepath.Base(os.Args[0])) - testuvm.Share(ctx, t, vm, os.Args[0], guestPath, true) + guestPath := shareSelf(ctx, t, vm, "") reexecCmd := fmt.Sprintf(`%s -test.run=%s`, guestPath, util.TestNameRegex(t)) if testing.Verbose() { @@ -231,8 +231,7 @@ func TestHVSock_UVM_GuestBind(t *testing.T) { ctx, cancel := context.WithTimeout(ctx, hvsockTestTimeout) //nolint:govet // ctx is shadowed t.Cleanup(cancel) - guestPath := filepath.Join(`C:\`, filepath.Base(os.Args[0])) - testuvm.Share(ctx, t, vm, os.Args[0], guestPath, true) + guestPath := shareSelf(ctx, t, vm, "") reexecCmd := fmt.Sprintf(`%s -test.run=%s`, guestPath, util.TestNameRegex(t)) if testing.Verbose() { @@ -1148,3 +1147,22 @@ func goBlockT[T any](f func() T) <-chan T { return ch } + +func shareSelf(ctx context.Context, tb testing.TB, vm *uvm.UtilityVM, base string) string { + tb.Helper() + + if base == "" { + base = `C:\` + } + + // use [os.Executable] over `os.Args[0]` to make sure path is absolute + self, err := os.Executable() + if err != nil { + tb.Fatalf("get testing binary path: %v", err) + } + + guestPath := filepath.Join(base, filepath.Base(self)) + testuvm.Share(ctx, tb, vm, self, guestPath, true) + + return guestPath +}