Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store the runhcs.exe path for faster invocation #390

Merged
merged 1 commit into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions pkg/go-runhcs/runhcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"strings"
"sync"
"sync/atomic"

irunhcs "github.com/Microsoft/hcsshim/internal/runhcs"
"github.com/containerd/go-runc"
Expand All @@ -23,10 +24,35 @@ const (
Text Format = "text"
// JSON is the JSON formatted log output.
JSON Format = "json"

command = "runhcs"
)

var runhcsPath atomic.Value

func getCommandPath() string {
const command = "runhcs.exe"

pathi := runhcsPath.Load()
if pathi == nil {
path, err := exec.LookPath(command)
if err != nil {
// Failed to look up command just use it directly and let the
// Windows loader find it.
path = command
runhcsPath.Store(path)
return path
}
apath, err := filepath.Abs(path)
if err != nil {
// We couldnt make `path` an `AbsPath`. Just use `path` directly and
// let the Windows loader find it.
apath = path
}
runhcsPath.Store(apath)
return apath
}
return pathi.(string)
}

var bytesBufferPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(nil)
Expand Down Expand Up @@ -84,7 +110,7 @@ func (r *Runhcs) args() []string {
}

func (r *Runhcs) command(context context.Context, args ...string) *exec.Cmd {
cmd := exec.CommandContext(context, command, append(r.args(), args...)...)
cmd := exec.CommandContext(context, getCommandPath(), append(r.args(), args...)...)
cmd.Env = os.Environ()
return cmd
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/go-runhcs/runhcs_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build runhcs_test

package runhcs

import (
_ "github.com/Microsoft/hcsshim/functional/manifest"
)
67 changes: 64 additions & 3 deletions pkg/go-runhcs/runhcs_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,68 @@
// +build runhcs_test

package runhcs

import (
_ "github.com/Microsoft/hcsshim/functional/manifest"
"os"
"path/filepath"
"sync/atomic"
"testing"
)

func resetRunhcsPath() {
runhcsPath = atomic.Value{}
}

func TestGetCommandPath_NoLookPath(t *testing.T) {
resetRunhcsPath()

path := getCommandPath()
if path != "runhcs.exe" {
t.Fatalf("expected path 'runhcs.exe' got '%s'", path)
}
pathi := runhcsPath.Load()
if pathi == nil {
t.Fatal("cache state should be set after first query")
}
if path != pathi.(string) {
t.Fatalf("expected: '%s' in cache got '%s'", path, pathi.(string))
}
}

func TestGetCommandPath_WithLookPath(t *testing.T) {
resetRunhcsPath()

wd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get cwd with err: %v", err)
}
fakePath := filepath.Join(wd, "runhcs.exe")
f, err := os.Create(fakePath)
if err != nil {
t.Fatalf("failed to create fake runhcs.exe in path with err: %v", err)
}
f.Close()
defer os.Remove(fakePath)

path := getCommandPath()
if path != fakePath {
t.Fatalf("expected fake path '%s' got '%s'", fakePath, path)
}
pathi := runhcsPath.Load()
if pathi == nil {
t.Fatal("cache state should be set after first query")
}
if path != pathi.(string) {
t.Fatalf("expected: '%s' in cache got '%s'", fakePath, pathi.(string))
}
}

func TestGetCommandPath_WithCache(t *testing.T) {
resetRunhcsPath()

value := "this is a test"
runhcsPath.Store(value)

path := getCommandPath()
if path != value {
t.Fatalf("expected fake cached path: '%s' got '%s'", value, path)
}
}