From a3e0a280a2e7499de1b008343a65159da0f5587d Mon Sep 17 00:00:00 2001 From: Hamza El-Saawy Date: Mon, 16 Dec 2024 15:23:29 -0500 Subject: [PATCH] Use abs path to testing binary Use the full path to the `functional.test.exe` binary when sharing into the uVM for the `TestHVSock_*` test cases in `test\functional\hvsock_test.go` to prevent vSMB share issues. Otherwise, `os.Args[0]` will return the path that the tests were run with (e.g., `.\functional.test.exe`), which can cause vSMB to fail with `The parameter is incorrect.` (likely because it cannot find the current file). Signed-off-by: Hamza El-Saawy --- test/functional/hvsock_test.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/test/functional/hvsock_test.go b/test/functional/hvsock_test.go index a762692268..19a581281b 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 := share_self(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 := share_self(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 share_self(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 +}