Skip to content

Commit

Permalink
Merge pull request #390 from Microsoft/runhcs_cmd_context
Browse files Browse the repository at this point in the history
Store the runhcs.exe path for faster invocation
  • Loading branch information
jterry75 committed Nov 27, 2018
2 parents 4f64a59 + 15ca54b commit 8200ad0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 6 deletions.
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)
}
}

0 comments on commit 8200ad0

Please sign in to comment.