Skip to content

Commit

Permalink
Use abs path to testing binary
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
helsaawy committed Dec 16, 2024
1 parent 1a8c2e3 commit a295eca
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions test/functional/hvsock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}

0 comments on commit a295eca

Please sign in to comment.