...

Source file src/github.com/Microsoft/hcsshim/pkg/go-runhcs/runhcs_test.go

Documentation: github.com/Microsoft/hcsshim/pkg/go-runhcs

     1  //go:build windows
     2  
     3  package runhcs
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  	"sync/atomic"
     9  	"testing"
    10  )
    11  
    12  func resetRunhcsPath() {
    13  	runhcsPath = atomic.Value{}
    14  }
    15  
    16  func TestGetCommandPath_NoLookPath(t *testing.T) {
    17  	resetRunhcsPath()
    18  
    19  	path := getCommandPath()
    20  	if path != "runhcs.exe" {
    21  		t.Fatalf("expected path 'runhcs.exe' got '%s'", path)
    22  	}
    23  	pathi := runhcsPath.Load()
    24  	if pathi == nil {
    25  		t.Fatal("cache state should be set after first query")
    26  	}
    27  	if path != pathi.(string) {
    28  		t.Fatalf("expected: '%s' in cache got '%s'", path, pathi.(string))
    29  	}
    30  }
    31  
    32  func TestGetCommandPath_WithLookPath(t *testing.T) {
    33  	resetRunhcsPath()
    34  
    35  	wd, err := os.Getwd()
    36  	if err != nil {
    37  		t.Fatalf("failed to get cwd with err: %v", err)
    38  	}
    39  	fakePath := filepath.Join(wd, "runhcs.exe")
    40  	f, err := os.Create(fakePath)
    41  	if err != nil {
    42  		t.Fatalf("failed to create fake runhcs.exe in path with err: %v", err)
    43  	}
    44  	f.Close()
    45  	defer os.Remove(fakePath)
    46  
    47  	path := getCommandPath()
    48  	if path != fakePath {
    49  		t.Fatalf("expected fake path '%s' got '%s'", fakePath, path)
    50  	}
    51  	pathi := runhcsPath.Load()
    52  	if pathi == nil {
    53  		t.Fatal("cache state should be set after first query")
    54  	}
    55  	if path != pathi.(string) {
    56  		t.Fatalf("expected: '%s' in cache got '%s'", fakePath, pathi.(string))
    57  	}
    58  }
    59  
    60  func TestGetCommandPath_WithCache(t *testing.T) {
    61  	resetRunhcsPath()
    62  
    63  	value := "this is a test"
    64  	runhcsPath.Store(value)
    65  
    66  	path := getCommandPath()
    67  	if path != value {
    68  		t.Fatalf("expected fake cached path: '%s' got '%s'", value, path)
    69  	}
    70  }
    71  

View as plain text